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