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/base.h>
34 namespace xmltooling {
35 class XMLTOOL_API Credential;
36 class XMLTOOL_API SOAPTransport;
37 class XMLTOOL_API XSECCryptoX509CRL;
40 * A helper class for working with keys, certificates, etc.
42 class XMLTOOL_API SecurityHelper
46 * Access a file to try and guess the encoding format used.
48 * @param pathname path to file
49 * @return constant identifying encoding format
51 static const char* guessEncodingFormat(const char* pathname);
54 * Loads a private key from a local file.
56 * @param pathname path to file containing key
57 * @param format optional constant identifying key encoding format
58 * @param password optional password to decrypt key
59 * @return a populated key object
61 static XSECCryptoKey* loadKeyFromFile(const char* pathname, const char* format=NULL, const char* password=NULL);
64 * Loads certificate(s) from a local file.
66 * @param certs array to populate with certificate(s)
67 * @param pathname path to file containing certificate(s)
68 * @param format optional constant identifying certificate encoding format
69 * @param password optional password to decrypt certificate(s)
70 * @return size of the resulting array
72 static std::vector<XSECCryptoX509*>::size_type loadCertificatesFromFile(
73 std::vector<XSECCryptoX509*>& certs, const char* pathname, const char* format=NULL, const char* password=NULL
77 * Loads CRL(s) from a local file.
79 * @param crls array to populate with CRL(s)
80 * @param pathname path to file containing CRL(s)
81 * @param format optional constant identifying CRL encoding format
82 * @return size of the resulting array
84 static std::vector<XSECCryptoX509CRL*>::size_type loadCRLsFromFile(
85 std::vector<XSECCryptoX509CRL*>& crls, const char* pathname, const char* format=NULL
89 * Loads a private key from a URL.
91 * @param transport object to use to acquire key
92 * @param backing backing file for key (written to or read from if download fails)
93 * @param format optional constant identifying key encoding format
94 * @param password optional password to decrypt key
95 * @return a populated key object
97 static XSECCryptoKey* loadKeyFromURL(SOAPTransport& transport, const char* backing, const char* format=NULL, const char* password=NULL);
100 * Loads certificate(s) from a URL.
102 * @param certs array to populate with certificate(s)
103 * @param transport object to use to acquire certificate(s)
104 * @param backing backing file for certificate(s) (written to or read from if download fails)
105 * @param format optional constant identifying certificate encoding format
106 * @param password optional password to decrypt certificate(s)
107 * @return size of the resulting array
109 static std::vector<XSECCryptoX509*>::size_type loadCertificatesFromURL(
110 std::vector<XSECCryptoX509*>& certs, SOAPTransport& transport, const char* backing, const char* format=NULL, const char* password=NULL
114 * Loads CRL(s) from a URL.
116 * @param crls array to populate with CRL(s)
117 * @param transport object to use to acquire CRL(s)
118 * @param backing backing file for CRL(s) (written to or read from if download fails)
119 * @param format optional constant identifying CRL encoding format
120 * @return size of the resulting array
122 static std::vector<XSECCryptoX509CRL*>::size_type loadCRLsFromURL(
123 std::vector<XSECCryptoX509CRL*>& crls, SOAPTransport& transport, const char* backing, const char* format=NULL
127 * Compares two keys for equality.
129 * @param key1 first key to compare
130 * @param key2 second key to compare
131 * @return true iff the keys match
133 static bool matches(const XSECCryptoKey& key1, const XSECCryptoKey& key2);
136 * Performs a hash operation over the supplied data and returns a hex-encoded string.
138 * @param hashAlg name of hash algorithm, syntax specific to crypto provider
139 * @param buf input data to hash
140 * @param buflen length of input data
141 * @return hex-encoded result of hash operation, or an empty string
143 static std::string doHash(const char* hashAlg, const char* buf, unsigned long buflen);
146 * Returns the base64-encoded DER encoding of a public key in SubjectPublicKeyInfo format.
147 * <p>If a hash algorithm is provided, the data is digested before being base64-encoded.
149 * @param cred the credential containing the key to encode
150 * @param hash optional name of hash algorithm, syntax specific to crypto provider
151 * @param nowrap if true, any linefeeds will be stripped from the result
152 * @return the base64 encoded key value
154 static std::string getDEREncoding(const Credential& cred, const char* hash, bool nowrap=true);
157 * Returns the base64-encoded DER encoding of a public key in SubjectPublicKeyInfo format.
158 * <p>If a hash algorithm is provided, the data is digested before being base64-encoded.
160 * @param key the key to encode
161 * @param hash optional name of hash algorithm, syntax specific to crypto provider
162 * @param nowrap if true, any linefeeds will be stripped from the result
163 * @param hashAlg name of hash algorithm, syntax specific to crypto provider
164 * @return the base64 encoded key value
166 static std::string getDEREncoding(const XSECCryptoKey& key, const char* hash, bool nowrap=true);
169 * Returns the base64-encoded DER encoding of a certifiate's public key in SubjectPublicKeyInfo format.
170 * <p>If a hash algorithm is provided, the data is digested before being base64-encoded.
172 * @param cert the certificate's key to encode
173 * @param hash optional name of hash algorithm, syntax specific to crypto provider
174 * @param nowrap if true, any linefeeds will be stripped from the result
175 * @param hashAlg name of hash algorithm, syntax specific to crypto provider
176 * @return the base64 encoded key value
178 static std::string getDEREncoding(const XSECCryptoX509& cert, const char* hash, bool nowrap=true);
182 * Returns the base64-encoded DER encoding of a public key in SubjectPublicKeyInfo format.
184 * @param cred the credential containing the key to encode
185 * @param hash if true, the DER encoded data is hashed with SHA-1 before base64 encoding
186 * @param nowrap if true, any linefeeds will be stripped from the result
187 * @return the base64 encoded key value
189 static std::string getDEREncoding(const Credential& cred, bool hash=false, bool nowrap=true);
193 * Returns the base64-encoded DER encoding of a public key in SubjectPublicKeyInfo format.
195 * @param key the key to encode
196 * @param hash if true, the DER encoded data is hashed with SHA-1 before base64 encoding
197 * @param nowrap if true, any linefeeds will be stripped from the result
198 * @return the base64 encoded key value
200 static std::string getDEREncoding(const XSECCryptoKey& key, bool hash=false, bool nowrap=true);
204 * Returns the base64-encoded DER encoding of a certifiate's public key in SubjectPublicKeyInfo format.
206 * @param cert the certificate's key to encode
207 * @param hash if true, the DER encoded data is hashed with SHA-1 before base64 encoding
208 * @param nowrap if true, any linefeeds will be stripped from the result
209 * @return the base64 encoded key value
211 static std::string getDEREncoding(const XSECCryptoX509& cert, bool hash=false, bool nowrap=true);
215 #endif /* __xmltooling_sechelper_h__ */