xref: /trunk/main/xmlsecurity/tools/demo/multisigdemo.cxx (revision e3436d4ae0b85884ae4c2c4aacc2424eed2fc63a)
106b3ce53SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
306b3ce53SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
406b3ce53SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
506b3ce53SAndrew Rist  * distributed with this work for additional information
606b3ce53SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
706b3ce53SAndrew Rist  * to you under the Apache License, Version 2.0 (the
806b3ce53SAndrew Rist  * "License"); you may not use this file except in compliance
906b3ce53SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
1106b3ce53SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
1306b3ce53SAndrew Rist  * Unless required by applicable law or agreed to in writing,
1406b3ce53SAndrew Rist  * software distributed under the License is distributed on an
1506b3ce53SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1606b3ce53SAndrew Rist  * KIND, either express or implied.  See the License for the
1706b3ce53SAndrew Rist  * specific language governing permissions and limitations
1806b3ce53SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
2006b3ce53SAndrew Rist  *************************************************************/
2106b3ce53SAndrew Rist 
2206b3ce53SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_xmlsecurity.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <stdio.h>
28cdf0e10cSrcweir #include "util.hxx"
29cdf0e10cSrcweir 
30cdf0e10cSrcweir #include <rtl/ustring.hxx>
31cdf0e10cSrcweir #include <cppuhelper/servicefactory.hxx>
32cdf0e10cSrcweir 
33cdf0e10cSrcweir #include <xmlsecurity/biginteger.hxx>
34cdf0e10cSrcweir #include <xmlsecurity/xmlsignaturehelper.hxx>
35cdf0e10cSrcweir #include "xmlsecurity/baseencoding.hxx"
36cdf0e10cSrcweir #include <tools/date.hxx>
37cdf0e10cSrcweir #include <tools/time.hxx>
38cdf0e10cSrcweir 
39cdf0e10cSrcweir using namespace ::com::sun::star;
40cdf0e10cSrcweir 
denyVerifyHandler(void *,void *)41cdf0e10cSrcweir long denyVerifyHandler( void *, void * )
42cdf0e10cSrcweir {
43cdf0e10cSrcweir     return  0;
44cdf0e10cSrcweir }
45cdf0e10cSrcweir 
startVerifyHandler(void *,void *)46cdf0e10cSrcweir long startVerifyHandler( void *, void * )
47cdf0e10cSrcweir {
48cdf0e10cSrcweir     return QueryVerifySignature();
49cdf0e10cSrcweir }
50cdf0e10cSrcweir 
main(int argc,char ** argv)51cdf0e10cSrcweir int SAL_CALL main( int argc, char **argv )
52cdf0e10cSrcweir {
53cdf0e10cSrcweir     if( argc < 5 )
54cdf0e10cSrcweir     {
55cdf0e10cSrcweir         fprintf( stderr, "Usage: %s <signature file 1> <signature file 2> <xml stream file> <binary stream file> [<cryptoken>]\n" , argv[0] ) ;
56cdf0e10cSrcweir         return -1 ;
57cdf0e10cSrcweir     }
58cdf0e10cSrcweir 
59cdf0e10cSrcweir     uno::Reference< lang::XMultiServiceFactory > xMSF = CreateDemoServiceFactory();
60cdf0e10cSrcweir 
61cdf0e10cSrcweir     rtl::OUString aSIGFileName = rtl::OUString::createFromAscii(argv[1]);
62cdf0e10cSrcweir     rtl::OUString aSIGFileName2 = rtl::OUString::createFromAscii(argv[2]);
63cdf0e10cSrcweir     rtl::OUString aXMLFileName = rtl::OUString::createFromAscii(argv[3]);
64cdf0e10cSrcweir     rtl::OUString aBINFileName = rtl::OUString::createFromAscii(argv[4]);
65cdf0e10cSrcweir     rtl::OUString aCryptoToken;
66cdf0e10cSrcweir     if ( argc >= 7 )
67cdf0e10cSrcweir         aCryptoToken = rtl::OUString::createFromAscii(argv[6]);
68cdf0e10cSrcweir 
69cdf0e10cSrcweir     sal_Int32 nSecurityId;
70cdf0e10cSrcweir     uno::Reference< io::XOutputStream > xOutputStream;
71cdf0e10cSrcweir     uno::Reference< io::XInputStream > xInputStream;
72cdf0e10cSrcweir     bool bDone;
73cdf0e10cSrcweir     SignatureInformations signatureInformations;
74cdf0e10cSrcweir     uno::Reference< ::com::sun::star::xml::sax::XDocumentHandler> xDocumentHandler;
75cdf0e10cSrcweir 
76cdf0e10cSrcweir     // -------- START -------
77cdf0e10cSrcweir 
78cdf0e10cSrcweir     XMLSignatureHelper aSignatureHelper( xMSF );
79cdf0e10cSrcweir 
80cdf0e10cSrcweir     bool bInit = aSignatureHelper.Init( aCryptoToken );
81cdf0e10cSrcweir     if ( !bInit )
82cdf0e10cSrcweir     {
83cdf0e10cSrcweir         fprintf( stderr, "Error initializing security context!\n" );
84cdf0e10cSrcweir         return -1;
85cdf0e10cSrcweir     }
86cdf0e10cSrcweir 
87cdf0e10cSrcweir     fprintf( stdout, "\n\nTEST MISSION 1: Create the first signature file\n");
88cdf0e10cSrcweir 
89cdf0e10cSrcweir     aSignatureHelper.StartMission();
90cdf0e10cSrcweir 
91cdf0e10cSrcweir     /*
92cdf0e10cSrcweir      * select a private key certificate
93cdf0e10cSrcweir      */
94cdf0e10cSrcweir     uno::Reference< xml::crypto::XSecurityEnvironment > xSecurityEnvironment = aSignatureHelper.GetSecurityEnvironment();
95cdf0e10cSrcweir     uno::Sequence< uno::Reference< ::com::sun::star::security::XCertificate > > xPersonalCerts = xSecurityEnvironment->getPersonalCertificates() ;
96cdf0e10cSrcweir 
97cdf0e10cSrcweir     fprintf( stdout, "\nPlease select two certificates:\n" );
98cdf0e10cSrcweir 
99cdf0e10cSrcweir     for ( int nSig = 0; nSig < 2; nSig++ )
100cdf0e10cSrcweir     {
101cdf0e10cSrcweir         // New security ID for signature...
102cdf0e10cSrcweir         nSecurityId = aSignatureHelper.GetNewSecurityId();
103cdf0e10cSrcweir 
104cdf0e10cSrcweir         // Select certificate...
105cdf0e10cSrcweir         uno::Reference< ::com::sun::star::security::XCertificate > xPersonalCert = getCertificateFromEnvironment( xSecurityEnvironment, true );
106cdf0e10cSrcweir         aSignatureHelper.SetX509Certificate(
107cdf0e10cSrcweir             nSecurityId, xPersonalCert->getIssuerName(),
108cdf0e10cSrcweir             bigIntegerToNumericString( xPersonalCert->getSerialNumber()),
109cdf0e10cSrcweir             baseEncode(xPersonalCert->getEncoded(), BASE64));
110cdf0e10cSrcweir         aSignatureHelper.AddForSigning( nSecurityId, aXMLFileName, aXMLFileName, sal_False );
111cdf0e10cSrcweir         aSignatureHelper.AddForSigning( nSecurityId, aBINFileName, aBINFileName, sal_True );
112cdf0e10cSrcweir         aSignatureHelper.SetDateTime( nSecurityId, Date(), Time() );
113cdf0e10cSrcweir     }
114cdf0e10cSrcweir     /*
115cdf0e10cSrcweir      * creates signature
116cdf0e10cSrcweir      */
117cdf0e10cSrcweir     xOutputStream = OpenOutputStream( aSIGFileName );
118cdf0e10cSrcweir     bDone = aSignatureHelper.CreateAndWriteSignature( xOutputStream );
119cdf0e10cSrcweir     if ( !bDone )
120cdf0e10cSrcweir         fprintf( stderr, "\nSTATUS MISSION 1: Error creating Signature!\n" );
121cdf0e10cSrcweir     else
122cdf0e10cSrcweir         fprintf( stdout, "\nSTATUS MISSION 1: Signature successfully created!\n" );
123cdf0e10cSrcweir 
124cdf0e10cSrcweir     aSignatureHelper.EndMission();
125cdf0e10cSrcweir 
126cdf0e10cSrcweir 
127cdf0e10cSrcweir     fprintf( stdout, "\n\nTEST MISSION 2: Transfer the second signature to a new signature file\n");
128cdf0e10cSrcweir 
129cdf0e10cSrcweir     /*
130cdf0e10cSrcweir      * You can use an uninitialized SignatureHelper to perform this mission.
131cdf0e10cSrcweir      */
132cdf0e10cSrcweir 
133cdf0e10cSrcweir     /*
134c2cc1a3cSJohn Bampton      * configures the start-verify handler. Don't need to verify for transferring...
135cdf0e10cSrcweir      */
136cdf0e10cSrcweir     aSignatureHelper.SetStartVerifySignatureHdl( Link( NULL, denyVerifyHandler ) );
137cdf0e10cSrcweir     aSignatureHelper.StartMission();
138cdf0e10cSrcweir 
139cdf0e10cSrcweir     xInputStream = OpenInputStream( aSIGFileName );
140cdf0e10cSrcweir     bDone = aSignatureHelper.ReadAndVerifySignature( xInputStream );
141cdf0e10cSrcweir     xInputStream->closeInput();
142cdf0e10cSrcweir 
143cdf0e10cSrcweir     if ( !bDone )
144cdf0e10cSrcweir         fprintf( stderr, "\nSTATUS MISSION 2: Error in reading Signature!\n" );
145cdf0e10cSrcweir     else
146c2cc1a3cSJohn Bampton         fprintf( stdout, "\nSTATUS MISSION 2: Signature successfully transferred!\n" );
147cdf0e10cSrcweir 
148cdf0e10cSrcweir     /*
149cdf0e10cSrcweir      * get all signature information
150cdf0e10cSrcweir      */
151cdf0e10cSrcweir     signatureInformations = aSignatureHelper.GetSignatureInformations();
152cdf0e10cSrcweir 
153cdf0e10cSrcweir     /*
154cdf0e10cSrcweir      * write the first signature into the second signature file.
155cdf0e10cSrcweir      */
156cdf0e10cSrcweir 
157cdf0e10cSrcweir     xOutputStream = OpenOutputStream( aSIGFileName2 );
158cdf0e10cSrcweir     xDocumentHandler = aSignatureHelper.CreateDocumentHandlerWithHeader( xOutputStream);
159cdf0e10cSrcweir     aSignatureHelper.ExportSignature( xDocumentHandler, signatureInformations[1]);
160cdf0e10cSrcweir     aSignatureHelper.CloseDocumentHandler( xDocumentHandler);
161cdf0e10cSrcweir     aSignatureHelper.EndMission();
162cdf0e10cSrcweir 
163cdf0e10cSrcweir     fprintf( stdout, "\n\nTEST MISSION 3: Insert a new signature to the first signature file\n");
164cdf0e10cSrcweir 
165cdf0e10cSrcweir     aSignatureHelper.StartMission();
166cdf0e10cSrcweir 
167cdf0e10cSrcweir     nSecurityId = aSignatureHelper.GetNewSecurityId();
168cdf0e10cSrcweir 
169cdf0e10cSrcweir     // Select certificate...
170cdf0e10cSrcweir     uno::Reference< ::com::sun::star::security::XCertificate > xPersonalCert = getCertificateFromEnvironment( xSecurityEnvironment, true );
171cdf0e10cSrcweir     aSignatureHelper.SetX509Certificate(
172cdf0e10cSrcweir         nSecurityId, xPersonalCert->getIssuerName(),
173cdf0e10cSrcweir         bigIntegerToNumericString( xPersonalCert->getSerialNumber()),
174cdf0e10cSrcweir         baseEncode(xPersonalCert->getEncoded(), BASE64));
175cdf0e10cSrcweir     aSignatureHelper.AddForSigning( nSecurityId, aXMLFileName, aXMLFileName, sal_False );
176cdf0e10cSrcweir     aSignatureHelper.AddForSigning( nSecurityId, aBINFileName, aBINFileName, sal_True );
177cdf0e10cSrcweir     aSignatureHelper.SetDateTime( nSecurityId, Date(), Time() );
178cdf0e10cSrcweir 
179cdf0e10cSrcweir 
180cdf0e10cSrcweir     xOutputStream = OpenOutputStream( aSIGFileName );
181cdf0e10cSrcweir     xDocumentHandler = aSignatureHelper.CreateDocumentHandlerWithHeader( xOutputStream);
182cdf0e10cSrcweir 
183cdf0e10cSrcweir     aSignatureHelper.ExportSignature( xDocumentHandler, signatureInformations[0]);
184cdf0e10cSrcweir     bDone = aSignatureHelper.CreateAndWriteSignature( xDocumentHandler );
185cdf0e10cSrcweir     aSignatureHelper.ExportSignature( xDocumentHandler, signatureInformations[1]);
186cdf0e10cSrcweir     aSignatureHelper.CloseDocumentHandler( xDocumentHandler);
187cdf0e10cSrcweir 
188cdf0e10cSrcweir     if ( !bDone )
189cdf0e10cSrcweir         fprintf( stderr, "\nSTATUS MISSION 3: Error creating Signature!\n" );
190cdf0e10cSrcweir     else
191cdf0e10cSrcweir         fprintf( stdout, "\nSTATUS MISSION 3: Signature successfully created!\n" );
192cdf0e10cSrcweir 
193cdf0e10cSrcweir     aSignatureHelper.EndMission();
194cdf0e10cSrcweir 
195cdf0e10cSrcweir     fprintf( stdout, "\n\nTEST MISSION 4 : Verify the first signature file\n");
196cdf0e10cSrcweir 
197cdf0e10cSrcweir     aSignatureHelper.SetStartVerifySignatureHdl( Link( NULL, startVerifyHandler ) );
198cdf0e10cSrcweir 
199cdf0e10cSrcweir     aSignatureHelper.StartMission();
200cdf0e10cSrcweir 
201cdf0e10cSrcweir     xInputStream = OpenInputStream( aSIGFileName );
202cdf0e10cSrcweir     bDone = aSignatureHelper.ReadAndVerifySignature( xInputStream );
203cdf0e10cSrcweir     xInputStream->closeInput();
204cdf0e10cSrcweir 
205cdf0e10cSrcweir     if ( !bDone )
206cdf0e10cSrcweir         fprintf( stderr, "\nSTATUS MISSION 4: Error verifying Signatures!\n" );
207cdf0e10cSrcweir     else
208*e3436d4aSJohn Bampton         fprintf( stdout, "\nSTATUS MISSION 4: All chosen Signatures verified successfully!\n" );
209cdf0e10cSrcweir 
210cdf0e10cSrcweir     aSignatureHelper.EndMission();
211cdf0e10cSrcweir 
212cdf0e10cSrcweir     QueryPrintSignatureDetails( aSignatureHelper.GetSignatureInformations(), aSignatureHelper.GetSecurityEnvironment() );
213cdf0e10cSrcweir 
214cdf0e10cSrcweir     fprintf( stdout, "\n\nTEST MISSION 5: Verify the second signature file\n");
215cdf0e10cSrcweir 
216cdf0e10cSrcweir     aSignatureHelper.StartMission();
217cdf0e10cSrcweir 
218cdf0e10cSrcweir     xInputStream = OpenInputStream( aSIGFileName2 );
219cdf0e10cSrcweir     bDone = aSignatureHelper.ReadAndVerifySignature( xInputStream );
220cdf0e10cSrcweir     xInputStream->closeInput();
221cdf0e10cSrcweir 
222cdf0e10cSrcweir     if ( !bDone )
223cdf0e10cSrcweir         fprintf( stderr, "\nSTATUS MISSION 5: Error verifying Signatures!\n" );
224cdf0e10cSrcweir     else
225*e3436d4aSJohn Bampton         fprintf( stdout, "\nSTATUS MISSION 5: All chosen Signatures verified successfully!\n" );
226cdf0e10cSrcweir 
227cdf0e10cSrcweir     aSignatureHelper.EndMission();
228cdf0e10cSrcweir 
229cdf0e10cSrcweir     QueryPrintSignatureDetails( aSignatureHelper.GetSignatureInformations(), aSignatureHelper.GetSecurityEnvironment() );
230cdf0e10cSrcweir 
231cdf0e10cSrcweir     return 0;
232cdf0e10cSrcweir }
233