https://issues.shibboleth.net/jira/browse/CPPXT-43
[shibboleth/cpp-xmltooling.git] / xmltooling / security / CredentialCriteria.h
index 6777bd4..4011bba 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright 2001-2007 Internet2
+ *  Copyright 2001-2009 Internet2
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 /**
  * @file xmltooling/security/CredentialCriteria.h
  * 
- * Class for specifying criteria by which a CredentialResolver should resolve credentials. 
+ * Class for specifying criteria by which a CredentialResolver should resolve credentials.
  */
 
 #if !defined(__xmltooling_credcrit_h__) && !defined(XMLTOOLING_NO_XMLSEC)
 #define __xmltooling_credcrit_h__
 
-#include <xmltooling/unicode.h>
+#include <xmltooling/XMLToolingConfig.h>
+#include <xmltooling/security/KeyInfoResolver.h>
+#include <xmltooling/security/Credential.h>
 #include <xmltooling/signature/KeyInfo.h>
 #include <xmltooling/signature/Signature.h>
 
-#include <string>
+#include <set>
 #include <xsec/dsig/DSIGKeyInfoList.hpp>
 #include <xsec/dsig/DSIGKeyInfoName.hpp>
 
@@ -40,31 +42,36 @@ namespace xmltooling {
     {
         MAKE_NONCOPYABLE(CredentialCriteria);
     public:
-        CredentialCriteria() : m_keyUsage(UNSPECIFIED_CREDENTIAL), m_keyInfo(NULL), m_nativeKeyInfo(NULL) {}
-        virtual ~CredentialCriteria() {}
-
-        enum UsageType {
-            UNSPECIFIED_CREDENTIAL,
-            SIGNING_CREDENTIAL,
-            TLS_CREDENTIAL,
-            ENCRYPTION_CREDENTIAL
-        };
-        
+        CredentialCriteria() : m_keyUsage(Credential::UNSPECIFIED_CREDENTIAL), m_keySize(0), m_key(NULL),
+            m_keyInfo(NULL), m_nativeKeyInfo(NULL), m_credential(NULL) {
+        }
+        virtual ~CredentialCriteria() {
+            delete m_credential;
+        }
+
+        /**
+         * Determines whether the supplied Credential matches this CredentialCriteria.
+         *
+         * @param credential    the Credential to evaluate
+         * @return true iff the Credential is consistent with this criteria
+         */
+        virtual bool matches(const Credential& credential) const;
+       
         /**
-         * Get the key usage criteria.
+         * Get key usage criteria.
          * 
-         * @return the usage.
+         * @return the usage mask
          */
-        UsageType getUsage() const {
+        unsigned int getUsage() const {
             return m_keyUsage;
         }
     
         /**
-         * Set the key usage criteria.
+         * Set key usage criteria.
          * 
-         * @param usage the usage to set
+         * @param usage the usage mask to set
          */
-        void setUsage(UsageType usage) {
+        void setUsage(unsigned int usage) {
             m_keyUsage = usage;
         }
 
@@ -91,7 +98,7 @@ namespace xmltooling {
         /**
          * Get the key algorithm criteria.
          * 
-         * @return returns the keyAlgorithm.
+         * @return the key algorithm
          */
         const char* getKeyAlgorithm() const {
             return m_keyAlgorithm.c_str();
@@ -100,34 +107,98 @@ namespace xmltooling {
         /**
          * Set the key algorithm criteria.
          * 
-         * @param keyAlgorithm The keyAlgorithm to set.
+         * @param keyAlgorithm The key algorithm to set
          */
         void setKeyAlgorithm(const char* keyAlgorithm) {
             m_keyAlgorithm.erase();
             if (keyAlgorithm)
                 m_keyAlgorithm = keyAlgorithm;
         }
+
+        /**
+         * Get the key size criteria.
+         *
+         * @return  the key size, or 0
+         */
+        unsigned int getKeySize() const {
+            return m_keySize;
+        }
+
+        /**
+         * Set the key size criteria.
+         *
+         * @param keySize Key size to set
+         */
+        void setKeySize(unsigned int keySize) {
+            m_keySize = keySize;
+        }
     
         /**
-         * Get the key name criteria.
+         * Set the key algorithm and size criteria based on an XML algorithm specifier.
+         *
+         * @param algorithm XML algorithm specifier
+         */
+        void setXMLAlgorithm(const XMLCh* algorithm) {
+            if (algorithm) {
+                std::pair<const char*,unsigned int> mapped =
+                    XMLToolingConfig::getConfig().mapXMLAlgorithmToKeyAlgorithm(algorithm);
+                setKeyAlgorithm(mapped.first);
+                setKeySize(mapped.second);
+            }
+            else {
+                setKeyAlgorithm(NULL);
+                setKeySize(0);
+            }
+        }
+
+        /**
+         * Gets key name criteria.
          * 
-         * @return the key name
+         * @return an immutable set of key names
          */
-        const char* getKeyName() const {
-            return m_keyName.c_str();
+        const std::set<std::string>& getKeyNames() const {
+            return m_keyNames;
         }
-    
+
         /**
-         * Set the key name criteria.
+         * Gets key name criteria.
          * 
-         * @param keyName key name to set
+         * @return a mutable set of key names
          */
-        void setKeyName(const char* keyName) {
-            m_keyName.erase();
-            if (keyName)
-                m_keyName = keyName;
+        std::set<std::string>& getKeyNames() {
+            return m_keyNames;
         }
-        
+
+        /**
+         * Returns the public key criteria.
+         * 
+         * @return  a public key
+         */
+        virtual XSECCryptoKey* getPublicKey() const {
+            return m_key;
+        }
+
+        /**
+         * Sets the public key criteria.
+         *
+         * <p>The lifetime of the key <strong>MUST</strong> extend
+         * for the lifetime of this object.
+         * 
+         * @param key a public key
+         */
+        void setPublicKey(XSECCryptoKey* key) {
+            m_key = key;
+        }
+
+        /**
+         * Bitmask constants controlling the kinds of criteria set automatically
+         * based on a KeyInfo object.
+         */
+        enum keyinfo_extraction_t {
+            KEYINFO_EXTRACTION_KEY = 1,
+            KEYINFO_EXTRACTION_KEYNAMES = 2
+        };
+
         /**
          * Gets the KeyInfo criteria.
          * 
@@ -140,11 +211,10 @@ namespace xmltooling {
         /**
          * Sets the KeyInfo criteria.
          * 
-         * @param keyInfo   the KeyInfo criteria
+         * @param keyInfo       the KeyInfo criteria
+         * @param extraction    bitmask of criteria to auto-extract from KeyInfo
          */
-        void setKeyInfo(const xmlsignature::KeyInfo* keyInfo) {
-            m_keyInfo = keyInfo;
-        } 
+        virtual void setKeyInfo(const xmlsignature::KeyInfo* keyInfo, int extraction=0);
 
         /**
          * Gets the native KeyInfo criteria.
@@ -158,26 +228,28 @@ namespace xmltooling {
         /**
          * Sets the KeyInfo criteria.
          * 
-         * @param keyInfo   the KeyInfo criteria
+         * @param keyInfo       the KeyInfo criteria
+         * @param extraction    bitmask of criteria to auto-extract from KeyInfo
          */
-        void setNativeKeyInfo(DSIGKeyInfoList* keyInfo) {
-            m_nativeKeyInfo = keyInfo;
-        }
+        virtual void setNativeKeyInfo(DSIGKeyInfoList* keyInfo, int extraction=0);
 
-        void setSignature(const xmlsignature::Signature& sig) {
-            xmlsignature::KeyInfo* k = sig.getKeyInfo();
-            if (k)
-                return setKeyInfo(k);
-            DSIGSignature* dsig = sig.getXMLSignature();
-            if (dsig)
-                setNativeKeyInfo(dsig->getKeyInfoList());
-        }
+        /**
+         * Sets the KeyInfo criteria from an XML Signature.
+         * 
+         * @param sig           the Signature containing KeyInfo criteria
+         * @param extraction    bitmask of criteria to auto-extract from KeyInfo
+         */
+        void setSignature(const xmlsignature::Signature& sig, int extraction=0);
 
     private:
-        UsageType m_keyUsage;
-        std::string m_peerName,m_keyAlgorithm,m_keyName;
+        unsigned int m_keyUsage;
+        unsigned int m_keySize;
+        std::string m_peerName,m_keyAlgorithm;
+        std::set<std::string> m_keyNames;
+        XSECCryptoKey* m_key;
         const xmlsignature::KeyInfo* m_keyInfo;
         DSIGKeyInfoList* m_nativeKeyInfo;
+        Credential* m_credential;
     };
 };