1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_xmlsecurity.hxx"
31 #include <rtl/locale.h>
32 #include <osl/nlsupport.h>
33 #include <osl/process.h>
39 #include <com/sun/star/registry/XImplementationRegistration.hpp>
40 #include <com/sun/star/security/KeyUsage.hpp>
41 #include <cppuhelper/bootstrap.hxx>
42 #include <xmlsecurity/biginteger.hxx>
43 #include <comphelper/processfactory.hxx>
44 #include <unotools/streamhelper.hxx>
46 #include <rtl/ustrbuf.hxx>
47 #include <tools/string.hxx>
49 namespace cssu
= com::sun::star::uno
;
50 namespace cssl
= com::sun::star::lang
;
51 namespace cssxc
= com::sun::star::xml::crypto
;
52 namespace cssi
= com::sun::star::io
;
54 using namespace ::com::sun::star
;
56 /** convert util::DateTime to ISO Date String */
57 void convertDateTime( ::rtl::OUStringBuffer
& rBuffer
,
58 const com::sun::star::util::DateTime
& rDateTime
)
60 String
aString( String::CreateFromInt32( rDateTime
.Year
) );
62 if( rDateTime
.Month
< 10 )
64 aString
+= String::CreateFromInt32( rDateTime
.Month
);
66 if( rDateTime
.Day
< 10 )
68 aString
+= String::CreateFromInt32( rDateTime
.Day
);
70 if( rDateTime
.Seconds
!= 0 ||
71 rDateTime
.Minutes
!= 0 ||
72 rDateTime
.Hours
!= 0 )
75 if( rDateTime
.Hours
< 10 )
77 aString
+= String::CreateFromInt32( rDateTime
.Hours
);
79 if( rDateTime
.Minutes
< 10 )
81 aString
+= String::CreateFromInt32( rDateTime
.Minutes
);
83 if( rDateTime
.Seconds
< 10 )
85 aString
+= String::CreateFromInt32( rDateTime
.Seconds
);
86 if ( rDateTime
.HundredthSeconds
> 0)
89 if (rDateTime
.HundredthSeconds
< 10)
91 aString
+= String::CreateFromInt32( rDateTime
.HundredthSeconds
);
95 rBuffer
.append( aString
);
98 ::rtl::OUString
printHexString(cssu::Sequence
< sal_Int8
> data
)
100 int length
= data
.getLength();
101 ::rtl::OUString result
;
104 for (int j
=0; j
<length
; j
++)
106 sprintf(number
, "%02X ", (unsigned char)data
[j
]);
107 result
+= rtl::OUString::createFromAscii( number
);
114 ::rtl::OUString
getSignatureInformation(
115 const SignatureInformation
& infor
,
116 cssu::Reference
< ::com::sun::star::xml::crypto::XSecurityEnvironment
>& xSecurityEnvironment
)
120 "OPERATION_SUCCEEDED",
121 "RUNTIMEERROR_FAILED",
140 "INVALID_NODE_CONTENT",
141 "INVALID_NODE_ATTRIBUTE",
142 "MISSING_NODE_ATTRIBUTE",
143 "NODE_ALREADY_PRESENT",
147 "INVALID_TRANSFORM_KEY",
149 "TRANSFORM_SAME_DOCUMENT_REQUIRED",
150 "TRANSFORM_DISABLED",
152 "KEY_DATA_NOT_FOUND",
153 "KEY_DATA_ALREADY_EXIST",
154 "INVALID_KEY_DATA_SIZE",
157 "MAX_RETRIEVALS_LEVEL",
158 "MAX_RETRIEVAL_TYPE_MISMATCH",
160 "CERT_VERIFY_FAILED",
163 "CERT_ISSUER_FAILED",
164 "CERT_NOT_YET_VALID",
166 "DSIG_NO_REFERENCES",
167 "DSIG_INVALID_REFERENCE",
170 rtl::OUString result
;
172 result
+= rtl::OUString::createFromAscii( "Security Id : " )
173 +rtl::OUString::valueOf(infor
.nSecurityId
)
174 +rtl::OUString::createFromAscii( "\n" );
175 result
+= rtl::OUString::createFromAscii( "Status : [" )
176 +rtl::OUString::valueOf((sal_Int32
)(infor
.nStatus
))
177 +rtl::OUString::createFromAscii( "] " )
178 +rtl::OUString::createFromAscii(status
[infor
.nStatus
])
179 +rtl::OUString::createFromAscii( "\n" );
181 const SignatureReferenceInformations
& rInfors
= infor
.vSignatureReferenceInfors
;
183 int size
= rInfors
.size();
185 result
+= rtl::OUString::createFromAscii( "--References :\n" );
186 for (i
=0; i
<size
; i
++)
188 result
+= rtl::OUString::createFromAscii( "---URI : " );
189 result
+= rInfors
[i
].ouURI
;
190 result
+= rtl::OUString::createFromAscii( "\n" );
191 result
+= rtl::OUString::createFromAscii( "---DigestValue : " );
192 result
+= rInfors
[i
].ouDigestValue
;
193 result
+= rtl::OUString::createFromAscii( "\n" );
196 if (infor
.ouX509IssuerName
.getLength()>0)
198 result
+= rtl::OUString::createFromAscii( "--X509IssuerName :\n" );
199 result
+= infor
.ouX509IssuerName
;
200 result
+= rtl::OUString::createFromAscii( "\n" );
203 if (infor
.ouX509SerialNumber
.getLength()>0)
205 result
+= rtl::OUString::createFromAscii( "--X509SerialNumber :\n" );
206 result
+= infor
.ouX509SerialNumber
;
207 result
+= rtl::OUString::createFromAscii( "\n" );
210 if (infor
.ouX509Certificate
.getLength()>0)
212 result
+= rtl::OUString::createFromAscii( "--X509Certificate :\n" );
213 result
+= infor
.ouX509Certificate
;
214 result
+= rtl::OUString::createFromAscii( "\n" );
217 if (infor
.ouSignatureValue
.getLength()>0)
219 result
+= rtl::OUString::createFromAscii( "--SignatureValue :\n" );
220 result
+= infor
.ouSignatureValue
;
221 result
+= rtl::OUString::createFromAscii( "\n" );
224 result
+= rtl::OUString::createFromAscii( "--Date :\n" );
226 ::rtl::OUStringBuffer buffer
;
227 convertDateTime( buffer
, infor
.stDateTime
);
228 result
+= buffer
.makeStringAndClear();
229 result
+= rtl::OUString::createFromAscii( "\n" );
231 if (infor
.ouX509IssuerName
.getLength()>0 && infor
.ouX509SerialNumber
.getLength()>0 && xSecurityEnvironment
.is())
233 result
+= rtl::OUString::createFromAscii( "--Certificate Path :\n" );
234 cssu::Reference
< ::com::sun::star::security::XCertificate
> xCert
= xSecurityEnvironment
->getCertificate( infor
.ouX509IssuerName
, numericStringToBigInteger(infor
.ouX509SerialNumber
) );
235 cssu::Sequence
< cssu::Reference
< ::com::sun::star::security::XCertificate
> > xCertPath
;
238 fprintf(stdout
, " xCert is NULL , so can not buildCertificatePath\n");
243 xCertPath
= xSecurityEnvironment
->buildCertificatePath( xCert
) ;
246 for( int i
= 0; i
< xCertPath
.getLength(); i
++ )
248 result
+= xCertPath
[i
]->getSubjectName();
249 result
+= rtl::OUString::createFromAscii( "\n Subject public key algorithm : " );
250 result
+= xCertPath
[i
]->getSubjectPublicKeyAlgorithm();
251 result
+= rtl::OUString::createFromAscii( "\n Signature algorithm : " );
252 result
+= xCertPath
[i
]->getSignatureAlgorithm();
254 result
+= rtl::OUString::createFromAscii( "\n Subject public key value : " );
255 cssu::Sequence
< sal_Int8
> keyValue
= xCertPath
[i
]->getSubjectPublicKeyValue();
256 result
+= printHexString(keyValue
);
258 result
+= rtl::OUString::createFromAscii( "\n Thumbprint (SHA1) : " );
259 cssu::Sequence
< sal_Int8
> SHA1Thumbprint
= xCertPath
[i
]->getSHA1Thumbprint();
260 result
+= printHexString(SHA1Thumbprint
);
262 result
+= rtl::OUString::createFromAscii( "\n Thumbprint (MD5) : " );
263 cssu::Sequence
< sal_Int8
> MD5Thumbprint
= xCertPath
[i
]->getMD5Thumbprint();
264 result
+= printHexString(MD5Thumbprint
);
266 result
+= rtl::OUString::createFromAscii( "\n <<\n" );
269 result
+= rtl::OUString::createFromAscii( "\n Key Usage : " );
270 sal_Int32 usage
= xCert
->getCertificateUsage();
272 if (usage
& ::com::sun::star::security::KeyUsage::DIGITAL_SIGNATURE
)
274 result
+= rtl::OUString::createFromAscii( "DIGITAL_SIGNATURE " );
277 if (usage
& ::com::sun::star::security::KeyUsage::NON_REPUDIATION
)
279 result
+= rtl::OUString::createFromAscii( "NON_REPUDIATION " );
282 if (usage
& ::com::sun::star::security::KeyUsage::KEY_ENCIPHERMENT
)
284 result
+= rtl::OUString::createFromAscii( "KEY_ENCIPHERMENT " );
287 if (usage
& ::com::sun::star::security::KeyUsage::DATA_ENCIPHERMENT
)
289 result
+= rtl::OUString::createFromAscii( "DATA_ENCIPHERMENT " );
292 if (usage
& ::com::sun::star::security::KeyUsage::KEY_AGREEMENT
)
294 result
+= rtl::OUString::createFromAscii( "KEY_AGREEMENT " );
297 if (usage
& ::com::sun::star::security::KeyUsage::KEY_CERT_SIGN
)
299 result
+= rtl::OUString::createFromAscii( "KEY_CERT_SIGN " );
302 if (usage
& ::com::sun::star::security::KeyUsage::CRL_SIGN
)
304 result
+= rtl::OUString::createFromAscii( "CRL_SIGN " );
307 result
+= rtl::OUString::createFromAscii( "\n" );
310 result
+= rtl::OUString::createFromAscii( "\n" );
314 ::rtl::OUString
getSignatureInformations(
315 const SignatureInformations
& SignatureInformations
,
316 cssu::Reference
< ::com::sun::star::xml::crypto::XSecurityEnvironment
> xSecurityEnvironment
)
318 rtl::OUString result
;
320 int size
= SignatureInformations
.size();
322 for (i
=0; i
<size
; i
++)
324 const SignatureInformation
& infor
= SignatureInformations
[i
];
325 result
+= getSignatureInformation( infor
, xSecurityEnvironment
);
328 result
+= rtl::OUString::createFromAscii( "\n" );
333 ::com::sun::star::uno::Reference
< ::com::sun::star::security::XCertificate
>
334 getCertificateFromEnvironment( ::com::sun::star::uno::Reference
< ::com::sun::star::xml::crypto::XSecurityEnvironment
> xSecurityEnvironment
, BOOL nType
)
336 cssu::Sequence
< cssu::Reference
< ::com::sun::star::security::XCertificate
> > xPersonalCerts
;
341 sal_uInt16 encoding
;
342 rtl_Locale
*pLocale
= NULL
;
343 osl_getProcessLocale( &pLocale
) ;
344 encoding
= osl_getTextEncodingFromLocale( pLocale
) ;
348 xPersonalCerts
= xSecurityEnvironment
->getPersonalCertificates() ;
350 return NULL
; // not support then;
352 length
= xPersonalCerts
.getLength();
355 fprintf( stdout
, "\nNo certificate found!\n" ) ;
359 fprintf( stdout
, "\nSelect a certificate:\n" ) ;
360 for( i
= 0; i
< length
; i
++ )
362 rtl::OUString xxxIssuer
;
363 rtl::OUString xxxSubject
;
364 rtl::OString yyyIssuer
;
365 rtl::OString yyySubject
;
367 xxxIssuer
=xPersonalCerts
[i
]->getIssuerName();
368 yyyIssuer
=rtl::OUStringToOString( xxxIssuer
, encoding
);
370 xxxSubject
=xPersonalCerts
[i
]->getSubjectName();
371 yyySubject
=rtl::OUStringToOString( xxxSubject
, encoding
);
373 fprintf( stdout
, "\n%d:\nsubject=[%s]\nissuer=[%s]\n",
379 int sel
= QuerySelectNumber( 1, length
) -1;
380 return xPersonalCerts
[sel
] ;
383 void QueryPrintSignatureDetails( const SignatureInformations
& SignatureInformations
, ::com::sun::star::uno::Reference
< ::com::sun::star::xml::crypto::XSecurityEnvironment
> rSecEnv
)
386 fprintf( stdout
, "\nDisplay details (y/n) [y]?" );
388 fscanf( stdin
, "%c", &cShowDetails
);
389 if ( cShowDetails
== 'y' )
391 rtl_Locale
*pLocale
= NULL
;
392 osl_getProcessLocale( &pLocale
) ;
393 sal_uInt16 encoding
= osl_getTextEncodingFromLocale( pLocale
) ;
395 fprintf( stdout
, "------------- Signature details START -------------\n" );
396 fprintf( stdout
, "%s",
397 rtl::OUStringToOString(
398 getSignatureInformations( SignatureInformations
, rSecEnv
),
401 fprintf( stdout
, "------------- Signature details END -------------\n" );
405 int QuerySelectNumber( int nMin
, int nMax
)
407 fprintf( stdout
, "\n" ) ;
411 fprintf( stdout
, "\nSelect <%d-%d>:", nMin
, nMax
) ;
413 fscanf( stdin
, "%d", &sel
) ;
414 } while( ( sel
< nMin
) || ( sel
> nMax
) );
419 long QueryVerifySignature()
422 fprintf( stdout
, "\nFound a signature - verify this one (y/n) [y]?" );
424 fscanf( stdin
, "%c", &answer
);
425 return (answer
== 'n')?0:1;