2 * Copyright 2001-2007 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.
18 * BasicX509Credential.cpp
20 * Wraps an X.509-based Credential by storing key/cert objects inside.
24 #include "security/BasicX509Credential.h"
25 #include "signature/KeyInfo.h"
28 #include <openssl/x509v3.h>
29 #include <xsec/enc/OpenSSL/OpenSSLCryptoX509.hpp>
31 using namespace xmlsignature;
32 using namespace xmltooling;
35 BasicX509Credential::~BasicX509Credential()
39 for_each(m_xseccerts.begin(), m_xseccerts.end(), xmltooling::cleanup<XSECCryptoX509>());
42 delete m_compactKeyInfo;
45 void BasicX509Credential::initKeyInfo()
49 delete m_compactKeyInfo;
50 m_compactKeyInfo = NULL;
52 const set<string>& names = getKeyNames();
54 m_compactKeyInfo = KeyInfoBuilder::buildKeyInfo();
55 VectorOf(KeyName) knames = m_compactKeyInfo->getKeyNames();
56 for (set<string>::const_iterator n = names.begin(); n!=names.end(); ++n) {
57 auto_ptr_XMLCh wide(n->c_str());
58 KeyName* kname = KeyNameBuilder::buildKeyName();
59 kname->setName(wide.get());
60 knames.push_back(kname);
64 if (!m_xseccerts.empty()) {
65 m_keyInfo = m_compactKeyInfo ? m_compactKeyInfo->cloneKeyInfo() : KeyInfoBuilder::buildKeyInfo();
66 X509Data* x509Data=X509DataBuilder::buildX509Data();
67 m_keyInfo->getX509Datas().push_back(x509Data);
68 for (vector<XSECCryptoX509*>::const_iterator x = m_xseccerts.begin(); x!=m_xseccerts.end(); ++x) {
69 safeBuffer& buf=(*x)->getDEREncodingSB();
70 X509Certificate* x509=X509CertificateBuilder::buildX509Certificate();
71 x509->setValue(buf.sbStrToXMLCh());
72 x509Data->getX509Certificates().push_back(x509);
77 void X509Credential::extractNames(XSECCryptoX509* x509, set<string>& names)
79 if (!x509 || x509->getProviderName()!=DSIGConstants::s_unicodeStrPROVOpenSSL)
82 X509* cert = static_cast<OpenSSLCryptoX509*>(x509)->getOpenSSLX509();
86 X509_NAME* subject=X509_get_subject_name(cert);
89 memset(buf,0,sizeof(buf));
90 if (X509_NAME_get_text_by_NID(subject,NID_commonName,buf,255)>0)
93 STACK_OF(GENERAL_NAME)* altnames=(STACK_OF(GENERAL_NAME)*)X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
96 int numalts = sk_GENERAL_NAME_num(altnames);
97 for (int an=0; an<numalts; an++) {
98 const GENERAL_NAME* check = sk_GENERAL_NAME_value(altnames, an);
99 if (check->type==GEN_DNS || check->type==GEN_URI) {
100 const char* altptr = (char*)ASN1_STRING_data(check->d.ia5);
101 const int altlen = ASN1_STRING_length(check->d.ia5);
104 alt.append(altptr,altlen);
110 GENERAL_NAMES_free(altnames);
114 const char* BasicX509Credential::getAlgorithm() const
117 switch (m_key->getKeyType()) {
118 case XSECCryptoKey::KEY_RSA_PRIVATE:
119 case XSECCryptoKey::KEY_RSA_PUBLIC:
120 case XSECCryptoKey::KEY_RSA_PAIR:
123 case XSECCryptoKey::KEY_DSA_PRIVATE:
124 case XSECCryptoKey::KEY_DSA_PUBLIC:
125 case XSECCryptoKey::KEY_DSA_PAIR:
128 case XSECCryptoKey::KEY_HMAC:
131 case XSECCryptoKey::KEY_SYMMETRIC: {
132 XSECCryptoSymmetricKey* skey = static_cast<XSECCryptoSymmetricKey*>(m_key);
133 switch (skey->getSymmetricKeyType()) {
134 case XSECCryptoSymmetricKey::KEY_3DES_192:
136 case XSECCryptoSymmetricKey::KEY_AES_128:
138 case XSECCryptoSymmetricKey::KEY_AES_192:
140 case XSECCryptoSymmetricKey::KEY_AES_256:
149 unsigned int BasicX509Credential::getKeySize() const
152 switch (m_key->getKeyType()) {
153 case XSECCryptoKey::KEY_RSA_PRIVATE:
154 case XSECCryptoKey::KEY_RSA_PUBLIC:
155 case XSECCryptoKey::KEY_RSA_PAIR: {
156 XSECCryptoKeyRSA* rkey = static_cast<XSECCryptoKeyRSA*>(m_key);
157 return rkey->getLength();
160 case XSECCryptoKey::KEY_SYMMETRIC: {
161 XSECCryptoSymmetricKey* skey = static_cast<XSECCryptoSymmetricKey*>(m_key);
162 switch (skey->getSymmetricKeyType()) {
163 case XSECCryptoSymmetricKey::KEY_3DES_192:
165 case XSECCryptoSymmetricKey::KEY_AES_128:
167 case XSECCryptoSymmetricKey::KEY_AES_192:
169 case XSECCryptoSymmetricKey::KEY_AES_256: