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