Update copyright.
[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 KeyInfoSource
21  * to perform PKIX validation of signatures and certificates.
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 KeyInfoSource
34      * to perform PKIX validation of signatures and certificates.
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;KeyResolver&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);
53         
54         /**
55          * Checks that either the ID for the entity with the given role or the key names
56          * for the given role match the subject or subject alternate names
57          * of the entity's certificate.
58          * 
59          * @param certEE        the credential for the entity to validate
60          * @param keyInfoSource supplies KeyInfo objects to the TrustEngine
61          * 
62          * @return true the name check succeeds, false if not
63          */
64         bool checkEntityNames(X509* certEE, const KeyInfoSource& keyInfoSource) const;
65         
66         /** An inline KeyResolver for extracting certificates out of a signature. */
67         xmlsignature::KeyResolver* m_inlineResolver;
68         
69     public:
70         virtual ~AbstractPKIXTrustEngine();
71
72         virtual bool validate(
73             xmlsignature::Signature& sig,
74             const KeyInfoSource& keyInfoSource,
75             const xmlsignature::KeyResolver* keyResolver=NULL
76             ) const;
77
78         virtual bool validate(
79             const XMLCh* sigAlgorithm,
80             const char* sig,
81             xmlsignature::KeyInfo* keyInfo,
82             const char* in,
83             unsigned int in_len,
84             const KeyInfoSource& keyInfoSource,
85             const xmlsignature::KeyResolver* keyResolver=NULL
86             ) const;
87
88         virtual bool validate(
89             XSECCryptoX509* certEE,
90             const std::vector<XSECCryptoX509*>& certChain,
91             const KeyInfoSource& keyInfoSource,
92             bool checkName=true,
93             const xmlsignature::KeyResolver* keyResolver=NULL
94             ) const;
95
96         virtual bool validate(
97             X509* certEE,
98             STACK_OF(X509)* certChain,
99             const KeyInfoSource& keyInfoSource,
100             bool checkName=true,
101             const xmlsignature::KeyResolver* keyResolver=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             /** Reference to KeyResolver to use. */
114             const xmlsignature::KeyResolver& m_keyResolver;
115             
116             /**
117              * Constructor
118              * 
119              * @param keyResolver   reference to KeyResolver to use
120              */
121             PKIXValidationInfoIterator(const xmlsignature::KeyResolver& keyResolver) : m_keyResolver(keyResolver) {}
122             
123         public:
124             virtual ~PKIXValidationInfoIterator() {}
125             
126             /**
127              * Advances to the next set of information, if any.
128              * 
129              * @return true iff another set of information is available
130              */
131             virtual bool next()=0;
132             
133             /**
134              * Returns the allowable trust chain verification depth for the
135              * validation data in the current position.
136              * 
137              * @return  allowable trust chain verification depth
138              */
139             virtual int getVerificationDepth() const=0;
140             
141             /**
142              * Returns the set of trust anchors for the validation data in the
143              * current position. Keeping the certificates beyond the lifetime
144              * of the iterator or after advancing to the next position requires
145              * copying them.
146              * 
147              * @return  set of trust anchors
148              */
149             virtual const std::vector<XSECCryptoX509*>& getTrustAnchors() const=0;
150
151             /**
152              * Returns the set of CRLs for the validation data in the
153              * current position. Keeping the CRLs beyond the lifetime
154              * of the iterator or after advancing to the next position requires
155              * copying them.
156              * 
157              * @return  set of CRLs
158              */
159             virtual const std::vector<XSECCryptoX509CRL*>& getCRLs() const=0;
160         };
161         
162         /**
163          * Provides access to the information necessary, for the given key source, for
164          * PKIX validation of credentials. Each set of validation information returned
165          * will be tried, in turn, until one succeeds or no more remain.
166          * The caller must free the returned interface when finished with it.
167          * 
168          * @param pkixSource    the peer for which validation rules are required
169          * @param keyResolver   reference to KeyResolver to use for any KeyInfo operations
170          * @return interface for obtaining validation data
171          */
172         virtual PKIXValidationInfoIterator* getPKIXValidationInfoIterator(
173             const KeyInfoSource& pkixSource,
174             const xmlsignature::KeyResolver& keyResolver
175             ) const=0;
176     };
177 };
178
179 #endif /* __xmltooling_pkixtrust_h__ */