Multi-line svn commit, see body.
[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 #include <xmltooling/signature/KeyInfo.h>
28
29 #include <algorithm>
30
31 namespace xmltooling {
32
33     /**
34      * Wraps an X.509-based Credential by storing key/cert objects inside.
35      */
36     class XMLTOOL_API BasicX509Credential : public virtual X509Credential
37     {
38     protected:
39         BasicX509Credential(bool ownCerts) : m_key(NULL), m_ownCerts(ownCerts), m_crl(NULL), m_keyInfo(NULL), m_compactKeyInfo(NULL) {
40         }
41
42         /**
43          * Constructor.
44          * 
45          * @param key   key pair or secret key
46          * @param certs array of X.509 certificates, the first entry being the entity certificate
47          * @param crl   optional CRL
48          */
49         BasicX509Credential(XSECCryptoKey* key, const std::vector<XSECCryptoX509*>& certs, XSECCryptoX509CRL* crl=NULL)
50                 : m_key(key), m_xseccerts(certs), m_ownCerts(true), m_crl(crl), m_keyInfo(NULL), m_compactKeyInfo(NULL) {
51         }
52
53         /** The private/secret key/keypair. */
54         XSECCryptoKey* m_key;
55
56         /** Key names (derived from credential, KeyInfo, or both). */
57         std::set<std::string> m_keyNames;
58
59         /** The X.509 certificate chain. */
60         std::vector<XSECCryptoX509*> m_xseccerts;
61
62         /** Indicates whether to destroy certificates. */
63         bool m_ownCerts;
64
65         /** The X.509 CRL. */
66         XSECCryptoX509CRL* m_crl;
67
68         /** The KeyInfo object representing the information. */
69         xmlsignature::KeyInfo* m_keyInfo;
70
71         /** The KeyInfo object representing the information in compact form. */
72         xmlsignature::KeyInfo* m_compactKeyInfo;
73
74         /**
75          * Initializes (or reinitializes) a ds:KeyInfo to represent the Credential.
76          */
77         void initKeyInfo();
78         
79     public:
80         virtual ~BasicX509Credential();
81         
82         const char* getAlgorithm() const {
83             if (m_key) {
84                 switch (m_key->getKeyType()) {
85                     case XSECCryptoKey::KEY_RSA_PRIVATE:
86                     case XSECCryptoKey::KEY_RSA_PUBLIC:
87                     case XSECCryptoKey::KEY_RSA_PAIR:
88                         return "RSA";
89
90                     case XSECCryptoKey::KEY_DSA_PRIVATE:
91                     case XSECCryptoKey::KEY_DSA_PUBLIC:
92                     case XSECCryptoKey::KEY_DSA_PAIR:
93                         return "DSA";
94                     
95                     case XSECCryptoKey::KEY_HMAC:
96                         return "HMAC";
97
98                     case XSECCryptoKey::KEY_SYMMETRIC: {
99                         XSECCryptoSymmetricKey* skey = static_cast<XSECCryptoSymmetricKey*>(m_key);
100                         switch (skey->getSymmetricKeyType()) {
101                             case XSECCryptoSymmetricKey::KEY_3DES_192:
102                                 return "DESede";
103                             case XSECCryptoSymmetricKey::KEY_AES_128:
104                                 return "AES";
105                             case XSECCryptoSymmetricKey::KEY_AES_192:
106                                 return "AES";
107                             case XSECCryptoSymmetricKey::KEY_AES_256:
108                                 return "AES";
109                         }
110                     }
111                 }
112             }
113             return NULL;
114         }
115
116         unsigned int getKeySize() const {
117             if (m_key) {
118                 switch (m_key->getKeyType()) {
119                     case XSECCryptoKey::KEY_RSA_PRIVATE:
120                     case XSECCryptoKey::KEY_RSA_PUBLIC:
121                     case XSECCryptoKey::KEY_RSA_PAIR: {
122                         XSECCryptoKeyRSA* rkey = static_cast<XSECCryptoKeyRSA*>(m_key);
123                         return rkey->getLength();
124                     }
125
126                     case XSECCryptoKey::KEY_SYMMETRIC: {
127                         XSECCryptoSymmetricKey* skey = static_cast<XSECCryptoSymmetricKey*>(m_key);
128                         switch (skey->getSymmetricKeyType()) {
129                             case XSECCryptoSymmetricKey::KEY_3DES_192:
130                                 return 192;
131                             case XSECCryptoSymmetricKey::KEY_AES_128:
132                                 return 128;
133                             case XSECCryptoSymmetricKey::KEY_AES_192:
134                                 return 192;
135                             case XSECCryptoSymmetricKey::KEY_AES_256:
136                                 return 256;
137                         }
138                     }
139                 }
140             }
141             return 0;
142         }
143
144         XSECCryptoKey* getPrivateKey() const {
145             if (m_key) {
146                 XSECCryptoKey::KeyType type = m_key->getKeyType();
147                 if (type!=XSECCryptoKey::KEY_RSA_PUBLIC && type!=XSECCryptoKey::KEY_DSA_PUBLIC)
148                     return m_key;
149             }
150             return NULL;
151         }
152
153         XSECCryptoKey* getPublicKey() const {
154             if (m_key) {
155                 XSECCryptoKey::KeyType type = m_key->getKeyType();
156                 if (type!=XSECCryptoKey::KEY_RSA_PRIVATE && type!=XSECCryptoKey::KEY_DSA_PRIVATE)
157                     return m_key;
158             }
159             return NULL;
160         }
161         
162         const std::set<std::string>& getKeyNames() const {
163             return m_keyNames;
164         }
165
166         xmlsignature::KeyInfo* getKeyInfo(bool compact=false) const {
167             if (compact || !m_keyInfo)
168                 return m_compactKeyInfo ? m_compactKeyInfo->cloneKeyInfo() : NULL;
169             return m_keyInfo->cloneKeyInfo();
170         }
171         
172         const std::vector<XSECCryptoX509*>& getEntityCertificateChain() const {
173             return m_xseccerts;
174         }
175
176         XSECCryptoX509CRL* getCRL() const {
177             return m_crl;
178         }
179     };
180 };
181
182 #endif /* __xmltooling_basicx509cred_h__ */