New KeyResolver/Validator/Encrypter/Decrypter classes.
[shibboleth/cpp-xmltooling.git] / xmltooling / encryption / Encrypter.h
1 /*
2  *  Copyright 2001-2006 Internet2
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /**
18  * @file Encrypter.h
19  * 
20  * Methods for encrypting XMLObjects and other data.
21  */
22
23 #if !defined(__xmltooling_encrypter_h__) && !defined(XMLTOOLING_NO_XMLSEC)
24 #define __xmltooling_encrypter_h__
25
26 #include <xmltooling/encryption/Encryption.h>
27
28 #include <xsec/enc/XSECCryptoKey.hpp>
29 #include <xsec/xenc/XENCCipher.hpp>
30
31 namespace xmlencryption {
32
33     /**
34      * Wrapper API for XML Encryption functionality.
35      * Designed to allow both external and internal key generation as follows:
36      * 
37      * If no keying material is supplied, then the algorithm MAY be recognized
38      * and a key can be generated internally. This is only done if a KeyEncryptionParams
39      * structure is also supplied to the operation (otherwise the key would be lost).
40      * 
41      * If an XSECCryptoKey is supplied, then it is used directly, but if KeyEncryptionParams
42      * are supplied, an exception will result unless the raw key buffer is also supplied.
43      * 
44      * If a raw key is provided, then a key object can also be created internally if the
45      * algorithm is recognized.
46      * 
47      * Summing up, if KeyEncryptionParams are used, a raw key must be available or the
48      * key can be generated when the encryption algorithm itself is a standard one. If
49      * no KeyEncryptionParams are supplied, then the key must be supplied either in raw
50      * or object form. 
51      */
52     class XMLTOOL_API Encrypter
53     {
54     public:
55
56         /**
57          * Structure to collect encryption requirements.
58          */
59         struct XMLTOOL_API EncryptionParams {
60             
61             /**
62              * Constructor.
63              * The algorithm constant and key buffer <strong>MUST</strong> be accessible for the life of
64              * the structure. The other objects will be destroyed if need be when the structure is destroyed. 
65              * 
66              * @param algorithm     the XML Encryption key wrapping or transport algorithm constant
67              * @param keyBuffer     buffer containing the raw key information
68              * @param keyBufferSize the size of the raw key buffer in bytes  
69              * @param key           the key encryption key to use, or NULL
70              * @param keyInfo       a KeyInfo object to place within the EncryptedData structure
71              */
72             EncryptionParams(
73                 const XMLCh* algorithm=DSIGConstants::s_unicodeStrURIAES256_CBC,
74                 const unsigned char* keyBuffer=NULL,
75                 unsigned int keyBufferSize=0,
76                 XSECCryptoKey* key=NULL,
77                 xmlsignature::KeyInfo* keyInfo=NULL
78                 ) : m_keyBuffer(keyBuffer), m_keyBufferSize(keyBufferSize), m_key(key), m_keyInfo(keyInfo), m_algorithm(algorithm) {
79             }
80
81             ~EncryptionParams() {
82                 delete m_key;
83                 delete m_keyInfo;
84             }
85         private:
86             const unsigned char* m_keyBuffer;
87             unsigned int m_keyBufferSize;
88             XSECCryptoKey* m_key;
89             xmlsignature::KeyInfo* m_keyInfo;
90             const XMLCh* m_algorithm;
91             
92             friend class Encrypter;
93         };
94         
95         /**
96          * Structure to collect key wrapping/transport requirements.
97          */
98         struct XMLTOOL_API KeyEncryptionParams {
99             
100             /**
101              * Constructor.
102              * The algorithm constant <strong>MUST</strong> be accessible for the life of the structure.
103              * Using a static constant suffices for this. The other objects will be destroyed if need be
104              * when the structure is destroyed. 
105              * 
106              * @param algorithm     the XML Encryption key wrapping or transport algorithm constant
107              * @param key           the key encryption key to use
108              * @param keyInfo       a KeyInfo object to place within the EncryptedKey structure that describes the KEK
109              */
110             KeyEncryptionParams(const XMLCh* algorithm, XSECCryptoKey* key, xmlsignature::KeyInfo* keyInfo=NULL)
111                 : m_key(key), m_keyInfo(keyInfo), m_algorithm(algorithm) {
112             }
113         
114             ~KeyEncryptionParams() {
115                 delete m_key;
116                 delete m_keyInfo;
117             }
118         private:
119             XSECCryptoKey* m_key;
120             xmlsignature::KeyInfo* m_keyInfo;
121             const XMLCh* m_algorithm;
122             
123             friend class Encrypter;
124         };
125     
126         Encrypter() : m_cipher(NULL) {}
127
128         ~Encrypter();
129         
130         /**
131          * Encrypts the supplied element and returns the resulting object.
132          * 
133          * If an encryption algorithm is set, but no key, a random key will be
134          * generated iff kencParams is non-NULL and the algorithm is known.
135          * 
136          * If key encryption parameters are supplied, then the encryption key
137          * is wrapped and the result placed into an EncryptedKey object in the
138          * KeyInfo of the returned EncryptedData.
139          * 
140          * @param element       the DOM element to encrypt
141          * @param encParams     primary encryption settings
142          * @param kencParams    key encryption settings, or NULL
143          */
144         EncryptedData* encryptElement(DOMElement* element, EncryptionParams& encParams, KeyEncryptionParams* kencParams=NULL);
145
146         /**
147          * Encrypts the supplied element's children and returns the resulting object.
148          * 
149          * If an encryption algorithm is set, but no key, a random key will be
150          * generated iff kencParams is non-NULL and the algorithm is known.
151
152          * If key encryption parameters are supplied, then the encryption key
153          * is wrapped and the result placed into an EncryptedKey object in the
154          * KeyInfo of the returned EncryptedData.
155          * 
156          * @param element       parent element of children to encrypt
157          * @param encParams     primary encryption settings
158          * @param kencParams    key encryption settings, or NULL
159          */
160         EncryptedData* encryptElementContent(DOMElement* element, EncryptionParams& encParams, KeyEncryptionParams* kencParams=NULL);
161
162         /**
163          * Encrypts the supplied input stream and returns the resulting object.
164          * 
165          * If an encryption algorithm is set, but no key, a random key will be
166          * generated iff kencParams is non-NULL and the algorithm is known.
167
168          * If key encryption parameters are supplied, then the encryption key
169          * is wrapped and the result placed into an EncryptedKey object in the
170          * KeyInfo of the returned EncryptedData.
171          * 
172          * @param input         the stream to encrypt
173          * @param encParams     primary encryption settings
174          * @param kencParams    key encryption settings, or NULL
175          */
176         EncryptedData* encryptStream(std::istream& input, EncryptionParams& encParams, KeyEncryptionParams* kencParams=NULL);
177         
178         /**
179          * Encrypts the supplied key and returns the resulting object.
180          * 
181          * @param keyBuffer     raw key material to encrypt
182          * @param keyBufferSize size in bytes of raw key material
183          * @param kencParams    key encryption settings
184          */
185         EncryptedKey* encryptKey(const unsigned char* keyBuffer, unsigned int keyBufferSize, KeyEncryptionParams& kencParams);
186         
187     private:
188         void checkParams(EncryptionParams& encParams, KeyEncryptionParams* kencParams);
189         EncryptedData* decorateAndUnmarshall(EncryptionParams& encParams, KeyEncryptionParams* kencParams);
190     
191         XENCCipher* m_cipher;
192         unsigned char m_keyBuffer[32];
193     };
194
195     DECL_XMLTOOLING_EXCEPTION(EncryptionException,XMLTOOL_EXCEPTIONAPI(XMLTOOL_API),xmlencryption,xmltooling::XMLToolingException,Exceptions in encryption processing);
196
197 };
198
199 #endif /* __xmltooling_encrypter_h__ */