1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: signdemo.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_xmlsecurity.hxx"
37 #include <tools/date.hxx>
38 #include <tools/time.hxx>
39 #include <cppuhelper/servicefactory.hxx>
41 #include <xmlsecurity/biginteger.hxx>
42 #include <xmlsecurity/xmlsignaturehelper.hxx>
43 #include "xmlsecurity/baseencoding.hxx"
45 using namespace ::com::sun::star
;
47 int SAL_CALL
main( int argc
, char **argv
)
51 fprintf( stderr
, "Usage: %s <signature file> <xml stream file> <binary stream file> [<cryptoken>]\n" , argv
[0] ) ;
55 rtl::OUString aSIGFileName
= rtl::OUString::createFromAscii(argv
[1]);
56 rtl::OUString aXMLFileName
= rtl::OUString::createFromAscii(argv
[2]);
57 rtl::OUString aBINFileName
= rtl::OUString::createFromAscii(argv
[3]);
58 rtl::OUString aCryptoToken
;
60 aCryptoToken
= rtl::OUString::createFromAscii(argv
[4]);
62 uno::Reference
< lang::XMultiServiceFactory
> xMSF
= CreateDemoServiceFactory();
65 * creates a signature helper
67 XMLSignatureHelper
aSignatureHelper( xMSF
);
70 * creates a security context.
72 bool bInit
= aSignatureHelper
.Init( aCryptoToken
);
75 fprintf( stderr
, "Error initializing security context!\n" );
79 aSignatureHelper
.StartMission();
82 * select a private key certificate
85 sal_Int32 nEnvCount
= aSignatureHelper
.GetSecurityEnvironmentNumber();
88 fprintf( stdout
, "\nNo SecurityEnvironment found!\n" ) ;
92 uno::Sequence
< uno::Reference
< xml::crypto::XSecurityEnvironment
> > xSecurityEnvironments(nEnvCount
) ;
93 for( i
=0; i
< nEnvCount
; i
++ )
94 xSecurityEnvironments
[i
] = aSignatureHelper
.GetSecurityEnvironmentByIndex(i
);
96 fprintf( stdout
, "\nSelect a SecurityEnvironment:\n" ) ;
97 for( i
= 0; i
< nEnvCount
; i
++ )
98 fprintf( stdout
, "\n[%d] %s", i
+1, rtl::OUStringToOString( xSecurityEnvironments
[i
]->getSecurityEnvironmentInformation() ,RTL_TEXTENCODING_ASCII_US
).getStr());
100 sal_Int32 nEnvIndex
= QuerySelectNumber( 1, nEnvCount
) -1;
102 uno::Reference
< ::com::sun::star::security::XCertificate
> xPersonalCert
= getCertificateFromEnvironment(xSecurityEnvironments
[nEnvIndex
], true);
104 if ( !xPersonalCert
.is() )
106 fprintf( stdout
, "No certificate choosen - exit.\n" );
111 * creates a new signature id
113 sal_Int32 nSecurityId
= aSignatureHelper
.GetNewSecurityId();
116 * configures the X509 certificate
118 aSignatureHelper
.SetX509Certificate(
119 nSecurityId
, nEnvIndex
,
120 xPersonalCert
->getIssuerName(),
121 bigIntegerToNumericString( xPersonalCert
->getSerialNumber()),
122 baseEncode(xPersonalCert
->getEncoded(), BASE64
));
125 * configures date/time
127 aSignatureHelper
.SetDateTime( nSecurityId
, Date(), Time());
130 * signs the xml stream
132 aSignatureHelper
.AddForSigning( nSecurityId
, aXMLFileName
, aXMLFileName
, sal_False
);
135 * signs the binary stream
137 aSignatureHelper
.AddForSigning( nSecurityId
, aBINFileName
, aBINFileName
, sal_True
);
142 uno::Reference
< io::XOutputStream
> xOutputStream
= OpenOutputStream( aSIGFileName
);
143 bool bDone
= aSignatureHelper
.CreateAndWriteSignature( xOutputStream
);
147 fprintf( stderr
, "\nSTATUS: Error creating Signature!\n" );
151 fprintf( stdout
, "\nSTATUS: Signature successfully created!\n" );
154 aSignatureHelper
.EndMission();
156 QueryPrintSignatureDetails( aSignatureHelper
.GetSignatureInformations(), aSignatureHelper
.GetSecurityEnvironment() );