Debian Codegraph @

src/lib/crypto/OSSLECPrivateKey.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 OSSLECPrivateKey.cpp
29
30 OpenSSL EC private key class
31 *****************************************************************************/
32
33 #include "config.h"
34 #ifdef WITH_ECC
35 #include "log.h"
36 #include "OSSLECPrivateKey.h"
37 #include "OSSLUtil.h"
38 #include <openssl/bn.h>
39 #include <openssl/x509.h>
40
41 // Constructors
42 OSSLECPrivateKey::OSSLECPrivateKey()
43 {
44 eckey = EC_KEY_new();
45
46 // For PKCS#8 encoding
47 EC_KEY_set_enc_flags(eckey, EC_PKEY_NO_PUBKEY);
48 }
49
50 OSSLECPrivateKey::OSSLECPrivateKey(const EC_KEY* inECKEY)
51 {
52 eckey = EC_KEY_new();
53
54 // For PKCS#8 encoding
55 EC_KEY_set_enc_flags(eckey, EC_PKEY_NO_PUBKEY);
56
57 setFromOSSL(inECKEY);
58 }
59
60 // Destructor
61 OSSLECPrivateKey::~OSSLECPrivateKey()
62 {
63 EC_KEY_free(eckey);
64 }
65
66 // The type
67 /*static*/ const char* OSSLECPrivateKey::type = "OpenSSL EC Private Key";
68
69 // Get the base point order length
70 unsigned long OSSLECPrivateKey::getOrderLength() const
71 {
72 const EC_GROUP* grp = EC_KEY_get0_group(eckey);
73 if (grp != NULL)
74 {
75 BIGNUM* order = BN_new();
76 if (order == NULL)
77 return 0;
78 if (!EC_GROUP_get_order(grp, order, NULL))
79 {
80 BN_clear_free(order);
81 return 0;
82 }
83 unsigned long len = BN_num_bytes(order);
84 BN_clear_free(order);
85 return len;
86 }
87 return 0;
88 }
89
90 // Set from OpenSSL representation
91 void OSSLECPrivateKey::setFromOSSL(const EC_KEY* inECKEY)
92 {
93 const EC_GROUP* grp = EC_KEY_get0_group(inECKEY);
94 if (grp != NULL)
95 {
96 ByteString inEC = OSSL::grp2ByteString(grp);
97 setEC(inEC);
98 }
99 const BIGNUM* pk = EC_KEY_get0_private_key(inECKEY);
100 if (pk != NULL)
101 {
102 ByteString inD = OSSL::bn2ByteString(pk);
103 setD(inD);
104 }
105 }
106
107 // Check if the key is of the given type
108 bool OSSLECPrivateKey::isOfType(const char* inType)
109 {
110 return !strcmp(type, inType);
111 }
112
113 // Setters for the EC private key components
114 void OSSLECPrivateKey::setD(const ByteString& inD)
115 {
116 ECPrivateKey::setD(inD);
117
118 BIGNUM* pk = OSSL::byteString2bn(inD);
119 EC_KEY_set_private_key(eckey, pk);
120 BN_clear_free(pk);
121 }
122
123
124 // Setters for the EC public key components
125 void OSSLECPrivateKey::setEC(const ByteString& inEC)
126 {
127 ECPrivateKey::setEC(inEC);
128
129 EC_GROUP* grp = OSSL::byteString2grp(inEC);
130 EC_KEY_set_group(eckey, grp);
131 EC_GROUP_free(grp);
132 }
133
134 // Encode into PKCS#8 DER
135 ByteString OSSLECPrivateKey::PKCS8Encode()
136 {
137 ByteString der;
138 if (eckey == NULL) return der;
139 EVP_PKEY* pkey = EVP_PKEY_new();
140 if (pkey == NULL) return der;
141 if (!EVP_PKEY_set1_EC_KEY(pkey, eckey))
142 {
143 EVP_PKEY_free(pkey);
144 return der;
145 }
146 PKCS8_PRIV_KEY_INFO* p8inf = EVP_PKEY2PKCS8(pkey);
147 EVP_PKEY_free(pkey);
148 if (p8inf == NULL) return der;
149 int len = i2d_PKCS8_PRIV_KEY_INFO(p8inf, NULL);
150 if (len < 0)
151 {
152 PKCS8_PRIV_KEY_INFO_free(p8inf);
153 return der;
154 }
155 der.resize(len);
156 unsigned char* priv = &der[0];
157 int len2 = i2d_PKCS8_PRIV_KEY_INFO(p8inf, &priv);
158 PKCS8_PRIV_KEY_INFO_free(p8inf);
159 if (len2 != len) der.wipe();
160 return der;
161 }
162
163 // Decode from PKCS#8 BER
164 bool OSSLECPrivateKey::PKCS8Decode(const ByteString& ber)
165 {
166 int len = ber.size();
167 if (len <= 0) return false;
168 const unsigned char* priv = ber.const_byte_str();
169 PKCS8_PRIV_KEY_INFO* p8 = d2i_PKCS8_PRIV_KEY_INFO(NULL, &priv, len);
170 if (p8 == NULL) return false;
171 EVP_PKEY* pkey = EVP_PKCS82PKEY(p8);
172 PKCS8_PRIV_KEY_INFO_free(p8);
173 if (pkey == NULL) return false;
174 EC_KEY* key = EVP_PKEY_get1_EC_KEY(pkey);
175 EVP_PKEY_free(pkey);
176 if (key == NULL) return false;
177 setFromOSSL(key);
178 EC_KEY_free(key);
179 return true;
180 }
181
182 // Retrieve the OpenSSL representation of the key
183 EC_KEY* OSSLECPrivateKey::getOSSLKey()
184 {
185 return eckey;
186 }
187 #endif