f1eeb21a9803777ce33c4d0b4032810d322d9161
[shibboleth/cpp-xmltooling.git] / xmltooling / encryption / Decrypter.h
1 /*
2  *  Copyright 2001-2009 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/Decrypter.h
19  * 
20  * Wrapper API for XML Decryption functionality.
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
28 #include <xsec/xenc/XENCCipher.hpp>
29
30 namespace xmltooling {
31     class XMLTOOL_API CredentialCriteria;
32     class XMLTOOL_API CredentialResolver;
33 };
34
35 namespace xmlencryption {
36
37     class XMLTOOL_API EncryptedKeyResolver;
38
39     /**
40      * Wrapper API for XML Decryption functionality.
41      */
42     class XMLTOOL_API Decrypter
43     {
44     public:
45         /**
46          * Constructor.
47          * 
48          * @param credResolver  locked credential resolver to supply decryption keys
49          * @param criteria      optional external criteria to use with resolver
50          * @param EKResolver    locates an EncryptedKey pertaining to the EncryptedData
51          */
52         Decrypter(
53             const xmltooling::CredentialResolver* credResolver=NULL,
54             xmltooling::CredentialCriteria* criteria=NULL,
55             const EncryptedKeyResolver* EKResolver=NULL
56             ) : m_cipher(NULL), m_credResolver(credResolver), m_criteria(criteria), m_EKResolver(EKResolver) {
57         }
58
59         virtual ~Decrypter();
60         
61         /**
62          * Replace the current EncryptedKeyResolver interface, if any, with a new one.
63          * 
64          * @param EKResolver  the EncryptedKeyResolver to attach 
65          */
66         void setEncryptedKeyResolver(const EncryptedKeyResolver* EKResolver) {
67             m_EKResolver=EKResolver;
68         }
69
70         /**
71          * Replace the current CredentialResolver interface, if any, with a new one.
72          * 
73          * @param resolver  the locked CredentialResolver to attach, or NULL to clear
74          * @param criteria  optional external criteria to use with resolver
75          */
76         void setKEKResolver(const xmltooling::CredentialResolver* resolver, xmltooling::CredentialCriteria* criteria) {
77             m_credResolver=resolver;
78             m_criteria=criteria;
79         }
80
81         /**
82          * Decrypts the supplied information using the supplied key, and returns
83          * the resulting as a DOM fragment owned by the document associated with the
84          * marshalled EncryptedData object.
85          * 
86          * Note that the DOM nodes will be invalidated once that document
87          * is released. The caller should therefore process the DOM fragment as
88          * required and drop all references to it before that happens. The usual
89          * approach should be to unmarshall the DOM and then release it, or the
90          * DOM can also be imported into a separately owned document.
91          * 
92          * @param encryptedData the data to decrypt
93          * @param key           the decryption key to use (it will not be freed internally)
94          * @return  the decrypted DOM fragment
95          */
96         xercesc::DOMDocumentFragment* decryptData(const EncryptedData& encryptedData, XSECCryptoKey* key);
97
98         /**
99          * Decrypts the supplied information and returns the resulting as a DOM
100          * fragment owned by the document associated with the marshalled EncryptedData
101          * object.
102          * 
103          * Note that the DOM nodes will be invalidated once that document
104          * is released. The caller should therefore process the DOM fragment as
105          * required and drop all references to it before that happens. The usual
106          * approach should be to unmarshall the DOM and then release it, or the
107          * DOM can also be imported into a separately owned document.
108          * 
109          * @param encryptedData the data to decrypt
110          * @param recipient identifier of decrypting entity for use in identifying multi-cast keys
111          * @return  the decrypted DOM fragment
112          */
113         xercesc::DOMDocumentFragment* decryptData(const EncryptedData& encryptedData, const XMLCh* recipient=NULL);
114         
115         /**
116          * Decrypts the supplied information to an output stream.
117          *
118          * @param out           output stream to receive the decrypted data 
119          * @param encryptedData the data to decrypt
120          * @param key           the decryption key to use (it will not be freed internally)
121          */
122         void decryptData(std::ostream& out, const EncryptedData& encryptedData, XSECCryptoKey* key);
123
124         /**
125          * Decrypts the supplied information to an output stream.
126          *
127          * @param out           output stream to receive the decrypted data 
128          * @param encryptedData the data to decrypt
129          * @param recipient     identifier of decrypting entity for use in identifying multi-cast keys
130          */
131         void decryptData(std::ostream& out, const EncryptedData& encryptedData, const XMLCh* recipient=NULL);
132
133         /**
134          * Decrypts the supplied information and returns the resulting key.
135          * The caller is responsible for deleting the key. The algorithm of the
136          * key must be supplied by the caller based on knowledge of the associated
137          * EncryptedData information.
138          * 
139          * @param encryptedKey  the encrypted/wrapped key to decrypt
140          * @param algorithm     the algorithm associated with the decrypted key
141          * @return  the decrypted key
142          */
143         XSECCryptoKey* decryptKey(const EncryptedKey& encryptedKey, const XMLCh* algorithm);
144         
145     private:
146         XENCCipher* m_cipher;
147         const xmltooling::CredentialResolver* m_credResolver;
148         xmltooling::CredentialCriteria* m_criteria;
149         const EncryptedKeyResolver* m_EKResolver;
150     };
151
152     DECL_XMLTOOLING_EXCEPTION(DecryptionException,XMLTOOL_EXCEPTIONAPI(XMLTOOL_API),xmlencryption,xmltooling::XMLToolingException,Exceptions in decryption processing);
153
154 };
155
156 #endif /* __xmltooling_decrypter_h__ */