2 * Copyright 2001-2010 Internet2
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @file xmltooling/encryption/Encrypter.h
20 * Methods for encrypting XMLObjects and other data.
23 #if !defined(__xmltooling_encrypter_h__) && !defined(XMLTOOLING_NO_XMLSEC)
24 #define __xmltooling_encrypter_h__
26 #include <xmltooling/exceptions.h>
28 #include <xsec/dsig/DSIGConstants.hpp>
32 namespace xmltooling {
33 class XMLTOOL_API Credential;
36 namespace xmlencryption {
38 class XMLTOOL_API EncryptedData;
39 class XMLTOOL_API EncryptedKey;
42 * Wrapper API for XML Encryption functionality.
43 * Designed to allow both external and internal key generation as follows:
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).
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.
52 * If a raw key is provided, then a key object can also be created internally if the
53 * algorithm is recognized.
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
60 * Finally, when encrypting data, the key transport algorithm can be left blank to
61 * derive it from the data encryption algorithm.
63 class XMLTOOL_API Encrypter
68 * Structure to collect encryption requirements.
70 struct XMLTOOL_API EncryptionParams {
74 * The algorithm constant and key buffer <strong>MUST</strong> be accessible for the life of
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
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,
93 /** Data encryption algorithm. */
94 const XMLCh* m_algorithm;
96 /** Buffer containing encryption key. */
97 const unsigned char* m_keyBuffer;
99 /** Size of buffer. */
100 unsigned int m_keyBufferSize;
102 /** Credential containing the encryption key. */
103 const xmltooling::Credential* m_credential;
105 /** Flag limiting the size of the encrypted XML representation. */
110 * Structure to collect key wrapping/transport requirements.
112 struct XMLTOOL_API KeyEncryptionParams {
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
121 const xmltooling::Credential& credential, const XMLCh* algorithm=nullptr, const XMLCh* recipient=nullptr
124 ~KeyEncryptionParams();
126 /** Credential containing key encryption key. */
127 const xmltooling::Credential& m_credential;
129 /** Key transport or wrapping algorithm. */
130 const XMLCh* m_algorithm;
132 /** Name of recipient that owns the key encryption key. */
133 const XMLCh* m_recipient;
138 virtual ~Encrypter();
141 * Encrypts the supplied element and returns the resulting object.
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.
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.
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
155 EncryptedData* encryptElement(
156 xercesc::DOMElement* element, EncryptionParams& encParams, KeyEncryptionParams* kencParams=nullptr
160 * Encrypts the supplied element's children and returns the resulting object.
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.
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.
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
174 EncryptedData* encryptElementContent(
175 xercesc::DOMElement* element, EncryptionParams& encParams, KeyEncryptionParams* kencParams=nullptr
179 * Encrypts the supplied input stream and returns the resulting object.
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.
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.
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
193 EncryptedData* encryptStream(std::istream& input, EncryptionParams& encParams, KeyEncryptionParams* kencParams=nullptr);
196 * Encrypts the supplied key and returns the resulting object.
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
204 EncryptedKey* encryptKey(
205 const unsigned char* keyBuffer, unsigned int keyBufferSize, KeyEncryptionParams& kencParams, bool compact=false
209 * Maps a data encryption algorithm to an appropriate key transport algorithm to use.
211 * @param credential the key encryption key
212 * @param encryptionAlg data encryption algorithm
213 * @return a key transport algorithm
215 static const XMLCh* getKeyTransportAlgorithm(const xmltooling::Credential& credential, const XMLCh* encryptionAlg);
218 void checkParams(EncryptionParams& encParams, KeyEncryptionParams* kencParams);
219 EncryptedData* decorateAndUnmarshall(EncryptionParams& encParams, KeyEncryptionParams* kencParams);
221 XENCCipher* m_cipher;
222 unsigned char m_keyBuffer[32];
225 DECL_XMLTOOLING_EXCEPTION(EncryptionException,XMLTOOL_EXCEPTIONAPI(XMLTOOL_API),xmlencryption,xmltooling::XMLSecurityException,Exceptions in encryption processing);
229 #endif /* __xmltooling_encrypter_h__ */