-/*
- * Copyright 2001-2010 Internet2
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
+/**
+ * Licensed to the University Corporation for Advanced Internet
+ * Development, Inc. (UCAID) under one or more contributor license
+ * agreements. See the NOTICE file distributed with this work for
+ * additional information regarding copyright ownership.
+ *
+ * UCAID licenses this file to you under the Apache License,
+ * Version 2.0 (the "License"); you may not use this file except
+ * in compliance with the License. You may obtain a copy of the
+ * License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
+ * either express or implied. See the License for the specific
+ * language governing permissions and limitations under the License.
*/
/**
#include "security/BasicX509Credential.h"
#include "security/KeyInfoCredentialContext.h"
#include "security/KeyInfoResolver.h"
+#include "security/SecurityHelper.h"
#include "security/XSECCryptoX509CRL.h"
#include "signature/KeyInfo.h"
#include "signature/Signature.h"
#include <xercesc/util/XMLUniDefs.hpp>
#include <xsec/dsig/DSIGKeyInfoX509.hpp>
#include <xsec/enc/XSECKeyInfoResolverDefault.hpp>
-#include <xsec/enc/OpenSSL/OpenSSLCryptoX509.hpp>
-#include <xsec/enc/OpenSSL/OpenSSLCryptoKeyRSA.hpp>
-#include <xsec/enc/OpenSSL/OpenSSLCryptoKeyDSA.hpp>
+#include <xsec/enc/XSECCryptoX509.hpp>
+#include <xsec/enc/XSECCryptoKeyRSA.hpp>
+#include <xsec/enc/XSECCryptoKeyDSA.hpp>
+#ifdef XMLTOOLING_XMLSEC_ECC
+# include <xsec/enc/XSECCryptoKeyEC.hpp>
+#endif
#include <xsec/enc/XSECCryptoException.hpp>
#include <xsec/framework/XSECException.hpp>
class XMLTOOL_DLLLOCAL InlineCredential : public BasicX509Credential
{
public:
- InlineCredential(const KeyInfo* keyInfo=NULL) : BasicX509Credential(keyInfo!=NULL), m_credctx(new KeyInfoCredentialContext(keyInfo)) {
+ InlineCredential(const KeyInfo* keyInfo=nullptr) : BasicX509Credential(keyInfo!=nullptr), m_credctx(new KeyInfoCredentialContext(keyInfo)) {
}
InlineCredential(DSIGKeyInfoList* keyInfo) : BasicX509Credential(false), m_credctx(new KeyInfoCredentialContext(keyInfo)) {
}
- InlineCredential(KeyInfoCredentialContext* context) : BasicX509Credential(context->getKeyInfo()!=NULL), m_credctx(NULL) {
+ InlineCredential(KeyInfoCredentialContext* context) : BasicX509Credential(context->getKeyInfo()!=nullptr), m_credctx(nullptr) {
}
virtual ~InlineCredential() {
delete m_credctx;
}
XSECCryptoKey* getPrivateKey() const {
- return NULL;
+ return nullptr;
}
KeyInfo* getKeyInfo(bool compact=false) const {
- KeyInfo* ret = m_credctx->getKeyInfo() ? m_credctx->getKeyInfo()->cloneKeyInfo() : NULL;
+ KeyInfo* ret = m_credctx->getKeyInfo() ? m_credctx->getKeyInfo()->cloneKeyInfo() : nullptr;
if (ret) {
- ret->setId(NULL);
+ ret->setId(nullptr);
ret->getRetrievalMethods().clear();
+ ret->getKeyInfoReferences().clear();
if (compact) {
ret->getKeyValues().clear();
+ ret->getDEREncodedKeyValues().clear();
ret->getSPKIDatas().clear();
ret->getPGPDatas().clear();
ret->getUnknownXMLObjects().clear();
for (VectorOf(X509Data)::size_type pos = 0; pos < x509Datas.size();) {
x509Datas[pos]->getX509Certificates().clear();
x509Datas[pos]->getX509CRLs().clear();
+ x509Datas[pos]->getOCSPResponses().clear();
x509Datas[pos]->getUnknownXMLObjects().clear();
if (x509Datas[pos]->hasChildren())
++pos;
}
if (!ret->hasChildren()) {
delete ret;
- ret = NULL;
+ ret = nullptr;
}
return ret;
}
m_credctx = context;
}
- void resolve(const KeyInfo* keyInfo, int types=0);
- void resolve(DSIGKeyInfoList* keyInfo, int types=0);
+ void resolve(const KeyInfo* keyInfo, int types=0, bool followRefs=false);
+ void resolve(DSIGKeyInfoList* keyInfo, int types=0, bool followRefs=false);
private:
- bool resolveCerts(const KeyInfo* keyInfo);
- bool resolveKey(const KeyInfo* keyInfo);
- bool resolveCRLs(const KeyInfo* keyInfo);
+ bool resolveCerts(const KeyInfo* keyInfo, bool followRefs=false);
+ bool resolveKey(const KeyInfo* keyInfo, bool followRefs=false);
+ bool resolveCRLs(const KeyInfo* keyInfo, bool followRefs=false);
KeyInfoCredentialContext* m_credctx;
};
+ static const XMLCh keyInfoReferences[] = UNICODE_LITERAL_17(k,e,y,I,n,f,o,R,e,f,e,r,e,n,c,e,s);
+
class XMLTOOL_DLLLOCAL InlineKeyResolver : public KeyInfoResolver
{
public:
- InlineKeyResolver() {}
+ InlineKeyResolver(const DOMElement* e)
+ : m_followRefs(XMLHelper::getAttrBool(e, false, keyInfoReferences)) {
+ }
+
virtual ~InlineKeyResolver() {}
Credential* resolve(const KeyInfo* keyInfo, int types=0) const {
if (!keyInfo)
- return NULL;
+ return nullptr;
if (types == 0)
types = Credential::RESOLVE_KEYS|X509Credential::RESOLVE_CERTS|X509Credential::RESOLVE_CRLS;
auto_ptr<InlineCredential> credential(new InlineCredential(keyInfo));
- credential->resolve(keyInfo, types);
+ credential->resolve(keyInfo, types, m_followRefs);
return credential.release();
}
Credential* resolve(DSIGKeyInfoList* keyInfo, int types=0) const {
if (!keyInfo)
- return NULL;
+ return nullptr;
if (types == 0)
types = Credential::RESOLVE_KEYS|X509Credential::RESOLVE_CERTS|X509Credential::RESOLVE_CRLS;
auto_ptr<InlineCredential> credential(new InlineCredential(keyInfo));
- credential->resolve(keyInfo, types);
+ credential->resolve(keyInfo, types, m_followRefs);
return credential.release();
}
Credential* resolve(KeyInfoCredentialContext* context, int types=0) const {
if (!context)
- return NULL;
+ return nullptr;
if (types == 0)
types = Credential::RESOLVE_KEYS|X509Credential::RESOLVE_CERTS|X509Credential::RESOLVE_CRLS;
auto_ptr<InlineCredential> credential(new InlineCredential(context));
if (context->getKeyInfo())
- credential->resolve(context->getKeyInfo(), types);
+ credential->resolve(context->getKeyInfo(), types, m_followRefs);
else if (context->getNativeKeyInfo())
- credential->resolve(context->getNativeKeyInfo(), types);
+ credential->resolve(context->getNativeKeyInfo(), types, m_followRefs);
credential->setCredentialContext(context);
return credential.release();
}
+
+ private:
+ bool m_followRefs;
};
KeyInfoResolver* XMLTOOL_DLLLOCAL InlineKeyInfoResolverFactory(const DOMElement* const & e)
{
- return new InlineKeyResolver();
+ return new InlineKeyResolver(e);
}
};
-void InlineCredential::resolve(const KeyInfo* keyInfo, int types)
+void InlineCredential::resolve(const KeyInfo* keyInfo, int types, bool followRefs)
{
#ifdef _DEBUG
NDC ndc("resolve");
#endif
if (types & X509Credential::RESOLVE_CERTS)
- resolveCerts(keyInfo);
+ resolveCerts(keyInfo, followRefs);
if (types & Credential::RESOLVE_KEYS) {
if (types & X509Credential::RESOLVE_CERTS) {
if (!m_xseccerts.empty())
m_key = m_xseccerts.front()->clonePublicKey();
else
- resolveKey(keyInfo);
+ resolveKey(keyInfo, followRefs);
}
// Otherwise try directly for a key and then go for certs if none is found.
- else if (!resolveKey(keyInfo) && resolveCerts(keyInfo)) {
+ else if (!resolveKey(keyInfo, followRefs) && resolveCerts(keyInfo, followRefs)) {
m_key = m_xseccerts.front()->clonePublicKey();
}
}
if (types & X509Credential::RESOLVE_CRLS)
- resolveCRLs(keyInfo);
+ resolveCRLs(keyInfo, followRefs);
const XMLCh* n;
char* kn;
}
}
-bool InlineCredential::resolveKey(const KeyInfo* keyInfo)
+bool InlineCredential::resolveKey(const KeyInfo* keyInfo, bool followRefs)
{
Category& log = Category::getInstance(XMLTOOLING_LOGCAT".KeyInfoResolver."INLINE_KEYINFO_RESOLVER);
// Check for ds:KeyValue
const vector<KeyValue*>& keyValues = keyInfo->getKeyValues();
- for (vector<KeyValue*>::const_iterator i=keyValues.begin(); i!=keyValues.end(); ++i) {
+ for (vector<KeyValue*>::const_iterator i = keyValues.begin(); i != keyValues.end(); ++i) {
try {
SchemaValidators.validate(*i); // see if it's a "valid" key
RSAKeyValue* rsakv = (*i)->getRSAKeyValue();
m_key = dsa.release();
return true;
}
+#ifdef XMLTOOLING_XMLSEC_ECC
+ ECKeyValue* eckv = (*i)->getECKeyValue();
+ if (eckv && eckv->getNamedCurve() && eckv->getPublicKey()) {
+ log.warn("resolving ds11:ECKeyValue");
+ auto_ptr<XSECCryptoKeyEC> ec(XSECPlatformUtils::g_cryptoProvider->keyEC());
+ auto_ptr_char uri(eckv->getNamedCurve()->getURI());
+ auto_ptr_char val(eckv->getPublicKey()->getValue());
+ if (uri.get() && val.get()) {
+ ec->loadPublicKeyBase64(uri.get(), val.get(), XMLString::stringLen(val.get()));
+ m_key = ec.release();
+ return true;
+ }
+ }
+#endif
}
catch (ValidationException& ex) {
log.warn("skipping invalid ds:KeyValue (%s)", ex.what());
}
}
+ // Check for ds11:DEREncodedKeyValue
+ const vector<DEREncodedKeyValue*>& derValues = keyInfo->getDEREncodedKeyValues();
+ for (vector<DEREncodedKeyValue*>::const_iterator j = derValues.begin(); j != derValues.end(); ++j) {
+ log.debug("resolving ds11:DEREncodedKeyValue");
+ m_key = SecurityHelper::fromDEREncoding((*j)->getValue());
+ if (m_key)
+ return true;
+ log.warn("failed to resolve ds11:DEREncodedKeyValue");
+ }
+
+
+ if (followRefs) {
+ // Check for KeyInfoReference.
+ const XMLCh* fragID=nullptr;
+ const XMLObject* treeRoot=nullptr;
+ const vector<KeyInfoReference*>& refs = keyInfo->getKeyInfoReferences();
+ for (vector<KeyInfoReference*>::const_iterator ref = refs.begin(); ref != refs.end(); ++ref) {
+ fragID = (*ref)->getURI();
+ if (!fragID || *fragID != chPound || !*(fragID+1)) {
+ log.warn("skipping ds11:KeyInfoReference with an empty or non-local reference");
+ continue;
+ }
+ if (!treeRoot) {
+ treeRoot = keyInfo;
+ while (treeRoot->getParent())
+ treeRoot = treeRoot->getParent();
+ }
+ keyInfo = dynamic_cast<const KeyInfo*>(XMLHelper::getXMLObjectById(*treeRoot, fragID+1));
+ if (!keyInfo) {
+ log.warn("skipping ds11:KeyInfoReference, local reference did not resolve to a ds:KeyInfo");
+ continue;
+ }
+ if (resolveKey(keyInfo, false))
+ return true;
+ }
+ }
+
return false;
}
-bool InlineCredential::resolveCerts(const KeyInfo* keyInfo)
+bool InlineCredential::resolveCerts(const KeyInfo* keyInfo, bool followRefs)
{
Category& log = Category::getInstance(XMLTOOLING_LOGCAT".KeyInfoResolver."INLINE_KEYINFO_RESOLVER);
}
}
}
+
+ if (followRefs && m_xseccerts.empty()) {
+ // Check for KeyInfoReference.
+ const XMLCh* fragID=NULL;
+ const XMLObject* treeRoot=NULL;
+ const vector<KeyInfoReference*>& refs = keyInfo->getKeyInfoReferences();
+ for (vector<KeyInfoReference*>::const_iterator ref = refs.begin(); ref != refs.end(); ++ref) {
+ fragID = (*ref)->getURI();
+ if (!fragID || *fragID != chPound || !*(fragID+1)) {
+ log.warn("skipping ds11:KeyInfoReference with an empty or non-local reference");
+ continue;
+ }
+ if (!treeRoot) {
+ treeRoot = keyInfo;
+ while (treeRoot->getParent())
+ treeRoot = treeRoot->getParent();
+ }
+ keyInfo = dynamic_cast<const KeyInfo*>(XMLHelper::getXMLObjectById(*treeRoot, fragID+1));
+ if (!keyInfo) {
+ log.warn("skipping ds11:KeyInfoReference, local reference did not resolve to a ds:KeyInfo");
+ continue;
+ }
+ if (resolveCerts(keyInfo, false))
+ return true;
+ }
+ return false;
+ }
log.debug("resolved %d certificate(s)", m_xseccerts.size());
return !m_xseccerts.empty();
}
-bool InlineCredential::resolveCRLs(const KeyInfo* keyInfo)
+bool InlineCredential::resolveCRLs(const KeyInfo* keyInfo, bool followRefs)
{
Category& log = Category::getInstance(XMLTOOLING_LOGCAT".KeyInfoResolver."INLINE_KEYINFO_RESOLVER);
}
}
+ if (followRefs && m_crls.empty()) {
+ // Check for KeyInfoReference.
+ const XMLCh* fragID=NULL;
+ const XMLObject* treeRoot=NULL;
+ const vector<KeyInfoReference*>& refs = keyInfo->getKeyInfoReferences();
+ for (vector<KeyInfoReference*>::const_iterator ref = refs.begin(); ref != refs.end(); ++ref) {
+ fragID = (*ref)->getURI();
+ if (!fragID || *fragID != chPound || !*(fragID+1)) {
+ log.warn("skipping ds11:KeyInfoReference with an empty or non-local reference");
+ continue;
+ }
+ if (!treeRoot) {
+ treeRoot = keyInfo;
+ while (treeRoot->getParent())
+ treeRoot = treeRoot->getParent();
+ }
+ keyInfo = dynamic_cast<const KeyInfo*>(XMLHelper::getXMLObjectById(*treeRoot, fragID+1));
+ if (!keyInfo) {
+ log.warn("skipping ds11:KeyInfoReference, local reference did not resolve to a ds:KeyInfo");
+ continue;
+ }
+ if (resolveCRLs(keyInfo, false))
+ return true;
+ }
+ return false;
+ }
+
log.debug("resolved %d CRL(s)", m_crls.size());
return !m_crls.empty();
}
-void InlineCredential::resolve(DSIGKeyInfoList* keyInfo, int types)
+void InlineCredential::resolve(DSIGKeyInfoList* keyInfo, int types, bool followRefs)
{
#ifdef _DEBUG
NDC ndc("resolve");
// The current xmlsec API is limited to one CRL per KeyInfo.
// For now, I'm going to process the DOM directly.
DOMNode* x509Node = keyInfo->item(i)->getKeyInfoDOMNode();
- DOMElement* crlElement = x509Node ? XMLHelper::getFirstChildElement(x509Node, xmlconstants::XMLSIG_NS, X509CRL::LOCAL_NAME) : NULL;
+ DOMElement* crlElement = x509Node ? XMLHelper::getFirstChildElement(x509Node, xmlconstants::XMLSIG_NS, X509CRL::LOCAL_NAME) : nullptr;
while (crlElement) {
if (crlElement->hasChildNodes()) {
auto_ptr_char buf(crlElement->getFirstChild()->getNodeValue());