Debian Codegraph @

src/lib/crypto/OSSLGOSTPrivateKey.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 OSSLGOSTPrivateKey.cpp
29
30 OpenSSL GOST R 34.10-2001 private key class
31 *****************************************************************************/
32
33 #include "config.h"
34 #ifdef WITH_GOST
35 #include "log.h"
36 #include "OSSLGOSTPrivateKey.h"
37 #include "OSSLUtil.h"
38 #include <string.h>
39 #include <openssl/x509.h>
40 #include <openssl/ec.h>
41
42 // DER of a private key
43 const unsigned char dummyKey[] = {
44 0x30, 0x45, 0x02, 0x01, 0x00, 0x30, 0x1c, 0x06,
45 0x06, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x13, 0x30,
46 0x12, 0x06, 0x07, 0x2a, 0x85, 0x03, 0x02, 0x02,
47 0x23, 0x01, 0x06, 0x07, 0x2a, 0x85, 0x03, 0x02,
48 0x02, 0x1e, 0x01, 0x04, 0x22, 0x02, 0x20, 0x1b,
49 0x3f, 0x94, 0xf7, 0x1a, 0x5f, 0x2f, 0xe7, 0xe5,
50 0x74, 0x0b, 0x8c, 0xd4, 0xb7, 0x18, 0xdd, 0x65,
51 0x68, 0x26, 0xd1, 0x54, 0xfb, 0x77, 0xba, 0x63,
52 0x72, 0xd9, 0xf0, 0x63, 0x87, 0xe0, 0xd6
53 };
54
55 // Constructors
56 OSSLGOSTPrivateKey::OSSLGOSTPrivateKey()
57 {
58 pkey = EVP_PKEY_new();
59 }
60
61 OSSLGOSTPrivateKey::OSSLGOSTPrivateKey(const EVP_PKEY* inPKEY)
62 {
63 OSSLGOSTPrivateKey();
64
65 setFromOSSL(inPKEY);
66 }
67
68 // Destructor
69 OSSLGOSTPrivateKey::~OSSLGOSTPrivateKey()
70 {
71 EVP_PKEY_free(pkey);
72 }
73
74 // The type
75 /*static*/ const char* OSSLGOSTPrivateKey::type = "OpenSSL GOST Private Key";
76
77 // Get the output length
78 unsigned long OSSLGOSTPrivateKey::getOutputLength() const
79 {
80 return 64;
81 }
82
83 // Set from OpenSSL representation
84 void OSSLGOSTPrivateKey::setFromOSSL(const EVP_PKEY* pkey)
85 {
86 const EC_KEY* eckey = (const EC_KEY*) EVP_PKEY_get0((EVP_PKEY*) pkey);
87 const BIGNUM* priv = EC_KEY_get0_private_key(eckey);
88 setD(OSSL::bn2ByteString(priv));
89
90 ByteString inEC;
91 int nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(eckey));
92 inEC.resize(i2d_ASN1_OBJECT(OBJ_nid2obj(nid), NULL));
93 unsigned char *p = &inEC[0];
94 i2d_ASN1_OBJECT(OBJ_nid2obj(nid), &p);
95 setEC(inEC);
96 }
97
98 // Check if the key is of the given type
99 bool OSSLGOSTPrivateKey::isOfType(const char* inType)
100 {
101 return !strcmp(type, inType);
102 }
103
104 // Setters for the GOST private key components
105 void OSSLGOSTPrivateKey::setD(const ByteString& inD)
106 {
107 GOSTPrivateKey::setD(inD);
108
109 EC_KEY* inEC = (EC_KEY*) EVP_PKEY_get0((EVP_PKEY*) pkey);
110 if (inEC == NULL)
111 {
112 const unsigned char* p = dummyKey;
113 if (d2i_PrivateKey(NID_id_GostR3410_2001, &pkey, &p, (long) sizeof(dummyKey)) == NULL)
114 {
115 ERROR_MSG("d2i_PrivateKey failed");
116
117 return;
118 }
119 inEC = (EC_KEY*) EVP_PKEY_get0((EVP_PKEY*) pkey);
120 }
121
122 const BIGNUM* priv = OSSL::byteString2bn(inD);
123 if (EC_KEY_set_private_key(inEC, priv) <= 0)
124 {
125 ERROR_MSG("EC_KEY_set_private_key failed");
126 return;
127 }
128 BN_clear_free((BIGNUM*)priv);
129
130 #ifdef notyet
131 if (gost2001_compute_public(inEC) <= 0)
132 ERROR_MSG("gost2001_compute_public failed");
133 #endif
134 }
135
136 // Setters for the GOST public key components
137 void OSSLGOSTPrivateKey::setEC(const ByteString& inEC)
138 {
139 GOSTPrivateKey::setEC(inEC);
140 }
141
142 // Retrieve the OpenSSL representation of the key
143 EVP_PKEY* OSSLGOSTPrivateKey::getOSSLKey()
144 {
145 return pkey;
146 }
147
148 // Serialisation
149 ByteString OSSLGOSTPrivateKey::serialise() const
150 {
151 return ec.serialise() +
152 d.serialise();
153 }
154
155 bool OSSLGOSTPrivateKey::deserialise(ByteString& serialised)
156 {
157 ByteString dEC = ByteString::chainDeserialise(serialised);
158 ByteString dD = ByteString::chainDeserialise(serialised);
159
160 if ((dEC.size() == 0) ||
161 (dD.size() == 0))
162 {
163 return false;
164 }
165
166 setEC(dEC);
167 setD(dD);
168
169 return true;
170 }
171
172 // Encode into PKCS#8 DER
173 ByteString OSSLGOSTPrivateKey::PKCS8Encode()
174 {
175 ByteString der;
176 if (pkey == NULL) return der;
177 PKCS8_PRIV_KEY_INFO* p8inf = EVP_PKEY2PKCS8(pkey);
178 if (p8inf == NULL) return der;
179 int len = i2d_PKCS8_PRIV_KEY_INFO(p8inf, NULL);
180 if (len < 0)
181 {
182 PKCS8_PRIV_KEY_INFO_free(p8inf);
183 return der;
184 }
185 der.resize(len);
186 unsigned char* priv = &der[0];
187 int len2 = i2d_PKCS8_PRIV_KEY_INFO(p8inf, &priv);
188 PKCS8_PRIV_KEY_INFO_free(p8inf);
189 if (len2 != len) der.wipe();
190 return der;
191 }
192
193 // Decode from PKCS#8 BER
194 bool OSSLGOSTPrivateKey::PKCS8Decode(const ByteString& ber)
195 {
196 int len = ber.size();
197 if (len <= 0) return false;
198 const unsigned char* priv = ber.const_byte_str();
199 PKCS8_PRIV_KEY_INFO* p8 = d2i_PKCS8_PRIV_KEY_INFO(NULL, &priv, len);
200 if (p8 == NULL) return false;
201 EVP_PKEY* key = EVP_PKCS82PKEY(p8);
202 PKCS8_PRIV_KEY_INFO_free(p8);
203 if (key == NULL) return false;
204 setFromOSSL(key);
205 EVP_PKEY_free(key);
206 return true;
207 }
208 #endif