b828fbecb1da36f4747702a045b5bf156074750a
[shibboleth/cpp-xmltooling.git] / xmltooling / security / AbstractPKIXTrustEngine.h
1 /**
2  * Licensed to the University Corporation for Advanced Internet
3  * Development, Inc. (UCAID) under one or more contributor license
4  * agreements. See the NOTICE file distributed with this work for
5  * additional information regarding copyright ownership.
6  *
7  * UCAID licenses this file to you under the Apache License,
8  * Version 2.0 (the "License"); you may not use this file except
9  * in compliance with the License. You may obtain a copy of the
10  * License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17  * either express or implied. See the License for the specific
18  * language governing permissions and limitations under the License.
19  */
20
21 /**
22  * @file xmltooling/security/AbstractPKIXTrustEngine.h
23  * 
24  * A trust engine that uses X.509 trust anchors and CRLs associated with a peer
25  * to perform PKIX validation of signatures and credentials.
26  */
27
28 #if !defined(__xmltooling_pkixtrust_h__) && !defined(XMLTOOLING_NO_XMLSEC)
29 #define __xmltooling_pkixtrust_h__
30
31 #include <xmltooling/security/OpenSSLTrustEngine.h>
32 #include <xmltooling/security/SignatureTrustEngine.h>
33
34 #include <set>
35 #include <string>
36
37 namespace xmltooling {
38
39     class XMLTOOL_API OpenSSLPathValidator;
40     class XMLTOOL_API XSECCryptoX509CRL;
41
42     /**
43      * A trust engine that uses X.509 trust anchors and CRLs associated with a peer
44      * to perform PKIX validation of signatures and credentials.
45      */
46     class XMLTOOL_API AbstractPKIXTrustEngine : public SignatureTrustEngine, public OpenSSLTrustEngine
47     {
48     protected:
49         /**
50          * Constructor.
51          * 
52          * If a DOM is supplied, the following XML content is supported:
53          * 
54          * <ul>
55          *  <li>checkRevocation attribute (off, entityOnly, fullChain)
56          *  <li>policyMappingInhibit attribute (boolean)
57          *  <li>anyPolicyInhibit attribute (boolean)
58          *  <li>&t;PathValidator&gt; element (zero or more)
59          *  <li>&lt;TrustedName&gt; element (zero or more)
60          *  <li>&lt;PolicyOID&gt; element (zero or more)
61          * </ul>
62          * 
63          * @param e DOM to supply configuration for provider
64          */
65         AbstractPKIXTrustEngine(const xercesc::DOMElement* e=nullptr);
66
67         /** Plugins used to perform path validation. */
68         std::vector<OpenSSLPathValidator*> m_pathValidators;
69
70         /** Controls revocation checking, currently limited to CRLs and supports "off", "entityOnly", "fullChain". */
71         std::string m_checkRevocation;
72
73         /** Deprecated option, equivalent to checkRevocation="fullChain". */
74         bool m_fullCRLChain;
75
76         /** Disable policy mapping when applying PKIX policy checking. */
77         bool m_policyMappingInhibit;
78
79         /** Disallow the anyPolicy OID (2.5.29.32.0) when applying PKIX policy checking. */
80         bool m_anyPolicyInhibit;
81
82         /** A list of acceptable policy OIDs (explicit policy checking). */
83         std::set<std::string> m_policyOIDs;
84
85         /** A list of trusted names (subject DNs / CN attributes / subjectAltName entries). */
86         std::set<std::string> m_trustedNames;
87
88         /**
89          * Checks that either the name of the peer with the given credentials or the names
90          * of the credentials match the subject or subject alternate names of the certificate.
91          * Alternatively explicit trusted names can be supplied statically via configuration.
92          * 
93          * @param certEE        the credential for the entity to validate
94          * @param credResolver  source of trusted credentials
95          * @param criteria      criteria for selecting credentials, including the peer name
96          * 
97          * @return true the name check succeeds, false if not
98          */
99         bool checkEntityNames(X509* certEE, const CredentialResolver& credResolver, const CredentialCriteria& criteria) const;
100
101     public:
102         virtual ~AbstractPKIXTrustEngine();
103
104         bool validate(
105             xmlsignature::Signature& sig,
106             const CredentialResolver& credResolver,
107             CredentialCriteria* criteria=nullptr
108             ) const;
109
110         bool validate(
111             const XMLCh* sigAlgorithm,
112             const char* sig,
113             xmlsignature::KeyInfo* keyInfo,
114             const char* in,
115             unsigned int in_len,
116             const CredentialResolver& credResolver,
117             CredentialCriteria* criteria=nullptr
118             ) const;
119
120         bool validate(
121             XSECCryptoX509* certEE,
122             const std::vector<XSECCryptoX509*>& certChain,
123             const CredentialResolver& credResolver,
124             CredentialCriteria* criteria=nullptr
125             ) const;
126
127         bool validate(
128             X509* certEE,
129             STACK_OF(X509)* certChain,
130             const CredentialResolver& credResolver,
131             CredentialCriteria* criteria=nullptr
132             ) const;
133
134         /**
135          * Stateful interface that supplies PKIX validation data to the trust engine.
136          * Applications can adapt this TrustEngine to their environment by returning
137          * implementations of this interface from the getPKIXValidationInfoIterator
138          * method.
139          */
140         class XMLTOOL_API PKIXValidationInfoIterator {
141             MAKE_NONCOPYABLE(PKIXValidationInfoIterator);
142         protected:
143             PKIXValidationInfoIterator();
144             
145         public:
146             virtual ~PKIXValidationInfoIterator();
147             
148             /**
149              * Advances to the next set of information, if any.
150              * 
151              * @return true iff another set of information is available
152              */
153             virtual bool next()=0;
154             
155             /**
156              * Returns the allowable trust chain verification depth for the
157              * validation data in the current position.
158              * 
159              * @return  allowable trust chain verification depth
160              */
161             virtual int getVerificationDepth() const=0;
162             
163             /**
164              * Returns the set of trust anchors for the validation data in the
165              * current position. Keeping the certificates beyond the lifetime
166              * of the iterator or after advancing to the next position requires
167              * copying them.
168              * 
169              * @return  set of trust anchors
170              */
171             virtual const std::vector<XSECCryptoX509*>& getTrustAnchors() const=0;
172
173             /**
174              * Returns the set of CRLs for the validation data in the
175              * current position. Keeping the CRLs beyond the lifetime
176              * of the iterator or after advancing to the next position requires
177              * copying them.
178              * 
179              * @return  set of CRLs
180              */
181             virtual const std::vector<XSECCryptoX509CRL*>& getCRLs() const=0;
182         };
183         
184         /**
185          * Provides access to the information necessary, for the given credential source, for
186          * PKIX validation of credentials. Each set of validation information returned
187          * will be tried, in turn, until one succeeds or no more remain.
188          * The caller must free the returned interface when finished with it.
189          * 
190          * @param pkixSource        the peer for which validation rules are required
191          * @param criteria          criteria for selecting validation rules
192          * @return interface for obtaining validation data
193          */
194         virtual PKIXValidationInfoIterator* getPKIXValidationInfoIterator(
195             const CredentialResolver& pkixSource, CredentialCriteria* criteria=nullptr
196             ) const=0;
197
198     private:
199         bool validateWithCRLs(
200             X509* certEE,
201             STACK_OF(X509)* certChain,
202             const CredentialResolver& credResolver,
203             CredentialCriteria* criteria=nullptr,
204             const std::vector<XSECCryptoX509CRL*>* inlineCRLs=nullptr
205             ) const;
206
207         friend class XMLTOOL_DLLLOCAL PKIXParams;
208     };
209 };
210
211 #endif /* __xmltooling_pkixtrust_h__ */