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