Debian Codegraph @

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