1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: util2.cxx,v $
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>
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
) );
65 if( rDateTime
.Month
< 10 )
67 aString
+= String::CreateFromInt32( rDateTime
.Month
);
69 if( rDateTime
.Day
< 10 )
71 aString
+= String::CreateFromInt32( rDateTime
.Day
);
73 if( rDateTime
.Seconds
!= 0 ||
74 rDateTime
.Minutes
!= 0 ||
75 rDateTime
.Hours
!= 0 )
78 if( rDateTime
.Hours
< 10 )
80 aString
+= String::CreateFromInt32( rDateTime
.Hours
);
82 if( rDateTime
.Minutes
< 10 )
84 aString
+= String::CreateFromInt32( rDateTime
.Minutes
);
86 if( rDateTime
.Seconds
< 10 )
88 aString
+= String::CreateFromInt32( rDateTime
.Seconds
);
89 if ( rDateTime
.HundredthSeconds
> 0)
92 if (rDateTime
.HundredthSeconds
< 10)
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
;
107 for (int j
=0; j
<length
; j
++)
109 sprintf(number
, "%02X ", (unsigned char)data
[j
]);
110 result
+= rtl::OUString::createFromAscii( number
);
117 ::rtl::OUString
getSignatureInformation(
118 const SignatureInformation
& infor
,
119 cssu::Reference
< ::com::sun::star::xml::crypto::XSecurityEnvironment
>& xSecurityEnvironment
)
123 "OPERATION_SUCCEEDED",
124 "RUNTIMEERROR_FAILED",
143 "INVALID_NODE_CONTENT",
144 "INVALID_NODE_ATTRIBUTE",
145 "MISSING_NODE_ATTRIBUTE",
146 "NODE_ALREADY_PRESENT",
150 "INVALID_TRANSFORM_KEY",
152 "TRANSFORM_SAME_DOCUMENT_REQUIRED",
153 "TRANSFORM_DISABLED",
155 "KEY_DATA_NOT_FOUND",
156 "KEY_DATA_ALREADY_EXIST",
157 "INVALID_KEY_DATA_SIZE",
160 "MAX_RETRIEVALS_LEVEL",
161 "MAX_RETRIEVAL_TYPE_MISMATCH",
163 "CERT_VERIFY_FAILED",
166 "CERT_ISSUER_FAILED",
167 "CERT_NOT_YET_VALID",
169 "DSIG_NO_REFERENCES",
170 "DSIG_INVALID_REFERENCE",
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
;
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
;
241 fprintf(stdout
, " xCert is NULL , so can not buildCertificatePath\n");
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" );
317 ::rtl::OUString
getSignatureInformations(
318 const SignatureInformations
& SignatureInformations
,
319 cssu::Reference
< ::com::sun::star::xml::crypto::XSecurityEnvironment
> xSecurityEnvironment
)
321 rtl::OUString result
;
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" );
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
;
344 sal_uInt16 encoding
;
345 rtl_Locale
*pLocale
= NULL
;
346 osl_getProcessLocale( &pLocale
) ;
347 encoding
= osl_getTextEncodingFromLocale( pLocale
) ;
351 xPersonalCerts
= xSecurityEnvironment
->getPersonalCertificates() ;
353 return NULL
; // not support then;
355 length
= xPersonalCerts
.getLength();
358 fprintf( stdout
, "\nNo certificate found!\n" ) ;
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",
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
)
389 fprintf( stdout
, "\nDisplay details (y/n) [y]?" );
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
),
404 fprintf( stdout
, "------------- Signature details END -------------\n" );
408 int QuerySelectNumber( int nMin
, int nMax
)
410 fprintf( stdout
, "\n" ) ;
414 fprintf( stdout
, "\nSelect <%d-%d>:", nMin
, nMax
) ;
416 fscanf( stdin
, "%d", &sel
) ;
417 } while( ( sel
< nMin
) || ( sel
> nMax
) );
422 long QueryVerifySignature()
425 fprintf( stdout
, "\nFound a signature - verify this one (y/n) [y]?" );
427 fscanf( stdin
, "%c", &answer
);
428 return (answer
== 'n')?0:1;