6efe3957c60905c8deec45095f6b3e65197b0057
[shibboleth/cpp-xmltooling.git] / xmltooling / encryption / impl / EncryptedKeyResolver.cpp
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  * EncryptedKeyResolver.cpp
19  * 
20  * Resolves encrypted keys based on EncryptedData information or other external factors.
21  */
22
23 #include "internal.h"
24 #include "encryption/EncryptedKeyResolver.h"
25 #include "signature/KeyInfo.h"
26
27 using namespace xmlencryption;
28 using namespace xmlsignature;
29 using namespace xmltooling;
30 using namespace xercesc;
31 using namespace std;
32
33 EncryptedKeyResolver::EncryptedKeyResolver()
34 {
35 }
36
37 EncryptedKeyResolver::~EncryptedKeyResolver()
38 {
39 }
40
41 const EncryptedKey* EncryptedKeyResolver::resolveKey(const EncryptedData& encryptedData, const XMLCh* recipient) const
42 {
43     if (!encryptedData.getKeyInfo())
44         return nullptr;
45
46     const vector<XMLObject*>& others = const_cast<const KeyInfo*>(encryptedData.getKeyInfo())->getUnknownXMLObjects();
47     for (vector<XMLObject*>::const_iterator i = others.begin(); i != others.end(); i++) {
48         EncryptedKey* encKey = dynamic_cast<EncryptedKey*>(*i);
49         if (encKey) {
50             if (!recipient || !encKey->getRecipient() || XMLString::equals(recipient,encKey->getRecipient()))
51                 return encKey;
52         }
53     }
54
55     static const XMLCh rmtype[] = { // http://www.w3.org/2001/04/xmlenc#EncryptedKey
56         chLatin_h, chLatin_t, chLatin_t, chLatin_p, chColon, chForwardSlash, chForwardSlash,
57         chLatin_w, chLatin_w, chLatin_w, chPeriod, chLatin_w, chDigit_3, chPeriod, chLatin_o, chLatin_r, chLatin_g, chForwardSlash,
58         chDigit_2, chDigit_0, chDigit_0, chDigit_1, chForwardSlash, chDigit_0, chDigit_4, chForwardSlash,
59         chLatin_x, chLatin_m, chLatin_l, chLatin_e, chLatin_n, chLatin_c, chPound,
60         chLatin_E, chLatin_n, chLatin_c, chLatin_r, chLatin_y, chLatin_p, chLatin_t, chLatin_e, chLatin_d, chLatin_K, chLatin_e, chLatin_y, chNull
61     };
62
63     const XMLObject* treeRoot = nullptr;
64     const vector<RetrievalMethod*>& methods = const_cast<const KeyInfo*>(encryptedData.getKeyInfo())->getRetrievalMethods();
65     for (vector<RetrievalMethod*>::const_iterator m = methods.begin(); m != methods.end(); ++m) {
66         if (XMLString::equals((*m)->getType(), rmtype)) {
67             const XMLCh* ref = (*m)->getURI();
68             if (ref && *ref == chPound) {
69                 if (!treeRoot) {
70                     treeRoot = &encryptedData;
71                     while (treeRoot->getParent())
72                         treeRoot = treeRoot->getParent();
73                 }
74                 const EncryptedKey* encKey = dynamic_cast<const EncryptedKey*>(XMLHelper::getXMLObjectById(*treeRoot, ref+1));
75                 if (encKey)
76                     return encKey;
77             }
78         }
79     }
80
81     return nullptr;
82 }