update dev300-m58
[ooovba.git] / xmlsecurity / tools / demo / util2.cxx
blob5fe036fc00f2d62655528dbe28db353c912fb1bc
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: util2.cxx,v $
10 * $Revision: 1.6 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_xmlsecurity.hxx"
34 #include <rtl/locale.h>
35 #include <osl/nlsupport.h>
36 #include <osl/process.h>
38 #include <util.hxx>
40 #include <stdio.h>
42 #include <com/sun/star/registry/XImplementationRegistration.hpp>
43 #include <com/sun/star/security/KeyUsage.hpp>
44 #include <cppuhelper/bootstrap.hxx>
45 #include <xmlsecurity/biginteger.hxx>
46 #include <comphelper/processfactory.hxx>
47 #include <unotools/streamhelper.hxx>
49 #include <rtl/ustrbuf.hxx>
50 #include <tools/string.hxx>
52 namespace cssu = com::sun::star::uno;
53 namespace cssl = com::sun::star::lang;
54 namespace cssxc = com::sun::star::xml::crypto;
55 namespace cssi = com::sun::star::io;
57 using namespace ::com::sun::star;
59 /** convert util::DateTime to ISO Date String */
60 void convertDateTime( ::rtl::OUStringBuffer& rBuffer,
61 const com::sun::star::util::DateTime& rDateTime )
63 String aString( String::CreateFromInt32( rDateTime.Year ) );
64 aString += '-';
65 if( rDateTime.Month < 10 )
66 aString += '0';
67 aString += String::CreateFromInt32( rDateTime.Month );
68 aString += '-';
69 if( rDateTime.Day < 10 )
70 aString += '0';
71 aString += String::CreateFromInt32( rDateTime.Day );
73 if( rDateTime.Seconds != 0 ||
74 rDateTime.Minutes != 0 ||
75 rDateTime.Hours != 0 )
77 aString += 'T';
78 if( rDateTime.Hours < 10 )
79 aString += '0';
80 aString += String::CreateFromInt32( rDateTime.Hours );
81 aString += ':';
82 if( rDateTime.Minutes < 10 )
83 aString += '0';
84 aString += String::CreateFromInt32( rDateTime.Minutes );
85 aString += ':';
86 if( rDateTime.Seconds < 10 )
87 aString += '0';
88 aString += String::CreateFromInt32( rDateTime.Seconds );
89 if ( rDateTime.HundredthSeconds > 0)
91 aString += ',';
92 if (rDateTime.HundredthSeconds < 10)
93 aString += '0';
94 aString += String::CreateFromInt32( rDateTime.HundredthSeconds );
98 rBuffer.append( aString );
101 ::rtl::OUString printHexString(cssu::Sequence< sal_Int8 > data)
103 int length = data.getLength();
104 ::rtl::OUString result;
106 char number[4];
107 for (int j=0; j<length; j++)
109 sprintf(number, "%02X ", (unsigned char)data[j]);
110 result += rtl::OUString::createFromAscii( number );
113 return result;
117 ::rtl::OUString getSignatureInformation(
118 const SignatureInformation& infor,
119 cssu::Reference< ::com::sun::star::xml::crypto::XSecurityEnvironment >& xSecurityEnvironment )
121 char* status[50] = {
122 "STATUS_UNKNOWN",
123 "OPERATION_SUCCEEDED",
124 "RUNTIMEERROR_FAILED",
125 "ENGINE_FAILED",
126 "MALLOC_FAILED",
127 "STRDUP_FAILED",
128 "CRYPTO_FAILED",
129 "XML_FAILED",
130 "XSLT_FAILED",
131 "IO_FAILED",
132 "DISABLED",
133 "NOT_IMPLEMENTED",
134 "INVALID_SIZE",
135 "INVALID_DATA",
136 "INVALID_RESULT",
137 "INVALID_TYPE",
138 "INVALID_OPERATION",
139 "INVALID_STATUS",
140 "INVALID_FORMAT",
141 "DATA_NOT_MATCH",
142 "INVALID_NODE",
143 "INVALID_NODE_CONTENT",
144 "INVALID_NODE_ATTRIBUTE",
145 "MISSING_NODE_ATTRIBUTE",
146 "NODE_ALREADY_PRESENT",
147 "UNEXPECTED_NODE",
148 "NODE_NOT_FOUND",
149 "INVALID_TRANSFORM",
150 "INVALID_TRANSFORM_KEY",
151 "INVALID_URI_TYPE",
152 "TRANSFORM_SAME_DOCUMENT_REQUIRED",
153 "TRANSFORM_DISABLED",
154 "INVALID_KEY_DATA",
155 "KEY_DATA_NOT_FOUND",
156 "KEY_DATA_ALREADY_EXIST",
157 "INVALID_KEY_DATA_SIZE",
158 "KEY_NOT_FOUND",
159 "KEYDATA_DISABLED",
160 "MAX_RETRIEVALS_LEVEL",
161 "MAX_RETRIEVAL_TYPE_MISMATCH",
162 "MAX_ENCKEY_LEVEL",
163 "CERT_VERIFY_FAILED",
164 "CERT_NOT_FOUND",
165 "CERT_REVOKED",
166 "CERT_ISSUER_FAILED",
167 "CERT_NOT_YET_VALID",
168 "CERT_HAS_EXPIRED",
169 "DSIG_NO_REFERENCES",
170 "DSIG_INVALID_REFERENCE",
171 "ASSERTION"};
173 rtl::OUString result;
175 result += rtl::OUString::createFromAscii( "Security Id : " )
176 +rtl::OUString::valueOf(infor.nSecurityId)
177 +rtl::OUString::createFromAscii( "\n" );
178 result += rtl::OUString::createFromAscii( "Status : [" )
179 +rtl::OUString::valueOf((sal_Int32)(infor.nStatus))
180 +rtl::OUString::createFromAscii( "] " )
181 +rtl::OUString::createFromAscii(status[infor.nStatus])
182 +rtl::OUString::createFromAscii( "\n" );
184 const SignatureReferenceInformations& rInfors = infor.vSignatureReferenceInfors;
185 int i;
186 int size = rInfors.size();
188 result += rtl::OUString::createFromAscii( "--References :\n" );
189 for (i=0; i<size; i++)
191 result += rtl::OUString::createFromAscii( "---URI : " );
192 result += rInfors[i].ouURI;
193 result += rtl::OUString::createFromAscii( "\n" );
194 result += rtl::OUString::createFromAscii( "---DigestValue : " );
195 result += rInfors[i].ouDigestValue;
196 result += rtl::OUString::createFromAscii( "\n" );
199 if (infor.ouX509IssuerName.getLength()>0)
201 result += rtl::OUString::createFromAscii( "--X509IssuerName :\n" );
202 result += infor.ouX509IssuerName;
203 result += rtl::OUString::createFromAscii( "\n" );
206 if (infor.ouX509SerialNumber.getLength()>0)
208 result += rtl::OUString::createFromAscii( "--X509SerialNumber :\n" );
209 result += infor.ouX509SerialNumber;
210 result += rtl::OUString::createFromAscii( "\n" );
213 if (infor.ouX509Certificate.getLength()>0)
215 result += rtl::OUString::createFromAscii( "--X509Certificate :\n" );
216 result += infor.ouX509Certificate;
217 result += rtl::OUString::createFromAscii( "\n" );
220 if (infor.ouSignatureValue.getLength()>0)
222 result += rtl::OUString::createFromAscii( "--SignatureValue :\n" );
223 result += infor.ouSignatureValue;
224 result += rtl::OUString::createFromAscii( "\n" );
227 result += rtl::OUString::createFromAscii( "--Date :\n" );
229 ::rtl::OUStringBuffer buffer;
230 convertDateTime( buffer, infor.stDateTime );
231 result += buffer.makeStringAndClear();
232 result += rtl::OUString::createFromAscii( "\n" );
234 if (infor.ouX509IssuerName.getLength()>0 && infor.ouX509SerialNumber.getLength()>0 && xSecurityEnvironment.is())
236 result += rtl::OUString::createFromAscii( "--Certificate Path :\n" );
237 cssu::Reference< ::com::sun::star::security::XCertificate > xCert = xSecurityEnvironment->getCertificate( infor.ouX509IssuerName, numericStringToBigInteger(infor.ouX509SerialNumber) );
238 cssu::Sequence < cssu::Reference< ::com::sun::star::security::XCertificate > > xCertPath;
239 if(! xCert.is() )
241 fprintf(stdout , " xCert is NULL , so can not buildCertificatePath\n");
242 return result ;
244 else
246 xCertPath = xSecurityEnvironment->buildCertificatePath( xCert ) ;
249 for( int i = 0; i < xCertPath.getLength(); i++ )
251 result += xCertPath[i]->getSubjectName();
252 result += rtl::OUString::createFromAscii( "\n Subject public key algorithm : " );
253 result += xCertPath[i]->getSubjectPublicKeyAlgorithm();
254 result += rtl::OUString::createFromAscii( "\n Signature algorithm : " );
255 result += xCertPath[i]->getSignatureAlgorithm();
257 result += rtl::OUString::createFromAscii( "\n Subject public key value : " );
258 cssu::Sequence< sal_Int8 > keyValue = xCertPath[i]->getSubjectPublicKeyValue();
259 result += printHexString(keyValue);
261 result += rtl::OUString::createFromAscii( "\n Thumbprint (SHA1) : " );
262 cssu::Sequence< sal_Int8 > SHA1Thumbprint = xCertPath[i]->getSHA1Thumbprint();
263 result += printHexString(SHA1Thumbprint);
265 result += rtl::OUString::createFromAscii( "\n Thumbprint (MD5) : " );
266 cssu::Sequence< sal_Int8 > MD5Thumbprint = xCertPath[i]->getMD5Thumbprint();
267 result += printHexString(MD5Thumbprint);
269 result += rtl::OUString::createFromAscii( "\n <<\n" );
272 result += rtl::OUString::createFromAscii( "\n Key Usage : " );
273 sal_Int32 usage = xCert->getCertificateUsage();
275 if (usage & ::com::sun::star::security::KeyUsage::DIGITAL_SIGNATURE)
277 result += rtl::OUString::createFromAscii( "DIGITAL_SIGNATURE " );
280 if (usage & ::com::sun::star::security::KeyUsage::NON_REPUDIATION)
282 result += rtl::OUString::createFromAscii( "NON_REPUDIATION " );
285 if (usage & ::com::sun::star::security::KeyUsage::KEY_ENCIPHERMENT)
287 result += rtl::OUString::createFromAscii( "KEY_ENCIPHERMENT " );
290 if (usage & ::com::sun::star::security::KeyUsage::DATA_ENCIPHERMENT)
292 result += rtl::OUString::createFromAscii( "DATA_ENCIPHERMENT " );
295 if (usage & ::com::sun::star::security::KeyUsage::KEY_AGREEMENT)
297 result += rtl::OUString::createFromAscii( "KEY_AGREEMENT " );
300 if (usage & ::com::sun::star::security::KeyUsage::KEY_CERT_SIGN)
302 result += rtl::OUString::createFromAscii( "KEY_CERT_SIGN " );
305 if (usage & ::com::sun::star::security::KeyUsage::CRL_SIGN)
307 result += rtl::OUString::createFromAscii( "CRL_SIGN " );
310 result += rtl::OUString::createFromAscii( "\n" );
313 result += rtl::OUString::createFromAscii( "\n" );
314 return result;
317 ::rtl::OUString getSignatureInformations(
318 const SignatureInformations& SignatureInformations,
319 cssu::Reference< ::com::sun::star::xml::crypto::XSecurityEnvironment > xSecurityEnvironment )
321 rtl::OUString result;
322 int i;
323 int size = SignatureInformations.size();
325 for (i=0; i<size; i++)
327 const SignatureInformation& infor = SignatureInformations[i];
328 result += getSignatureInformation( infor, xSecurityEnvironment );
331 result += rtl::OUString::createFromAscii( "\n" );
333 return result;
336 ::com::sun::star::uno::Reference< ::com::sun::star::security::XCertificate >
337 getCertificateFromEnvironment( ::com::sun::star::uno::Reference< ::com::sun::star::xml::crypto::XSecurityEnvironment > xSecurityEnvironment , BOOL nType)
339 cssu::Sequence< cssu::Reference< ::com::sun::star::security::XCertificate > > xPersonalCerts ;
340 int length = 0;
341 int i;
343 // add By CP
344 sal_uInt16 encoding ;
345 rtl_Locale *pLocale = NULL ;
346 osl_getProcessLocale( &pLocale ) ;
347 encoding = osl_getTextEncodingFromLocale( pLocale ) ;
348 // CP end
350 if( nType != FALSE )
351 xPersonalCerts = xSecurityEnvironment->getPersonalCertificates() ;
352 else
353 return NULL; // not support then;
355 length = xPersonalCerts.getLength();
356 if(length == 0)
358 fprintf( stdout, "\nNo certificate found!\n" ) ;
359 return NULL;
362 fprintf( stdout, "\nSelect a certificate:\n" ) ;
363 for( i = 0; i < length; i ++ )
365 rtl::OUString xxxIssuer;
366 rtl::OUString xxxSubject;
367 rtl::OString yyyIssuer;
368 rtl::OString yyySubject;
370 xxxIssuer=xPersonalCerts[i]->getIssuerName();
371 yyyIssuer=rtl::OUStringToOString( xxxIssuer, encoding );
373 xxxSubject=xPersonalCerts[i]->getSubjectName();
374 yyySubject=rtl::OUStringToOString( xxxSubject, encoding );
376 fprintf( stdout, "\n%d:\nsubject=[%s]\nissuer=[%s]\n",
377 i+1,
378 yyySubject.getStr(),
379 yyyIssuer.getStr());
382 int sel = QuerySelectNumber( 1, length ) -1;
383 return xPersonalCerts[sel] ;
386 void QueryPrintSignatureDetails( const SignatureInformations& SignatureInformations, ::com::sun::star::uno::Reference< ::com::sun::star::xml::crypto::XSecurityEnvironment > rSecEnv )
388 char cShowDetails;
389 fprintf( stdout, "\nDisplay details (y/n) [y]?" );
390 fflush(stdin);
391 fscanf( stdin, "%c", &cShowDetails);
392 if ( cShowDetails == 'y' )
394 rtl_Locale *pLocale = NULL ;
395 osl_getProcessLocale( &pLocale ) ;
396 sal_uInt16 encoding = osl_getTextEncodingFromLocale( pLocale ) ;
398 fprintf( stdout, "------------- Signature details START -------------\n" );
399 fprintf( stdout, "%s",
400 rtl::OUStringToOString(
401 getSignatureInformations( SignatureInformations, rSecEnv),
402 encoding).getStr());
404 fprintf( stdout, "------------- Signature details END -------------\n" );
408 int QuerySelectNumber( int nMin, int nMax )
410 fprintf( stdout, "\n" ) ;
411 int sel = 0;
414 fprintf( stdout, "\nSelect <%d-%d>:", nMin, nMax ) ;
415 fflush(stdin);
416 fscanf( stdin, "%d", &sel ) ;
417 } while( ( sel < nMin ) || ( sel > nMax ) );
419 return sel;
422 long QueryVerifySignature()
424 char answer;
425 fprintf( stdout, "\nFound a signature - verify this one (y/n) [y]?" );
426 fflush(stdin);
427 fscanf( stdin, "%c", &answer);
428 return (answer == 'n')?0:1;