2 * Copyright 2001-2009 Internet2
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 * A helper class for working with keys, certificates, etc.
25 #include "security/OpenSSLCryptoX509CRL.h"
26 #include "security/SecurityHelper.h"
27 #include "security/X509Credential.h"
31 #include <openssl/pem.h>
32 #include <openssl/pkcs12.h>
33 #include <xsec/enc/OpenSSL/OpenSSLCryptoX509.hpp>
34 #include <xsec/enc/OpenSSL/OpenSSLCryptoKeyRSA.hpp>
35 #include <xsec/enc/OpenSSL/OpenSSLCryptoKeyDSA.hpp>
37 using namespace xmltooling::logging;
38 using namespace xmltooling;
41 // OpenSSL password callback...
42 static int passwd_callback(char* buf, int len, int verify, void* passwd)
46 if(passwd && len > strlen(reinterpret_cast<char*>(passwd)))
48 strcpy(buf,reinterpret_cast<char*>(passwd));
55 const char* SecurityHelper::guessEncodingFormat(const char* pathname)
57 const char* format=NULL;
58 BIO* in=BIO_new(BIO_s_file_internal());
59 if (in && BIO_read_filename(in, pathname)>0) {
60 const int READSIZE = 1;
64 // Examine the first byte.
66 if ((mark = BIO_tell(in)) < 0)
67 throw XMLSecurityException("Error loading file: BIO_tell() can't get the file position.");
68 if (BIO_read(in, buf, READSIZE) <= 0)
69 throw XMLSecurityException("Error loading file: BIO_read() can't read from the stream.");
70 if (BIO_seek(in, mark) < 0)
71 throw XMLSecurityException("Error loading file: BIO_seek() can't reset the file position.");
79 // Check the first byte of the file. If it's some kind of DER-encoded structure
80 // (including PKCS12), it will begin with ASCII 048. Otherwise, assume it's PEM.
85 // Here we know it's DER-encoded, now try to parse it as a PKCS12 ASN.1 structure.
86 // If it fails, must be another kind of DER-encoded structure.
88 if ((p12=d2i_PKCS12_bio(in, NULL)) == NULL) {
101 throw XMLSecurityException("Unable to determine encoding for file ($1).", params(1,pathname));
104 XSECCryptoKey* SecurityHelper::loadKeyFromFile(const char* pathname, const char* format, const char* password)
107 NDC ndc("loadKeyFromFile");
109 Category& log = Category::getInstance(XMLTOOLING_LOGCAT".SecurityHelper");
110 log.info("loading private key from file (%s)", pathname);
116 BIO* in=BIO_new(BIO_s_file_internal());
117 if (in && BIO_read_filename(in, pathname)>0) {
118 // If the format isn't set, try and guess it.
119 if (!format || !*format) {
120 const int READSIZE = 1;
124 // Examine the first byte.
126 if ((mark = BIO_tell(in)) < 0)
127 throw XMLSecurityException("Error loading key: BIO_tell() can't get the file position.");
128 if (BIO_read(in, buf, READSIZE) <= 0)
129 throw XMLSecurityException("Error loading key: BIO_read() can't read from the stream.");
130 if (BIO_seek(in, mark) < 0)
131 throw XMLSecurityException("Error loading key: BIO_seek() can't reset the file position.");
139 // Check the first byte of the file. If it's some kind of DER-encoded structure
140 // (including PKCS12), it will begin with ASCII 048. Otherwise, assume it's PEM.
145 // Here we know it's DER-encoded, now try to parse it as a PKCS12 ASN.1 structure.
146 // If it fails, must be another kind of DER-encoded structure.
147 if ((p12=d2i_PKCS12_bio(in, NULL)) == NULL) {
149 if (BIO_seek(in, mark) < 0) {
152 throw XMLSecurityException("Error loading key: BIO_seek() can't reset the file position.");
159 log.debug("key encoding format for (%s) dynamically resolved as (%s)", pathname, format);
162 // The format should be known, so parse accordingly.
163 if (!strcmp(format, "PEM")) {
164 pkey = PEM_read_bio_PrivateKey(in, NULL, passwd_callback, const_cast<char*>(password));
166 else if (!strcmp(format, "DER")) {
167 pkey=d2i_PrivateKey_bio(in, NULL);
169 else if (!strcmp(format, "PKCS12")) {
171 p12 = d2i_PKCS12_bio(in, NULL);
174 PKCS12_parse(p12, const_cast<char*>(password), &pkey, &x, NULL);
180 log.error("unknown key encoding format (%s)", format);
186 // Now map it to an XSEC wrapper.
188 XSECCryptoKey* ret=NULL;
189 switch (pkey->type) {
191 ret=new OpenSSLCryptoKeyRSA(pkey);
195 ret=new OpenSSLCryptoKeyDSA(pkey);
199 log.error("unsupported private key type");
207 throw XMLSecurityException("Unable to load private key from file ($1).", params(1, pathname));
210 vector<XSECCryptoX509*>::size_type SecurityHelper::loadCertificatesFromFile(
211 vector<XSECCryptoX509*>& certs, const char* pathname, const char* format, const char* password
215 NDC ndc("loadCertificatesFromFile");
217 Category& log = Category::getInstance(XMLTOOLING_LOGCAT".SecurityHelper");
218 log.info("loading certificate(s) from file (%s)", pathname);
220 vector<XSECCryptoX509*>::size_type count = certs.size();
226 BIO* in=BIO_new(BIO_s_file_internal());
227 if (in && BIO_read_filename(in, pathname)>0) {
228 // If the format isn't set, try and guess it.
229 if (!format || !*format) {
230 const int READSIZE = 1;
234 // Examine the first byte.
236 if ((mark = BIO_tell(in)) < 0)
237 throw XMLSecurityException("Error loading certificate: BIO_tell() can't get the file position.");
238 if (BIO_read(in, buf, READSIZE) <= 0)
239 throw XMLSecurityException("Error loading certificate: BIO_read() can't read from the stream.");
240 if (BIO_seek(in, mark) < 0)
241 throw XMLSecurityException("Error loading certificate: BIO_seek() can't reset the file position.");
249 // Check the first byte of the file. If it's some kind of DER-encoded structure
250 // (including PKCS12), it will begin with ASCII 048. Otherwise, assume it's PEM.
255 // Here we know it's DER-encoded, now try to parse it as a PKCS12 ASN.1 structure.
256 // If it fails, must be another kind of DER-encoded structure.
257 if ((p12=d2i_PKCS12_bio(in, NULL)) == NULL) {
259 if (BIO_seek(in, mark) < 0) {
262 throw XMLSecurityException("Error loading certificate: BIO_seek() can't reset the file position.");
271 // The format should be known, so parse accordingly.
272 if (!strcmp(format, "PEM")) {
273 while (x=PEM_read_bio_X509(in, NULL, NULL, NULL)) {
274 certs.push_back(new OpenSSLCryptoX509(x));
278 else if (!strcmp(format, "DER")) {
279 x=d2i_X509_bio(in, NULL);
281 certs.push_back(new OpenSSLCryptoX509(x));
285 else if (!strcmp(format, "PKCS12")) {
287 p12 = d2i_PKCS12_bio(in, NULL);
290 STACK_OF(X509)* CAstack = sk_X509_new_null();
291 PKCS12_parse(p12, const_cast<char*>(password), &pkey, &x, &CAstack);
295 certs.push_back(new OpenSSLCryptoX509(x));
298 x = sk_X509_pop(CAstack);
300 certs.push_back(new OpenSSLCryptoX509(x));
302 x = sk_X509_pop(CAstack);
304 sk_X509_free(CAstack);
311 if (certs.size() == count) {
313 throw XMLSecurityException("Unable to load certificate(s) from file ($1).", params(1, pathname));
319 vector<XSECCryptoX509CRL*>::size_type SecurityHelper::loadCRLsFromFile(
320 vector<XSECCryptoX509CRL*>& crls, const char* pathname, const char* format
324 NDC ndc("loadCRLsFromFile");
326 Category& log = Category::getInstance(XMLTOOLING_LOGCAT".SecurityHelper");
327 log.info("loading CRL(s) from file (%s)", pathname);
329 vector<XSECCryptoX509CRL*>::size_type count = crls.size();
331 BIO* in=BIO_new(BIO_s_file_internal());
332 if (in && BIO_read_filename(in, pathname)>0) {
333 // If the format isn't set, try and guess it.
334 if (!format || !*format) {
335 const int READSIZE = 1;
339 // Examine the first byte.
341 if ((mark = BIO_tell(in)) < 0)
342 throw XMLSecurityException("Error loading CRL: BIO_tell() can't get the file position.");
343 if (BIO_read(in, buf, READSIZE) <= 0)
344 throw XMLSecurityException("Error loading CRL: BIO_read() can't read from the stream.");
345 if (BIO_seek(in, mark) < 0)
346 throw XMLSecurityException("Error loading CRL: BIO_seek() can't reset the file position.");
354 // Check the first byte of the file. If it's some kind of DER-encoded structure
355 // it will begin with ASCII 048. Otherwise, assume it's PEM.
362 log.debug("CRL encoding format for (%s) dynamically resolved as (%s)", pathname, format);
366 if (!strcmp(format, "PEM")) {
367 while (crl=PEM_read_bio_X509_CRL(in, NULL, NULL, NULL)) {
368 crls.push_back(new OpenSSLCryptoX509CRL(crl));
372 else if (!strcmp(format, "DER")) {
373 crl=d2i_X509_CRL_bio(in, NULL);
375 crls.push_back(new OpenSSLCryptoX509CRL(crl));
380 log.error("unknown CRL encoding format (%s)", format);
386 if (crls.size() == count) {
388 throw XMLSecurityException("Unable to load CRL(s) from file ($1).", params(1, pathname));
394 XSECCryptoKey* SecurityHelper::loadKeyFromURL(SOAPTransport& transport, const char* backing, const char* format, const char* password)
398 istream& msg = transport.receive();
400 // Dump to output file.
401 ofstream out(backing, fstream::trunc|fstream::binary);
405 return loadKeyFromFile(backing, format, password);
408 vector<XSECCryptoX509*>::size_type SecurityHelper::loadCertificatesFromURL(
409 vector<XSECCryptoX509*>& certs, SOAPTransport& transport, const char* backing, const char* format, const char* password
413 istream& msg = transport.receive();
415 // Dump to output file.
416 ofstream out(backing, fstream::trunc|fstream::binary);
420 return loadCertificatesFromFile(certs, backing, format, password);
423 vector<XSECCryptoX509CRL*>::size_type SecurityHelper::loadCRLsFromURL(
424 vector<XSECCryptoX509CRL*>& crls, SOAPTransport& transport, const char* backing, const char* format
429 istream& msg = transport.receive();
431 // Dump to output file.
432 ofstream out(backing, fstream::trunc|fstream::binary);
436 return loadCRLsFromFile(crls, backing, format);
439 bool SecurityHelper::matches(const XSECCryptoKey& key1, const XSECCryptoKey& key2)
441 if (key1.getProviderName()!=DSIGConstants::s_unicodeStrPROVOpenSSL ||
442 key2.getProviderName()!=DSIGConstants::s_unicodeStrPROVOpenSSL) {
443 Category::getInstance(XMLTOOLING_LOGCAT".SecurityHelper").warn("comparison of non-OpenSSL keys not supported");
447 // If one key is public or both, just compare the public key half.
448 if (key1.getKeyType()==XSECCryptoKey::KEY_RSA_PUBLIC || key1.getKeyType()==XSECCryptoKey::KEY_RSA_PAIR) {
449 if (key2.getKeyType()!=XSECCryptoKey::KEY_RSA_PUBLIC && key2.getKeyType()!=XSECCryptoKey::KEY_RSA_PAIR)
451 const RSA* rsa1 = static_cast<const OpenSSLCryptoKeyRSA&>(key1).getOpenSSLRSA();
452 const RSA* rsa2 = static_cast<const OpenSSLCryptoKeyRSA&>(key2).getOpenSSLRSA();
453 return (rsa1 && rsa2 && BN_cmp(rsa1->n,rsa2->n) == 0 && BN_cmp(rsa1->e,rsa2->e) == 0);
456 // For a private key, compare the private half.
457 if (key1.getKeyType()==XSECCryptoKey::KEY_RSA_PRIVATE) {
458 if (key2.getKeyType()!=XSECCryptoKey::KEY_RSA_PRIVATE && key2.getKeyType()!=XSECCryptoKey::KEY_RSA_PAIR)
460 const RSA* rsa1 = static_cast<const OpenSSLCryptoKeyRSA&>(key1).getOpenSSLRSA();
461 const RSA* rsa2 = static_cast<const OpenSSLCryptoKeyRSA&>(key2).getOpenSSLRSA();
462 return (rsa1 && rsa2 && BN_cmp(rsa1->n,rsa2->n) == 0 && BN_cmp(rsa1->d,rsa2->d) == 0);
465 // If one key is public or both, just compare the public key half.
466 if (key1.getKeyType()==XSECCryptoKey::KEY_DSA_PUBLIC || key1.getKeyType()==XSECCryptoKey::KEY_DSA_PAIR) {
467 if (key2.getKeyType()!=XSECCryptoKey::KEY_DSA_PUBLIC && key2.getKeyType()!=XSECCryptoKey::KEY_DSA_PAIR)
469 const DSA* dsa1 = static_cast<const OpenSSLCryptoKeyDSA&>(key1).getOpenSSLDSA();
470 const DSA* dsa2 = static_cast<const OpenSSLCryptoKeyDSA&>(key2).getOpenSSLDSA();
471 return (dsa1 && dsa2 && BN_cmp(dsa1->pub_key,dsa2->pub_key) == 0);
474 // For a private key, compare the private half.
475 if (key1.getKeyType()==XSECCryptoKey::KEY_DSA_PRIVATE) {
476 if (key2.getKeyType()!=XSECCryptoKey::KEY_DSA_PRIVATE && key2.getKeyType()!=XSECCryptoKey::KEY_DSA_PAIR)
478 const DSA* dsa1 = static_cast<const OpenSSLCryptoKeyDSA&>(key1).getOpenSSLDSA();
479 const DSA* dsa2 = static_cast<const OpenSSLCryptoKeyDSA&>(key2).getOpenSSLDSA();
480 return (dsa1 && dsa2 && BN_cmp(dsa1->priv_key,dsa2->priv_key) == 0);
483 Category::getInstance(XMLTOOLING_LOGCAT".SecurityHelper").warn("unsupported key type for comparison");
487 string SecurityHelper::doHash(const char* hashAlg, const char* buf, unsigned long buflen)
489 static char DIGITS[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
492 const EVP_MD* md = EVP_get_digestbyname(hashAlg);
494 Category::getInstance(XMLTOOLING_LOGCAT".SecurityHelper").error("hash algorithm (%s) not available", hashAlg);
498 BIO* chain = BIO_new(BIO_s_mem());
499 BIO* b = BIO_new(BIO_f_md());
501 chain = BIO_push(b, chain);
502 BIO_write(chain, buf, buflen);
505 char digest[EVP_MAX_MD_SIZE];
506 int len = BIO_gets(chain, digest, EVP_MD_size(md));
508 if (len != EVP_MD_size(md)) {
509 Category::getInstance(XMLTOOLING_LOGCAT".SecurityHelper").error(
510 "hash result length (%d) did not match expected value (%d)", len, EVP_MD_size(md)
514 for (unsigned int i=0; i < len; ++i) {
515 ret+=(DIGITS[((unsigned char)(0xF0 & digest[i])) >> 4 ]);
516 ret+=(DIGITS[0x0F & digest[i]]);
521 string SecurityHelper::getDEREncoding(const XSECCryptoKey& key, const char* hash, bool nowrap)
525 if (key.getProviderName()!=DSIGConstants::s_unicodeStrPROVOpenSSL) {
526 Category::getInstance(XMLTOOLING_LOGCAT".SecurityHelper").warn("encoding of non-OpenSSL keys not supported");
530 if (key.getKeyType() == XSECCryptoKey::KEY_RSA_PUBLIC || key.getKeyType() == XSECCryptoKey::KEY_RSA_PAIR) {
531 const RSA* rsa = static_cast<const OpenSSLCryptoKeyRSA&>(key).getOpenSSLRSA();
533 Category::getInstance(XMLTOOLING_LOGCAT".SecurityHelper").warn("key was not populated");
536 const EVP_MD* md=NULL;
538 md = EVP_get_digestbyname(hash);
540 Category::getInstance(XMLTOOLING_LOGCAT".SecurityHelper").error("hash algorithm (%s) not available", hash);
544 BIO* chain = BIO_new(BIO_s_mem());
545 BIO* b = BIO_new(BIO_f_base64());
547 BIO_set_flags(b, BIO_FLAGS_BASE64_NO_NL);
548 chain = BIO_push(b, chain);
550 b = BIO_new(BIO_f_md());
552 chain = BIO_push(b, chain);
554 i2d_RSA_PUBKEY_bio(chain, const_cast<RSA*>(rsa));
557 char digest[EVP_MAX_MD_SIZE];
558 int len = BIO_gets(chain, digest, EVP_MD_size(md));
559 if (len != EVP_MD_size(md)) {
567 BIO_write(chain, digest, len);
571 BIO_get_mem_ptr(chain, &bptr);
572 if (bptr && bptr->length > 0)
573 ret.append(bptr->data, bptr->length);
576 else if (key.getKeyType() == XSECCryptoKey::KEY_DSA_PUBLIC || key.getKeyType() == XSECCryptoKey::KEY_DSA_PAIR) {
577 const DSA* dsa = static_cast<const OpenSSLCryptoKeyDSA&>(key).getOpenSSLDSA();
579 Category::getInstance(XMLTOOLING_LOGCAT".SecurityHelper").warn("key was not populated");
582 const EVP_MD* md=NULL;
584 md = EVP_get_digestbyname(hash);
586 Category::getInstance(XMLTOOLING_LOGCAT".SecurityHelper").error("hash algorithm (%s) not available", hash);
590 BIO* chain = BIO_new(BIO_s_mem());
591 BIO* b = BIO_new(BIO_f_base64());
593 BIO_set_flags(b, BIO_FLAGS_BASE64_NO_NL);
594 chain = BIO_push(b, chain);
596 b = BIO_new(BIO_f_md());
598 chain = BIO_push(b, chain);
600 i2d_DSA_PUBKEY_bio(chain, const_cast<DSA*>(dsa));
603 char digest[EVP_MAX_MD_SIZE];
604 int len = BIO_gets(chain, digest, EVP_MD_size(md));
605 if (len != EVP_MD_size(md)) {
613 BIO_write(chain, digest, len);
617 BIO_get_mem_ptr(chain, &bptr);
618 if (bptr && bptr->length > 0)
619 ret.append(bptr->data, bptr->length);
623 Category::getInstance(XMLTOOLING_LOGCAT".SecurityHelper").warn("encoding of non-RSA/DSA public keys not supported");
628 string SecurityHelper::getDEREncoding(const XSECCryptoX509& cert, const char* hash, bool nowrap)
632 if (cert.getProviderName()!=DSIGConstants::s_unicodeStrPROVOpenSSL) {
633 Category::getInstance(XMLTOOLING_LOGCAT".SecurityHelper").warn("encoding of non-OpenSSL keys not supported");
637 const EVP_MD* md=NULL;
639 md = EVP_get_digestbyname(hash);
641 Category::getInstance(XMLTOOLING_LOGCAT".SecurityHelper").error("hash algorithm (%s) not available", hash);
646 const X509* x = static_cast<const OpenSSLCryptoX509&>(cert).getOpenSSLX509();
647 EVP_PKEY* key = X509_get_pubkey(const_cast<X509*>(x));
649 BIO* chain = BIO_new(BIO_s_mem());
650 BIO* b = BIO_new(BIO_f_base64());
652 BIO_set_flags(b, BIO_FLAGS_BASE64_NO_NL);
653 chain = BIO_push(b, chain);
655 b = BIO_new(BIO_f_md());
657 chain = BIO_push(b, chain);
659 i2d_PUBKEY_bio(chain, key);
663 char digest[EVP_MAX_MD_SIZE];
664 int len = BIO_gets(chain, digest, EVP_MD_size(md));
665 if (len != EVP_MD_size(md)) {
673 BIO_write(chain, digest, len);
677 BIO_get_mem_ptr(chain, &bptr);
678 if (bptr && bptr->length > 0)
679 ret.append(bptr->data, bptr->length);
684 string SecurityHelper::getDEREncoding(const Credential& cred, const char* hash, bool nowrap)
686 const X509Credential* x509 = dynamic_cast<const X509Credential*>(&cred);
687 if (x509 && !x509->getEntityCertificateChain().empty())
688 return getDEREncoding(*(x509->getEntityCertificateChain().front()), hash, nowrap);
689 else if (cred.getPublicKey())
690 return getDEREncoding(*(cred.getPublicKey()), hash, nowrap);
694 string SecurityHelper::getDEREncoding(const XSECCryptoKey& key, bool hash, bool nowrap)
696 return getDEREncoding(key, hash ? "SHA1" : NULL, nowrap);
699 string SecurityHelper::getDEREncoding(const XSECCryptoX509& cert, bool hash, bool nowrap)
701 return getDEREncoding(cert, hash ? "SHA1" : NULL, nowrap);
704 string SecurityHelper::getDEREncoding(const Credential& cred, bool hash, bool nowrap)
706 return getDEREncoding(cred, hash ? "SHA1" : NULL, nowrap);