Merge branch '1.x' of ssh://authdev.it.ohio-state.edu/~scantor/git/cpp-xmltooling...
[shibboleth/cpp-xmltooling.git] / xmltooling / signature / impl / SignatureValidator.cpp
1 /**
2  * Licensed to the University Corporation for Advanced Internet
3  * Development, Inc. (UCAID) under one or more contributor license
4  * agreements. See the NOTICE file distributed with this work for
5  * additional information regarding copyright ownership.
6  *
7  * UCAID licenses this file to you under the Apache License,
8  * Version 2.0 (the "License"); you may not use this file except
9  * in compliance with the License. You may obtain a copy of the
10  * License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17  * either express or implied. See the License for the specific
18  * language governing permissions and limitations under the License.
19  */
20
21 /**
22  * SignatureValidator.cpp
23  * 
24  * Validator for signatures based on an externally-supplied key 
25  */
26  
27 #include "internal.h"
28 #include "security/Credential.h"
29 #include "signature/Signature.h"
30 #include "signature/SignatureValidator.h"
31
32 #include <xsec/enc/XSECCryptoException.hpp>
33 #include <xsec/framework/XSECException.hpp>
34
35 using namespace xmlsignature;
36 using namespace xmltooling;
37 using namespace std;
38
39 SignatureValidator::SignatureValidator(XSECCryptoKey* key) : m_key(key), m_credential(nullptr)
40 {
41 }
42
43 SignatureValidator::SignatureValidator(const Credential* credential) : m_key(nullptr), m_credential(credential)
44 {
45 }
46
47 SignatureValidator::~SignatureValidator()
48 {
49 }
50
51 void SignatureValidator::setKey(XSECCryptoKey* key)
52 {
53     m_key = key;
54     m_credential = nullptr;
55 }
56
57 void SignatureValidator::setCredential(const Credential* credential)
58 {
59     m_key = nullptr;
60     m_credential = credential;
61 }
62
63 void SignatureValidator::validate(const XMLObject* xmlObject) const
64 {
65     const Signature* sigObj=dynamic_cast<const Signature*>(xmlObject);
66     if (!sigObj)
67         throw ValidationException("Validator only applies to Signature objects.");
68     validate(sigObj);
69 }
70
71 void SignatureValidator::validate(const Signature* sigObj) const
72 {
73     DSIGSignature* sig=sigObj->getXMLSignature();
74     if (!sig)
75         throw ValidationException("Signature does not exist yet.");
76     else if (!m_key && !m_credential)
77         throw ValidationException("No Credential or key set on Validator.");
78
79     XSECCryptoKey* key = m_key ? m_key : (m_credential ? m_credential->getPublicKey() : nullptr);
80     if (!key)
81         throw ValidationException("Credential did not contain a verification key.");
82
83     try {
84         sig->setSigningKey(key->clone());
85         if (!sig->verify())
86             throw ValidationException("Digital signature does not validate with the supplied key.");
87     }
88     catch(XSECException& e) {
89         auto_ptr_char temp(e.getMsg());
90         throw ValidationException(string("Caught an XMLSecurity exception verifying signature: ") + temp.get());
91     }
92     catch(XSECCryptoException& e) {
93         throw ValidationException(string("Caught an XMLSecurity exception verifying signature: ") + e.getMsg());
94     }
95 }