New KeyResolver/Validator/Encrypter/Decrypter classes.
[shibboleth/cpp-xmltooling.git] / xmltooling / encryption / Decrypter.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_decrypter_h__) && !defined(XMLTOOLING_NO_XMLSEC)
24 #define __xmltooling_decrypter_h__
25
26 #include <xmltooling/encryption/Encryption.h>
27 #include <xmltooling/signature/KeyResolver.h>
28
29 #include <xsec/enc/XSECCryptoKey.hpp>
30 #include <xsec/xenc/XENCCipher.hpp>
31
32 namespace xmlencryption {
33
34     /**
35      * Wrapper API for XML Decryption functionality.
36      */
37     class XMLTOOL_API Decrypter
38     {
39     public:
40         /**
41          * Constructor.
42          * Resolvers will be deleted when Decrypter is.
43          * 
44          * @param KEKresolver   resolves key decryption key based on KeyInfo information 
45          * @param resolver      resolves data decryption key based on KeyInfo information 
46          */
47         Decrypter(xmlsignature::KeyResolver* KEKresolver=NULL, xmlsignature::KeyResolver* resolver=NULL)
48             : m_cipher(NULL), m_resolver(resolver), m_KEKresolver(KEKresolver) {
49         }
50
51         ~Decrypter();
52         
53         /**
54          * Replace the current KeyResolver interface, if any, with a new one.
55          * 
56          * @param resolver  the KeyResolver to attach 
57          */
58         void setKeyResolver(xmlsignature::KeyResolver* resolver) {
59             delete m_resolver;
60             m_resolver=resolver;
61         }
62
63         /**
64          * Replace the current key encryption KeyResolver interface, if any, with a new one.
65          * 
66          * @param resolver  the KeyResolver to attach 
67          */
68         void setKEKResolver(xmlsignature::KeyResolver* resolver) {
69             delete m_KEKresolver;
70             m_KEKresolver=resolver;
71         }
72
73         /**
74          * Decrypts the supplied information and returns the resulting as a DOM
75          * fragment owned by the document associated with the marshalled EncryptedData
76          * object.
77          * 
78          * Note that the DOM nodes will be invalidated once that document
79          * is released. The caller should therefore process the DOM fragment as
80          * required and drop all references to it before that happens. The usual
81          * approach should be to unmarshall the DOM and then release it, or the
82          * DOM can also be imported into a separately owned document.
83          * 
84          * @param encryptedData the encrypted data to decrypt
85          * @return  the decrypted DOM fragment
86          */
87         DOMDocumentFragment* decryptData(EncryptedData* encryptedData);
88         
89         /**
90          * Decrypts the supplied information and returns the resulting key.
91          * The caller is responsible for deleting the key. The algorithm of the
92          * key must be supplied by the caller based on knowledge of the associated
93          * EncryptedData information.
94          * 
95          * @param encryptedKey  the encrypted/wrapped key to decrypt
96          * @param algorithm     the algorithm associated with the decrypted key
97          * @return  the decrypted key
98          */
99         XSECCryptoKey* decryptKey(EncryptedKey* encryptedKey, const XMLCh* algorithm);
100         
101     private:
102         XENCCipher* m_cipher;
103         xmlsignature::KeyResolver* m_resolver;
104         xmlsignature::KeyResolver* m_KEKresolver;
105     };
106
107     DECL_XMLTOOLING_EXCEPTION(DecryptionException,XMLTOOL_EXCEPTIONAPI(XMLTOOL_API),xmlencryption,xmltooling::XMLToolingException,Exceptions in decryption processing);
108
109 };
110
111 #endif /* __xmltooling_decrypter_h__ */