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