140a8601f9c703ea02cbc19f6e7ac95a3d3e5195
[shibboleth/cpp-xmltooling.git] / xmltooling / encryption / Encrypter.h
1 /*
2  *  Copyright 2001-2010 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 xmltooling/encryption/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/exceptions.h>
27
28 #include <xsec/dsig/DSIGConstants.hpp>
29
30 class XENCCipher;
31
32 namespace xmltooling {
33     class XMLTOOL_API Credential;
34 };
35
36 namespace xmlencryption {
37
38     class XMLTOOL_API EncryptedData;
39     class XMLTOOL_API EncryptedKey;
40
41     /**
42      * Wrapper API for XML Encryption functionality.
43      * Designed to allow both external and internal key generation as follows:
44      * 
45      * If no keying material is supplied, then the algorithm MAY be recognized
46      * and a key can be generated internally. This is only done if a KeyEncryptionParams
47      * structure is also supplied to the operation (otherwise the key would be lost).
48      * 
49      * If an XSECCryptoKey is supplied, then it is used directly, but if KeyEncryptionParams
50      * are supplied, an exception will result unless the raw key buffer is also supplied.
51      * 
52      * If a raw key is provided, then a key object can also be created internally if the
53      * algorithm is recognized.
54      * 
55      * Summing up, if KeyEncryptionParams are used, a raw key must be available or the
56      * key can be generated when the encryption algorithm itself is a standard one. If
57      * no KeyEncryptionParams are supplied, then the key must be supplied either in raw
58      * or object form.
59      *
60      * Finally, when encrypting data, the key transport algorithm can be left blank to
61      * derive it from the data encryption algorithm.
62      */
63     class XMLTOOL_API Encrypter
64     {
65     public:
66
67         /**
68          * Structure to collect encryption requirements.
69          */
70         struct XMLTOOL_API EncryptionParams {
71             /**
72              * Constructor.
73              *
74              * The algorithm constant and key buffer <strong>MUST</strong> be accessible for the life of
75              * the structure.
76              * 
77              * @param algorithm     the XML Encryption algorithm constant
78              * @param keyBuffer     buffer containing the raw key information
79              * @param keyBufferSize the size of the raw key buffer in bytes  
80              * @param credential    optional Credential supplying the encryption key
81              * @param compact       true iff the encrypted representation should be made as small as possible
82              */
83             EncryptionParams(
84                 const XMLCh* algorithm=DSIGConstants::s_unicodeStrURIAES128_CBC,
85                 const unsigned char* keyBuffer=nullptr,
86                 unsigned int keyBufferSize=0,
87                 const xmltooling::Credential* credential=nullptr,
88                 bool compact=false
89                 );
90
91             ~EncryptionParams();
92
93             /** Data encryption algorithm. */
94             const XMLCh* m_algorithm;
95             
96             /** Buffer containing encryption key. */
97             const unsigned char* m_keyBuffer;
98
99             /** Size of buffer. */
100             unsigned int m_keyBufferSize;
101
102             /** Credential containing the encryption key. */
103             const xmltooling::Credential* m_credential;
104
105             /** Flag limiting the size of the encrypted XML representation. */
106             bool m_compact;
107         };
108         
109         /**
110          * Structure to collect key wrapping/transport requirements.
111          */
112         struct XMLTOOL_API KeyEncryptionParams {
113             /**
114              * Constructor.
115              * 
116              * @param credential    a Credential supplying the key encryption key
117              * @param algorithm     XML Encryption key wrapping or transport algorithm constant
118              * @param recipient     optional name of recipient of encrypted key
119              */
120             KeyEncryptionParams(
121                 const xmltooling::Credential& credential, const XMLCh* algorithm=nullptr, const XMLCh* recipient=nullptr
122                 );
123         
124             ~KeyEncryptionParams();
125
126             /** Credential containing key encryption key. */
127             const xmltooling::Credential& m_credential;
128
129             /** Key transport or wrapping algorithm. */
130             const XMLCh* m_algorithm;
131
132             /** Name of recipient that owns the key encryption key. */
133             const XMLCh* m_recipient;
134         };
135     
136         Encrypter();
137
138         virtual ~Encrypter();
139         
140         /**
141          * Encrypts the supplied element and returns the resulting object.
142          * 
143          * If an encryption algorithm is set, but no key, a random key will be
144          * generated iff kencParams is non-NULL and the algorithm is known.
145          * 
146          * If key encryption parameters are supplied, then the encryption key
147          * is wrapped and the result placed into an EncryptedKey object in the
148          * KeyInfo of the returned EncryptedData.
149          * 
150          * @param element       the DOM element to encrypt
151          * @param encParams     primary encryption settings
152          * @param kencParams    key encryption settings, or nullptr
153          * @return a stand-alone EncryptedData object, unconnected to the source DOM 
154          */
155         EncryptedData* encryptElement(
156             xercesc::DOMElement* element, EncryptionParams& encParams, KeyEncryptionParams* kencParams=nullptr
157             );
158
159         /**
160          * Encrypts the supplied element's children and returns the resulting object.
161          * 
162          * If an encryption algorithm is set, but no key, a random key will be
163          * generated iff kencParams is non-NULL and the algorithm is known.
164
165          * If key encryption parameters are supplied, then the encryption key
166          * is wrapped and the result placed into an EncryptedKey object in the
167          * KeyInfo of the returned EncryptedData.
168          * 
169          * @param element       parent element of children to encrypt
170          * @param encParams     primary encryption settings
171          * @param kencParams    key encryption settings, or nullptr
172          * @return a stand-alone EncryptedData object, unconnected to the source DOM 
173          */
174         EncryptedData* encryptElementContent(
175             xercesc::DOMElement* element, EncryptionParams& encParams, KeyEncryptionParams* kencParams=nullptr
176             );
177
178         /**
179          * Encrypts the supplied input stream and returns the resulting object.
180          * 
181          * If an encryption algorithm is set, but no key, a random key will be
182          * generated iff kencParams is non-NULL and the algorithm is known.
183
184          * If key encryption parameters are supplied, then the encryption key
185          * is wrapped and the result placed into an EncryptedKey object in the
186          * KeyInfo of the returned EncryptedData.
187          * 
188          * @param input         the stream to encrypt
189          * @param encParams     primary encryption settings
190          * @param kencParams    key encryption settings, or nullptr
191          * @return a stand-alone EncryptedData object, unconnected to any DOM 
192          */
193         EncryptedData* encryptStream(std::istream& input, EncryptionParams& encParams, KeyEncryptionParams* kencParams=nullptr);
194         
195         /**
196          * Encrypts the supplied key and returns the resulting object.
197          * 
198          * @param keyBuffer     raw key material to encrypt
199          * @param keyBufferSize size in bytes of raw key material
200          * @param kencParams    key encryption settings
201          * @param compact       true iff the encrypted representation should be made as small as possible
202          * @return a stand-alone EncryptedKey object, unconnected to any DOM 
203          */
204         EncryptedKey* encryptKey(
205             const unsigned char* keyBuffer, unsigned int keyBufferSize, KeyEncryptionParams& kencParams, bool compact=false
206             );
207         
208         /**
209          * Maps a data encryption algorithm to an appropriate key transport algorithm to use.
210          * 
211          * @param credential    the key encryption key
212          * @param encryptionAlg data encryption algorithm
213          * @return a key transport algorithm
214          */
215         static const XMLCh* getKeyTransportAlgorithm(const xmltooling::Credential& credential, const XMLCh* encryptionAlg);
216         
217     private:
218         void checkParams(EncryptionParams& encParams, KeyEncryptionParams* kencParams);
219         EncryptedData* decorateAndUnmarshall(EncryptionParams& encParams, KeyEncryptionParams* kencParams);
220     
221         XENCCipher* m_cipher;
222         unsigned char m_keyBuffer[32];
223     };
224
225     DECL_XMLTOOLING_EXCEPTION(EncryptionException,XMLTOOL_EXCEPTIONAPI(XMLTOOL_API),xmlencryption,xmltooling::XMLSecurityException,Exceptions in encryption processing);
226
227 };
228
229 #endif /* __xmltooling_encrypter_h__ */