3dcddb1a8fa1c1c20b0ffa2db89fd5ac3fc7b7a6
[shibboleth/cpp-xmltooling.git] / xmltooling / signature / impl / SignatureValidator.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  * SignatureValidator.cpp
19  * 
20  * Validator for signatures based on an externally-supplied key 
21  */
22  
23 #include "internal.h"
24 #include "security/Credential.h"
25 #include "signature/Signature.h"
26 #include "signature/SignatureValidator.h"
27
28 #include <xsec/enc/XSECCryptoException.hpp>
29 #include <xsec/framework/XSECException.hpp>
30
31 using namespace xmlsignature;
32 using namespace xmltooling;
33 using namespace std;
34
35 SignatureValidator::SignatureValidator(XSECCryptoKey* key) : m_key(key), m_credential(nullptr)
36 {
37 }
38
39 SignatureValidator::SignatureValidator(const Credential* credential) : m_key(nullptr), m_credential(credential)
40 {
41 }
42
43 SignatureValidator::~SignatureValidator()
44 {
45 }
46
47 void SignatureValidator::setKey(XSECCryptoKey* key)
48 {
49     m_key = key;
50     m_credential = nullptr;
51 }
52
53 void SignatureValidator::setCredential(const Credential* credential)
54 {
55     m_key = nullptr;
56     m_credential = credential;
57 }
58
59 void SignatureValidator::validate(const XMLObject* xmlObject) const
60 {
61     const Signature* sigObj=dynamic_cast<const Signature*>(xmlObject);
62     if (!sigObj)
63         throw ValidationException("Validator only applies to Signature objects.");
64     validate(sigObj);
65 }
66
67 void SignatureValidator::validate(const Signature* sigObj) const
68 {
69     DSIGSignature* sig=sigObj->getXMLSignature();
70     if (!sig)
71         throw ValidationException("Signature does not exist yet.");
72     else if (!m_key && !m_credential)
73         throw ValidationException("No Credential or key set on Validator.");
74
75     XSECCryptoKey* key = m_key ? m_key : (m_credential ? m_credential->getPublicKey() : nullptr);
76     if (!key)
77         throw ValidationException("Credential did not contain a verification key.");
78
79     try {
80         sig->setSigningKey(key->clone());
81         if (!sig->verify())
82             throw ValidationException("Digital signature does not validate with the supplied key.");
83     }
84     catch(XSECException& e) {
85         auto_ptr_char temp(e.getMsg());
86         throw ValidationException(string("Caught an XMLSecurity exception verifying signature: ") + temp.get());
87     }
88     catch(XSECCryptoException& e) {
89         throw ValidationException(string("Caught an XMLSecurity exception verifying signature: ") + e.getMsg());
90     }
91 }