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