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 "util.hxx"
32 
33 #include <stdio.h>
34 #include <tools/date.hxx>
35 #include <tools/time.hxx>
36 #include <cppuhelper/servicefactory.hxx>
37 
38 #include <xmlsecurity/biginteger.hxx>
39 #include <xmlsecurity/xmlsignaturehelper.hxx>
40 #include "xmlsecurity/baseencoding.hxx"
41 
42 using namespace ::com::sun::star;
43 
44 int SAL_CALL main( int argc, char **argv )
45 {
46 	if( argc < 4 )
47 	{
48 		fprintf( stderr, "Usage: %s <signature file> <xml stream file> <binary stream file> [<cryptoken>]\n" , argv[0] ) ;
49 		return -1 ;
50 	}
51 
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]);
58 
59 	uno::Reference< lang::XMultiServiceFactory > xMSF = CreateDemoServiceFactory();
60 
61 	/*
62 	 * creates a signature helper
63 	 */
64 	XMLSignatureHelper aSignatureHelper( xMSF );
65 
66 	/*
67 	 * creates a security context.
68 	 */
69 	bool bInit = aSignatureHelper.Init( aCryptoToken );
70 	if ( !bInit )
71 	{
72 		fprintf( stderr, "Error initializing security context!\n" );
73 		return -1;
74 	}
75 
76 	aSignatureHelper.StartMission();
77 
78 	/*
79 	 * select a private key certificate
80 	 */
81 	sal_Int32 i;
82 	sal_Int32 nEnvCount = aSignatureHelper.GetSecurityEnvironmentNumber();
83 	if( nEnvCount == 0 )
84 	{
85 		fprintf( stdout, "\nNo SecurityEnvironment found!\n" ) ;
86 		return -1;
87 	}
88 
89 	uno::Sequence< uno::Reference< xml::crypto::XSecurityEnvironment > > xSecurityEnvironments(nEnvCount) ;
90 	for( i=0; i < nEnvCount; i++ )
91 		xSecurityEnvironments[i] = aSignatureHelper.GetSecurityEnvironmentByIndex(i);
92 
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());
96 
97 	sal_Int32 nEnvIndex = QuerySelectNumber( 1, nEnvCount ) -1;
98 
99 	uno::Reference< ::com::sun::star::security::XCertificate > xPersonalCert = getCertificateFromEnvironment(xSecurityEnvironments[nEnvIndex], true);
100 
101 	if ( !xPersonalCert.is() )
102 	{
103 		fprintf( stdout, "No certificate choosen - exit.\n" );
104 		return (-2);
105 	}
106 
107 	/*
108 	 * creates a new signature id
109 	 */
110 	sal_Int32 nSecurityId = aSignatureHelper.GetNewSecurityId();
111 
112 	/*
113 	 * configures the X509 certificate
114 	 */
115 	aSignatureHelper.SetX509Certificate(
116         nSecurityId, nEnvIndex,
117         xPersonalCert->getIssuerName(),
118         bigIntegerToNumericString( xPersonalCert->getSerialNumber()),
119         baseEncode(xPersonalCert->getEncoded(), BASE64));
120 
121 	/*
122 	 * configures date/time
123 	 */
124 	aSignatureHelper.SetDateTime( nSecurityId, Date(), Time());
125 
126 	/*
127 	 * signs the xml stream
128 	 */
129 	aSignatureHelper.AddForSigning( nSecurityId, aXMLFileName, aXMLFileName, sal_False );
130 
131 	/*
132 	 * signs the binary stream
133 	 */
134 	aSignatureHelper.AddForSigning( nSecurityId, aBINFileName, aBINFileName, sal_True );
135 
136 	/*
137 	 * creates signature
138 	 */
139 	uno::Reference< io::XOutputStream > xOutputStream = OpenOutputStream( aSIGFileName );
140 	bool bDone = aSignatureHelper.CreateAndWriteSignature( xOutputStream );
141 
142 	if ( !bDone )
143 	{
144 		fprintf( stderr, "\nSTATUS: Error creating Signature!\n" );
145 	}
146 	else
147 	{
148 		fprintf( stdout, "\nSTATUS: Signature successfully created!\n" );
149 	}
150 
151 	aSignatureHelper.EndMission();
152 
153 	QueryPrintSignatureDetails( aSignatureHelper.GetSignatureInformations(), aSignatureHelper.GetSecurityEnvironment() );
154 
155 	return 0;
156 }
157 
158