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: multisigdemo.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 <rtl/ustring.hxx>
38 #include <cppuhelper/servicefactory.hxx>
40 #include <xmlsecurity/biginteger.hxx>
41 #include <xmlsecurity/xmlsignaturehelper.hxx>
42 #include "xmlsecurity/baseencoding.hxx"
43 #include <tools/date.hxx>
44 #include <tools/time.hxx>
46 using namespace ::com::sun::star
;
48 long denyVerifyHandler( void *, void * )
53 long startVerifyHandler( void *, void * )
55 return QueryVerifySignature();
58 int SAL_CALL
main( int argc
, char **argv
)
62 fprintf( stderr
, "Usage: %s <signature file 1> <signature file 2> <xml stream file> <binary stream file> [<cryptoken>]\n" , argv
[0] ) ;
66 uno::Reference
< lang::XMultiServiceFactory
> xMSF
= CreateDemoServiceFactory();
68 rtl::OUString aSIGFileName
= rtl::OUString::createFromAscii(argv
[1]);
69 rtl::OUString aSIGFileName2
= rtl::OUString::createFromAscii(argv
[2]);
70 rtl::OUString aXMLFileName
= rtl::OUString::createFromAscii(argv
[3]);
71 rtl::OUString aBINFileName
= rtl::OUString::createFromAscii(argv
[4]);
72 rtl::OUString aCryptoToken
;
74 aCryptoToken
= rtl::OUString::createFromAscii(argv
[6]);
76 sal_Int32 nSecurityId
;
77 uno::Reference
< io::XOutputStream
> xOutputStream
;
78 uno::Reference
< io::XInputStream
> xInputStream
;
80 SignatureInformations signatureInformations
;
81 uno::Reference
< ::com::sun::star::xml::sax::XDocumentHandler
> xDocumentHandler
;
83 // -------- START -------
85 XMLSignatureHelper
aSignatureHelper( xMSF
);
87 bool bInit
= aSignatureHelper
.Init( aCryptoToken
);
90 fprintf( stderr
, "Error initializing security context!\n" );
94 fprintf( stdout
, "\n\nTEST MISSION 1: Create the first signature file\n");
96 aSignatureHelper
.StartMission();
99 * select a private key certificate
101 uno::Reference
< xml::crypto::XSecurityEnvironment
> xSecurityEnvironment
= aSignatureHelper
.GetSecurityEnvironment();
102 uno::Sequence
< uno::Reference
< ::com::sun::star::security::XCertificate
> > xPersonalCerts
= xSecurityEnvironment
->getPersonalCertificates() ;
104 fprintf( stdout
, "\nPlease select two certificates:\n" );
106 for ( int nSig
= 0; nSig
< 2; nSig
++ )
108 // New security ID for signature...
109 nSecurityId
= aSignatureHelper
.GetNewSecurityId();
111 // Select certificate...
112 uno::Reference
< ::com::sun::star::security::XCertificate
> xPersonalCert
= getCertificateFromEnvironment( xSecurityEnvironment
, true );
113 aSignatureHelper
.SetX509Certificate(
114 nSecurityId
, xPersonalCert
->getIssuerName(),
115 bigIntegerToNumericString( xPersonalCert
->getSerialNumber()),
116 baseEncode(xPersonalCert
->getEncoded(), BASE64
));
117 aSignatureHelper
.AddForSigning( nSecurityId
, aXMLFileName
, aXMLFileName
, sal_False
);
118 aSignatureHelper
.AddForSigning( nSecurityId
, aBINFileName
, aBINFileName
, sal_True
);
119 aSignatureHelper
.SetDateTime( nSecurityId
, Date(), Time() );
124 xOutputStream
= OpenOutputStream( aSIGFileName
);
125 bDone
= aSignatureHelper
.CreateAndWriteSignature( xOutputStream
);
127 fprintf( stderr
, "\nSTATUS MISSION 1: Error creating Signature!\n" );
129 fprintf( stdout
, "\nSTATUS MISSION 1: Signature successfully created!\n" );
131 aSignatureHelper
.EndMission();
134 fprintf( stdout
, "\n\nTEST MISSION 2: Transfer the second signature to a new signature file\n");
137 * You can use an uninitialized SignatureHelper to perform this mission.
141 * configures the start-verify handler. Don't need to verify for transfering...
143 aSignatureHelper
.SetStartVerifySignatureHdl( Link( NULL
, denyVerifyHandler
) );
144 aSignatureHelper
.StartMission();
146 xInputStream
= OpenInputStream( aSIGFileName
);
147 bDone
= aSignatureHelper
.ReadAndVerifySignature( xInputStream
);
148 xInputStream
->closeInput();
151 fprintf( stderr
, "\nSTATUS MISSION 2: Error in reading Signature!\n" );
153 fprintf( stdout
, "\nSTATUS MISSION 2: Signature successfully transfered!\n" );
156 * get all signature information
158 signatureInformations
= aSignatureHelper
.GetSignatureInformations();
161 * write the first signature into the second signature file.
164 xOutputStream
= OpenOutputStream( aSIGFileName2
);
165 xDocumentHandler
= aSignatureHelper
.CreateDocumentHandlerWithHeader( xOutputStream
);
166 aSignatureHelper
.ExportSignature( xDocumentHandler
, signatureInformations
[1]);
167 aSignatureHelper
.CloseDocumentHandler( xDocumentHandler
);
168 aSignatureHelper
.EndMission();
170 fprintf( stdout
, "\n\nTEST MISSION 3: Insert a new signature to the first signature file\n");
172 aSignatureHelper
.StartMission();
174 nSecurityId
= aSignatureHelper
.GetNewSecurityId();
176 // Select certificate...
177 uno::Reference
< ::com::sun::star::security::XCertificate
> xPersonalCert
= getCertificateFromEnvironment( xSecurityEnvironment
, true );
178 aSignatureHelper
.SetX509Certificate(
179 nSecurityId
, xPersonalCert
->getIssuerName(),
180 bigIntegerToNumericString( xPersonalCert
->getSerialNumber()),
181 baseEncode(xPersonalCert
->getEncoded(), BASE64
));
182 aSignatureHelper
.AddForSigning( nSecurityId
, aXMLFileName
, aXMLFileName
, sal_False
);
183 aSignatureHelper
.AddForSigning( nSecurityId
, aBINFileName
, aBINFileName
, sal_True
);
184 aSignatureHelper
.SetDateTime( nSecurityId
, Date(), Time() );
187 xOutputStream
= OpenOutputStream( aSIGFileName
);
188 xDocumentHandler
= aSignatureHelper
.CreateDocumentHandlerWithHeader( xOutputStream
);
190 aSignatureHelper
.ExportSignature( xDocumentHandler
, signatureInformations
[0]);
191 bDone
= aSignatureHelper
.CreateAndWriteSignature( xDocumentHandler
);
192 aSignatureHelper
.ExportSignature( xDocumentHandler
, signatureInformations
[1]);
193 aSignatureHelper
.CloseDocumentHandler( xDocumentHandler
);
196 fprintf( stderr
, "\nSTATUS MISSION 3: Error creating Signature!\n" );
198 fprintf( stdout
, "\nSTATUS MISSION 3: Signature successfully created!\n" );
200 aSignatureHelper
.EndMission();
202 fprintf( stdout
, "\n\nTEST MISSION 4 : Verify the first signature file\n");
204 aSignatureHelper
.SetStartVerifySignatureHdl( Link( NULL
, startVerifyHandler
) );
206 aSignatureHelper
.StartMission();
208 xInputStream
= OpenInputStream( aSIGFileName
);
209 bDone
= aSignatureHelper
.ReadAndVerifySignature( xInputStream
);
210 xInputStream
->closeInput();
213 fprintf( stderr
, "\nSTATUS MISSION 4: Error verifying Signatures!\n" );
215 fprintf( stdout
, "\nSTATUS MISSION 4: All choosen Signatures veryfied successfully!\n" );
217 aSignatureHelper
.EndMission();
219 QueryPrintSignatureDetails( aSignatureHelper
.GetSignatureInformations(), aSignatureHelper
.GetSecurityEnvironment() );
221 fprintf( stdout
, "\n\nTEST MISSION 5: Verify the second signature file\n");
223 aSignatureHelper
.StartMission();
225 xInputStream
= OpenInputStream( aSIGFileName2
);
226 bDone
= aSignatureHelper
.ReadAndVerifySignature( xInputStream
);
227 xInputStream
->closeInput();
230 fprintf( stderr
, "\nSTATUS MISSION 5: Error verifying Signatures!\n" );
232 fprintf( stdout
, "\nSTATUS MISSION 5: All choosen Signatures veryfied successfully!\n" );
234 aSignatureHelper
.EndMission();
236 QueryPrintSignatureDetails( aSignatureHelper
.GetSignatureInformations(), aSignatureHelper
.GetSecurityEnvironment() );