Debian Codegraph @

src/lib/crypto/OSSLEDPublicKey.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 OSSLEDPublicKey.cpp
29
30 OpenSSL EDDSA public key class
31 *****************************************************************************/
32
33 #include "config.h"
34 #ifdef WITH_EDDSA
35 #include "log.h"
36 #include "DerUtil.h"
37 #include "OSSLEDPublicKey.h"
38 #include "OSSLUtil.h"
39 #include <openssl/x509.h>
40 #include <string.h>
41
42 #define X25519_KEYLEN 32
43 #define X448_KEYLEN 56
44 #define ED448_KEYLEN 57
45
46 #define PREFIXLEN 12
47
48 // Prefixes
49 const unsigned char x25519_prefix[] = {
50 0x30, 0x2a, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65,
51 0x6e, 0x03, 0x21, 0x00
52 };
53
54 const unsigned char x448_prefix[] = {
55 0x30, 0x42, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65,
56 0x6f, 0x03, 0x39, 0x00
57 };
58
59 const unsigned char ed25519_prefix[] = {
60 0x30, 0x2a, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65,
61 0x70, 0x03, 0x21, 0x00
62 };
63
64 const unsigned char ed448_prefix[] = {
65 0x30, 0x43, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65,
66 0x71, 0x03, 0x3a, 0x00
67 };
68
69 // Constructors
70 OSSLEDPublicKey::OSSLEDPublicKey()
71 {
72 nid = NID_undef;
73 pkey = NULL;
74 }
75
76 OSSLEDPublicKey::OSSLEDPublicKey(const EVP_PKEY* inPKEY)
77 {
78 nid = NID_undef;
79 pkey = NULL;
80
81 setFromOSSL(inPKEY);
82 }
83
84 // Destructor
85 OSSLEDPublicKey::~OSSLEDPublicKey()
86 {
87 EVP_PKEY_free(pkey);
88 }
89
90 // The type
91 /*static*/ const char* OSSLEDPublicKey::type = "OpenSSL EDDSA Public Key";
92
93 // Get the base point order length
94 unsigned long OSSLEDPublicKey::getOrderLength() const
95 {
96 if (nid == NID_ED25519)
97 return X25519_KEYLEN;
98 if (nid == NID_ED448)
99 return ED448_KEYLEN;
100 return 0;
101 }
102
103 // Set from OpenSSL representation
104 void OSSLEDPublicKey::setFromOSSL(const EVP_PKEY* inPKEY)
105 {
106 nid = EVP_PKEY_id(inPKEY);
107 if (nid == NID_undef)
108 {
109 return;
110 }
111 ByteString inEC = OSSL::oid2ByteString(nid);
112 EDPublicKey::setEC(inEC);
113
114 // i2d_PUBKEY incorrectly does not const the key argument?!
115 EVP_PKEY* key = const_cast<EVP_PKEY*>(inPKEY);
116 int len = i2d_PUBKEY(key, NULL);
117 if (len <= 0)
118 {
119 ERROR_MSG("Could not encode EDDSA public key");
120 return;
121 }
122 ByteString der;
123 der.resize(len);
124 unsigned char *p = &der[0];
125 i2d_PUBKEY(key, &p);
126 ByteString raw;
127 switch (nid) {
128 case NID_X25519:
129 case NID_ED25519:
130 if (len != (X25519_KEYLEN + PREFIXLEN))
131 {
132 ERROR_MSG("Invalid size. Expected: %lu, Actual: %lu", X25519_KEYLEN + PREFIXLEN, len);
133 return;
134 }
135 raw.resize(X25519_KEYLEN);
136 memcpy(&raw[0], &der[PREFIXLEN], X25519_KEYLEN);
137 break;
138 case NID_X448:
139 if (len != (X448_KEYLEN + PREFIXLEN))
140 {
141 ERROR_MSG("Invalid size. Expected: %lu, Actual: %lu", X448_KEYLEN + PREFIXLEN, len);
142 return;
143 }
144 raw.resize(X448_KEYLEN);
145 memcpy(&raw[0], &der[PREFIXLEN], X448_KEYLEN);
146 break;
147 case NID_ED448:
148 if (len != (ED448_KEYLEN + PREFIXLEN))
149 {
150 ERROR_MSG("Invalid size. Expected: %lu, Actual: %lu",
151 ED448_KEYLEN + PREFIXLEN, len);
152 return;
153 }
154 raw.resize(ED448_KEYLEN);
155 memcpy(&raw[0], &der[PREFIXLEN], ED448_KEYLEN);
156 break;
157 default:
158 return;
159 }
160 setA(DERUTIL::raw2Octet(raw));
161 }
162
163 // Check if the key is of the given type
164 bool OSSLEDPublicKey::isOfType(const char* inType)
165 {
166 return !strcmp(type, inType);
167 }
168
169 // Setters for the EDDSA public key components
170 void OSSLEDPublicKey::setEC(const ByteString& inEC)
171 {
172 EDPublicKey::setEC(inEC);
173
174 nid = OSSL::byteString2oid(inEC);
175 if (pkey)
176 {
177 EVP_PKEY_free(pkey);
178 pkey = NULL;
179 }
180 }
181
182 void OSSLEDPublicKey::setA(const ByteString& inA)
183 {
184 EDPublicKey::setA(inA);
185
186 if (pkey)
187 {
188 EVP_PKEY_free(pkey);
189 pkey = NULL;
190 }
191 }
192
193 // Retrieve the OpenSSL representation of the key
194 EVP_PKEY* OSSLEDPublicKey::getOSSLKey()
195 {
196 if (pkey == NULL) createOSSLKey();
197
198 return pkey;
199 }
200
201 // Create the OpenSSL representation of the key
202 void OSSLEDPublicKey::createOSSLKey()
203 {
204 if (pkey != NULL) return;
205
206 ByteString der;
207 ByteString raw = DERUTIL::octet2Raw(a);
208 size_t len = raw.size();
209 if (len == 0) return;
210
211 switch (nid) {
212 case NID_X25519:
213 if (len != X25519_KEYLEN)
214 {
215 ERROR_MSG("Invalid size. Expected: %lu, Actual: %lu", X25519_KEYLEN, len);
216 return;
217 }
218 der.resize(PREFIXLEN + X25519_KEYLEN);
219 memcpy(&der[0], x25519_prefix, PREFIXLEN);
220 memcpy(&der[PREFIXLEN], raw.const_byte_str(), X25519_KEYLEN);
221 break;
222 case NID_ED25519:
223 if (len != X25519_KEYLEN)
224 {
225 ERROR_MSG("Invalid size. Expected: %lu, Actual: %lu", X25519_KEYLEN, len);
226 return;
227 }
228 der.resize(PREFIXLEN + X25519_KEYLEN);
229 memcpy(&der[0], ed25519_prefix, PREFIXLEN);
230 memcpy(&der[PREFIXLEN], raw.const_byte_str(), X25519_KEYLEN);
231 break;
232 case NID_X448:
233 if (len != X448_KEYLEN)
234 {
235 ERROR_MSG("Invalid size. Expected: %lu, Actual: %lu", X448_KEYLEN, len);
236 return;
237 }
238 der.resize(PREFIXLEN + X448_KEYLEN);
239 memcpy(&der[0], x448_prefix, PREFIXLEN);
240 memcpy(&der[PREFIXLEN], raw.const_byte_str(), X448_KEYLEN);
241 break;
242 case NID_ED448:
243 if (len != ED448_KEYLEN)
244 {
245 ERROR_MSG("Invalid size. Expected: %lu, Actual: %lu", ED448_KEYLEN, len);
246 return;
247 }
248 der.resize(PREFIXLEN + ED448_KEYLEN);
249 memcpy(&der[0], ed448_prefix, PREFIXLEN);
250 memcpy(&der[PREFIXLEN], raw.const_byte_str(), ED448_KEYLEN);
251 break;
252 default:
253 return;
254 }
255 const unsigned char *p = &der[0];
256 pkey = d2i_PUBKEY(NULL, &p, (long)der.size());
257 }
258 #endif