Major revamp of credential and trust handling code, PKIX engine still needs work.
[shibboleth/cpp-xmltooling.git] / xmltooling / security / impl / Credential.cpp
1 /*
2  *  Copyright 2001-2007 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  * Credential.cpp
19  * 
20  * Wraps keys and related functionality. 
21  */
22
23 #include "internal.h"
24 #include "security/Credential.h"
25
26 #include <log4cpp/Category.hh>
27 #include <openssl/dsa.h>
28 #include <openssl/rsa.h>
29 #include <xsec/enc/OpenSSL/OpenSSLCryptoKeyDSA.hpp>
30 #include <xsec/enc/OpenSSL/OpenSSLCryptoKeyRSA.hpp>
31
32 using namespace xmltooling;
33
34 bool Credential::isEqual(XSECCryptoKey& key) const
35 {
36     XSECCryptoKey* key2 = getPublicKey();
37     if (!key2) {
38         log4cpp::Category::getInstance(XMLTOOLING_LOGCAT".Credential").warn("no public key in credential for comparison");
39         return false;
40     }
41
42     if (key.getProviderName()!=DSIGConstants::s_unicodeStrPROVOpenSSL ||
43         key2->getProviderName()!=DSIGConstants::s_unicodeStrPROVOpenSSL) {
44         log4cpp::Category::getInstance(XMLTOOLING_LOGCAT".Credential").warn("non-OpenSSL credentials are not supported.");
45         return false;
46     }
47
48     if (key.getKeyType()==XSECCryptoKey::KEY_RSA_PUBLIC || key.getKeyType()==XSECCryptoKey::KEY_RSA_PAIR) {
49         if (key2->getKeyType()!=XSECCryptoKey::KEY_RSA_PUBLIC && key2->getKeyType()==XSECCryptoKey::KEY_RSA_PAIR)
50             return false;
51         RSA* rsa1 = static_cast<OpenSSLCryptoKeyRSA*>(&key)->getOpenSSLRSA();
52         RSA* rsa2 = static_cast<OpenSSLCryptoKeyRSA*>(key2)->getOpenSSLRSA();
53         return (BN_cmp(rsa1->n,rsa2->n) == 0 && BN_cmp(rsa1->e,rsa2->e) == 0);
54     }
55
56     if (key.getKeyType()==XSECCryptoKey::KEY_DSA_PUBLIC || key.getKeyType()==XSECCryptoKey::KEY_DSA_PAIR) {
57         if (key2->getKeyType()!=XSECCryptoKey::KEY_DSA_PUBLIC && key2->getKeyType()==XSECCryptoKey::KEY_DSA_PAIR)
58             return false;
59         DSA* dsa1 = static_cast<OpenSSLCryptoKeyDSA*>(&key)->getOpenSSLDSA();
60         DSA* dsa2 = static_cast<OpenSSLCryptoKeyDSA*>(key2)->getOpenSSLDSA();
61         return (BN_cmp(dsa1->pub_key,dsa2->pub_key) == 0);
62     }
63     
64     log4cpp::Category::getInstance(XMLTOOLING_LOGCAT".Credential").warn("unsupported key type for comparison");
65     return false;
66 }