6435655253626f8262910075444fdc5fbe311c23
[shibboleth/cpp-xmltooling.git] / xmltooling / security / BasicX509Credential.h
1 /*
2  *  Copyright 2001-2007 Internet2
3  * 
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 /**
18  * @file xmltooling/security/BasicX509Credential.h
19  * 
20  * Wraps an X.509-based Credential by storing key/cert objects inside. 
21  */
22
23 #if !defined(__xmltooling_basicx509cred_h__) && !defined(XMLTOOLING_NO_XMLSEC)
24 #define __xmltooling_basicx509cred_h__
25
26 #include <xmltooling/security/X509Credential.h>
27
28 #include <algorithm>
29
30 namespace xmlsignature {
31     class XMLTOOL_API KeyInfo;
32 };
33
34 namespace xmltooling {
35
36     /**
37      * Wraps an X.509-based Credential by storing key/cert objects inside.
38      */
39     class XMLTOOL_API BasicX509Credential : public virtual X509Credential
40     {
41     protected:
42         BasicX509Credential(bool ownCerts) : m_key(NULL), m_ownCerts(ownCerts), m_crl(NULL), m_keyInfo(NULL), m_compactKeyInfo(NULL) {
43         }
44
45         /**
46          * Constructor.
47          * 
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
51          */
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) {
54         }
55
56         /** The private/secret key/keypair. */
57         XSECCryptoKey* m_key;
58
59         /** Key names (derived from credential, KeyInfo, or both). */
60         std::set<std::string> m_keyNames;
61
62         /** The X.509 certificate chain. */
63         std::vector<XSECCryptoX509*> m_xseccerts;
64
65         /** Indicates whether to destroy certificates. */
66         bool m_ownCerts;
67
68         /** The X.509 CRL. */
69         XSECCryptoX509CRL* m_crl;
70
71         /** The KeyInfo object representing the information. */
72         xmlsignature::KeyInfo* m_keyInfo;
73
74         /** The KeyInfo object representing the information in compact form. */
75         xmlsignature::KeyInfo* m_compactKeyInfo;
76
77         /**
78          * Initializes (or reinitializes) a ds:KeyInfo to represent the Credential.
79          */
80         void initKeyInfo();
81         
82     public:
83         virtual ~BasicX509Credential();
84         
85         const char* getAlgorithm() const {
86             if (m_key) {
87                 switch (m_key->getKeyType()) {
88                     case XSECCryptoKey::KEY_RSA_PRIVATE:
89                     case XSECCryptoKey::KEY_RSA_PUBLIC:
90                     case XSECCryptoKey::KEY_RSA_PAIR:
91                         return "RSA";
92
93                     case XSECCryptoKey::KEY_DSA_PRIVATE:
94                     case XSECCryptoKey::KEY_DSA_PUBLIC:
95                     case XSECCryptoKey::KEY_DSA_PAIR:
96                         return "DSA";
97                     
98                     case XSECCryptoKey::KEY_HMAC:
99                         return "HMAC";
100
101                     case XSECCryptoKey::KEY_SYMMETRIC: {
102                         XSECCryptoSymmetricKey* skey = static_cast<XSECCryptoSymmetricKey*>(m_key);
103                         switch (skey->getSymmetricKeyType()) {
104                             case XSECCryptoSymmetricKey::KEY_3DES_192:
105                                 return "DESede";
106                             case XSECCryptoSymmetricKey::KEY_AES_128:
107                                 return "AES";
108                             case XSECCryptoSymmetricKey::KEY_AES_192:
109                                 return "AES";
110                             case XSECCryptoSymmetricKey::KEY_AES_256:
111                                 return "AES";
112                         }
113                     }
114                 }
115             }
116             return NULL;
117         }
118
119         unsigned int getKeySize() const {
120             if (m_key) {
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();
127                     }
128
129                     case XSECCryptoKey::KEY_SYMMETRIC: {
130                         XSECCryptoSymmetricKey* skey = static_cast<XSECCryptoSymmetricKey*>(m_key);
131                         switch (skey->getSymmetricKeyType()) {
132                             case XSECCryptoSymmetricKey::KEY_3DES_192:
133                                 return 192;
134                             case XSECCryptoSymmetricKey::KEY_AES_128:
135                                 return 128;
136                             case XSECCryptoSymmetricKey::KEY_AES_192:
137                                 return 192;
138                             case XSECCryptoSymmetricKey::KEY_AES_256:
139                                 return 256;
140                         }
141                     }
142                 }
143             }
144             return 0;
145         }
146
147         XSECCryptoKey* getPrivateKey() const {
148             if (m_key) {
149                 XSECCryptoKey::KeyType type = m_key->getKeyType();
150                 if (type!=XSECCryptoKey::KEY_RSA_PUBLIC && type!=XSECCryptoKey::KEY_DSA_PUBLIC)
151                     return m_key;
152             }
153             return NULL;
154         }
155
156         XSECCryptoKey* getPublicKey() const {
157             if (m_key) {
158                 XSECCryptoKey::KeyType type = m_key->getKeyType();
159                 if (type!=XSECCryptoKey::KEY_RSA_PRIVATE && type!=XSECCryptoKey::KEY_DSA_PRIVATE)
160                     return m_key;
161             }
162             return NULL;
163         }
164         
165         const std::set<std::string>& getKeyNames() const {
166             return m_keyNames;
167         }
168
169         const xmlsignature::KeyInfo* getKeyInfo(bool compact=false) const {
170             return compact ? m_compactKeyInfo : (m_keyInfo ? m_keyInfo : m_compactKeyInfo);
171         }
172         
173         const std::vector<XSECCryptoX509*>& getEntityCertificateChain() const {
174             return m_xseccerts;
175         }
176
177         XSECCryptoX509CRL* getCRL() const {
178             return m_crl;
179         }
180     };
181 };
182
183 #endif /* __xmltooling_basicx509cred_h__ */