3dfe9e83b52f80362cea870b6b7baac484d740ea
[shibboleth/cpp-xmltooling.git] / xmltooling / security / SecurityHelper.h
1 /*
2  *  Copyright 2001-2009 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  * @file xmltooling/security/SecurityHelper.h
19  *
20  * A helper class for working with keys, certificates, etc.
21  */
22
23 #if !defined(__xmltooling_sechelper_h__) && !defined(XMLTOOLING_NO_XMLSEC)
24 #define __xmltooling_sechelper_h__
25
26 #include <xmltooling/security/XSECCryptoX509CRL.h>
27 #include <xmltooling/soap/SOAPTransport.h>
28
29 #include <vector>
30 #include <xsec/enc/XSECCryptoKey.hpp>
31 #include <xsec/enc/XSECCryptoX509.hpp>
32
33 namespace xmltooling {
34     class XMLTOOL_API Credential;
35
36     /**
37      * A helper class for working with keys, certificates, etc.
38      */
39     class XMLTOOL_API SecurityHelper
40     {
41     public:
42         /**
43          * Access a file to try and guess the encoding format used.
44          *
45          * @param pathname  path to file
46          * @return  constant identifying encoding format
47          */
48         static const char* guessEncodingFormat(const char* pathname);
49
50         /**
51          * Loads a private key from a local file.
52          *
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
57          */
58         static XSECCryptoKey* loadKeyFromFile(const char* pathname, const char* format=NULL, const char* password=NULL);
59
60         /**
61          * Loads certificate(s) from a local file.
62          *
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
68          */
69         static std::vector<XSECCryptoX509*>::size_type loadCertificatesFromFile(
70             std::vector<XSECCryptoX509*>& certs, const char* pathname, const char* format=NULL, const char* password=NULL
71             );
72
73         /**
74          * Loads CRL(s) from a local file.
75          *
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
80          */
81         static std::vector<XSECCryptoX509CRL*>::size_type loadCRLsFromFile(
82             std::vector<XSECCryptoX509CRL*>& crls, const char* pathname, const char* format=NULL
83             );
84
85         /**
86          * Loads a private key from a URL.
87          *
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
93          */
94         static XSECCryptoKey* loadKeyFromURL(SOAPTransport& transport, const char* backing, const char* format=NULL, const char* password=NULL);
95
96         /**
97          * Loads certificate(s) from a URL.
98          *
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
105          */
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
108             );
109
110         /**
111          * Loads CRL(s) from a URL.
112          *
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
118          */
119         static std::vector<XSECCryptoX509CRL*>::size_type loadCRLsFromURL(
120             std::vector<XSECCryptoX509CRL*>& crls, SOAPTransport& transport, const char* backing, const char* format=NULL
121             );
122
123         /**
124          * Compares two keys for equality.
125          *
126          * @param key1 first key to compare
127          * @param key2 second key to compare
128          * @return  true iff the keys match
129          */
130         static bool matches(const XSECCryptoKey& key1, const XSECCryptoKey& key2);
131
132         /**
133          * Returns the base64-encoded DER encoding of a public key in SubjectPublicKeyInfo format.
134          *
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
139          */
140         static std::string getDEREncoding(const Credential& cred, bool hash=false, bool nowrap=true);
141
142         /**
143          * Returns the base64-encoded DER encoding of a public key in SubjectPublicKeyInfo format.
144          *
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
149          */
150         static std::string getDEREncoding(const XSECCryptoKey& key, bool hash=false, bool nowrap=true);
151
152         /**
153          * Returns the base64-encoded DER encoding of a certifiate's public key in SubjectPublicKeyInfo format.
154          *
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
159          */
160         static std::string getDEREncoding(const XSECCryptoX509& cert, bool hash=false, bool nowrap=true);
161     };
162 };
163
164 #endif /* __xmltooling_sechelper_h__ */