1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_xmlsecurity.hxx"
34 #include <rtl/ustring.hxx>
35 #include <cppuhelper/servicefactory.hxx>
37 #include <xmlsecurity/biginteger.hxx>
38 #include <xmlsecurity/xmlsignaturehelper.hxx>
39 #include "xmlsecurity/baseencoding.hxx"
40 #include <tools/date.hxx>
41 #include <tools/time.hxx>
43 using namespace ::com::sun::star
;
45 long denyVerifyHandler( void *, void * )
50 long startVerifyHandler( void *, void * )
52 return QueryVerifySignature();
55 int SAL_CALL
main( int argc
, char **argv
)
59 fprintf( stderr
, "Usage: %s <signature file 1> <signature file 2> <xml stream file> <binary stream file> [<cryptoken>]\n" , argv
[0] ) ;
63 uno::Reference
< lang::XMultiServiceFactory
> xMSF
= CreateDemoServiceFactory();
65 rtl::OUString aSIGFileName
= rtl::OUString::createFromAscii(argv
[1]);
66 rtl::OUString aSIGFileName2
= rtl::OUString::createFromAscii(argv
[2]);
67 rtl::OUString aXMLFileName
= rtl::OUString::createFromAscii(argv
[3]);
68 rtl::OUString aBINFileName
= rtl::OUString::createFromAscii(argv
[4]);
69 rtl::OUString aCryptoToken
;
71 aCryptoToken
= rtl::OUString::createFromAscii(argv
[6]);
73 sal_Int32 nSecurityId
;
74 uno::Reference
< io::XOutputStream
> xOutputStream
;
75 uno::Reference
< io::XInputStream
> xInputStream
;
77 SignatureInformations signatureInformations
;
78 uno::Reference
< ::com::sun::star::xml::sax::XDocumentHandler
> xDocumentHandler
;
80 // -------- START -------
82 XMLSignatureHelper
aSignatureHelper( xMSF
);
84 bool bInit
= aSignatureHelper
.Init( aCryptoToken
);
87 fprintf( stderr
, "Error initializing security context!\n" );
91 fprintf( stdout
, "\n\nTEST MISSION 1: Create the first signature file\n");
93 aSignatureHelper
.StartMission();
96 * select a private key certificate
98 uno::Reference
< xml::crypto::XSecurityEnvironment
> xSecurityEnvironment
= aSignatureHelper
.GetSecurityEnvironment();
99 uno::Sequence
< uno::Reference
< ::com::sun::star::security::XCertificate
> > xPersonalCerts
= xSecurityEnvironment
->getPersonalCertificates() ;
101 fprintf( stdout
, "\nPlease select two certificates:\n" );
103 for ( int nSig
= 0; nSig
< 2; nSig
++ )
105 // New security ID for signature...
106 nSecurityId
= aSignatureHelper
.GetNewSecurityId();
108 // Select certificate...
109 uno::Reference
< ::com::sun::star::security::XCertificate
> xPersonalCert
= getCertificateFromEnvironment( xSecurityEnvironment
, true );
110 aSignatureHelper
.SetX509Certificate(
111 nSecurityId
, xPersonalCert
->getIssuerName(),
112 bigIntegerToNumericString( xPersonalCert
->getSerialNumber()),
113 baseEncode(xPersonalCert
->getEncoded(), BASE64
));
114 aSignatureHelper
.AddForSigning( nSecurityId
, aXMLFileName
, aXMLFileName
, sal_False
);
115 aSignatureHelper
.AddForSigning( nSecurityId
, aBINFileName
, aBINFileName
, sal_True
);
116 aSignatureHelper
.SetDateTime( nSecurityId
, Date(), Time() );
121 xOutputStream
= OpenOutputStream( aSIGFileName
);
122 bDone
= aSignatureHelper
.CreateAndWriteSignature( xOutputStream
);
124 fprintf( stderr
, "\nSTATUS MISSION 1: Error creating Signature!\n" );
126 fprintf( stdout
, "\nSTATUS MISSION 1: Signature successfully created!\n" );
128 aSignatureHelper
.EndMission();
131 fprintf( stdout
, "\n\nTEST MISSION 2: Transfer the second signature to a new signature file\n");
134 * You can use an uninitialized SignatureHelper to perform this mission.
138 * configures the start-verify handler. Don't need to verify for transfering...
140 aSignatureHelper
.SetStartVerifySignatureHdl( Link( NULL
, denyVerifyHandler
) );
141 aSignatureHelper
.StartMission();
143 xInputStream
= OpenInputStream( aSIGFileName
);
144 bDone
= aSignatureHelper
.ReadAndVerifySignature( xInputStream
);
145 xInputStream
->closeInput();
148 fprintf( stderr
, "\nSTATUS MISSION 2: Error in reading Signature!\n" );
150 fprintf( stdout
, "\nSTATUS MISSION 2: Signature successfully transfered!\n" );
153 * get all signature information
155 signatureInformations
= aSignatureHelper
.GetSignatureInformations();
158 * write the first signature into the second signature file.
161 xOutputStream
= OpenOutputStream( aSIGFileName2
);
162 xDocumentHandler
= aSignatureHelper
.CreateDocumentHandlerWithHeader( xOutputStream
);
163 aSignatureHelper
.ExportSignature( xDocumentHandler
, signatureInformations
[1]);
164 aSignatureHelper
.CloseDocumentHandler( xDocumentHandler
);
165 aSignatureHelper
.EndMission();
167 fprintf( stdout
, "\n\nTEST MISSION 3: Insert a new signature to the first signature file\n");
169 aSignatureHelper
.StartMission();
171 nSecurityId
= aSignatureHelper
.GetNewSecurityId();
173 // Select certificate...
174 uno::Reference
< ::com::sun::star::security::XCertificate
> xPersonalCert
= getCertificateFromEnvironment( xSecurityEnvironment
, true );
175 aSignatureHelper
.SetX509Certificate(
176 nSecurityId
, xPersonalCert
->getIssuerName(),
177 bigIntegerToNumericString( xPersonalCert
->getSerialNumber()),
178 baseEncode(xPersonalCert
->getEncoded(), BASE64
));
179 aSignatureHelper
.AddForSigning( nSecurityId
, aXMLFileName
, aXMLFileName
, sal_False
);
180 aSignatureHelper
.AddForSigning( nSecurityId
, aBINFileName
, aBINFileName
, sal_True
);
181 aSignatureHelper
.SetDateTime( nSecurityId
, Date(), Time() );
184 xOutputStream
= OpenOutputStream( aSIGFileName
);
185 xDocumentHandler
= aSignatureHelper
.CreateDocumentHandlerWithHeader( xOutputStream
);
187 aSignatureHelper
.ExportSignature( xDocumentHandler
, signatureInformations
[0]);
188 bDone
= aSignatureHelper
.CreateAndWriteSignature( xDocumentHandler
);
189 aSignatureHelper
.ExportSignature( xDocumentHandler
, signatureInformations
[1]);
190 aSignatureHelper
.CloseDocumentHandler( xDocumentHandler
);
193 fprintf( stderr
, "\nSTATUS MISSION 3: Error creating Signature!\n" );
195 fprintf( stdout
, "\nSTATUS MISSION 3: Signature successfully created!\n" );
197 aSignatureHelper
.EndMission();
199 fprintf( stdout
, "\n\nTEST MISSION 4 : Verify the first signature file\n");
201 aSignatureHelper
.SetStartVerifySignatureHdl( Link( NULL
, startVerifyHandler
) );
203 aSignatureHelper
.StartMission();
205 xInputStream
= OpenInputStream( aSIGFileName
);
206 bDone
= aSignatureHelper
.ReadAndVerifySignature( xInputStream
);
207 xInputStream
->closeInput();
210 fprintf( stderr
, "\nSTATUS MISSION 4: Error verifying Signatures!\n" );
212 fprintf( stdout
, "\nSTATUS MISSION 4: All choosen Signatures veryfied successfully!\n" );
214 aSignatureHelper
.EndMission();
216 QueryPrintSignatureDetails( aSignatureHelper
.GetSignatureInformations(), aSignatureHelper
.GetSecurityEnvironment() );
218 fprintf( stdout
, "\n\nTEST MISSION 5: Verify the second signature file\n");
220 aSignatureHelper
.StartMission();
222 xInputStream
= OpenInputStream( aSIGFileName2
);
223 bDone
= aSignatureHelper
.ReadAndVerifySignature( xInputStream
);
224 xInputStream
->closeInput();
227 fprintf( stderr
, "\nSTATUS MISSION 5: Error verifying Signatures!\n" );
229 fprintf( stdout
, "\nSTATUS MISSION 5: All choosen Signatures veryfied successfully!\n" );
231 aSignatureHelper
.EndMission();
233 QueryPrintSignatureDetails( aSignatureHelper
.GetSignatureInformations(), aSignatureHelper
.GetSecurityEnvironment() );