1bfe2ec45a1b267b9d170ce24da2f39c511aa5cf
[shibboleth/cpp-xmltooling.git] / xmltooling / security / BasicX509Credential.h
1 /*
2  *  Copyright 2001-2010 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 <set>
29 #include <vector>
30 #include <string>
31
32 namespace xmlsignature {
33     class XMLTOOL_API KeyInfo;
34 };
35
36 namespace xmltooling {
37
38     /**
39      * Wraps an X.509-based Credential by storing key/cert objects inside.
40      */
41     class XMLTOOL_API BasicX509Credential : public virtual X509Credential
42     {
43     protected:
44         /**
45          * Constructor.
46          * 
47          * @param ownCerts  true iff any certificates subsequently stored should be freed by destructor
48          */
49         BasicX509Credential(bool ownCerts);
50
51         /**
52          * Constructor.
53          * 
54          * @param key   key pair or secret key
55          * @param certs array of X.509 certificates, the first entry being the entity certificate
56          * @param crl   optional CRL
57          */
58         BasicX509Credential(XSECCryptoKey* key, const std::vector<XSECCryptoX509*>& certs, XSECCryptoX509CRL* crl=nullptr);
59
60         /**
61          * Constructor.
62          * 
63          * @param key   key pair or secret key
64          * @param certs array of X.509 certificates, the first entry being the entity certificate
65          * @param crls  array of X.509 CRLs
66          */
67         BasicX509Credential(XSECCryptoKey* key, const std::vector<XSECCryptoX509*>& certs, const std::vector<XSECCryptoX509CRL*>& crls);
68
69         /** The private/secret key/keypair. */
70         XSECCryptoKey* m_key;
71
72         /** Key names (derived from credential, KeyInfo, or both). */
73         std::set<std::string> m_keyNames;
74
75         /** Subject DN. */
76         std::string m_subjectName;
77
78         /** Issuer DN. */
79         std::string m_issuerName;
80
81         /** Serial number. */
82         std::string m_serial;
83
84         /** The X.509 certificate chain. */
85         std::vector<XSECCryptoX509*> m_xseccerts;
86
87         /** Indicates whether to destroy certificates. */
88         bool m_ownCerts;
89
90         /** The X.509 CRLs. */
91         std::vector<XSECCryptoX509CRL*> m_crls;
92
93         /** The KeyInfo object representing the information. */
94         xmlsignature::KeyInfo* m_keyInfo;
95
96         /** The KeyInfo object representing the information in compact form. */
97         xmlsignature::KeyInfo* m_compactKeyInfo;
98
99         /**
100          * Initializes (or reinitializes) a ds:KeyInfo to represent the Credential.
101          *
102          * @param types the kinds of KeyInfo content to include 
103          */
104         void initKeyInfo(unsigned int types=0);
105
106     public:
107         virtual ~BasicX509Credential();
108         
109         // Virtual function overrides.
110         unsigned int getUsage() const;
111         const char* getAlgorithm() const;
112         unsigned int getKeySize() const;
113         XSECCryptoKey* getPrivateKey() const;
114         XSECCryptoKey* getPublicKey() const;
115         const std::set<std::string>& getKeyNames() const;
116         xmlsignature::KeyInfo* getKeyInfo(bool compact=false) const;
117         const std::vector<XSECCryptoX509*>& getEntityCertificateChain() const;
118         XSECCryptoX509CRL* getCRL() const;
119         const std::vector<XSECCryptoX509CRL*>& getCRLs() const;
120         const char* getSubjectName() const;
121         const char* getIssuerName() const;
122         const char* getSerialNumber() const;
123         void extract();
124     };
125 };
126
127 #endif /* __xmltooling_basicx509cred_h__ */