2 * Copyright 2001-2009 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 * @file xmltooling/security/SecurityHelper.h
20 * A helper class for working with keys, certificates, etc.
23 #if !defined(__xmltooling_sechelper_h__) && !defined(XMLTOOLING_NO_XMLSEC)
24 #define __xmltooling_sechelper_h__
26 #include <xmltooling/security/XSECCryptoX509CRL.h>
27 #include <xmltooling/soap/SOAPTransport.h>
30 #include <xsec/enc/XSECCryptoKey.hpp>
31 #include <xsec/enc/XSECCryptoX509.hpp>
33 namespace xmltooling {
34 class XMLTOOL_API Credential;
37 * A helper class for working with keys, certificates, etc.
39 class XMLTOOL_API SecurityHelper
43 * Access a file to try and guess the encoding format used.
45 * @param pathname path to file
46 * @return constant identifying encoding format
48 static const char* guessEncodingFormat(const char* pathname);
51 * Loads a private key from a local file.
53 * @param pathname path to file containing key
54 * @param format optional constant identifying key encoding format
55 * @param password optional password to decrypt key
56 * @return a populated key object
58 static XSECCryptoKey* loadKeyFromFile(const char* pathname, const char* format=NULL, const char* password=NULL);
61 * Loads certificate(s) from a local file.
63 * @param certs array to populate with certificate(s)
64 * @param pathname path to file containing certificate(s)
65 * @param format optional constant identifying certificate encoding format
66 * @return size of the resulting array
68 static std::vector<XSECCryptoX509*>::size_type loadCertificatesFromFile(
69 std::vector<XSECCryptoX509*>& certs, const char* pathname, const char* format=NULL, const char* password=NULL
73 * Loads CRL(s) from a local file.
75 * @param crls array to populate with CRL(s)
76 * @param pathname path to file containing CRL(s)
77 * @param format optional constant identifying CRL encoding format
78 * @return size of the resulting array
80 static std::vector<XSECCryptoX509CRL*>::size_type loadCRLsFromFile(
81 std::vector<XSECCryptoX509CRL*>& crls, const char* pathname, const char* format=NULL
85 * Loads a private key from a URL.
87 * @param transport object to use to acquire key
88 * @param backing backing file for key (written to or read from if download fails)
89 * @param format optional constant identifying key encoding format
90 * @param password optional password to decrypt key
91 * @return a populated key object
93 static XSECCryptoKey* loadKeyFromURL(SOAPTransport& transport, const char* backing, const char* format=NULL, const char* password=NULL);
96 * Loads certificate(s) from a URL.
98 * @param certs array to populate with certificate(s)
99 * @param transport object to use to acquire certificate(s)
100 * @param backing backing file for certificate(s) (written to or read from if download fails)
101 * @param format optional constant identifying certificate encoding format
102 * @return size of the resulting array
104 static std::vector<XSECCryptoX509*>::size_type loadCertificatesFromURL(
105 std::vector<XSECCryptoX509*>& certs, SOAPTransport& transport, const char* backing, const char* format=NULL, const char* password=NULL
109 * Loads CRL(s) from a URL.
111 * @param crls array to populate with CRL(s)
112 * @param transport object to use to acquire CRL(s)
113 * @param backing backing file for CRL(s) (written to or read from if download fails)
114 * @param format optional constant identifying CRL encoding format
115 * @return size of the resulting array
117 static std::vector<XSECCryptoX509CRL*>::size_type loadCRLsFromURL(
118 std::vector<XSECCryptoX509CRL*>& crls, SOAPTransport& transport, const char* backing, const char* format=NULL
122 * Compares two keys for equality.
124 * @param key1 first key to compare
125 * @param key2 second key to compare
126 * @return true iff the keys match
128 static bool matches(const XSECCryptoKey* key1, const XSECCryptoKey* key2);
131 * Returns the base64-encoded DER encoding of a public key in SubjectPublicKeyInfo format.
133 * @param key the credential containing the key to encode
134 * @return the base64 encoded key value in a malloc'd string
136 static char* getDEREncoding(const Credential& cred);
139 * Returns the base64-encoded DER encoding of a public key in SubjectPublicKeyInfo format.
141 * @param key the key to encode
142 * @return the base64 encoded key value in a malloc'd string
144 static char* getDEREncoding(const XSECCryptoKey& key);
147 * Returns the base64-encoded DER encoding of a certifiate's public key in SubjectPublicKeyInfo format.
149 * @param cert the certificate's key to encode
150 * @return the base64 encoded key value in a malloc'd string
152 static char* getDEREncoding(const XSECCryptoX509& cert);
156 #endif /* __xmltooling_sechelper_h__ */