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