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