1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 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. 14 * 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). 20 * 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. 25 * 26 ************************************************************************/ 27 28 // MARKER(update_precomp.py): autogen include statement, do not remove 29 #include "precompiled_xmlsecurity.hxx" 30 31 #include <stdio.h> 32 #include "util.hxx" 33 34 #include <rtl/ustring.hxx> 35 #include <cppuhelper/servicefactory.hxx> 36 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> 42 43 using namespace ::com::sun::star; 44 45 long denyVerifyHandler( void *, void * ) 46 { 47 return 0; 48 } 49 50 long startVerifyHandler( void *, void * ) 51 { 52 return QueryVerifySignature(); 53 } 54 55 int SAL_CALL main( int argc, char **argv ) 56 { 57 if( argc < 5 ) 58 { 59 fprintf( stderr, "Usage: %s <signature file 1> <signature file 2> <xml stream file> <binary stream file> [<cryptoken>]\n" , argv[0] ) ; 60 return -1 ; 61 } 62 63 uno::Reference< lang::XMultiServiceFactory > xMSF = CreateDemoServiceFactory(); 64 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; 70 if ( argc >= 7 ) 71 aCryptoToken = rtl::OUString::createFromAscii(argv[6]); 72 73 sal_Int32 nSecurityId; 74 uno::Reference< io::XOutputStream > xOutputStream; 75 uno::Reference< io::XInputStream > xInputStream; 76 bool bDone; 77 SignatureInformations signatureInformations; 78 uno::Reference< ::com::sun::star::xml::sax::XDocumentHandler> xDocumentHandler; 79 80 // -------- START ------- 81 82 XMLSignatureHelper aSignatureHelper( xMSF ); 83 84 bool bInit = aSignatureHelper.Init( aCryptoToken ); 85 if ( !bInit ) 86 { 87 fprintf( stderr, "Error initializing security context!\n" ); 88 return -1; 89 } 90 91 fprintf( stdout, "\n\nTEST MISSION 1: Create the first signature file\n"); 92 93 aSignatureHelper.StartMission(); 94 95 /* 96 * select a private key certificate 97 */ 98 uno::Reference< xml::crypto::XSecurityEnvironment > xSecurityEnvironment = aSignatureHelper.GetSecurityEnvironment(); 99 uno::Sequence< uno::Reference< ::com::sun::star::security::XCertificate > > xPersonalCerts = xSecurityEnvironment->getPersonalCertificates() ; 100 101 fprintf( stdout, "\nPlease select two certificates:\n" ); 102 103 for ( int nSig = 0; nSig < 2; nSig++ ) 104 { 105 // New security ID for signature... 106 nSecurityId = aSignatureHelper.GetNewSecurityId(); 107 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() ); 117 } 118 /* 119 * creates signature 120 */ 121 xOutputStream = OpenOutputStream( aSIGFileName ); 122 bDone = aSignatureHelper.CreateAndWriteSignature( xOutputStream ); 123 if ( !bDone ) 124 fprintf( stderr, "\nSTATUS MISSION 1: Error creating Signature!\n" ); 125 else 126 fprintf( stdout, "\nSTATUS MISSION 1: Signature successfully created!\n" ); 127 128 aSignatureHelper.EndMission(); 129 130 131 fprintf( stdout, "\n\nTEST MISSION 2: Transfer the second signature to a new signature file\n"); 132 133 /* 134 * You can use an uninitialized SignatureHelper to perform this mission. 135 */ 136 137 /* 138 * configures the start-verify handler. Don't need to verify for transfering... 139 */ 140 aSignatureHelper.SetStartVerifySignatureHdl( Link( NULL, denyVerifyHandler ) ); 141 aSignatureHelper.StartMission(); 142 143 xInputStream = OpenInputStream( aSIGFileName ); 144 bDone = aSignatureHelper.ReadAndVerifySignature( xInputStream ); 145 xInputStream->closeInput(); 146 147 if ( !bDone ) 148 fprintf( stderr, "\nSTATUS MISSION 2: Error in reading Signature!\n" ); 149 else 150 fprintf( stdout, "\nSTATUS MISSION 2: Signature successfully transfered!\n" ); 151 152 /* 153 * get all signature information 154 */ 155 signatureInformations = aSignatureHelper.GetSignatureInformations(); 156 157 /* 158 * write the first signature into the second signature file. 159 */ 160 161 xOutputStream = OpenOutputStream( aSIGFileName2 ); 162 xDocumentHandler = aSignatureHelper.CreateDocumentHandlerWithHeader( xOutputStream); 163 aSignatureHelper.ExportSignature( xDocumentHandler, signatureInformations[1]); 164 aSignatureHelper.CloseDocumentHandler( xDocumentHandler); 165 aSignatureHelper.EndMission(); 166 167 fprintf( stdout, "\n\nTEST MISSION 3: Insert a new signature to the first signature file\n"); 168 169 aSignatureHelper.StartMission(); 170 171 nSecurityId = aSignatureHelper.GetNewSecurityId(); 172 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() ); 182 183 184 xOutputStream = OpenOutputStream( aSIGFileName ); 185 xDocumentHandler = aSignatureHelper.CreateDocumentHandlerWithHeader( xOutputStream); 186 187 aSignatureHelper.ExportSignature( xDocumentHandler, signatureInformations[0]); 188 bDone = aSignatureHelper.CreateAndWriteSignature( xDocumentHandler ); 189 aSignatureHelper.ExportSignature( xDocumentHandler, signatureInformations[1]); 190 aSignatureHelper.CloseDocumentHandler( xDocumentHandler); 191 192 if ( !bDone ) 193 fprintf( stderr, "\nSTATUS MISSION 3: Error creating Signature!\n" ); 194 else 195 fprintf( stdout, "\nSTATUS MISSION 3: Signature successfully created!\n" ); 196 197 aSignatureHelper.EndMission(); 198 199 fprintf( stdout, "\n\nTEST MISSION 4 : Verify the first signature file\n"); 200 201 aSignatureHelper.SetStartVerifySignatureHdl( Link( NULL, startVerifyHandler ) ); 202 203 aSignatureHelper.StartMission(); 204 205 xInputStream = OpenInputStream( aSIGFileName ); 206 bDone = aSignatureHelper.ReadAndVerifySignature( xInputStream ); 207 xInputStream->closeInput(); 208 209 if ( !bDone ) 210 fprintf( stderr, "\nSTATUS MISSION 4: Error verifying Signatures!\n" ); 211 else 212 fprintf( stdout, "\nSTATUS MISSION 4: All choosen Signatures veryfied successfully!\n" ); 213 214 aSignatureHelper.EndMission(); 215 216 QueryPrintSignatureDetails( aSignatureHelper.GetSignatureInformations(), aSignatureHelper.GetSecurityEnvironment() ); 217 218 fprintf( stdout, "\n\nTEST MISSION 5: Verify the second signature file\n"); 219 220 aSignatureHelper.StartMission(); 221 222 xInputStream = OpenInputStream( aSIGFileName2 ); 223 bDone = aSignatureHelper.ReadAndVerifySignature( xInputStream ); 224 xInputStream->closeInput(); 225 226 if ( !bDone ) 227 fprintf( stderr, "\nSTATUS MISSION 5: Error verifying Signatures!\n" ); 228 else 229 fprintf( stdout, "\nSTATUS MISSION 5: All choosen Signatures veryfied successfully!\n" ); 230 231 aSignatureHelper.EndMission(); 232 233 QueryPrintSignatureDetails( aSignatureHelper.GetSignatureInformations(), aSignatureHelper.GetSecurityEnvironment() ); 234 235 return 0; 236 } 237