Major revamp of credential and trust handling code, PKIX engine still needs work.
[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         /** 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         XSECCryptoKey* getPrivateKey() const {
83             if (m_key) {
84                 XSECCryptoKey::KeyType type = m_key->getKeyType();
85                 if (type!=XSECCryptoKey::KEY_RSA_PUBLIC && type!=XSECCryptoKey::KEY_DSA_PUBLIC)
86                     return m_key;
87             }
88             return NULL;
89         }
90
91         XSECCryptoKey* getPublicKey() const {
92             if (m_key) {
93                 XSECCryptoKey::KeyType type = m_key->getKeyType();
94                 if (type!=XSECCryptoKey::KEY_RSA_PRIVATE && type!=XSECCryptoKey::KEY_DSA_PRIVATE)
95                     return m_key;
96             }
97             return NULL;
98         }
99
100         std::vector<std::string>::size_type getKeyNames(std::vector<std::string>& results) const;
101
102         const xmlsignature::KeyInfo* getKeyInfo(bool compact=false) const {
103             return compact ? m_compactKeyInfo : (m_keyInfo ? m_keyInfo : m_compactKeyInfo);
104         }
105         
106         /**
107          * Gets an immutable collection of certificates in the entity's trust chain. The entity certificate is contained
108          * within this list. No specific ordering of the certificates is guaranteed.
109          * 
110          * @return a certificate chain
111          */
112         const std::vector<XSECCryptoX509*>& getEntityCertificateChain() const {
113             return m_xseccerts;
114         }
115
116         XSECCryptoX509CRL* getCRL() const {
117             return m_crl;
118         }
119     };
120 };
121
122 #endif /* __xmltooling_basicx509cred_h__ */