9241fd0aedbbe4dd55b3f04545e68c435cd8b6ca
[shibboleth/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         /**
40          * Constructor.
41          * 
42          * @param ownCerts  true iff any certificates subsequently stored should be freed by destructor
43          */
44         BasicX509Credential(bool ownCerts) : m_key(NULL), m_ownCerts(ownCerts), m_crl(NULL), m_keyInfo(NULL), m_compactKeyInfo(NULL) {
45         }
46
47         /**
48          * Constructor.
49          * 
50          * @param key   key pair or secret key
51          * @param certs array of X.509 certificates, the first entry being the entity certificate
52          * @param crl   optional CRL
53          */
54         BasicX509Credential(XSECCryptoKey* key, const std::vector<XSECCryptoX509*>& certs, XSECCryptoX509CRL* crl=NULL)
55                 : m_key(key), m_xseccerts(certs), m_ownCerts(true), m_crl(crl), m_keyInfo(NULL), m_compactKeyInfo(NULL) {
56         }
57
58         /** The private/secret key/keypair. */
59         XSECCryptoKey* m_key;
60
61         /** Key names (derived from credential, KeyInfo, or both). */
62         std::set<std::string> m_keyNames;
63
64         /** Subject DN. */
65         std::string m_subjectName;
66
67         /** Issuer DN. */
68         std::string m_issuerName;
69
70         /** Serial number. */
71         std::string m_serial;
72
73         /** The X.509 certificate chain. */
74         std::vector<XSECCryptoX509*> m_xseccerts;
75
76         /** Indicates whether to destroy certificates. */
77         bool m_ownCerts;
78
79         /** The X.509 CRL. */
80         XSECCryptoX509CRL* m_crl;
81
82         /** The KeyInfo object representing the information. */
83         xmlsignature::KeyInfo* m_keyInfo;
84
85         /** The KeyInfo object representing the information in compact form. */
86         xmlsignature::KeyInfo* m_compactKeyInfo;
87
88         /**
89          * Initializes (or reinitializes) a ds:KeyInfo to represent the Credential.
90          *
91          * @param types the kinds of KeyInfo content to include 
92          */
93         void initKeyInfo(unsigned int types=0);
94
95     public:
96         virtual ~BasicX509Credential();
97         
98         unsigned int getUsage() const {
99             return UNSPECIFIED_CREDENTIAL;
100         }
101         const char* getAlgorithm() const;
102         unsigned int getKeySize() const;
103
104         XSECCryptoKey* getPrivateKey() const {
105             if (m_key) {
106                 XSECCryptoKey::KeyType type = m_key->getKeyType();
107                 if (type!=XSECCryptoKey::KEY_RSA_PUBLIC && type!=XSECCryptoKey::KEY_DSA_PUBLIC)
108                     return m_key;
109             }
110             return NULL;
111         }
112
113         XSECCryptoKey* getPublicKey() const {
114             if (m_key) {
115                 XSECCryptoKey::KeyType type = m_key->getKeyType();
116                 if (type!=XSECCryptoKey::KEY_RSA_PRIVATE && type!=XSECCryptoKey::KEY_DSA_PRIVATE)
117                     return m_key;
118             }
119             return NULL;
120         }
121         
122         const std::set<std::string>& getKeyNames() const {
123             return m_keyNames;
124         }
125
126         xmlsignature::KeyInfo* getKeyInfo(bool compact=false) const {
127             if (compact || !m_keyInfo)
128                 return m_compactKeyInfo ? m_compactKeyInfo->cloneKeyInfo() : NULL;
129             return m_keyInfo->cloneKeyInfo();
130         }
131         
132         const std::vector<XSECCryptoX509*>& getEntityCertificateChain() const {
133             return m_xseccerts;
134         }
135
136         XSECCryptoX509CRL* getCRL() const {
137             return m_crl;
138         }
139
140         const char* getSubjectName() const {
141             return m_subjectName.c_str();
142         }
143
144         const char* getIssuerName() const {
145             return m_issuerName.c_str();
146         }
147
148         const char* getSerialNumber() const {
149             return m_serial.c_str();
150         }
151
152         void extract();
153     };
154 };
155
156 #endif /* __xmltooling_basicx509cred_h__ */