2437e2d671ac7671b29452b258fcfad4d2d50272
[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 XSECCryptoX509CRL;
40
41     /**
42      * A trust engine that uses X.509 trust anchors and CRLs associated with a peer
43      * to perform PKIX validation of signatures and credentials.
44      */
45     class XMLTOOL_API AbstractPKIXTrustEngine : public SignatureTrustEngine, public OpenSSLTrustEngine
46     {
47     protected:
48         /**
49          * Constructor.
50          * 
51          * If a DOM is supplied, the following XML content is supported:
52          * 
53          * <ul>
54          *  <li>checkRevocation attribute (off, entityOnly, fullChain)
55          *  <li>policyMappingInhibit attribute (boolean)
56          *  <li>anyPolicyInhibit attribute (boolean)
57          *  <li>&lt;TrustedName&gt; element (zero or more)
58          *  <li>&lt;PolicyOID&gt; element (zero or more)
59          * </ul>
60          * 
61          * @param e DOM to supply configuration for provider
62          */
63         AbstractPKIXTrustEngine(const xercesc::DOMElement* e=nullptr);
64
65         /** Controls revocation checking, currently limited to CRLs and supports "off", "entityOnly", "fullChain". */
66         std::string m_checkRevocation;
67
68         /** Deprecated option, equivalent to checkRevocation="fullChain". */
69         bool m_fullCRLChain;
70
71         /** Disable policy mapping when applying PKIX policy checking. */
72         bool m_policyMappingInhibit;
73
74         /** Disallow the anyPolicy OID (2.5.29.32.0) when applying PKIX policy checking. */
75         bool m_anyPolicyInhibit;
76
77         /** A list of acceptable policy OIDs (explicit policy checking). */
78         std::set<std::string> m_policyOIDs;
79
80         /** A list of trusted names (subject DNs / CN attributes / subjectAltName entries). */
81         std::set<std::string> m_trustedNames;
82
83         /**
84          * Checks that either the name of the peer with the given credentials or the names
85          * of the credentials match the subject or subject alternate names of the certificate.
86          * Alternatively explicit trusted names can be supplied statically via configuration.
87          * 
88          * @param certEE        the credential for the entity to validate
89          * @param credResolver  source of trusted credentials
90          * @param criteria      criteria for selecting credentials, including the peer name
91          * 
92          * @return true the name check succeeds, false if not
93          */
94         bool checkEntityNames(X509* certEE, const CredentialResolver& credResolver, const CredentialCriteria& criteria) const;
95
96     public:
97         virtual ~AbstractPKIXTrustEngine();
98
99         bool validate(
100             xmlsignature::Signature& sig,
101             const CredentialResolver& credResolver,
102             CredentialCriteria* criteria=nullptr
103             ) const;
104
105         bool validate(
106             const XMLCh* sigAlgorithm,
107             const char* sig,
108             xmlsignature::KeyInfo* keyInfo,
109             const char* in,
110             unsigned int in_len,
111             const CredentialResolver& credResolver,
112             CredentialCriteria* criteria=nullptr
113             ) const;
114
115         bool validate(
116             XSECCryptoX509* certEE,
117             const std::vector<XSECCryptoX509*>& certChain,
118             const CredentialResolver& credResolver,
119             CredentialCriteria* criteria=nullptr
120             ) const;
121
122         bool validate(
123             X509* certEE,
124             STACK_OF(X509)* certChain,
125             const CredentialResolver& credResolver,
126             CredentialCriteria* criteria=nullptr
127             ) const;
128
129         /**
130          * Stateful interface that supplies PKIX validation data to the trust engine.
131          * Applications can adapt this TrustEngine to their environment by returning
132          * implementations of this interface from the getPKIXValidationInfoIterator
133          * method.
134          */
135         class XMLTOOL_API PKIXValidationInfoIterator {
136             MAKE_NONCOPYABLE(PKIXValidationInfoIterator);
137         protected:
138             PKIXValidationInfoIterator();
139             
140         public:
141             virtual ~PKIXValidationInfoIterator();
142             
143             /**
144              * Advances to the next set of information, if any.
145              * 
146              * @return true iff another set of information is available
147              */
148             virtual bool next()=0;
149             
150             /**
151              * Returns the allowable trust chain verification depth for the
152              * validation data in the current position.
153              * 
154              * @return  allowable trust chain verification depth
155              */
156             virtual int getVerificationDepth() const=0;
157             
158             /**
159              * Returns the set of trust anchors for the validation data in the
160              * current position. Keeping the certificates beyond the lifetime
161              * of the iterator or after advancing to the next position requires
162              * copying them.
163              * 
164              * @return  set of trust anchors
165              */
166             virtual const std::vector<XSECCryptoX509*>& getTrustAnchors() const=0;
167
168             /**
169              * Returns the set of CRLs for the validation data in the
170              * current position. Keeping the CRLs beyond the lifetime
171              * of the iterator or after advancing to the next position requires
172              * copying them.
173              * 
174              * @return  set of CRLs
175              */
176             virtual const std::vector<XSECCryptoX509CRL*>& getCRLs() const=0;
177         };
178         
179         /**
180          * Provides access to the information necessary, for the given credential source, for
181          * PKIX validation of credentials. Each set of validation information returned
182          * will be tried, in turn, until one succeeds or no more remain.
183          * The caller must free the returned interface when finished with it.
184          * 
185          * @param pkixSource        the peer for which validation rules are required
186          * @param criteria          criteria for selecting validation rules
187          * @return interface for obtaining validation data
188          */
189         virtual PKIXValidationInfoIterator* getPKIXValidationInfoIterator(
190             const CredentialResolver& pkixSource, CredentialCriteria* criteria=nullptr
191             ) const=0;
192
193     private:
194         bool validateWithCRLs(
195             X509* certEE,
196             STACK_OF(X509)* certChain,
197             const CredentialResolver& credResolver,
198             CredentialCriteria* criteria=nullptr,
199             const std::vector<XSECCryptoX509CRL*>* inlineCRLs=nullptr
200             ) const;
201     };
202 };
203
204 #endif /* __xmltooling_pkixtrust_h__ */