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