Debian Codegraph @

src/lib/crypto/OSSLDHPrivateKey.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 OSSLDHPrivateKey.cpp
29
30 OpenSSL Diffie-Hellman private key class
31 *****************************************************************************/
32
33 #include "config.h"
34 #include "log.h"
35 #include "OSSLComp.h"
36 #include "OSSLDHPrivateKey.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 OSSLDHPrivateKey::OSSLDHPrivateKey()
47 {
48 dh = NULL;
49 }
50
51 OSSLDHPrivateKey::OSSLDHPrivateKey(const DH* inDH)
52 {
53 dh = NULL;
54
55 setFromOSSL(inDH);
56 }
57
58 // Destructor
59 OSSLDHPrivateKey::~OSSLDHPrivateKey()
60 {
61 DH_free(dh);
62 }
63
64 // The type
65 /*static*/ const char* OSSLDHPrivateKey::type = "OpenSSL DH Private Key";
66
67 // Set from OpenSSL representation
68 void OSSLDHPrivateKey::setFromOSSL(const DH* inDH)
69 {
70 const BIGNUM* bn_p = NULL;
71 const BIGNUM* bn_g = NULL;
72 const BIGNUM* bn_priv_key = NULL;
73
74 DH_get0_pqg(inDH, &bn_p, NULL, &bn_g);
75 DH_get0_key(inDH, NULL, &bn_priv_key);
76
77 if (bn_p)
78 {
79 ByteString inP = OSSL::bn2ByteString(bn_p);
80 setP(inP);
81 }
82 if (bn_g)
83 {
84 ByteString inG = OSSL::bn2ByteString(bn_g);
85 setG(inG);
86 }
87 if (bn_priv_key)
88 {
89 ByteString inX = OSSL::bn2ByteString(bn_priv_key);
90 setX(inX);
91 }
92 }
93
94 // Check if the key is of the given type
95 bool OSSLDHPrivateKey::isOfType(const char* inType)
96 {
97 return !strcmp(type, inType);
98 }
99
100 // Setters for the DH private key components
101 void OSSLDHPrivateKey::setX(const ByteString& inX)
102 {
103 DHPrivateKey::setX(inX);
104
105 if (dh)
106 {
107 DH_free(dh);
108 dh = NULL;
109 }
110 }
111
112
113 // Setters for the DH public key components
114 void OSSLDHPrivateKey::setP(const ByteString& inP)
115 {
116 DHPrivateKey::setP(inP);
117
118 if (dh)
119 {
120 DH_free(dh);
121 dh = NULL;
122 }
123 }
124
125 void OSSLDHPrivateKey::setG(const ByteString& inG)
126 {
127 DHPrivateKey::setG(inG);
128
129 if (dh)
130 {
131 DH_free(dh);
132 dh = NULL;
133 }
134 }
135
136 // Encode into PKCS#8 DER
137 ByteString OSSLDHPrivateKey::PKCS8Encode()
138 {
139 ByteString der;
140 if (dh == NULL) createOSSLKey();
141 if (dh == NULL) return der;
142 EVP_PKEY* pkey = EVP_PKEY_new();
143 if (pkey == NULL) return der;
144 if (!EVP_PKEY_set1_DH(pkey, dh))
145 {
146 EVP_PKEY_free(pkey);
147 return der;
148 }
149 PKCS8_PRIV_KEY_INFO* p8inf = EVP_PKEY2PKCS8(pkey);
150 EVP_PKEY_free(pkey);
151 if (p8inf == NULL) return der;
152 int len = i2d_PKCS8_PRIV_KEY_INFO(p8inf, NULL);
153 if (len < 0)
154 {
155 PKCS8_PRIV_KEY_INFO_free(p8inf);
156 return der;
157 }
158 der.resize(len);
159 unsigned char* priv = &der[0];
160 int len2 = i2d_PKCS8_PRIV_KEY_INFO(p8inf, &priv);
161 PKCS8_PRIV_KEY_INFO_free(p8inf);
162 if (len2 != len) der.wipe();
163 return der;
164 }
165
166 // Decode from PKCS#8 BER
167 bool OSSLDHPrivateKey::PKCS8Decode(const ByteString& ber)
168 {
169 int len = ber.size();
170 if (len <= 0) return false;
171 const unsigned char* priv = ber.const_byte_str();
172 PKCS8_PRIV_KEY_INFO* p8 = d2i_PKCS8_PRIV_KEY_INFO(NULL, &priv, len);
173 if (p8 == NULL) return false;
174 EVP_PKEY* pkey = EVP_PKCS82PKEY(p8);
175 PKCS8_PRIV_KEY_INFO_free(p8);
176 if (pkey == NULL) return false;
177 DH* key = EVP_PKEY_get1_DH(pkey);
178 EVP_PKEY_free(pkey);
179 if (key == NULL) return false;
180 setFromOSSL(key);
181 DH_free(key);
182 return true;
183 }
184
185 // Retrieve the OpenSSL representation of the key
186 DH* OSSLDHPrivateKey::getOSSLKey()
187 {
188 if (dh == NULL) createOSSLKey();
189
190 return dh;
191 }
192
193 // Create the OpenSSL representation of the key
194 void OSSLDHPrivateKey::createOSSLKey()
195 {
196 if (dh != NULL) return;
197
198 BN_CTX *ctx = BN_CTX_new();
199 if (ctx == NULL)
200 {
201 ERROR_MSG("Could not create BN_CTX");
202 return;
203 }
204
205 dh = DH_new();
206 if (dh == NULL)
207 {
208 ERROR_MSG("Could not create DH object");
209 return;
210 }
211
212 // Use the OpenSSL implementation and not any engine
213 #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
214
215 #ifdef WITH_FIPS
216 if (FIPS_mode())
217 DH_set_method(dh, FIPS_dh_openssl());
218 else
219 DH_set_method(dh, DH_OpenSSL());
220 #else
221 DH_set_method(dh, DH_OpenSSL());
222 #endif
223
224 #else
225 DH_set_method(dh, DH_OpenSSL());
226 #endif
227
228 BIGNUM* bn_p = OSSL::byteString2bn(p);
229 BIGNUM* bn_g = OSSL::byteString2bn(g);
230 BIGNUM* bn_priv_key = OSSL::byteString2bn(x);
231 BIGNUM* bn_pub_key = BN_new();
232
233 BN_mod_exp(bn_pub_key, bn_g, bn_priv_key, bn_p, ctx);
234 BN_CTX_free(ctx);
235
236 DH_set0_pqg(dh, bn_p, NULL, bn_g);
237 DH_set0_key(dh, bn_pub_key, bn_priv_key);
238 }
239