merged tag ooo/OOO330_m14
[LibreOffice.git] / xmlsecurity / tools / demo / signdemo.cxx
blob3cfad147beb35b48abd25525387d0fee05623b8f
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"
31 #include "util.hxx"
33 #include <stdio.h>
34 #include <tools/date.hxx>
35 #include <tools/time.hxx>
36 #include <cppuhelper/servicefactory.hxx>
38 #include <xmlsecurity/biginteger.hxx>
39 #include <xmlsecurity/xmlsignaturehelper.hxx>
40 #include "xmlsecurity/baseencoding.hxx"
42 using namespace ::com::sun::star;
44 int SAL_CALL main( int argc, char **argv )
46 if( argc < 4 )
48 fprintf( stderr, "Usage: %s <signature file> <xml stream file> <binary stream file> [<cryptoken>]\n" , argv[0] ) ;
49 return -1 ;
52 rtl::OUString aSIGFileName = rtl::OUString::createFromAscii(argv[1]);
53 rtl::OUString aXMLFileName = rtl::OUString::createFromAscii(argv[2]);
54 rtl::OUString aBINFileName = rtl::OUString::createFromAscii(argv[3]);
55 rtl::OUString aCryptoToken;
56 if ( argc >= 5 )
57 aCryptoToken = rtl::OUString::createFromAscii(argv[4]);
59 uno::Reference< lang::XMultiServiceFactory > xMSF = CreateDemoServiceFactory();
62 * creates a signature helper
64 XMLSignatureHelper aSignatureHelper( xMSF );
67 * creates a security context.
69 bool bInit = aSignatureHelper.Init( aCryptoToken );
70 if ( !bInit )
72 fprintf( stderr, "Error initializing security context!\n" );
73 return -1;
76 aSignatureHelper.StartMission();
79 * select a private key certificate
81 sal_Int32 i;
82 sal_Int32 nEnvCount = aSignatureHelper.GetSecurityEnvironmentNumber();
83 if( nEnvCount == 0 )
85 fprintf( stdout, "\nNo SecurityEnvironment found!\n" ) ;
86 return -1;
89 uno::Sequence< uno::Reference< xml::crypto::XSecurityEnvironment > > xSecurityEnvironments(nEnvCount) ;
90 for( i=0; i < nEnvCount; i++ )
91 xSecurityEnvironments[i] = aSignatureHelper.GetSecurityEnvironmentByIndex(i);
93 fprintf( stdout, "\nSelect a SecurityEnvironment:\n" ) ;
94 for( i = 0; i < nEnvCount; i ++ )
95 fprintf( stdout, "\n[%d] %s", i+1, rtl::OUStringToOString( xSecurityEnvironments[i]->getSecurityEnvironmentInformation() ,RTL_TEXTENCODING_ASCII_US ).getStr());
97 sal_Int32 nEnvIndex = QuerySelectNumber( 1, nEnvCount ) -1;
99 uno::Reference< ::com::sun::star::security::XCertificate > xPersonalCert = getCertificateFromEnvironment(xSecurityEnvironments[nEnvIndex], true);
101 if ( !xPersonalCert.is() )
103 fprintf( stdout, "No certificate choosen - exit.\n" );
104 return (-2);
108 * creates a new signature id
110 sal_Int32 nSecurityId = aSignatureHelper.GetNewSecurityId();
113 * configures the X509 certificate
115 aSignatureHelper.SetX509Certificate(
116 nSecurityId, nEnvIndex,
117 xPersonalCert->getIssuerName(),
118 bigIntegerToNumericString( xPersonalCert->getSerialNumber()),
119 baseEncode(xPersonalCert->getEncoded(), BASE64));
122 * configures date/time
124 aSignatureHelper.SetDateTime( nSecurityId, Date(), Time());
127 * signs the xml stream
129 aSignatureHelper.AddForSigning( nSecurityId, aXMLFileName, aXMLFileName, sal_False );
132 * signs the binary stream
134 aSignatureHelper.AddForSigning( nSecurityId, aBINFileName, aBINFileName, sal_True );
137 * creates signature
139 uno::Reference< io::XOutputStream > xOutputStream = OpenOutputStream( aSIGFileName );
140 bool bDone = aSignatureHelper.CreateAndWriteSignature( xOutputStream );
142 if ( !bDone )
144 fprintf( stderr, "\nSTATUS: Error creating Signature!\n" );
146 else
148 fprintf( stdout, "\nSTATUS: Signature successfully created!\n" );
151 aSignatureHelper.EndMission();
153 QueryPrintSignatureDetails( aSignatureHelper.GetSignatureInformations(), aSignatureHelper.GetSecurityEnvironment() );
155 return 0;