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 * @file xmltooling/security/BasicX509Credential.h
20 * Wraps an X.509-based Credential by storing key/cert objects inside.
23 #if !defined(__xmltooling_basicx509cred_h__) && !defined(XMLTOOLING_NO_XMLSEC)
24 #define __xmltooling_basicx509cred_h__
26 #include <xmltooling/security/X509Credential.h>
30 namespace xmlsignature {
31 class XMLTOOL_API KeyInfo;
34 namespace xmltooling {
37 * Wraps an X.509-based Credential by storing key/cert objects inside.
39 class XMLTOOL_API BasicX509Credential : public virtual X509Credential
42 BasicX509Credential(bool ownCerts) : m_key(NULL), m_ownCerts(ownCerts), m_crl(NULL), m_keyInfo(NULL), m_compactKeyInfo(NULL) {
48 * @param key key pair or secret key
49 * @param certs array of X.509 certificates, the first entry being the entity certificate
50 * @param crl optional CRL
52 BasicX509Credential(XSECCryptoKey* key, const std::vector<XSECCryptoX509*>& certs, XSECCryptoX509CRL* crl=NULL)
53 : m_key(key), m_xseccerts(certs), m_ownCerts(true), m_crl(crl), m_keyInfo(NULL), m_compactKeyInfo(NULL) {
56 /** The private/secret key/keypair. */
59 /** Key names (derived from credential, KeyInfo, or both). */
60 std::set<std::string> m_keyNames;
62 /** The X.509 certificate chain. */
63 std::vector<XSECCryptoX509*> m_xseccerts;
65 /** Indicates whether to destroy certificates. */
69 XSECCryptoX509CRL* m_crl;
71 /** The KeyInfo object representing the information. */
72 xmlsignature::KeyInfo* m_keyInfo;
74 /** The KeyInfo object representing the information in compact form. */
75 xmlsignature::KeyInfo* m_compactKeyInfo;
78 * Initializes (or reinitializes) a ds:KeyInfo to represent the Credential.
83 virtual ~BasicX509Credential();
85 const char* getAlgorithm() const {
87 switch (m_key->getKeyType()) {
88 case XSECCryptoKey::KEY_RSA_PRIVATE:
89 case XSECCryptoKey::KEY_RSA_PUBLIC:
90 case XSECCryptoKey::KEY_RSA_PAIR:
93 case XSECCryptoKey::KEY_DSA_PRIVATE:
94 case XSECCryptoKey::KEY_DSA_PUBLIC:
95 case XSECCryptoKey::KEY_DSA_PAIR:
98 case XSECCryptoKey::KEY_HMAC:
101 case XSECCryptoKey::KEY_SYMMETRIC: {
102 XSECCryptoSymmetricKey* skey = static_cast<XSECCryptoSymmetricKey*>(m_key);
103 switch (skey->getSymmetricKeyType()) {
104 case XSECCryptoSymmetricKey::KEY_3DES_192:
106 case XSECCryptoSymmetricKey::KEY_AES_128:
108 case XSECCryptoSymmetricKey::KEY_AES_192:
110 case XSECCryptoSymmetricKey::KEY_AES_256:
119 unsigned int getKeySize() const {
121 switch (m_key->getKeyType()) {
122 case XSECCryptoKey::KEY_RSA_PRIVATE:
123 case XSECCryptoKey::KEY_RSA_PUBLIC:
124 case XSECCryptoKey::KEY_RSA_PAIR: {
125 XSECCryptoKeyRSA* rkey = static_cast<XSECCryptoKeyRSA*>(m_key);
126 return rkey->getLength();
129 case XSECCryptoKey::KEY_SYMMETRIC: {
130 XSECCryptoSymmetricKey* skey = static_cast<XSECCryptoSymmetricKey*>(m_key);
131 switch (skey->getSymmetricKeyType()) {
132 case XSECCryptoSymmetricKey::KEY_3DES_192:
134 case XSECCryptoSymmetricKey::KEY_AES_128:
136 case XSECCryptoSymmetricKey::KEY_AES_192:
138 case XSECCryptoSymmetricKey::KEY_AES_256:
147 XSECCryptoKey* getPrivateKey() const {
149 XSECCryptoKey::KeyType type = m_key->getKeyType();
150 if (type!=XSECCryptoKey::KEY_RSA_PUBLIC && type!=XSECCryptoKey::KEY_DSA_PUBLIC)
156 XSECCryptoKey* getPublicKey() const {
158 XSECCryptoKey::KeyType type = m_key->getKeyType();
159 if (type!=XSECCryptoKey::KEY_RSA_PRIVATE && type!=XSECCryptoKey::KEY_DSA_PRIVATE)
165 const std::set<std::string>& getKeyNames() const {
169 const xmlsignature::KeyInfo* getKeyInfo(bool compact=false) const {
170 return compact ? m_compactKeyInfo : (m_keyInfo ? m_keyInfo : m_compactKeyInfo);
173 const std::vector<XSECCryptoX509*>& getEntityCertificateChain() const {
177 XSECCryptoX509CRL* getCRL() const {
183 #endif /* __xmltooling_basicx509cred_h__ */