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 * EncryptedKeyResolver.cpp
20 * Resolves encrypted keys based on EncryptedData information or other external factors.
24 #include "encryption/EncryptedKeyResolver.h"
25 #include "signature/KeyInfo.h"
27 using namespace xmlencryption;
28 using namespace xmlsignature;
29 using namespace xmltooling;
30 using namespace xercesc;
33 EncryptedKeyResolver::EncryptedKeyResolver()
37 EncryptedKeyResolver::~EncryptedKeyResolver()
41 const EncryptedKey* EncryptedKeyResolver::resolveKey(const EncryptedData& encryptedData, const XMLCh* recipient) const
43 if (!encryptedData.getKeyInfo())
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);
50 if (!recipient || !encKey->getRecipient() || XMLString::equals(recipient,encKey->getRecipient()))
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
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) {
70 treeRoot = &encryptedData;
71 while (treeRoot->getParent())
72 treeRoot = treeRoot->getParent();
74 const EncryptedKey* encKey = dynamic_cast<const EncryptedKey*>(XMLHelper::getXMLObjectById(*treeRoot, ref+1));