Multi-line svn commit, see body.
[shibboleth/cpp-xmltooling.git] / xmltooling / security / SecurityHelper.h
1 /*
2  *  Copyright 2001-2008 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     /**
35      * A helper class for working with keys, certificates, etc.
36      */
37     class XMLTOOL_API SecurityHelper
38     {
39     public:
40         /**
41          * Loads a private key from a local file.
42          *
43          * @param pathname  path to file containing key
44          * @param format    optional constant identifying key encoding format
45          * @param password  optional password to decrypt key
46          * @return  a populated key object
47          */
48         static XSECCryptoKey* loadKeyFromFile(const char* pathname, const char* format=NULL, const char* password=NULL);
49
50         /**
51          * Loads certificate(s) from a local file.
52          *
53          * @param certs     array to populate with certificate(s)
54          * @param pathname  path to file containing certificate(s)
55          * @param format    optional constant identifying certificate encoding format
56          * @return  size of the resulting array
57          */
58         static std::vector<XSECCryptoX509*>::size_type loadCertificatesFromFile(
59             std::vector<XSECCryptoX509*>& certs, const char* pathname, const char* format=NULL, const char* password=NULL
60             );
61
62         /**
63          * Loads CRL(s) from a local file.
64          *
65          * @param crls      array to populate with CRL(s)
66          * @param pathname  path to file containing CRL(s)
67          * @param format    optional constant identifying CRL encoding format
68          * @return  size of the resulting array
69          */
70         static std::vector<XSECCryptoX509CRL*>::size_type loadCRLsFromFile(
71             std::vector<XSECCryptoX509CRL*>& crls, const char* pathname, const char* format=NULL
72             );
73
74         /**
75          * Loads a private key from a URL.
76          *
77          * @param transport object to use to acquire key
78          * @param backing   backing file for key (written to or read from if download fails)
79          * @param format    optional constant identifying key encoding format
80          * @param password  optional password to decrypt key
81          * @return  a populated key object
82          */
83         static XSECCryptoKey* loadKeyFromURL(SOAPTransport& transport, const char* backing, const char* format=NULL, const char* password=NULL);
84
85         /**
86          * Loads certificate(s) from a URL.
87          *
88          * @param certs     array to populate with certificate(s)
89          * @param transport object to use to acquire certificate(s)
90          * @param backing   backing file for certificate(s) (written to or read from if download fails)
91          * @param format    optional constant identifying certificate encoding format
92          * @return  size of the resulting array
93          */
94         static std::vector<XSECCryptoX509*>::size_type loadCertificatesFromURL(
95             std::vector<XSECCryptoX509*>& certs, SOAPTransport& transport, const char* backing, const char* format=NULL, const char* password=NULL
96             );
97
98         /**
99          * Loads CRL(s) from a URL.
100          *
101          * @param crls      array to populate with CRL(s)
102          * @param transport object to use to acquire CRL(s)
103          * @param backing   backing file for CRL(s) (written to or read from if download fails)
104          * @param format    optional constant identifying CRL encoding format
105          * @return  size of the resulting array
106          */
107         static std::vector<XSECCryptoX509CRL*>::size_type loadCRLsFromURL(
108             std::vector<XSECCryptoX509CRL*>& crls, SOAPTransport& transport, const char* backing, const char* format=NULL
109             );
110
111         /**
112          * Compares two keys for equality.
113          *
114          * @param key1 first key to compare
115          * @param key2 second key to compare
116          * @return  true iff the keys match
117          */
118         static bool matches(const XSECCryptoKey* key1, const XSECCryptoKey* key2);
119     };
120 };
121
122 #endif /* __xmltooling_sechelper_h__ */