Optimize return values of DER encoding functions.
[shibboleth/cpp-xmltooling.git] / xmltooling / security / AbstractPKIXTrustEngine.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/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         bool m_fullCRLChain;
57         
58         /**
59          * Checks that either the name of the peer with the given credentials or the names
60          * of the credentials match the subject or subject alternate names of the certificate.
61          * 
62          * @param certEE        the credential for the entity to validate
63          * @param credResolver  source of credentials
64          * @param criteria      criteria for selecting credentials, including the peer name
65          * 
66          * @return true the name check succeeds, false if not
67          */
68         bool checkEntityNames(X509* certEE, const CredentialResolver& credResolver, const CredentialCriteria& criteria) const;
69
70     public:
71         virtual ~AbstractPKIXTrustEngine() {}
72
73         bool validate(
74             xmlsignature::Signature& sig,
75             const CredentialResolver& credResolver,
76             CredentialCriteria* criteria=NULL
77             ) const;
78
79         bool validate(
80             const XMLCh* sigAlgorithm,
81             const char* sig,
82             xmlsignature::KeyInfo* keyInfo,
83             const char* in,
84             unsigned int in_len,
85             const CredentialResolver& credResolver,
86             CredentialCriteria* criteria=NULL
87             ) const;
88
89         bool validate(
90             XSECCryptoX509* certEE,
91             const std::vector<XSECCryptoX509*>& certChain,
92             const CredentialResolver& credResolver,
93             CredentialCriteria* criteria=NULL
94             ) const;
95
96         bool validate(
97             X509* certEE,
98             STACK_OF(X509)* certChain,
99             const CredentialResolver& credResolver,
100             CredentialCriteria* criteria=NULL
101             ) const;
102
103         /**
104          * Stateful interface that supplies PKIX validation data to the trust engine.
105          * Applications can adapt this TrustEngine to their environment by returning
106          * implementations of this interface from the getPKIXValidationInfoIterator
107          * method.
108          */
109         class XMLTOOL_API PKIXValidationInfoIterator {
110             MAKE_NONCOPYABLE(PKIXValidationInfoIterator);
111         protected:
112             PKIXValidationInfoIterator() {}
113             
114         public:
115             virtual ~PKIXValidationInfoIterator() {}
116             
117             /**
118              * Advances to the next set of information, if any.
119              * 
120              * @return true iff another set of information is available
121              */
122             virtual bool next()=0;
123             
124             /**
125              * Returns the allowable trust chain verification depth for the
126              * validation data in the current position.
127              * 
128              * @return  allowable trust chain verification depth
129              */
130             virtual int getVerificationDepth() const=0;
131             
132             /**
133              * Returns the set of trust anchors for the validation data in the
134              * current position. Keeping the certificates beyond the lifetime
135              * of the iterator or after advancing to the next position requires
136              * copying them.
137              * 
138              * @return  set of trust anchors
139              */
140             virtual const std::vector<XSECCryptoX509*>& getTrustAnchors() const=0;
141
142             /**
143              * Returns the set of CRLs for the validation data in the
144              * current position. Keeping the CRLs beyond the lifetime
145              * of the iterator or after advancing to the next position requires
146              * copying them.
147              * 
148              * @return  set of CRLs
149              */
150             virtual const std::vector<XSECCryptoX509CRL*>& getCRLs() const=0;
151         };
152         
153         /**
154          * Provides access to the information necessary, for the given credential source, for
155          * PKIX validation of credentials. Each set of validation information returned
156          * will be tried, in turn, until one succeeds or no more remain.
157          * The caller must free the returned interface when finished with it.
158          * 
159          * @param pkixSource        the peer for which validation rules are required
160          * @param criteria          criteria for selecting validation rules
161          * @return interface for obtaining validation data
162          */
163         virtual PKIXValidationInfoIterator* getPKIXValidationInfoIterator(
164             const CredentialResolver& pkixSource, CredentialCriteria* criteria=NULL
165             ) const=0;
166
167     private:
168         bool validateWithCRLs(
169             X509* certEE,
170             STACK_OF(X509)* certChain,
171             const CredentialResolver& credResolver,
172             CredentialCriteria* criteria=NULL,
173             const std::vector<XSECCryptoX509CRL*>* inlineCRLs=NULL
174             ) const;
175     };
176 };
177
178 #endif /* __xmltooling_pkixtrust_h__ */