354821f8997fbbdf264cb2895dc2cd8dd52fd50d
[shibboleth/cpp-xmltooling.git] / xmltooling / security / AbstractPKIXTrustEngine.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/AbstractPKIXTrustEngine.h
19  * 
20  * A trust engine that uses X.509 trust anchors and CRLs associated with a peer
21  * to perform PKIX validation of signatures and credentials.
22  */
23
24 #if !defined(__xmltooling_pkixtrust_h__) && !defined(XMLTOOLING_NO_XMLSEC)
25 #define __xmltooling_pkixtrust_h__
26
27 #include <xmltooling/security/OpenSSLTrustEngine.h>
28 #include <xmltooling/security/SignatureTrustEngine.h>
29
30 #include <string>
31
32 namespace xmltooling {
33
34     class XMLTOOL_API XSECCryptoX509CRL;
35
36     /**
37      * A trust engine that uses X.509 trust anchors and CRLs associated with a peer
38      * to perform PKIX validation of signatures and credentials.
39      */
40     class XMLTOOL_API AbstractPKIXTrustEngine : public SignatureTrustEngine, public OpenSSLTrustEngine
41     {
42     protected:
43         /**
44          * Constructor.
45          * 
46          * If a DOM is supplied, the following XML content is supported:
47          * 
48          * <ul>
49          *  <li>checkRevocation attribute (off, entityOnly, fullChain)
50          * </ul>
51          * 
52          * @param e DOM to supply configuration for provider
53          */
54         AbstractPKIXTrustEngine(const xercesc::DOMElement* e=nullptr);
55
56                 /** Controls revocation checking, currently limited to CRLs and supports "off", "entityOnly", "fullChain". */
57                 std::string m_checkRevocation;
58
59         /** Deprecated option, equivalent to checkRevocation="fullChain". */
60         bool m_fullCRLChain;
61         
62         /**
63          * Checks that either the name of the peer with the given credentials or the names
64          * of the credentials match the subject or subject alternate names of the certificate.
65          * 
66          * @param certEE        the credential for the entity to validate
67          * @param credResolver  source of credentials
68          * @param criteria      criteria for selecting credentials, including the peer name
69          * 
70          * @return true the name check succeeds, false if not
71          */
72         bool checkEntityNames(X509* certEE, const CredentialResolver& credResolver, const CredentialCriteria& criteria) const;
73
74     public:
75         virtual ~AbstractPKIXTrustEngine();
76
77         bool validate(
78             xmlsignature::Signature& sig,
79             const CredentialResolver& credResolver,
80             CredentialCriteria* criteria=nullptr
81             ) const;
82
83         bool validate(
84             const XMLCh* sigAlgorithm,
85             const char* sig,
86             xmlsignature::KeyInfo* keyInfo,
87             const char* in,
88             unsigned int in_len,
89             const CredentialResolver& credResolver,
90             CredentialCriteria* criteria=nullptr
91             ) const;
92
93         bool validate(
94             XSECCryptoX509* certEE,
95             const std::vector<XSECCryptoX509*>& certChain,
96             const CredentialResolver& credResolver,
97             CredentialCriteria* criteria=nullptr
98             ) const;
99
100         bool validate(
101             X509* certEE,
102             STACK_OF(X509)* certChain,
103             const CredentialResolver& credResolver,
104             CredentialCriteria* criteria=nullptr
105             ) const;
106
107         /**
108          * Stateful interface that supplies PKIX validation data to the trust engine.
109          * Applications can adapt this TrustEngine to their environment by returning
110          * implementations of this interface from the getPKIXValidationInfoIterator
111          * method.
112          */
113         class XMLTOOL_API PKIXValidationInfoIterator {
114             MAKE_NONCOPYABLE(PKIXValidationInfoIterator);
115         protected:
116             PKIXValidationInfoIterator();
117             
118         public:
119             virtual ~PKIXValidationInfoIterator();
120             
121             /**
122              * Advances to the next set of information, if any.
123              * 
124              * @return true iff another set of information is available
125              */
126             virtual bool next()=0;
127             
128             /**
129              * Returns the allowable trust chain verification depth for the
130              * validation data in the current position.
131              * 
132              * @return  allowable trust chain verification depth
133              */
134             virtual int getVerificationDepth() const=0;
135             
136             /**
137              * Returns the set of trust anchors for the validation data in the
138              * current position. Keeping the certificates beyond the lifetime
139              * of the iterator or after advancing to the next position requires
140              * copying them.
141              * 
142              * @return  set of trust anchors
143              */
144             virtual const std::vector<XSECCryptoX509*>& getTrustAnchors() const=0;
145
146             /**
147              * Returns the set of CRLs for the validation data in the
148              * current position. Keeping the CRLs beyond the lifetime
149              * of the iterator or after advancing to the next position requires
150              * copying them.
151              * 
152              * @return  set of CRLs
153              */
154             virtual const std::vector<XSECCryptoX509CRL*>& getCRLs() const=0;
155         };
156         
157         /**
158          * Provides access to the information necessary, for the given credential source, for
159          * PKIX validation of credentials. Each set of validation information returned
160          * will be tried, in turn, until one succeeds or no more remain.
161          * The caller must free the returned interface when finished with it.
162          * 
163          * @param pkixSource        the peer for which validation rules are required
164          * @param criteria          criteria for selecting validation rules
165          * @return interface for obtaining validation data
166          */
167         virtual PKIXValidationInfoIterator* getPKIXValidationInfoIterator(
168             const CredentialResolver& pkixSource, CredentialCriteria* criteria=nullptr
169             ) const=0;
170
171     private:
172         bool validateWithCRLs(
173             X509* certEE,
174             STACK_OF(X509)* certChain,
175             const CredentialResolver& credResolver,
176             CredentialCriteria* criteria=nullptr,
177             const std::vector<XSECCryptoX509CRL*>* inlineCRLs=nullptr
178             ) const;
179     };
180 };
181
182 #endif /* __xmltooling_pkixtrust_h__ */