Prelim port to Apache xml-security lib
[shibboleth/cpp-sp.git] / shib / shib.h
1 /*
2  * The Shibboleth License, Version 1.
3  * Copyright (c) 2002
4  * University Corporation for Advanced Internet Development, Inc.
5  * All rights reserved
6  *
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * Redistributions of source code must retain the above copyright notice, this
12  * list of conditions and the following disclaimer.
13  *
14  * Redistributions in binary form must reproduce the above copyright notice,
15  * this list of conditions and the following disclaimer in the documentation
16  * and/or other materials provided with the distribution, if any, must include
17  * the following acknowledgment: "This product includes software developed by
18  * the University Corporation for Advanced Internet Development
19  * <http://www.ucaid.edu>Internet2 Project. Alternately, this acknowledegement
20  * may appear in the software itself, if and wherever such third-party
21  * acknowledgments normally appear.
22  *
23  * Neither the name of Shibboleth nor the names of its contributors, nor
24  * Internet2, nor the University Corporation for Advanced Internet Development,
25  * Inc., nor UCAID may be used to endorse or promote products derived from this
26  * software without specific prior written permission. For written permission,
27  * please contact shibboleth@shibboleth.org
28  *
29  * Products derived from this software may not be called Shibboleth, Internet2,
30  * UCAID, or the University Corporation for Advanced Internet Development, nor
31  * may Shibboleth appear in their name, without prior written permission of the
32  * University Corporation for Advanced Internet Development.
33  *
34  *
35  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
36  * AND WITH ALL FAULTS. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
38  * PARTICULAR PURPOSE, AND NON-INFRINGEMENT ARE DISCLAIMED AND THE ENTIRE RISK
39  * OF SATISFACTORY QUALITY, PERFORMANCE, ACCURACY, AND EFFORT IS WITH LICENSEE.
40  * IN NO EVENT SHALL THE COPYRIGHT OWNER, CONTRIBUTORS OR THE UNIVERSITY
41  * CORPORATION FOR ADVANCED INTERNET DEVELOPMENT, INC. BE LIABLE FOR ANY DIRECT,
42  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
43  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
44  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
46  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
47  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48  */
49
50
51 /* shib.h - Shibboleth header file
52
53    Scott Cantor
54    6/4/02
55
56    $History:$
57 */
58
59 #ifndef __shib_h__
60 #define __shib_h__
61
62 #include <ctime>
63 #include <saml/saml.h>
64
65 #ifdef WIN32
66 # ifndef SHIB_EXPORTS
67 #  define SHIB_EXPORTS __declspec(dllimport)
68 # endif
69 #else
70 # define SHIB_EXPORTS
71 #endif
72
73 namespace shibboleth
74 {
75 #ifdef NO_RTTI
76   extern SHIB_EXPORTS const unsigned short RTTI_UnsupportedProtocolException;
77   extern SHIB_EXPORTS const unsigned short RTTI_OriginSiteMapperException;
78 #endif
79
80     #define DECLARE_SHIB_EXCEPTION(name,base) \
81         class SHIB_EXPORTS name : public saml::base \
82         { \
83         public: \
84             name(const char* msg) : saml::base(msg) {RTTI(name); m_typename=#name;} \
85             name(const std::string& msg) : saml::base(msg) {RTTI(name); m_typename=#name;} \
86             name(const saml::Iterator<saml::QName>& codes, const char* msg) : saml::base(codes,msg) {RTTI(name); m_typename=#name;} \
87             name(const saml::Iterator<saml::QName>& codes, const std::string& msg) : saml::base(codes, msg) {RTTI(name); m_typename=#name;} \
88             name(const saml::QName& code, const char* msg) : saml::base(code,msg) {RTTI(name); m_typename=#name;} \
89             name(const saml::QName& code, const std::string& msg) : saml::base(code, msg) {RTTI(name); m_typename=#name;} \
90             name(DOMElement* e) : saml::base(e) {RTTI(name); m_typename=#name;} \
91             name(std::istream& in) : saml::base(in) {RTTI(name); m_typename=#name;} \
92             virtual ~name() throw () {} \
93         }
94
95     DECLARE_SHIB_EXCEPTION(UnsupportedProtocolException,SAMLException);
96     DECLARE_SHIB_EXCEPTION(OriginSiteMapperException,SAMLException);
97
98 #ifdef SHIB_INSTANTIATE
99 # ifdef NO_RTTI
100     const unsigned short RTTI_UnsupportedProtocolException=     RTTI_EXTENSION_BASE;
101     const unsigned short RTTI_OriginSiteMapperException=        RTTI_EXTENSION_BASE+1;
102 # endif
103     template class SHIB_EXPORTS saml::Iterator<std::pair<saml::xstring,bool> >;
104     template class SHIB_EXPORTS saml::ArrayIterator<std::pair<saml::xstring,bool> >;
105 #endif
106
107     struct SHIB_EXPORTS IOriginSiteMapper
108     {
109         virtual const char* getContactName(const XMLCh* originSite) const=0;
110         virtual const char* getContactEmail(const XMLCh* originSite) const=0;
111         virtual const char* getErrorURL(const XMLCh* originSite) const=0;
112         virtual saml::Iterator<saml::xstring> getHandleServiceNames(const XMLCh* originSite) const=0;
113         virtual XSECCryptoX509* getHandleServiceCert(const XMLCh* handleService) const=0;
114         virtual saml::Iterator<std::pair<saml::xstring,bool> > getSecurityDomains(const XMLCh* originSite) const=0;
115         virtual time_t getTimestamp() const=0;
116     };
117
118     class SHIB_EXPORTS OriginSiteMapper : public IOriginSiteMapper
119     {
120     public:
121         OriginSiteMapper();
122         ~OriginSiteMapper();
123         virtual const char* getContactName(const XMLCh* originSite) const;
124         virtual const char* getContactEmail(const XMLCh* originSite) const;
125         virtual const char* getErrorURL(const XMLCh* originSite) const;
126         virtual saml::Iterator<saml::xstring> getHandleServiceNames(const XMLCh* originSite) const;
127         virtual XSECCryptoX509* getHandleServiceCert(const XMLCh* handleService) const;
128         virtual saml::Iterator<std::pair<saml::xstring,bool> > getSecurityDomains(const XMLCh* originSite) const;
129         virtual time_t getTimestamp() const;
130
131     private:
132         OriginSiteMapper(const OriginSiteMapper&);
133         void operator=(const OriginSiteMapper&);
134         IOriginSiteMapper* m_mapper;
135     };
136
137     class SHIB_EXPORTS SimpleAttribute : public saml::SAMLAttribute
138     {
139     public:
140         SimpleAttribute(const XMLCh* name, const XMLCh* ns, long lifetime=0,
141                         const saml::Iterator<const XMLCh*>& values=saml::Iterator<const XMLCh*>());
142         SimpleAttribute(DOMElement* e);
143         virtual saml::SAMLObject* clone() const;
144         virtual ~SimpleAttribute();
145
146     protected:
147         virtual bool accept(DOMElement* e) const;
148
149         saml::xstring m_originSite;
150     };
151
152     class SHIB_EXPORTS ScopedAttribute : public SimpleAttribute
153     {
154     public:
155         ScopedAttribute(const XMLCh* name, const XMLCh* ns, long lifetime=0,
156                         const saml::Iterator<const XMLCh*>& scopes=saml::Iterator<const XMLCh*>(),
157                         const saml::Iterator<const XMLCh*>& values=saml::Iterator<const XMLCh*>());
158         ScopedAttribute(DOMElement* e);
159         virtual ~ScopedAttribute();
160
161         virtual DOMNode* toDOM(DOMDocument* doc=NULL, bool xmlns=true) const;
162         virtual saml::SAMLObject* clone() const;
163
164         virtual saml::Iterator<saml::xstring> getValues() const;
165         virtual saml::Iterator<std::string> getSingleByteValues() const;
166
167         static const XMLCh Scope[];
168
169     protected:
170         virtual bool accept(DOMElement* e) const;
171         virtual bool addValue(DOMElement* e);
172
173         std::vector<saml::xstring> m_scopes;
174         mutable std::vector<saml::xstring> m_scopedValues;
175     };
176
177     class SHIB_EXPORTS ShibPOSTProfile
178     {
179     public:
180         ShibPOSTProfile(const saml::Iterator<const XMLCh*>& policies, const XMLCh* receiver, int ttlSeconds);
181         ShibPOSTProfile(const saml::Iterator<const XMLCh*>& policies, const XMLCh* issuer);
182         virtual ~ShibPOSTProfile();
183
184         virtual saml::SAMLAssertion* getSSOAssertion(const saml::SAMLResponse& r);
185         virtual saml::SAMLAuthenticationStatement* getSSOStatement(const saml::SAMLAssertion& a);
186         virtual saml::SAMLResponse* accept(const XMLByte* buf);
187         virtual saml::SAMLResponse* prepare(
188             const XMLCh* recipient,
189             const XMLCh* name,
190             const XMLCh* nameQualifier,
191             const XMLCh* subjectIP,
192             const XMLCh* authMethod,
193             time_t authInstant,
194             const saml::Iterator<saml::SAMLAuthorityBinding*>& bindings,
195             XSECCryptoKey* responseKey,
196             const saml::Iterator<XSECCryptoX509*>& responseCerts=saml::Iterator<XSECCryptoX509*>(),
197             XSECCryptoKey* assertionKey=NULL,
198             const saml::Iterator<XSECCryptoX509*>& assertionCerts=saml::Iterator<XSECCryptoX509*>()
199             );
200         virtual bool checkReplayCache(const saml::SAMLAssertion& a);
201
202     protected:
203         virtual void verifySignature(const saml::SAMLSignedObject& obj, const XMLCh* signerName, XSECCryptoKey* knownKey);
204
205         signatureMethod m_algorithm;
206         std::vector<const XMLCh*> m_policies;
207         XMLCh* m_issuer;
208         XMLCh* m_receiver;
209         int m_ttlSeconds;
210
211     private:
212         ShibPOSTProfile(const ShibPOSTProfile&) {}
213         ShibPOSTProfile& operator=(const ShibPOSTProfile&) {return *this;}
214     };
215
216     class SHIB_EXPORTS ClubShibPOSTProfile : public ShibPOSTProfile
217     {
218     public:
219         ClubShibPOSTProfile(const saml::Iterator<const XMLCh*>& policies, const XMLCh* receiver, int ttlSeconds);
220         ClubShibPOSTProfile(const saml::Iterator<const XMLCh*>& policies, const XMLCh* issuer);
221         virtual ~ClubShibPOSTProfile();
222
223         virtual saml::SAMLResponse* prepare(
224             const XMLCh* recipient,
225             const XMLCh* name,
226             const XMLCh* nameQualifier,
227             const XMLCh* subjectIP,
228             const XMLCh* authMethod,
229             time_t authInstant,
230             const saml::Iterator<saml::SAMLAuthorityBinding*>& bindings,
231             XSECCryptoKey* responseKey,
232             const saml::Iterator<XSECCryptoX509*>& responseCerts=saml::Iterator<XSECCryptoX509*>(),
233             XSECCryptoKey* assertionKey=NULL,
234             const saml::Iterator<XSECCryptoX509*>& assertionCerts=saml::Iterator<XSECCryptoX509*>()
235             );
236
237     protected:
238         virtual void verifySignature(const saml::SAMLSignedObject& obj, const XMLCh* signerName, XSECCryptoKey* knownKey);
239     };
240
241     class SHIB_EXPORTS ShibPOSTProfileFactory
242     {
243     public:
244         static ShibPOSTProfile* getInstance(const saml::Iterator<const XMLCh*>& policies, const XMLCh* receiver, int ttlSeconds);
245         static ShibPOSTProfile* getInstance(const saml::Iterator<const XMLCh*>& policies, const XMLCh* issuer);
246     };
247
248     class SHIB_EXPORTS ShibConfig
249     {
250     public:
251         ShibConfig() {}
252         virtual ~ShibConfig();
253
254         // global per-process setup and shutdown of Shibboleth runtime
255         virtual bool init()=0;
256         virtual void term()=0;
257
258         // enables runtime and clients to access configuration
259         static ShibConfig& getConfig();
260
261         virtual IOriginSiteMapper* getMapper()=0;
262         virtual void releaseMapper(IOriginSiteMapper* mapper)=0;
263
264     /* start of external configuration */
265         std::string aapFile;
266         std::string mapperFile;
267     /* end of external configuration */
268     };
269
270     struct SHIB_EXPORTS Constants
271     {
272         static const XMLCh POLICY_INCOMMON[];
273         static const XMLCh SHIB_ATTRIBUTE_NAMESPACE_URI[];
274         static const XMLCh SHIB_NAMEID_FORMAT_URI[];
275         static saml::QName SHIB_ATTRIBUTE_VALUE_TYPE; 
276     };
277
278     class SHIB_EXPORTS XML
279     {
280     public:
281         // URI constants
282         static const XMLCh SHIB_NS[];
283         static const XMLCh SHIB_SCHEMA_ID[];
284
285         struct SHIB_EXPORTS Literals
286         {
287             // Shibboleth vocabulary
288             static const XMLCh AttributeValueType[];
289
290             static const XMLCh ContactEmail[];
291             static const XMLCh ContactName[];
292             static const XMLCh Domain[];
293             static const XMLCh ErrorURL[];
294             static const XMLCh HandleService[];
295             static const XMLCh InvalidHandle[];
296             static const XMLCh Name[];
297             static const XMLCh OriginSite[];
298             static const XMLCh Sites[];
299
300             static const XMLCh AnySite[];
301             static const XMLCh AttributeAcceptancePolicy[];
302             static const XMLCh AttributeRule[];
303             static const XMLCh SiteRule[];
304             static const XMLCh Type[];
305             static const XMLCh Value[];
306
307             static const XMLCh literal[];
308             static const XMLCh regexp[];
309             static const XMLCh xpath[];
310
311             // XML vocabulary
312             static const XMLCh xmlns_shib[];
313         };
314     };
315
316
317     class SHIB_EXPORTS SAMLBindingFactory
318     {
319     public:
320         static saml::SAMLBinding* getInstance(const XMLCh* protocol=saml::SAMLBinding::SAML_SOAP_HTTPS);
321     };
322 }
323
324 #endif