https://issues.shibboleth.net/jira/browse/SSPCPP-304
[shibboleth/cpp-xmltooling.git] / xmltooling / encryption / Encrypter.h
index c0f5463..e1723e9 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright 2001-2007 Internet2
+ *  Copyright 2001-2010 Internet2
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 #if !defined(__xmltooling_encrypter_h__) && !defined(XMLTOOLING_NO_XMLSEC)
 #define __xmltooling_encrypter_h__
 
-#include <xmltooling/encryption/Encryption.h>
+#include <xmltooling/exceptions.h>
 
-#include <xsec/enc/XSECCryptoKey.hpp>
-#include <xsec/xenc/XENCCipher.hpp>
+#include <xsec/dsig/DSIGConstants.hpp>
+
+class XENCCipher;
 
 namespace xmltooling {
     class XMLTOOL_API Credential;
@@ -34,6 +35,9 @@ namespace xmltooling {
 
 namespace xmlencryption {
 
+    class XMLTOOL_API EncryptedData;
+    class XMLTOOL_API EncryptedKey;
+
     /**
      * Wrapper API for XML Encryption functionality.
      * Designed to allow both external and internal key generation as follows:
@@ -64,7 +68,6 @@ namespace xmlencryption {
          * Structure to collect encryption requirements.
          */
         struct XMLTOOL_API EncryptionParams {
-            
             /**
              * Constructor.
              *
@@ -78,31 +81,39 @@ namespace xmlencryption {
              * @param compact       true iff the encrypted representation should be made as small as possible
              */
             EncryptionParams(
-                const XMLCh* algorithm=DSIGConstants::s_unicodeStrURIAES256_CBC,
-                const unsigned char* keyBuffer=NULL,
+#ifdef XSEC_OPENSSL_HAVE_AES
+                const XMLCh* algorithm=DSIGConstants::s_unicodeStrURIAES128_CBC,
+#else
+                const XMLCh* algorithm=DSIGConstants::s_unicodeStrURI3DES_CBC,
+#endif
+                const unsigned char* keyBuffer=nullptr,
                 unsigned int keyBufferSize=0,
-                const xmltooling::Credential* credential=NULL,
+                const xmltooling::Credential* credential=nullptr,
                 bool compact=false
-                ) :  m_algorithm(algorithm), m_keyBuffer(keyBuffer), m_keyBufferSize(keyBufferSize),
-                    m_credential(credential), m_compact(compact) {
-            }
+                );
+
+            ~EncryptionParams();
 
-            ~EncryptionParams() {}
-        private:
+            /** Data encryption algorithm. */
             const XMLCh* m_algorithm;
+            
+            /** Buffer containing encryption key. */
             const unsigned char* m_keyBuffer;
+
+            /** Size of buffer. */
             unsigned int m_keyBufferSize;
+
+            /** Credential containing the encryption key. */
             const xmltooling::Credential* m_credential;
+
+            /** Flag limiting the size of the encrypted XML representation. */
             bool m_compact;
-            
-            friend class Encrypter;
         };
         
         /**
          * Structure to collect key wrapping/transport requirements.
          */
         struct XMLTOOL_API KeyEncryptionParams {
-            
             /**
              * Constructor.
              * 
@@ -111,24 +122,24 @@ namespace xmlencryption {
              * @param recipient     optional name of recipient of encrypted key
              */
             KeyEncryptionParams(
-                const xmltooling::Credential& credential,
-                const XMLCh* algorithm=NULL,
-                const XMLCh* recipient=NULL
-                ) : m_credential(credential), m_algorithm(algorithm), m_recipient(recipient) {
-            }
+                const xmltooling::Credential& credential, const XMLCh* algorithm=nullptr, const XMLCh* recipient=nullptr
+                );
         
-            ~KeyEncryptionParams() {}
-        private:
+            ~KeyEncryptionParams();
+
+            /** Credential containing key encryption key. */
             const xmltooling::Credential& m_credential;
+
+            /** Key transport or wrapping algorithm. */
             const XMLCh* m_algorithm;
+
+            /** Name of recipient that owns the key encryption key. */
             const XMLCh* m_recipient;
-            
-            friend class Encrypter;
         };
     
-        Encrypter() : m_cipher(NULL) {}
+        Encrypter();
 
-        ~Encrypter();
+        virtual ~Encrypter();
         
         /**
          * Encrypts the supplied element and returns the resulting object.
@@ -142,11 +153,11 @@ namespace xmlencryption {
          * 
          * @param element       the DOM element to encrypt
          * @param encParams     primary encryption settings
-         * @param kencParams    key encryption settings, or NULL
+         * @param kencParams    key encryption settings, or nullptr
          * @return a stand-alone EncryptedData object, unconnected to the source DOM 
          */
         EncryptedData* encryptElement(
-            xercesc::DOMElement* element, EncryptionParams& encParams, KeyEncryptionParams* kencParams=NULL
+            xercesc::DOMElement* element, EncryptionParams& encParams, KeyEncryptionParams* kencParams=nullptr
             );
 
         /**
@@ -161,11 +172,11 @@ namespace xmlencryption {
          * 
          * @param element       parent element of children to encrypt
          * @param encParams     primary encryption settings
-         * @param kencParams    key encryption settings, or NULL
+         * @param kencParams    key encryption settings, or nullptr
          * @return a stand-alone EncryptedData object, unconnected to the source DOM 
          */
         EncryptedData* encryptElementContent(
-            xercesc::DOMElement* element, EncryptionParams& encParams, KeyEncryptionParams* kencParams=NULL
+            xercesc::DOMElement* element, EncryptionParams& encParams, KeyEncryptionParams* kencParams=nullptr
             );
 
         /**
@@ -180,10 +191,10 @@ namespace xmlencryption {
          * 
          * @param input         the stream to encrypt
          * @param encParams     primary encryption settings
-         * @param kencParams    key encryption settings, or NULL
+         * @param kencParams    key encryption settings, or nullptr
          * @return a stand-alone EncryptedData object, unconnected to any DOM 
          */
-        EncryptedData* encryptStream(std::istream& input, EncryptionParams& encParams, KeyEncryptionParams* kencParams=NULL);
+        EncryptedData* encryptStream(std::istream& input, EncryptionParams& encParams, KeyEncryptionParams* kencParams=nullptr);
         
         /**
          * Encrypts the supplied key and returns the resulting object.
@@ -201,15 +212,11 @@ namespace xmlencryption {
         /**
          * Maps a data encryption algorithm to an appropriate key transport algorithm to use.
          * 
-         * @param algorithm data encryption algorithm
+         * @param credential    the key encryption key
+         * @param encryptionAlg data encryption algorithm
          * @return a key transport algorithm
          */
-        static const XMLCh* getKeyTransportAlgorithm(const XMLCh* algorithm) {
-            if (xercesc::XMLString::equals(algorithm,DSIGConstants::s_unicodeStrURI3DES_CBC))
-                return DSIGConstants::s_unicodeStrURIRSA_1_5;
-            else
-                return DSIGConstants::s_unicodeStrURIRSA_OAEP_MGFP1;
-        }
+        static const XMLCh* getKeyTransportAlgorithm(const xmltooling::Credential& credential, const XMLCh* encryptionAlg);
         
     private:
         void checkParams(EncryptionParams& encParams, KeyEncryptionParams* kencParams);