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