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 * @param password optional password to decrypt certificate(s)
67 * @return size of the resulting array
69 static std::vector<XSECCryptoX509*>::size_type loadCertificatesFromFile(
70 std::vector<XSECCryptoX509*>& certs, const char* pathname, const char* format=NULL, const char* password=NULL
74 * Loads CRL(s) from a local file.
76 * @param crls array to populate with CRL(s)
77 * @param pathname path to file containing CRL(s)
78 * @param format optional constant identifying CRL encoding format
79 * @return size of the resulting array
81 static std::vector<XSECCryptoX509CRL*>::size_type loadCRLsFromFile(
82 std::vector<XSECCryptoX509CRL*>& crls, const char* pathname, const char* format=NULL
86 * Loads a private key from a URL.
88 * @param transport object to use to acquire key
89 * @param backing backing file for key (written to or read from if download fails)
90 * @param format optional constant identifying key encoding format
91 * @param password optional password to decrypt key
92 * @return a populated key object
94 static XSECCryptoKey* loadKeyFromURL(SOAPTransport& transport, const char* backing, const char* format=NULL, const char* password=NULL);
97 * Loads certificate(s) from a URL.
99 * @param certs array to populate with certificate(s)
100 * @param transport object to use to acquire certificate(s)
101 * @param backing backing file for certificate(s) (written to or read from if download fails)
102 * @param format optional constant identifying certificate encoding format
103 * @param password optional password to decrypt certificate(s)
104 * @return size of the resulting array
106 static std::vector<XSECCryptoX509*>::size_type loadCertificatesFromURL(
107 std::vector<XSECCryptoX509*>& certs, SOAPTransport& transport, const char* backing, const char* format=NULL, const char* password=NULL
111 * Loads CRL(s) from a URL.
113 * @param crls array to populate with CRL(s)
114 * @param transport object to use to acquire CRL(s)
115 * @param backing backing file for CRL(s) (written to or read from if download fails)
116 * @param format optional constant identifying CRL encoding format
117 * @return size of the resulting array
119 static std::vector<XSECCryptoX509CRL*>::size_type loadCRLsFromURL(
120 std::vector<XSECCryptoX509CRL*>& crls, SOAPTransport& transport, const char* backing, const char* format=NULL
124 * Compares two keys for equality.
126 * @param key1 first key to compare
127 * @param key2 second key to compare
128 * @return true iff the keys match
130 static bool matches(const XSECCryptoKey& key1, const XSECCryptoKey& key2);
133 * Returns the base64-encoded DER encoding of a public key in SubjectPublicKeyInfo format.
135 * @param cred the credential containing the key to encode
136 * @param hash if true, the DER encoded data is hashed with SHA-1 before base64 encoding
137 * @param nowrap if true, any linefeeds will be stripped from the result
138 * @return the base64 encoded key value
140 static std::string getDEREncoding(const Credential& cred, bool hash=false, bool nowrap=true);
143 * Returns the base64-encoded DER encoding of a public key in SubjectPublicKeyInfo format.
145 * @param key the key to encode
146 * @param hash if true, the DER encoded data is hashed with SHA-1 before base64 encoding
147 * @param nowrap if true, any linefeeds will be stripped from the result
148 * @return the base64 encoded key value
150 static std::string getDEREncoding(const XSECCryptoKey& key, bool hash=false, bool nowrap=true);
153 * Returns the base64-encoded DER encoding of a certifiate's public key in SubjectPublicKeyInfo format.
155 * @param cert the certificate's key to encode
156 * @param hash if true, the DER encoded data is hashed with SHA-1 before base64 encoding
157 * @param nowrap if true, any linefeeds will be stripped from the result
158 * @return the base64 encoded key value
160 static std::string getDEREncoding(const XSECCryptoX509& cert, bool hash=false, bool nowrap=true);
164 #endif /* __xmltooling_sechelper_h__ */