xref: /AOO41X/main/xmlsecurity/tools/demo/util2.cxx (revision 06b3ce531745799678cf4bb887ef37436d81238b)
1*06b3ce53SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*06b3ce53SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*06b3ce53SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*06b3ce53SAndrew Rist  * distributed with this work for additional information
6*06b3ce53SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*06b3ce53SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*06b3ce53SAndrew Rist  * "License"); you may not use this file except in compliance
9*06b3ce53SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*06b3ce53SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*06b3ce53SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*06b3ce53SAndrew Rist  * software distributed under the License is distributed on an
15*06b3ce53SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*06b3ce53SAndrew Rist  * KIND, either express or implied.  See the License for the
17*06b3ce53SAndrew Rist  * specific language governing permissions and limitations
18*06b3ce53SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*06b3ce53SAndrew Rist  *************************************************************/
21*06b3ce53SAndrew Rist 
22*06b3ce53SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_xmlsecurity.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <rtl/locale.h>
28cdf0e10cSrcweir #include <osl/nlsupport.h>
29cdf0e10cSrcweir #include <osl/process.h>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include <util.hxx>
32cdf0e10cSrcweir 
33cdf0e10cSrcweir #include <stdio.h>
34cdf0e10cSrcweir 
35cdf0e10cSrcweir #include <com/sun/star/registry/XImplementationRegistration.hpp>
36cdf0e10cSrcweir #include <com/sun/star/security/KeyUsage.hpp>
37cdf0e10cSrcweir #include <cppuhelper/bootstrap.hxx>
38cdf0e10cSrcweir #include <xmlsecurity/biginteger.hxx>
39cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
40cdf0e10cSrcweir #include <unotools/streamhelper.hxx>
41cdf0e10cSrcweir 
42cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
43cdf0e10cSrcweir #include <tools/string.hxx>
44cdf0e10cSrcweir 
45cdf0e10cSrcweir namespace cssu = com::sun::star::uno;
46cdf0e10cSrcweir namespace cssl = com::sun::star::lang;
47cdf0e10cSrcweir namespace cssxc = com::sun::star::xml::crypto;
48cdf0e10cSrcweir namespace cssi = com::sun::star::io;
49cdf0e10cSrcweir 
50cdf0e10cSrcweir using namespace ::com::sun::star;
51cdf0e10cSrcweir 
52cdf0e10cSrcweir /** convert util::DateTime to ISO Date String */
convertDateTime(::rtl::OUStringBuffer & rBuffer,const com::sun::star::util::DateTime & rDateTime)53cdf0e10cSrcweir void convertDateTime( ::rtl::OUStringBuffer& rBuffer,
54cdf0e10cSrcweir 	const com::sun::star::util::DateTime& rDateTime )
55cdf0e10cSrcweir {
56cdf0e10cSrcweir     String aString( String::CreateFromInt32( rDateTime.Year ) );
57cdf0e10cSrcweir     aString += '-';
58cdf0e10cSrcweir     if( rDateTime.Month < 10 )
59cdf0e10cSrcweir         aString += '0';
60cdf0e10cSrcweir     aString += String::CreateFromInt32( rDateTime.Month );
61cdf0e10cSrcweir     aString += '-';
62cdf0e10cSrcweir     if( rDateTime.Day < 10 )
63cdf0e10cSrcweir         aString += '0';
64cdf0e10cSrcweir     aString += String::CreateFromInt32( rDateTime.Day );
65cdf0e10cSrcweir 
66cdf0e10cSrcweir     if( rDateTime.Seconds != 0 ||
67cdf0e10cSrcweir         rDateTime.Minutes != 0 ||
68cdf0e10cSrcweir         rDateTime.Hours   != 0 )
69cdf0e10cSrcweir     {
70cdf0e10cSrcweir         aString += 'T';
71cdf0e10cSrcweir         if( rDateTime.Hours < 10 )
72cdf0e10cSrcweir             aString += '0';
73cdf0e10cSrcweir         aString += String::CreateFromInt32( rDateTime.Hours );
74cdf0e10cSrcweir         aString += ':';
75cdf0e10cSrcweir         if( rDateTime.Minutes < 10 )
76cdf0e10cSrcweir             aString += '0';
77cdf0e10cSrcweir         aString += String::CreateFromInt32( rDateTime.Minutes );
78cdf0e10cSrcweir         aString += ':';
79cdf0e10cSrcweir         if( rDateTime.Seconds < 10 )
80cdf0e10cSrcweir             aString += '0';
81cdf0e10cSrcweir         aString += String::CreateFromInt32( rDateTime.Seconds );
82cdf0e10cSrcweir 		if ( rDateTime.HundredthSeconds > 0)
83cdf0e10cSrcweir 		{
84cdf0e10cSrcweir 	        aString += ',';
85cdf0e10cSrcweir 			if (rDateTime.HundredthSeconds < 10)
86cdf0e10cSrcweir 				aString += '0';
87cdf0e10cSrcweir 			aString += String::CreateFromInt32( rDateTime.HundredthSeconds );
88cdf0e10cSrcweir 		}
89cdf0e10cSrcweir     }
90cdf0e10cSrcweir 
91cdf0e10cSrcweir     rBuffer.append( aString );
92cdf0e10cSrcweir }
93cdf0e10cSrcweir 
printHexString(cssu::Sequence<sal_Int8> data)94cdf0e10cSrcweir ::rtl::OUString printHexString(cssu::Sequence< sal_Int8 > data)
95cdf0e10cSrcweir {
96cdf0e10cSrcweir 	int length = data.getLength();
97cdf0e10cSrcweir 	::rtl::OUString result;
98cdf0e10cSrcweir 
99cdf0e10cSrcweir 	char number[4];
100cdf0e10cSrcweir 	for (int j=0; j<length; j++)
101cdf0e10cSrcweir 	{
102cdf0e10cSrcweir 		sprintf(number, "%02X ", (unsigned char)data[j]);
103cdf0e10cSrcweir 		result += rtl::OUString::createFromAscii( number );
104cdf0e10cSrcweir 	}
105cdf0e10cSrcweir 
106cdf0e10cSrcweir 	return result;
107cdf0e10cSrcweir }
108cdf0e10cSrcweir 
109cdf0e10cSrcweir 
getSignatureInformation(const SignatureInformation & infor,cssu::Reference<::com::sun::star::xml::crypto::XSecurityEnvironment> & xSecurityEnvironment)110cdf0e10cSrcweir ::rtl::OUString getSignatureInformation(
111cdf0e10cSrcweir 	const SignatureInformation& infor,
112cdf0e10cSrcweir 	cssu::Reference< ::com::sun::star::xml::crypto::XSecurityEnvironment >& xSecurityEnvironment )
113cdf0e10cSrcweir {
114cdf0e10cSrcweir 	char* status[50] = {
115cdf0e10cSrcweir 		"STATUS_UNKNOWN",
116cdf0e10cSrcweir 		"OPERATION_SUCCEEDED",
117cdf0e10cSrcweir 		"RUNTIMEERROR_FAILED",
118cdf0e10cSrcweir 		"ENGINE_FAILED",
119cdf0e10cSrcweir 		"MALLOC_FAILED",
120cdf0e10cSrcweir 		"STRDUP_FAILED",
121cdf0e10cSrcweir 		"CRYPTO_FAILED",
122cdf0e10cSrcweir 		"XML_FAILED",
123cdf0e10cSrcweir 		"XSLT_FAILED",
124cdf0e10cSrcweir 		"IO_FAILED",
125cdf0e10cSrcweir 		"DISABLED",
126cdf0e10cSrcweir 		"NOT_IMPLEMENTED",
127cdf0e10cSrcweir 		"INVALID_SIZE",
128cdf0e10cSrcweir 		"INVALID_DATA",
129cdf0e10cSrcweir 		"INVALID_RESULT",
130cdf0e10cSrcweir 		"INVALID_TYPE",
131cdf0e10cSrcweir 		"INVALID_OPERATION",
132cdf0e10cSrcweir 		"INVALID_STATUS",
133cdf0e10cSrcweir 		"INVALID_FORMAT",
134cdf0e10cSrcweir 		"DATA_NOT_MATCH",
135cdf0e10cSrcweir 		"INVALID_NODE",
136cdf0e10cSrcweir 		"INVALID_NODE_CONTENT",
137cdf0e10cSrcweir 		"INVALID_NODE_ATTRIBUTE",
138cdf0e10cSrcweir 		"MISSING_NODE_ATTRIBUTE",
139cdf0e10cSrcweir 		"NODE_ALREADY_PRESENT",
140cdf0e10cSrcweir 		"UNEXPECTED_NODE",
141cdf0e10cSrcweir 		"NODE_NOT_FOUND",
142cdf0e10cSrcweir 		"INVALID_TRANSFORM",
143cdf0e10cSrcweir 		"INVALID_TRANSFORM_KEY",
144cdf0e10cSrcweir 		"INVALID_URI_TYPE",
145cdf0e10cSrcweir 		"TRANSFORM_SAME_DOCUMENT_REQUIRED",
146cdf0e10cSrcweir 		"TRANSFORM_DISABLED",
147cdf0e10cSrcweir 		"INVALID_KEY_DATA",
148cdf0e10cSrcweir 		"KEY_DATA_NOT_FOUND",
149cdf0e10cSrcweir 		"KEY_DATA_ALREADY_EXIST",
150cdf0e10cSrcweir 		"INVALID_KEY_DATA_SIZE",
151cdf0e10cSrcweir 		"KEY_NOT_FOUND",
152cdf0e10cSrcweir 		"KEYDATA_DISABLED",
153cdf0e10cSrcweir 		"MAX_RETRIEVALS_LEVEL",
154cdf0e10cSrcweir 		"MAX_RETRIEVAL_TYPE_MISMATCH",
155cdf0e10cSrcweir 		"MAX_ENCKEY_LEVEL",
156cdf0e10cSrcweir 		"CERT_VERIFY_FAILED",
157cdf0e10cSrcweir 		"CERT_NOT_FOUND",
158cdf0e10cSrcweir 		"CERT_REVOKED",
159cdf0e10cSrcweir 		"CERT_ISSUER_FAILED",
160cdf0e10cSrcweir 		"CERT_NOT_YET_VALID",
161cdf0e10cSrcweir 		"CERT_HAS_EXPIRED",
162cdf0e10cSrcweir 		"DSIG_NO_REFERENCES",
163cdf0e10cSrcweir 		"DSIG_INVALID_REFERENCE",
164cdf0e10cSrcweir 		"ASSERTION"};
165cdf0e10cSrcweir 
166cdf0e10cSrcweir 	rtl::OUString result;
167cdf0e10cSrcweir 
168cdf0e10cSrcweir 	result += rtl::OUString::createFromAscii( "Security Id : " )
169cdf0e10cSrcweir 		+rtl::OUString::valueOf(infor.nSecurityId)
170cdf0e10cSrcweir 		+rtl::OUString::createFromAscii( "\n" );
171cdf0e10cSrcweir 	result += rtl::OUString::createFromAscii( "Status : [" )
172cdf0e10cSrcweir 		+rtl::OUString::valueOf((sal_Int32)(infor.nStatus))
173cdf0e10cSrcweir 		+rtl::OUString::createFromAscii( "] " )
174cdf0e10cSrcweir 		+rtl::OUString::createFromAscii(status[infor.nStatus])
175cdf0e10cSrcweir 		+rtl::OUString::createFromAscii( "\n" );
176cdf0e10cSrcweir 
177cdf0e10cSrcweir 	const SignatureReferenceInformations& rInfors = infor.vSignatureReferenceInfors;
178cdf0e10cSrcweir 	int i;
179cdf0e10cSrcweir 	int size = rInfors.size();
180cdf0e10cSrcweir 
181cdf0e10cSrcweir 	result += rtl::OUString::createFromAscii( "--References :\n" );
182cdf0e10cSrcweir 	for (i=0; i<size; i++)
183cdf0e10cSrcweir 	{
184cdf0e10cSrcweir         	result += rtl::OUString::createFromAscii( "---URI : " );
185cdf0e10cSrcweir 		result += rInfors[i].ouURI;
186cdf0e10cSrcweir 		result += rtl::OUString::createFromAscii( "\n" );
187cdf0e10cSrcweir         	result += rtl::OUString::createFromAscii( "---DigestValue : " );
188cdf0e10cSrcweir 		result += rInfors[i].ouDigestValue;
189cdf0e10cSrcweir 		result += rtl::OUString::createFromAscii( "\n" );
190cdf0e10cSrcweir 	}
191cdf0e10cSrcweir 
192cdf0e10cSrcweir         if (infor.ouX509IssuerName.getLength()>0)
193cdf0e10cSrcweir         {
194cdf0e10cSrcweir         	result += rtl::OUString::createFromAscii( "--X509IssuerName :\n" );
195cdf0e10cSrcweir         	result += infor.ouX509IssuerName;
196cdf0e10cSrcweir         	result += rtl::OUString::createFromAscii( "\n" );
197cdf0e10cSrcweir         }
198cdf0e10cSrcweir 
199cdf0e10cSrcweir         if (infor.ouX509SerialNumber.getLength()>0)
200cdf0e10cSrcweir         {
201cdf0e10cSrcweir         	result += rtl::OUString::createFromAscii( "--X509SerialNumber :\n" );
202cdf0e10cSrcweir         	result += infor.ouX509SerialNumber;
203cdf0e10cSrcweir         	result += rtl::OUString::createFromAscii( "\n" );
204cdf0e10cSrcweir         }
205cdf0e10cSrcweir 
206cdf0e10cSrcweir         if (infor.ouX509Certificate.getLength()>0)
207cdf0e10cSrcweir         {
208cdf0e10cSrcweir         	result += rtl::OUString::createFromAscii( "--X509Certificate :\n" );
209cdf0e10cSrcweir         	result += infor.ouX509Certificate;
210cdf0e10cSrcweir         	result += rtl::OUString::createFromAscii( "\n" );
211cdf0e10cSrcweir         }
212cdf0e10cSrcweir 
213cdf0e10cSrcweir         if (infor.ouSignatureValue.getLength()>0)
214cdf0e10cSrcweir         {
215cdf0e10cSrcweir         	result += rtl::OUString::createFromAscii( "--SignatureValue :\n" );
216cdf0e10cSrcweir         	result += infor.ouSignatureValue;
217cdf0e10cSrcweir         	result += rtl::OUString::createFromAscii( "\n" );
218cdf0e10cSrcweir         }
219cdf0e10cSrcweir 
220cdf0e10cSrcweir        	result += rtl::OUString::createFromAscii( "--Date :\n" );
221cdf0e10cSrcweir 
222cdf0e10cSrcweir 	::rtl::OUStringBuffer buffer;
223cdf0e10cSrcweir 	convertDateTime( buffer, infor.stDateTime );
224cdf0e10cSrcweir 	result += buffer.makeStringAndClear();
225cdf0e10cSrcweir        	result += rtl::OUString::createFromAscii( "\n" );
226cdf0e10cSrcweir 
227cdf0e10cSrcweir         if (infor.ouX509IssuerName.getLength()>0 && infor.ouX509SerialNumber.getLength()>0 && xSecurityEnvironment.is())
228cdf0e10cSrcweir         {
229cdf0e10cSrcweir         	result += rtl::OUString::createFromAscii( "--Certificate Path :\n" );
230cdf0e10cSrcweir         	cssu::Reference< ::com::sun::star::security::XCertificate > xCert = xSecurityEnvironment->getCertificate( infor.ouX509IssuerName, numericStringToBigInteger(infor.ouX509SerialNumber) );
231cdf0e10cSrcweir         	cssu::Sequence < cssu::Reference< ::com::sun::star::security::XCertificate > > xCertPath;
232cdf0e10cSrcweir 			if(! xCert.is() )
233cdf0e10cSrcweir 			{
234cdf0e10cSrcweir 				fprintf(stdout , " xCert is NULL , so can not buildCertificatePath\n");
235cdf0e10cSrcweir 				return result ;
236cdf0e10cSrcweir 			}
237cdf0e10cSrcweir 			else
238cdf0e10cSrcweir 			{
239cdf0e10cSrcweir 				xCertPath = xSecurityEnvironment->buildCertificatePath( xCert ) ;
240cdf0e10cSrcweir 			}
241cdf0e10cSrcweir 
242cdf0e10cSrcweir 		for( int i = 0; i < xCertPath.getLength(); i++ )
243cdf0e10cSrcweir 		{
244cdf0e10cSrcweir 			result += xCertPath[i]->getSubjectName();
245cdf0e10cSrcweir                 	result += rtl::OUString::createFromAscii( "\n    Subject public key algorithm : " );
246cdf0e10cSrcweir                 	result += xCertPath[i]->getSubjectPublicKeyAlgorithm();
247cdf0e10cSrcweir                 	result += rtl::OUString::createFromAscii( "\n    Signature algorithm : " );
248cdf0e10cSrcweir                 	result += xCertPath[i]->getSignatureAlgorithm();
249cdf0e10cSrcweir 
250cdf0e10cSrcweir                 	result += rtl::OUString::createFromAscii( "\n    Subject public key value : " );
251cdf0e10cSrcweir                 	cssu::Sequence< sal_Int8 > keyValue = xCertPath[i]->getSubjectPublicKeyValue();
252cdf0e10cSrcweir                 	result += printHexString(keyValue);
253cdf0e10cSrcweir 
254cdf0e10cSrcweir                 	result += rtl::OUString::createFromAscii( "\n    Thumbprint (SHA1) : " );
255cdf0e10cSrcweir                 	cssu::Sequence< sal_Int8 > SHA1Thumbprint = xCertPath[i]->getSHA1Thumbprint();
256cdf0e10cSrcweir                 	result += printHexString(SHA1Thumbprint);
257cdf0e10cSrcweir 
258cdf0e10cSrcweir                 	result += rtl::OUString::createFromAscii( "\n    Thumbprint (MD5) : " );
259cdf0e10cSrcweir                 	cssu::Sequence< sal_Int8 > MD5Thumbprint = xCertPath[i]->getMD5Thumbprint();
260cdf0e10cSrcweir                 	result += printHexString(MD5Thumbprint);
261cdf0e10cSrcweir 
262cdf0e10cSrcweir                 	result += rtl::OUString::createFromAscii( "\n  <<\n" );
263cdf0e10cSrcweir 		}
264cdf0e10cSrcweir 
265cdf0e10cSrcweir                	result += rtl::OUString::createFromAscii( "\n    Key Usage : " );
266cdf0e10cSrcweir                	sal_Int32 usage = xCert->getCertificateUsage();
267cdf0e10cSrcweir 
268cdf0e10cSrcweir                	if (usage & ::com::sun::star::security::KeyUsage::DIGITAL_SIGNATURE)
269cdf0e10cSrcweir                	{
270cdf0e10cSrcweir                		result += rtl::OUString::createFromAscii( "DIGITAL_SIGNATURE " );
271cdf0e10cSrcweir                	}
272cdf0e10cSrcweir 
273cdf0e10cSrcweir                	if (usage & ::com::sun::star::security::KeyUsage::NON_REPUDIATION)
274cdf0e10cSrcweir                	{
275cdf0e10cSrcweir                		result += rtl::OUString::createFromAscii( "NON_REPUDIATION " );
276cdf0e10cSrcweir                	}
277cdf0e10cSrcweir 
278cdf0e10cSrcweir                	if (usage & ::com::sun::star::security::KeyUsage::KEY_ENCIPHERMENT)
279cdf0e10cSrcweir                	{
280cdf0e10cSrcweir                		result += rtl::OUString::createFromAscii( "KEY_ENCIPHERMENT " );
281cdf0e10cSrcweir                	}
282cdf0e10cSrcweir 
283cdf0e10cSrcweir                	if (usage & ::com::sun::star::security::KeyUsage::DATA_ENCIPHERMENT)
284cdf0e10cSrcweir                	{
285cdf0e10cSrcweir                		result += rtl::OUString::createFromAscii( "DATA_ENCIPHERMENT " );
286cdf0e10cSrcweir                	}
287cdf0e10cSrcweir 
288cdf0e10cSrcweir                	if (usage & ::com::sun::star::security::KeyUsage::KEY_AGREEMENT)
289cdf0e10cSrcweir                	{
290cdf0e10cSrcweir                		result += rtl::OUString::createFromAscii( "KEY_AGREEMENT " );
291cdf0e10cSrcweir                	}
292cdf0e10cSrcweir 
293cdf0e10cSrcweir                	if (usage & ::com::sun::star::security::KeyUsage::KEY_CERT_SIGN)
294cdf0e10cSrcweir                	{
295cdf0e10cSrcweir                		result += rtl::OUString::createFromAscii( "KEY_CERT_SIGN " );
296cdf0e10cSrcweir                	}
297cdf0e10cSrcweir 
298cdf0e10cSrcweir                	if (usage & ::com::sun::star::security::KeyUsage::CRL_SIGN)
299cdf0e10cSrcweir                	{
300cdf0e10cSrcweir                		result += rtl::OUString::createFromAscii( "CRL_SIGN " );
301cdf0e10cSrcweir                	}
302cdf0e10cSrcweir 
303cdf0e10cSrcweir                	result += rtl::OUString::createFromAscii( "\n" );
304cdf0e10cSrcweir         }
305cdf0e10cSrcweir 
306cdf0e10cSrcweir 	result += rtl::OUString::createFromAscii( "\n" );
307cdf0e10cSrcweir 	return result;
308cdf0e10cSrcweir }
309cdf0e10cSrcweir 
getSignatureInformations(const SignatureInformations & SignatureInformations,cssu::Reference<::com::sun::star::xml::crypto::XSecurityEnvironment> xSecurityEnvironment)310cdf0e10cSrcweir ::rtl::OUString getSignatureInformations(
311cdf0e10cSrcweir 	const SignatureInformations& SignatureInformations,
312cdf0e10cSrcweir 	cssu::Reference< ::com::sun::star::xml::crypto::XSecurityEnvironment > xSecurityEnvironment )
313cdf0e10cSrcweir {
314cdf0e10cSrcweir 	rtl::OUString result;
315cdf0e10cSrcweir 	int i;
316cdf0e10cSrcweir 	int size = SignatureInformations.size();
317cdf0e10cSrcweir 
318cdf0e10cSrcweir 	for (i=0; i<size; i++)
319cdf0e10cSrcweir 	{
320cdf0e10cSrcweir 		const SignatureInformation& infor = SignatureInformations[i];
321cdf0e10cSrcweir 		result += getSignatureInformation( infor, xSecurityEnvironment );
322cdf0e10cSrcweir 	}
323cdf0e10cSrcweir 
324cdf0e10cSrcweir 	result += rtl::OUString::createFromAscii( "\n" );
325cdf0e10cSrcweir 
326cdf0e10cSrcweir 	return result;
327cdf0e10cSrcweir }
328cdf0e10cSrcweir 
329cdf0e10cSrcweir ::com::sun::star::uno::Reference< ::com::sun::star::security::XCertificate >
getCertificateFromEnvironment(::com::sun::star::uno::Reference<::com::sun::star::xml::crypto::XSecurityEnvironment> xSecurityEnvironment,sal_Bool nType)330cdf0e10cSrcweir 	getCertificateFromEnvironment( ::com::sun::star::uno::Reference< ::com::sun::star::xml::crypto::XSecurityEnvironment >	xSecurityEnvironment , sal_Bool nType)
331cdf0e10cSrcweir {
332cdf0e10cSrcweir 	cssu::Sequence< cssu::Reference< ::com::sun::star::security::XCertificate > > xPersonalCerts ;
333cdf0e10cSrcweir 	int length = 0;
334cdf0e10cSrcweir 	int i;
335cdf0e10cSrcweir 
336cdf0e10cSrcweir 	// add By CP
337cdf0e10cSrcweir 	sal_uInt16 encoding ;
338cdf0e10cSrcweir 	rtl_Locale *pLocale = NULL ;
339cdf0e10cSrcweir 	osl_getProcessLocale( &pLocale ) ;
340cdf0e10cSrcweir 	encoding = osl_getTextEncodingFromLocale( pLocale ) ;
341cdf0e10cSrcweir 	// CP end
342cdf0e10cSrcweir 
343cdf0e10cSrcweir 	if( nType != sal_False )
344cdf0e10cSrcweir 		xPersonalCerts = xSecurityEnvironment->getPersonalCertificates() ;
345cdf0e10cSrcweir 	else
346cdf0e10cSrcweir 		return NULL; // not support then;
347cdf0e10cSrcweir 
348cdf0e10cSrcweir 	length = xPersonalCerts.getLength();
349cdf0e10cSrcweir 	if(length == 0)
350cdf0e10cSrcweir 	{
351cdf0e10cSrcweir 		fprintf( stdout, "\nNo certificate found!\n" ) ;
352cdf0e10cSrcweir 		return NULL;
353cdf0e10cSrcweir 	}
354cdf0e10cSrcweir 
355cdf0e10cSrcweir 	fprintf( stdout, "\nSelect a certificate:\n" ) ;
356cdf0e10cSrcweir 	for( i = 0; i < length; i ++ )
357cdf0e10cSrcweir 	{
358cdf0e10cSrcweir 		rtl::OUString xxxIssuer;
359cdf0e10cSrcweir 		rtl::OUString xxxSubject;
360cdf0e10cSrcweir 		rtl::OString yyyIssuer;
361cdf0e10cSrcweir 		rtl::OString yyySubject;
362cdf0e10cSrcweir 
363cdf0e10cSrcweir 		xxxIssuer=xPersonalCerts[i]->getIssuerName();
364cdf0e10cSrcweir 		yyyIssuer=rtl::OUStringToOString( xxxIssuer, encoding );
365cdf0e10cSrcweir 
366cdf0e10cSrcweir 		xxxSubject=xPersonalCerts[i]->getSubjectName();
367cdf0e10cSrcweir 		yyySubject=rtl::OUStringToOString( xxxSubject, encoding );
368cdf0e10cSrcweir 
369cdf0e10cSrcweir 		fprintf( stdout, "\n%d:\nsubject=[%s]\nissuer=[%s]\n",
370cdf0e10cSrcweir 			i+1,
371cdf0e10cSrcweir 			yyySubject.getStr(),
372cdf0e10cSrcweir 			yyyIssuer.getStr());
373cdf0e10cSrcweir 	}
374cdf0e10cSrcweir 
375cdf0e10cSrcweir 	int sel = QuerySelectNumber( 1, length ) -1;
376cdf0e10cSrcweir 	return xPersonalCerts[sel] ;
377cdf0e10cSrcweir }
378cdf0e10cSrcweir 
QueryPrintSignatureDetails(const SignatureInformations & SignatureInformations,::com::sun::star::uno::Reference<::com::sun::star::xml::crypto::XSecurityEnvironment> rSecEnv)379cdf0e10cSrcweir void QueryPrintSignatureDetails( const SignatureInformations& SignatureInformations, ::com::sun::star::uno::Reference< ::com::sun::star::xml::crypto::XSecurityEnvironment > rSecEnv )
380cdf0e10cSrcweir {
381cdf0e10cSrcweir 	char cShowDetails;
382cdf0e10cSrcweir 	fprintf( stdout, "\nDisplay details (y/n) [y]?" );
383cdf0e10cSrcweir 	fflush( stdout );
384cdf0e10cSrcweir 	fscanf( stdin, "%c", &cShowDetails);
385cdf0e10cSrcweir 	if ( cShowDetails == 'y' )
386cdf0e10cSrcweir 	{
387cdf0e10cSrcweir 		rtl_Locale *pLocale = NULL ;
388cdf0e10cSrcweir 		osl_getProcessLocale( &pLocale ) ;
389cdf0e10cSrcweir 		sal_uInt16 encoding = osl_getTextEncodingFromLocale( pLocale ) ;
390cdf0e10cSrcweir 
391cdf0e10cSrcweir 		fprintf( stdout, "------------- Signature details START -------------\n" );
392cdf0e10cSrcweir 		fprintf( stdout, "%s",
393cdf0e10cSrcweir 			rtl::OUStringToOString(
394cdf0e10cSrcweir 				getSignatureInformations( SignatureInformations, rSecEnv),
395cdf0e10cSrcweir 				encoding).getStr());
396cdf0e10cSrcweir 
397cdf0e10cSrcweir 		fprintf( stdout, "------------- Signature details END -------------\n" );
398cdf0e10cSrcweir 	}
399cdf0e10cSrcweir }
400cdf0e10cSrcweir 
QuerySelectNumber(int nMin,int nMax)401cdf0e10cSrcweir int QuerySelectNumber( int nMin, int nMax )
402cdf0e10cSrcweir {
403cdf0e10cSrcweir 	fprintf( stdout, "\n" ) ;
404cdf0e10cSrcweir 	int sel = 0;
405cdf0e10cSrcweir 	do
406cdf0e10cSrcweir 	{
407cdf0e10cSrcweir 		fprintf( stdout, "\nSelect <%d-%d>:", nMin, nMax ) ;
408cdf0e10cSrcweir 		fflush( stdout );
409cdf0e10cSrcweir 		fscanf( stdin, "%d", &sel ) ;
410cdf0e10cSrcweir 	} while( ( sel < nMin ) || ( sel > nMax ) );
411cdf0e10cSrcweir 
412cdf0e10cSrcweir 	return sel;
413cdf0e10cSrcweir }
414cdf0e10cSrcweir 
QueryVerifySignature()415cdf0e10cSrcweir long QueryVerifySignature()
416cdf0e10cSrcweir {
417cdf0e10cSrcweir 	char answer;
418cdf0e10cSrcweir 	fprintf( stdout, "\nFound a signature - verify this one (y/n) [y]?" );
419cdf0e10cSrcweir 	fflush( stdout );
420cdf0e10cSrcweir 	fscanf( stdin, "%c", &answer);
421cdf0e10cSrcweir 	return  (answer == 'n')?0:1;
422cdf0e10cSrcweir }
423