Debian Codegraph @

src/lib/crypto/OSSLRSAPrivateKey.cpp

raw
1 /*
2 * Copyright (c) 2010 SURFnet bv
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 /*****************************************************************************
28 OSSLRSAPrivateKey.cpp
29
30 OpenSSL RSA private key class
31 *****************************************************************************/
32
33 #include "config.h"
34 #include "log.h"
35 #include "OSSLComp.h"
36 #include "OSSLRSAPrivateKey.h"
37 #include "OSSLUtil.h"
38 #include <openssl/bn.h>
39 #include <openssl/x509.h>
40 #ifdef WITH_FIPS
41 #include <openssl/fips.h>
42 #endif
43 #include <string.h>
44
45 // Constructors
46 OSSLRSAPrivateKey::OSSLRSAPrivateKey()
47 {
48 rsa = NULL;
49 }
50
51 OSSLRSAPrivateKey::OSSLRSAPrivateKey(const RSA* inRSA)
52 {
53 rsa = NULL;
54
55 setFromOSSL(inRSA);
56 }
57
58 // Destructor
59 OSSLRSAPrivateKey::~OSSLRSAPrivateKey()
60 {
61 RSA_free(rsa);
62 }
63
64 // The type
65 /*static*/ const char* OSSLRSAPrivateKey::type = "OpenSSL RSA Private Key";
66
67 // Set from OpenSSL representation
68 void OSSLRSAPrivateKey::setFromOSSL(const RSA* inRSA)
69 {
70 const BIGNUM* bn_p = NULL;
71 const BIGNUM* bn_q = NULL;
72 const BIGNUM* bn_dmp1 = NULL;
73 const BIGNUM* bn_dmq1 = NULL;
74 const BIGNUM* bn_iqmp = NULL;
75 const BIGNUM* bn_n = NULL;
76 const BIGNUM* bn_e = NULL;
77 const BIGNUM* bn_d = NULL;
78
79 RSA_get0_factors(inRSA, &bn_p, &bn_q);
80 RSA_get0_crt_params(inRSA, &bn_dmp1, &bn_dmq1, &bn_iqmp);
81 RSA_get0_key(inRSA, &bn_n, &bn_e, &bn_d);
82
83 if (bn_p)
84 {
85 ByteString inP = OSSL::bn2ByteString(bn_p);
86 setP(inP);
87 }
88 if (bn_q)
89 {
90 ByteString inQ = OSSL::bn2ByteString(bn_q);
91 setQ(inQ);
92 }
93 if (bn_dmp1)
94 {
95 ByteString inDP1 = OSSL::bn2ByteString(bn_dmp1);
96 setDP1(inDP1);
97 }
98 if (bn_dmq1)
99 {
100 ByteString inDQ1 = OSSL::bn2ByteString(bn_dmq1);
101 setDQ1(inDQ1);
102 }
103 if (bn_iqmp)
104 {
105 ByteString inPQ = OSSL::bn2ByteString(bn_iqmp);
106 setPQ(inPQ);
107 }
108 if (bn_n)
109 {
110 ByteString inN = OSSL::bn2ByteString(bn_n);
111 setN(inN);
112 }
113 if (bn_e)
114 {
115 ByteString inE = OSSL::bn2ByteString(bn_e);
116 setE(inE);
117 }
118 if (bn_d)
119 {
120 ByteString inD = OSSL::bn2ByteString(bn_d);
121 setD(inD);
122 }
123 }
124
125 // Check if the key is of the given type
126 bool OSSLRSAPrivateKey::isOfType(const char* inType)
127 {
128 return !strcmp(type, inType);
129 }
130
131 // Setters for the RSA private key components
132 void OSSLRSAPrivateKey::setP(const ByteString& inP)
133 {
134 RSAPrivateKey::setP(inP);
135
136 if (rsa)
137 {
138 RSA_free(rsa);
139 rsa = NULL;
140 }
141 }
142
143 void OSSLRSAPrivateKey::setQ(const ByteString& inQ)
144 {
145 RSAPrivateKey::setQ(inQ);
146
147 if (rsa)
148 {
149 RSA_free(rsa);
150 rsa = NULL;
151 }
152 }
153
154 void OSSLRSAPrivateKey::setPQ(const ByteString& inPQ)
155 {
156 RSAPrivateKey::setPQ(inPQ);
157
158 if (rsa)
159 {
160 RSA_free(rsa);
161 rsa = NULL;
162 }
163 }
164
165 void OSSLRSAPrivateKey::setDP1(const ByteString& inDP1)
166 {
167 RSAPrivateKey::setDP1(inDP1);
168
169 if (rsa)
170 {
171 RSA_free(rsa);
172 rsa = NULL;
173 }
174 }
175
176 void OSSLRSAPrivateKey::setDQ1(const ByteString& inDQ1)
177 {
178 RSAPrivateKey::setDQ1(inDQ1);
179
180 if (rsa)
181 {
182 RSA_free(rsa);
183 rsa = NULL;
184 }
185 }
186
187 void OSSLRSAPrivateKey::setD(const ByteString& inD)
188 {
189 RSAPrivateKey::setD(inD);
190
191 if (rsa)
192 {
193 RSA_free(rsa);
194 rsa = NULL;
195 }
196 }
197
198
199 // Setters for the RSA public key components
200 void OSSLRSAPrivateKey::setN(const ByteString& inN)
201 {
202 RSAPrivateKey::setN(inN);
203
204 if (rsa)
205 {
206 RSA_free(rsa);
207 rsa = NULL;
208 }
209 }
210
211 void OSSLRSAPrivateKey::setE(const ByteString& inE)
212 {
213 RSAPrivateKey::setE(inE);
214
215 if (rsa)
216 {
217 RSA_free(rsa);
218 rsa = NULL;
219 }
220 }
221
222 // Encode into PKCS#8 DER
223 ByteString OSSLRSAPrivateKey::PKCS8Encode()
224 {
225 ByteString der;
226 if (rsa == NULL) createOSSLKey();
227 if (rsa == NULL) return der;
228 EVP_PKEY* pkey = EVP_PKEY_new();
229 if (pkey == NULL) return der;
230 if (!EVP_PKEY_set1_RSA(pkey, rsa))
231 {
232 EVP_PKEY_free(pkey);
233 return der;
234 }
235 PKCS8_PRIV_KEY_INFO* p8inf = EVP_PKEY2PKCS8(pkey);
236 EVP_PKEY_free(pkey);
237 if (p8inf == NULL) return der;
238 int len = i2d_PKCS8_PRIV_KEY_INFO(p8inf, NULL);
239 if (len < 0)
240 {
241 PKCS8_PRIV_KEY_INFO_free(p8inf);
242 return der;
243 }
244 der.resize(len);
245 unsigned char* priv = &der[0];
246 int len2 = i2d_PKCS8_PRIV_KEY_INFO(p8inf, &priv);
247 PKCS8_PRIV_KEY_INFO_free(p8inf);
248 if (len2 != len) der.wipe();
249 return der;
250 }
251
252 // Decode from PKCS#8 BER
253 bool OSSLRSAPrivateKey::PKCS8Decode(const ByteString& ber)
254 {
255 int len = ber.size();
256 if (len <= 0) return false;
257 const unsigned char* priv = ber.const_byte_str();
258 PKCS8_PRIV_KEY_INFO* p8 = d2i_PKCS8_PRIV_KEY_INFO(NULL, &priv, len);
259 if (p8 == NULL) return false;
260 EVP_PKEY* pkey = EVP_PKCS82PKEY(p8);
261 PKCS8_PRIV_KEY_INFO_free(p8);
262 if (pkey == NULL) return false;
263 RSA* key = EVP_PKEY_get1_RSA(pkey);
264 EVP_PKEY_free(pkey);
265 if (key == NULL) return false;
266 setFromOSSL(key);
267 RSA_free(key);
268 return true;
269 }
270
271 // Retrieve the OpenSSL representation of the key
272 RSA* OSSLRSAPrivateKey::getOSSLKey()
273 {
274 if (rsa == NULL) createOSSLKey();
275
276 return rsa;
277 }
278
279 // Create the OpenSSL representation of the key
280 void OSSLRSAPrivateKey::createOSSLKey()
281 {
282 if (rsa != NULL) return;
283
284 rsa = RSA_new();
285 if (rsa == NULL)
286 {
287 ERROR_MSG("Could not create RSA object");
288 return;
289 }
290
291 // Use the OpenSSL implementation and not any engine
292 #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
293
294 #ifdef WITH_FIPS
295 if (FIPS_mode())
296 RSA_set_method(rsa, FIPS_rsa_pkcs1_ssleay());
297 else
298 RSA_set_method(rsa, RSA_PKCS1_SSLeay());
299 #else
300 RSA_set_method(rsa, RSA_PKCS1_SSLeay());
301 #endif
302
303 #else
304 RSA_set_method(rsa, RSA_PKCS1_OpenSSL());
305 #endif
306
307 BIGNUM* bn_p = OSSL::byteString2bn(p);
308 BIGNUM* bn_q = OSSL::byteString2bn(q);
309 BIGNUM* bn_dmp1 = OSSL::byteString2bn(dp1);
310 BIGNUM* bn_dmq1 = OSSL::byteString2bn(dq1);
311 BIGNUM* bn_iqmp = OSSL::byteString2bn(pq);
312 BIGNUM* bn_n = OSSL::byteString2bn(n);
313 BIGNUM* bn_e = OSSL::byteString2bn(e);
314 BIGNUM* bn_d = OSSL::byteString2bn(d);
315
316 RSA_set0_factors(rsa, bn_p, bn_q);
317 RSA_set0_crt_params(rsa, bn_dmp1, bn_dmq1, bn_iqmp);
318 RSA_set0_key(rsa, bn_n, bn_e, bn_d);
319 }
320