Move Shib constants to new lib, fixed symbol conflicts.
[shibboleth/cpp-sp.git] / shib / shib.h
index 2b20b5e..8e93490 100644 (file)
@@ -1,53 +1,19 @@
 /*
- * The Shibboleth License, Version 1.
- * Copyright (c) 2002
- * University Corporation for Advanced Internet Development, Inc.
- * All rights reserved
+ *  Copyright 2001-2005 Internet2
  *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
  *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
+ *     http://www.apache.org/licenses/LICENSE-2.0
  *
- * Redistributions of source code must retain the above copyright notice, this
- * list of conditions and the following disclaimer.
- *
- * Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution, if any, must include
- * the following acknowledgment: "This product includes software developed by
- * the University Corporation for Advanced Internet Development
- * <http://www.ucaid.edu>Internet2 Project. Alternately, this acknowledegement
- * may appear in the software itself, if and wherever such third-party
- * acknowledgments normally appear.
- *
- * Neither the name of Shibboleth nor the names of its contributors, nor
- * Internet2, nor the University Corporation for Advanced Internet Development,
- * Inc., nor UCAID may be used to endorse or promote products derived from this
- * software without specific prior written permission. For written permission,
- * please contact shibboleth@shibboleth.org
- *
- * Products derived from this software may not be called Shibboleth, Internet2,
- * UCAID, or the University Corporation for Advanced Internet Development, nor
- * may Shibboleth appear in their name, without prior written permission of the
- * University Corporation for Advanced Internet Development.
- *
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND WITH ALL FAULTS. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
- * PARTICULAR PURPOSE, AND NON-INFRINGEMENT ARE DISCLAIMED AND THE ENTIRE RISK
- * OF SATISFACTORY QUALITY, PERFORMANCE, ACCURACY, AND EFFORT IS WITH LICENSEE.
- * IN NO EVENT SHALL THE COPYRIGHT OWNER, CONTRIBUTORS OR THE UNIVERSITY
- * CORPORATION FOR ADVANCED INTERNET DEVELOPMENT, INC. BE LIABLE FOR ANY DIRECT,
- * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
  */
 
-
 /* shib.h - Shibboleth header file
 
    Scott Cantor
@@ -59,8 +25,9 @@
 #ifndef __shib_h__
 #define __shib_h__
 
+#include <xmltooling/util/Threads.h>
+
 #include <saml/saml.h>
-#include <shib/shib-threads.h>
 #include <xsec/xenc/XENCEncryptionMethod.hpp>
 
 #ifdef WIN32
@@ -76,7 +43,7 @@ namespace shibboleth
     DECLARE_SAML_EXCEPTION(SHIB_EXPORTS,ResourceAccessException,SAMLException);
     DECLARE_SAML_EXCEPTION(SHIB_EXPORTS,MetadataException,SAMLException);
     DECLARE_SAML_EXCEPTION(SHIB_EXPORTS,CredentialException,SAMLException);
-    DECLARE_SAML_EXCEPTION(SHIB_EXPORTS,InvalidHandleException,RetryableProfileException);
+    DECLARE_SAML_EXCEPTION(SHIB_EXPORTS,InvalidHandleException,SAMLException);
     DECLARE_SAML_EXCEPTION(SHIB_EXPORTS,InvalidSessionException,RetryableProfileException);
 
     // Metadata abstract interfaces, based on SAML 2.0
@@ -259,14 +226,7 @@ namespace shibboleth
         virtual ~IEntitiesDescriptor() {}
     };
     
-    // Supports Shib role extension describing attribute scoping rules
-    struct SHIB_EXPORTS IScopedRoleDescriptor : public virtual IRoleDescriptor
-    {
-        virtual saml::Iterator<std::pair<const XMLCh*,bool> > getScopes() const=0;
-        virtual ~IScopedRoleDescriptor() {}
-    };
-    
-    // Shib extension interfaces to key authority data
+    // Shib extension interfaces
     struct SHIB_EXPORTS IKeyAuthority
     {
         virtual int getVerifyDepth() const=0;
@@ -277,6 +237,7 @@ namespace shibboleth
     struct SHIB_EXPORTS IExtendedEntityDescriptor : public virtual IEntityDescriptor
     {
         virtual saml::Iterator<const IKeyAuthority*> getKeyAuthorities() const=0;
+        virtual saml::Iterator<std::pair<const XMLCh*,bool> > getScopes() const=0;
         virtual ~IExtendedEntityDescriptor() {}
     };
 
@@ -288,29 +249,38 @@ namespace shibboleth
        
     struct SHIB_EXPORTS IMetadata : public virtual saml::ILockable, public virtual saml::IPlugIn
     {
-        virtual const IEntityDescriptor* lookup(const char* id) const=0;
-        virtual const IEntityDescriptor* lookup(const XMLCh* id) const=0;
+        virtual const IEntityDescriptor* lookup(const char* id, bool strict=true) const=0;
+        virtual const IEntityDescriptor* lookup(const XMLCh* id, bool strict=true) const=0;
         virtual const IEntityDescriptor* lookup(const saml::SAMLArtifact* artifact) const=0;
+        virtual const IEntitiesDescriptor* lookupGroup(const char* name, bool strict=true) const=0;
+        virtual const IEntitiesDescriptor* lookupGroup(const XMLCh* name, bool strict=true) const=0;
+        virtual std::pair<const IEntitiesDescriptor*,const IEntityDescriptor*> getRoot() const=0;
         virtual ~IMetadata() {}
     };
 
-    struct SHIB_EXPORTS IRevocation : public virtual saml::ILockable, public virtual saml::IPlugIn
-    {
-        virtual saml::Iterator<void*> getRevocationLists(const IEntityDescriptor* provider, const IRoleDescriptor* role=NULL) const=0;
-        virtual ~IRevocation() {}
-    };
-
     // Trust interface hides *all* details of signature and SSL validation.
     // Pluggable providers can fully override the Shibboleth trust model here.
     
     struct SHIB_EXPORTS ITrust : public virtual saml::IPlugIn
     {
+        // Performs certificate validation processing of an untrusted certificates
+        // using a library-specific representation, in this case an OpenSSL X509*
         virtual bool validate(
-            const saml::Iterator<IRevocation*>& revocations,
-            const IRoleDescriptor* role, const saml::SAMLSignedObject& token,
-            const saml::Iterator<IMetadata*>& metadatas=EMPTY(IMetadata*)
+            void* certEE,
+            const saml::Iterator<void*>& certChain,
+            const IRoleDescriptor* role,
+            bool checkName=true
             )=0;
-        virtual bool attach(const saml::Iterator<IRevocation*>& revocations, const IRoleDescriptor* role, void* ctx)=0;
+
+        // Validates signed SAML messages and assertions sent by an entity acting in a specific role.
+        // If certificate validation is required, the trust provider used can be overridden using
+        // the last parameter, or left null and the provider will rely on itself.
+        virtual bool validate(
+            const saml::SAMLSignedObject& token,
+            const IRoleDescriptor* role,
+            ITrust* certValidator=NULL
+            )=0;
+        
         virtual ~ITrust() {}
     };
 
@@ -338,11 +308,10 @@ namespace shibboleth
     {
         virtual const XMLCh* getName() const=0;
         virtual const XMLCh* getNamespace() const=0;
-        virtual const char* getFactory() const=0;
         virtual const char* getAlias() const=0;
         virtual const char* getHeader() const=0;
         virtual bool getCaseSensitive() const=0;
-        virtual void apply(saml::SAMLAttribute& attribute, const IRoleDescriptor* role=NULL) const=0;
+        virtual void apply(saml::SAMLAttribute& attribute, const IEntityDescriptor* source=NULL) const=0;
         virtual ~IAttributeRule() {}
     };
     
@@ -354,6 +323,12 @@ namespace shibboleth
         virtual saml::Iterator<const IAttributeRule*> getAttributeRules() const=0;
         virtual ~IAAP() {}
     };
+    
+    struct SHIB_EXPORTS IAttributeFactory : public virtual saml::IPlugIn
+    {
+        virtual saml::SAMLAttribute* build(DOMElement* e) const=0;
+        virtual ~IAttributeFactory() {}
+    };
 
 #ifdef SHIB_INSTANTIATE
     template class SHIB_EXPORTS saml::Iterator<const IContactPerson*>;
@@ -371,23 +346,12 @@ namespace shibboleth
     template class SHIB_EXPORTS saml::ArrayIterator<IMetadata*>;
     template class SHIB_EXPORTS saml::Iterator<ITrust*>;
     template class SHIB_EXPORTS saml::ArrayIterator<ITrust*>;
-    template class SHIB_EXPORTS saml::Iterator<IRevocation*>;
-    template class SHIB_EXPORTS saml::ArrayIterator<IRevocation*>;
     template class SHIB_EXPORTS saml::Iterator<ICredentials*>;
     template class SHIB_EXPORTS saml::ArrayIterator<ICredentials*>;
     template class SHIB_EXPORTS saml::Iterator<IAAP*>;
     template class SHIB_EXPORTS saml::ArrayIterator<IAAP*>;
 #endif
 
-    struct SHIB_EXPORTS Constants
-    {
-        static const XMLCh SHIB_ATTRIBUTE_NAMESPACE_URI[];
-        static const XMLCh SHIB_NAMEID_FORMAT_URI[];
-        static const XMLCh SHIB_AUTHNREQUEST_PROFILE_URI[];
-        static const XMLCh SHIB_NS[];
-        static const XMLCh InvalidHandle[];
-    };
-
     // Glue classes between abstract metadata and concrete providers
     
     class SHIB_EXPORTS Metadata
@@ -396,30 +360,15 @@ namespace shibboleth
         Metadata(const saml::Iterator<IMetadata*>& metadatas) : m_metadatas(metadatas), m_mapper(NULL) {}
         ~Metadata();
 
-        const IEntityDescriptor* lookup(const char* id);
-        const IEntityDescriptor* lookup(const XMLCh* id);
+        const IEntityDescriptor* lookup(const char* id, bool strict=true);
+        const IEntityDescriptor* lookup(const XMLCh* id, bool strict=true);
         const IEntityDescriptor* lookup(const saml::SAMLArtifact* artifact);
 
     private:
         Metadata(const Metadata&);
         void operator=(const Metadata&);
         IMetadata* m_mapper;
-        const saml::Iterator<IMetadata*>& m_metadatas;
-    };
-
-    class SHIB_EXPORTS Revocation
-    {
-    public:
-        Revocation(const saml::Iterator<IRevocation*>& revocations) : m_revocations(revocations), m_mapper(NULL) {}
-        ~Revocation();
-
-        saml::Iterator<void*> getRevocationLists(const IEntityDescriptor* provider, const IRoleDescriptor* role=NULL);
-
-    private:
-        Revocation(const Revocation&);
-        void operator=(const Revocation&);
-        IRevocation* m_mapper;
-        const saml::Iterator<IRevocation*>& m_revocations;
+        saml::Iterator<IMetadata*> m_metadatas;
     };
 
     class SHIB_EXPORTS Trust
@@ -429,16 +378,17 @@ namespace shibboleth
         ~Trust() {}
 
         bool validate(
-            const saml::Iterator<IRevocation*>& revocations,
-            const IRoleDescriptor* role, const saml::SAMLSignedObject& token,
-            const saml::Iterator<IMetadata*>& metadatas=EMPTY(IMetadata*)
+            void* certEE,
+            const saml::Iterator<void*>& certChain,
+            const IRoleDescriptor* role,
+            bool checkName=true
             ) const;
-        bool attach(const saml::Iterator<IRevocation*>& revocations, const IRoleDescriptor* role, void* ctx) const;
+        bool validate(const saml::SAMLSignedObject& token, const IRoleDescriptor* role) const;
         
     private:
         Trust(const Trust&);
         void operator=(const Trust&);
-        const saml::Iterator<ITrust*>& m_trusts;
+        saml::Iterator<ITrust*> m_trusts;
     };
     
     class SHIB_EXPORTS Credentials
@@ -453,7 +403,7 @@ namespace shibboleth
         Credentials(const Credentials&);
         void operator=(const Credentials&);
         ICredentials* m_mapper;
-        const saml::Iterator<ICredentials*>& m_creds;
+        saml::Iterator<ICredentials*> m_creds;
     };
 
     class SHIB_EXPORTS AAP
@@ -466,7 +416,7 @@ namespace shibboleth
         const IAttributeRule* operator->() const {return m_rule;}
         operator const IAttributeRule*() const {return m_rule;}
         
-        static void apply(const saml::Iterator<IAAP*>& aaps, saml::SAMLAssertion& assertion, const IRoleDescriptor* role=NULL);
+        static void apply(const saml::Iterator<IAAP*>& aaps, saml::SAMLAssertion& assertion, const IEntityDescriptor* source=NULL);
         
     private:
         AAP(const AAP&);
@@ -480,63 +430,44 @@ namespace shibboleth
     class SHIB_EXPORTS ShibBrowserProfile : virtual public saml::SAMLBrowserProfile
     {
     public:
+        struct SHIB_EXPORTS ITokenValidator {
+            virtual void validateToken(
+                saml::SAMLAssertion* token,
+                time_t=0,
+                const IRoleDescriptor* role=NULL,
+                const saml::Iterator<ITrust*>& trusts=EMPTY(ITrust*)
+                ) const=0;
+            virtual ~ITokenValidator() {}
+        };
+
         ShibBrowserProfile(
+            const ITokenValidator* validator,
             const saml::Iterator<IMetadata*>& metadatas=EMPTY(IMetadata*),
-            const saml::Iterator<IRevocation*>& revocations=EMPTY(IRevocation*),
             const saml::Iterator<ITrust*>& trusts=EMPTY(ITrust*)
             );
         virtual ~ShibBrowserProfile();
 
-        virtual void setVersion(int major, int minor);
         virtual saml::SAMLBrowserProfile::BrowserProfileResponse receive(
-            const char* packet,
+            const char* samlResponse,
             const XMLCh* recipient,
-            int supportedProfiles,
-            saml::IReplayCache* replayCache=NULL,
-            saml::SAMLBrowserProfile::ArtifactMapper* callback=NULL
+            saml::IReplayCache* replayCache,
+            int minorVersion=1
+            ) const;
+        virtual saml::SAMLBrowserProfile::BrowserProfileResponse receive(
+            saml::Iterator<const char*> artifacts,
+            const XMLCh* recipient,
+            saml::SAMLBrowserProfile::ArtifactMapper* artifactMapper,
+            saml::IReplayCache* replayCache,
+            int minorVersion=1
             ) const;
 
     private:
+        void postprocess(saml::SAMLBrowserProfile::BrowserProfileResponse& bpr, int minorVersion=1) const;
+
         saml::SAMLBrowserProfile* m_profile;
         saml::Iterator<IMetadata*> m_metadatas;
-        saml::Iterator<IRevocation*> m_revocations;
-        saml::Iterator<ITrust*> m_trusts;
-    };
-
-    // Instead of wrapping the binding to deal with mutual authentication, we
-    // just use the HTTP hook functionality offered by OpenSAML. The hook will
-    // register "itself" as a globalCtx pointer with the SAML binding and the caller
-    // will declare and pass the embedded struct as callCtx for use by the hook.
-    class SHIB_EXPORTS ShibHTTPHook : virtual public saml::SAMLSOAPHTTPBinding::HTTPHook
-    {
-    public:
-        ShibHTTPHook(
-            const saml::Iterator<IRevocation*>& revocations,
-            const saml::Iterator<ITrust*>& trusts,
-            const saml::Iterator<ICredentials*>& creds
-            ) : m_revocations(revocations), m_trusts(trusts), m_creds(creds) {}
-        virtual ~ShibHTTPHook() {}
-        
-        // Only hook we need here is for outgoing connection to server.
-        virtual bool outgoing(saml::HTTPClient* conn, void* globalCtx=NULL, void* callCtx=NULL);
-
-        // Client declares a context object and pass as callCtx to send() method.
-        class SHIB_EXPORTS ShibHTTPHookCallContext {
-        public:
-            ShibHTTPHookCallContext(const char* credResolverId, const IRoleDescriptor* role)
-                : m_credResolverId(credResolverId), m_role(role), m_hook(NULL) {}
-        private:
-            const char* m_credResolverId;
-            const IRoleDescriptor* m_role;
-            ShibHTTPHook* m_hook;
-            friend class ShibHTTPHook;
-            friend bool ssl_ctx_callback(void* ssl_ctx, void* userptr);
-        };
-    private:
-        friend bool ssl_ctx_callback(void* ssl_ctx, void* userptr);
-        saml::Iterator<IRevocation*> m_revocations;
         saml::Iterator<ITrust*> m_trusts;
-        saml::Iterator<ICredentials*> m_creds;
+        const ITokenValidator* m_validator;
     };
 
     class SHIB_EXPORTS ShibConfig
@@ -549,6 +480,11 @@ namespace shibboleth
         virtual bool init();
         virtual void term();
 
+        // manages specific attribute name to factory mappings
+        void regAttributeMapping(const XMLCh* name, const IAttributeFactory* factory);
+        void unregAttributeMapping(const XMLCh* name);
+        void clearAttributeMappings();
+
         // enables runtime and clients to access configuration
         static ShibConfig& getConfig();
     };
@@ -591,7 +527,7 @@ namespace shibboleth
         const DOMElement* m_root;
         std::string m_source;
         time_t m_filestamp;
-        RWLock* m_lock;
+        xmltooling::RWLock* m_lock;
     };
 
     /* These helpers attach metadata-derived information as exception properties and then
@@ -602,8 +538,8 @@ namespace shibboleth
      *  contactName         A formatted support or technical contact name
      *  contactEmail        A contact email address
      */
-    SHIB_EXPORTS void annotateException(saml::SAMLException& e, const IEntityDescriptor* entity, bool rethrow=true);
-    SHIB_EXPORTS void annotateException(saml::SAMLException& e, const IRoleDescriptor* role, bool rethrow=true);
+    SHIB_EXPORTS void annotateException(saml::SAMLException* e, const IEntityDescriptor* entity, bool rethrow=true);
+    SHIB_EXPORTS void annotateException(saml::SAMLException* e, const IRoleDescriptor* role, bool rethrow=true);
 }
 
 #endif