| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <openssl/evp.h> |
| 16 |
| 17 | #include <string.h> |
| 18 |
| 19 | #include <openssl/bytestring.h> |
| 20 | #include <openssl/dsa.h> |
| 21 | #include <openssl/ec_key.h> |
| 22 | #include <openssl/err.h> |
| 23 | #include <openssl/rsa.h> |
| 24 |
| 25 | #include "internal.h" |
| 26 | #include "../bytestring/internal.h" |
| 27 | #include "../internal.h" |
| 28 |
| 29 |
| 30 | // We intentionally omit |dh_asn1_meth| from this list. It is not serializable. |
| 31 | static const EVP_PKEY_ASN1_METHOD *const kASN1Methods[] = { |
| 32 | &rsa_asn1_meth, |
| 33 | &ec_asn1_meth, |
| 34 | &dsa_asn1_meth, |
| 35 | &ed25519_asn1_meth, |
| 36 | &x25519_asn1_meth, |
| 37 | }; |
| 38 |
| 39 | static const EVP_PKEY_ASN1_METHOD *parse_key_type(CBS *cbs) { |
| 40 | CBS oid; |
| 41 | if (!CBS_get_asn1(cbs, &oid, CBS_ASN1_OBJECT)) { |
| 42 | return NULL; |
| 43 | } |
| 44 |
| 45 | for (unsigned i = 0; i < OPENSSL_ARRAY_SIZE(kASN1Methods); i++) { |
| 46 | const EVP_PKEY_ASN1_METHOD *method = kASN1Methods[i]; |
| 47 | if (CBS_len(&oid) == method->oid_len && |
| 48 | OPENSSL_memcmp(CBS_data(&oid), method->oid, method->oid_len) == 0) { |
| 49 | return method; |
| 50 | } |
| 51 | } |
| 52 |
| 53 | return NULL; |
| 54 | } |
| 55 |
| 56 | EVP_PKEY *EVP_parse_public_key(CBS *cbs) { |
| 57 | // Parse the SubjectPublicKeyInfo. |
| 58 | CBS spki, algorithm, key; |
| 59 | uint8_t padding; |
| 60 | if (!CBS_get_asn1(cbs, &spki, CBS_ASN1_SEQUENCE) || |
| 61 | !CBS_get_asn1(&spki, &algorithm, CBS_ASN1_SEQUENCE) || |
| 62 | !CBS_get_asn1(&spki, &key, CBS_ASN1_BITSTRING) || |
| 63 | CBS_len(&spki) != 0) { |
| 64 | OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); |
| 65 | return nullptr; |
| 66 | } |
| 67 | const EVP_PKEY_ASN1_METHOD *method = parse_key_type(&algorithm); |
| 68 | if (method == nullptr) { |
| 69 | OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM); |
| 70 | return nullptr; |
| 71 | } |
| 72 | if (// Every key type defined encodes the key as a byte string with the same |
| 73 | // conversion to BIT STRING. |
| 74 | !CBS_get_u8(&key, &padding) || |
| 75 | padding != 0) { |
| 76 | OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); |
| 77 | return nullptr; |
| 78 | } |
| 79 |
| 80 | // Set up an |EVP_PKEY| of the appropriate type. |
| 81 | bssl::UniquePtr<EVP_PKEY> ret(EVP_PKEY_new()); |
| 82 | if (ret == nullptr) { |
| 83 | return nullptr; |
| 84 | } |
| 85 | evp_pkey_set_method(ret.get(), method); |
| 86 |
| 87 | // Call into the type-specific SPKI decoding function. |
| 88 | if (ret->ameth->pub_decode == nullptr) { |
| 89 | OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM); |
| 90 | return nullptr; |
| 91 | } |
| 92 | if (!ret->ameth->pub_decode(ret.get(), &algorithm, &key)) { |
| 93 | return nullptr; |
| 94 | } |
| 95 |
| 96 | return ret.release(); |
| 97 | } |
| 98 |
| 99 | int EVP_marshal_public_key(CBB *cbb, const EVP_PKEY *key) { |
| 100 | if (key->ameth == NULL || key->ameth->pub_encode == NULL) { |
| 101 | OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM); |
| 102 | return 0; |
| 103 | } |
| 104 |
| 105 | return key->ameth->pub_encode(cbb, key); |
| 106 | } |
| 107 |
| 108 | EVP_PKEY *EVP_parse_private_key(CBS *cbs) { |
| 109 | // Parse the PrivateKeyInfo. |
| 110 | CBS pkcs8, algorithm, key; |
| 111 | uint64_t version; |
| 112 | if (!CBS_get_asn1(cbs, &pkcs8, CBS_ASN1_SEQUENCE) || |
| 113 | !CBS_get_asn1_uint64(&pkcs8, &version) || |
| 114 | version != 0 || |
| 115 | !CBS_get_asn1(&pkcs8, &algorithm, CBS_ASN1_SEQUENCE) || |
| 116 | !CBS_get_asn1(&pkcs8, &key, CBS_ASN1_OCTETSTRING)) { |
| 117 | OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); |
| 118 | return nullptr; |
| 119 | } |
| 120 | const EVP_PKEY_ASN1_METHOD *method = parse_key_type(&algorithm); |
| 121 | if (method == nullptr) { |
| 122 | OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM); |
| 123 | return nullptr; |
| 124 | } |
| 125 |
| 126 | // A PrivateKeyInfo ends with a SET of Attributes which we ignore. |
| 127 |
| 128 | // Set up an |EVP_PKEY| of the appropriate type. |
| 129 | bssl::UniquePtr<EVP_PKEY> ret(EVP_PKEY_new()); |
| 130 | if (ret == nullptr) { |
| 131 | return nullptr; |
| 132 | } |
| 133 | evp_pkey_set_method(ret.get(), method); |
| 134 |
| 135 | // Call into the type-specific PrivateKeyInfo decoding function. |
| 136 | if (ret->ameth->priv_decode == nullptr) { |
| 137 | OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM); |
| 138 | return nullptr; |
| 139 | } |
| 140 | if (!ret->ameth->priv_decode(ret.get(), &algorithm, &key)) { |
| 141 | return nullptr; |
| 142 | } |
| 143 |
| 144 | return ret.release(); |
| 145 | } |
| 146 |
| 147 | int EVP_marshal_private_key(CBB *cbb, const EVP_PKEY *key) { |
| 148 | if (key->ameth == NULL || key->ameth->priv_encode == NULL) { |
| 149 | OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM); |
| 150 | return 0; |
| 151 | } |
| 152 |
| 153 | return key->ameth->priv_encode(cbb, key); |
| 154 | } |
| 155 |
| 156 | static bssl::UniquePtr<EVP_PKEY> old_priv_decode(CBS *cbs, int type) { |
| 157 | bssl::UniquePtr<EVP_PKEY> ret(EVP_PKEY_new()); |
| 158 | if (ret == nullptr) { |
| 159 | return nullptr; |
| 160 | } |
| 161 |
| 162 | switch (type) { |
| 163 | case EVP_PKEY_EC: { |
| 164 | bssl::UniquePtr<EC_KEY> ec_key(EC_KEY_parse_private_key(cbs, nullptr)); |
| 165 | if (ec_key == nullptr) { |
| 166 | return nullptr; |
| 167 | } |
| 168 | EVP_PKEY_assign_EC_KEY(ret.get(), ec_key.release()); |
| 169 | return ret; |
| 170 | } |
| 171 | case EVP_PKEY_DSA: { |
| 172 | bssl::UniquePtr<DSA> dsa(DSA_parse_private_key(cbs)); |
| 173 | if (dsa == nullptr) { |
| 174 | return nullptr; |
| 175 | } |
| 176 | EVP_PKEY_assign_DSA(ret.get(), dsa.release()); |
| 177 | return ret; |
| 178 | } |
| 179 | case EVP_PKEY_RSA: { |
| 180 | bssl::UniquePtr<RSA> rsa(RSA_parse_private_key(cbs)); |
| 181 | if (rsa == nullptr) { |
| 182 | return nullptr; |
| 183 | } |
| 184 | EVP_PKEY_assign_RSA(ret.get(), rsa.release()); |
| 185 | return ret; |
| 186 | } |
| 187 | default: |
| 188 | OPENSSL_PUT_ERROR(EVP, EVP_R_UNKNOWN_PUBLIC_KEY_TYPE); |
| 189 | return nullptr; |
| 190 | } |
| 191 | } |
| 192 |
| 193 | EVP_PKEY *d2i_PrivateKey(int type, EVP_PKEY **out, const uint8_t **inp, |
| 194 | long len) { |
| 195 | if (len < 0) { |
| 196 | OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); |
| 197 | return nullptr; |
| 198 | } |
| 199 |
| 200 | // Parse with the legacy format. |
| 201 | CBS cbs; |
| 202 | CBS_init(&cbs, *inp, (size_t)len); |
| 203 | bssl::UniquePtr<EVP_PKEY> ret = old_priv_decode(&cbs, type); |
| 204 | if (ret == nullptr) { |
| 205 | // Try again with PKCS#8. |
| 206 | ERR_clear_error(); |
| 207 | CBS_init(&cbs, *inp, (size_t)len); |
| 208 | ret.reset(EVP_parse_private_key(&cbs)); |
| 209 | if (ret == nullptr) { |
| 210 | return nullptr; |
| 211 | } |
| 212 | if (ret->type != type) { |
| 213 | OPENSSL_PUT_ERROR(EVP, EVP_R_DIFFERENT_KEY_TYPES); |
| 214 | return nullptr; |
| 215 | } |
| 216 | } |
| 217 |
| 218 | if (out != nullptr) { |
| 219 | EVP_PKEY_free(*out); |
| 220 | *out = ret.get(); |
| 221 | } |
| 222 | *inp = CBS_data(&cbs); |
| 223 | return ret.release(); |
| 224 | } |
| 225 |
| 226 | // num_elements parses one SEQUENCE from |in| and returns the number of elements |
| 227 | // in it. On parse error, it returns zero. |
| 228 | static size_t num_elements(const uint8_t *in, size_t in_len) { |
| 229 | CBS cbs, sequence; |
| 230 | CBS_init(&cbs, in, (size_t)in_len); |
| 231 |
| 232 | if (!CBS_get_asn1(&cbs, &sequence, CBS_ASN1_SEQUENCE)) { |
| 233 | return 0; |
| 234 | } |
| 235 |
| 236 | size_t count = 0; |
| 237 | while (CBS_len(&sequence) > 0) { |
| 238 | if (!CBS_get_any_asn1_element(&sequence, NULL, NULL, NULL)) { |
| 239 | return 0; |
| 240 | } |
| 241 |
| 242 | count++; |
| 243 | } |
| 244 |
| 245 | return count; |
| 246 | } |
| 247 |
| 248 | EVP_PKEY *d2i_AutoPrivateKey(EVP_PKEY **out, const uint8_t **inp, long len) { |
| 249 | if (len < 0) { |
| 250 | OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); |
| 251 | return NULL; |
| 252 | } |
| 253 |
| 254 | // Parse the input as a PKCS#8 PrivateKeyInfo. |
| 255 | CBS cbs; |
| 256 | CBS_init(&cbs, *inp, (size_t)len); |
| 257 | EVP_PKEY *ret = EVP_parse_private_key(&cbs); |
| 258 | if (ret != NULL) { |
| 259 | if (out != NULL) { |
| 260 | EVP_PKEY_free(*out); |
| 261 | *out = ret; |
| 262 | } |
| 263 | *inp = CBS_data(&cbs); |
| 264 | return ret; |
| 265 | } |
| 266 | ERR_clear_error(); |
| 267 |
| 268 | // Count the elements to determine the legacy key format. |
| 269 | switch (num_elements(*inp, (size_t)len)) { |
| 270 | case 4: |
| 271 | return d2i_PrivateKey(EVP_PKEY_EC, out, inp, len); |
| 272 |
| 273 | case 6: |
| 274 | return d2i_PrivateKey(EVP_PKEY_DSA, out, inp, len); |
| 275 |
| 276 | default: |
| 277 | return d2i_PrivateKey(EVP_PKEY_RSA, out, inp, len); |
| 278 | } |
| 279 | } |
| 280 |
| 281 | int i2d_PublicKey(const EVP_PKEY *key, uint8_t **outp) { |
| 282 | switch (key->type) { |
| 283 | case EVP_PKEY_RSA: |
| 284 | return i2d_RSAPublicKey(EVP_PKEY_get0_RSA(key), outp); |
| 285 | case EVP_PKEY_DSA: |
| 286 | return i2d_DSAPublicKey(EVP_PKEY_get0_DSA(key), outp); |
| 287 | case EVP_PKEY_EC: |
| 288 | return i2o_ECPublicKey(EVP_PKEY_get0_EC_KEY(key), outp); |
| 289 | default: |
| 290 | OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_PUBLIC_KEY_TYPE); |
| 291 | return -1; |
| 292 | } |
| 293 | } |
| 294 |
| 295 | EVP_PKEY *d2i_PublicKey(int type, EVP_PKEY **out, const uint8_t **inp, |
| 296 | long len) { |
| 297 | bssl::UniquePtr<EVP_PKEY> ret(EVP_PKEY_new()); |
| 298 | if (ret == nullptr) { |
| 299 | return nullptr; |
| 300 | } |
| 301 |
| 302 | CBS cbs; |
| 303 | CBS_init(&cbs, *inp, len < 0 ? 0 : (size_t)len); |
| 304 | switch (type) { |
| 305 | case EVP_PKEY_RSA: { |
| 306 | bssl::UniquePtr<RSA> rsa(RSA_parse_public_key(&cbs)); |
| 307 | if (rsa == nullptr) { |
| 308 | return nullptr; |
| 309 | } |
| 310 | EVP_PKEY_assign_RSA(ret.get(), rsa.release()); |
| 311 | break; |
| 312 | } |
| 313 |
| 314 | // Unlike OpenSSL, we do not support EC keys with this API. The raw EC |
| 315 | // public key serialization requires knowing the group. In OpenSSL, calling |
| 316 | // this function with |EVP_PKEY_EC| and setting |out| to nullptr does not |
| 317 | // work. It requires |*out| to include a partially-initialized |EVP_PKEY| to |
| 318 | // extract the group. |
| 319 | default: |
| 320 | OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_PUBLIC_KEY_TYPE); |
| 321 | return nullptr; |
| 322 | } |
| 323 |
| 324 | *inp = CBS_data(&cbs); |
| 325 | if (out != nullptr) { |
| 326 | EVP_PKEY_free(*out); |
| 327 | *out = ret.get(); |
| 328 | } |
| 329 | return ret.release(); |
| 330 | } |
| 331 |
| 332 | EVP_PKEY *d2i_PUBKEY(EVP_PKEY **out, const uint8_t **inp, long len) { |
| 333 | if (len < 0) { |
| 334 | return nullptr; |
| 335 | } |
| 336 | CBS cbs; |
| 337 | CBS_init(&cbs, *inp, (size_t)len); |
| 338 | bssl::UniquePtr<EVP_PKEY> ret(EVP_parse_public_key(&cbs)); |
| 339 | if (ret == nullptr) { |
| 340 | return nullptr; |
| 341 | } |
| 342 | if (out != nullptr) { |
| 343 | EVP_PKEY_free(*out); |
| 344 | *out = ret.get(); |
| 345 | } |
| 346 | *inp = CBS_data(&cbs); |
| 347 | return ret.release(); |
| 348 | } |
| 349 |
| 350 | int i2d_PUBKEY(const EVP_PKEY *pkey, uint8_t **outp) { |
| 351 | if (pkey == NULL) { |
| 352 | return 0; |
| 353 | } |
| 354 |
| 355 | CBB cbb; |
| 356 | if (!CBB_init(&cbb, 128) || |
| 357 | !EVP_marshal_public_key(&cbb, pkey)) { |
| 358 | CBB_cleanup(&cbb); |
| 359 | return -1; |
| 360 | } |
| 361 | return CBB_finish_i2d(&cbb, outp); |
| 362 | } |
| 363 |
| 364 | RSA *d2i_RSA_PUBKEY(RSA **out, const uint8_t **inp, long len) { |
| 365 | if (len < 0) { |
| 366 | return nullptr; |
| 367 | } |
| 368 | CBS cbs; |
| 369 | CBS_init(&cbs, *inp, (size_t)len); |
| 370 | bssl::UniquePtr<EVP_PKEY> pkey(EVP_parse_public_key(&cbs)); |
| 371 | if (pkey == nullptr) { |
| 372 | return nullptr; |
| 373 | } |
| 374 | bssl::UniquePtr<RSA> rsa(EVP_PKEY_get1_RSA(pkey.get())); |
| 375 | if (rsa == nullptr) { |
| 376 | return nullptr; |
| 377 | } |
| 378 | if (out != nullptr) { |
| 379 | RSA_free(*out); |
| 380 | *out = rsa.get(); |
| 381 | } |
| 382 | *inp = CBS_data(&cbs); |
| 383 | return rsa.release(); |
| 384 | } |
| 385 |
| 386 | int i2d_RSA_PUBKEY(const RSA *rsa, uint8_t **outp) { |
| 387 | if (rsa == nullptr) { |
| 388 | return 0; |
| 389 | } |
| 390 |
| 391 | bssl::UniquePtr<EVP_PKEY> pkey(EVP_PKEY_new()); |
| 392 | if (pkey == nullptr || |
| 393 | !EVP_PKEY_set1_RSA(pkey.get(), const_cast<RSA *>(rsa))) { |
| 394 | return -1; |
| 395 | } |
| 396 |
| 397 | return i2d_PUBKEY(pkey.get(), outp); |
| 398 | } |
| 399 |
| 400 | DSA *d2i_DSA_PUBKEY(DSA **out, const uint8_t **inp, long len) { |
| 401 | if (len < 0) { |
| 402 | return nullptr; |
| 403 | } |
| 404 | CBS cbs; |
| 405 | CBS_init(&cbs, *inp, (size_t)len); |
| 406 | bssl::UniquePtr<EVP_PKEY> pkey(EVP_parse_public_key(&cbs)); |
| 407 | if (pkey == nullptr) { |
| 408 | return nullptr; |
| 409 | } |
| 410 | bssl::UniquePtr<DSA> dsa(EVP_PKEY_get1_DSA(pkey.get())); |
| 411 | if (dsa == nullptr) { |
| 412 | return nullptr; |
| 413 | } |
| 414 | if (out != nullptr) { |
| 415 | DSA_free(*out); |
| 416 | *out = dsa.get(); |
| 417 | } |
| 418 | *inp = CBS_data(&cbs); |
| 419 | return dsa.release(); |
| 420 | } |
| 421 |
| 422 | int i2d_DSA_PUBKEY(const DSA *dsa, uint8_t **outp) { |
| 423 | if (dsa == nullptr) { |
| 424 | return 0; |
| 425 | } |
| 426 |
| 427 | bssl::UniquePtr<EVP_PKEY> pkey(EVP_PKEY_new()); |
| 428 | if (pkey == nullptr || |
| 429 | !EVP_PKEY_set1_DSA(pkey.get(), const_cast<DSA *>(dsa))) { |
| 430 | return -1; |
| 431 | } |
| 432 |
| 433 | return i2d_PUBKEY(pkey.get(), outp); |
| 434 | } |
| 435 |
| 436 | EC_KEY *d2i_EC_PUBKEY(EC_KEY **out, const uint8_t **inp, long len) { |
| 437 | if (len < 0) { |
| 438 | return NULL; |
| 439 | } |
| 440 | CBS cbs; |
| 441 | CBS_init(&cbs, *inp, (size_t)len); |
| 442 | EVP_PKEY *pkey = EVP_parse_public_key(&cbs); |
| 443 | if (pkey == NULL) { |
| 444 | return NULL; |
| 445 | } |
| 446 | EC_KEY *ec_key = EVP_PKEY_get1_EC_KEY(pkey); |
| 447 | EVP_PKEY_free(pkey); |
| 448 | if (ec_key == NULL) { |
| 449 | return NULL; |
| 450 | } |
| 451 | if (out != NULL) { |
| 452 | EC_KEY_free(*out); |
| 453 | *out = ec_key; |
| 454 | } |
| 455 | *inp = CBS_data(&cbs); |
| 456 | return ec_key; |
| 457 | } |
| 458 |
| 459 | int i2d_EC_PUBKEY(const EC_KEY *ec_key, uint8_t **outp) { |
| 460 | if (ec_key == NULL) { |
| 461 | return 0; |
| 462 | } |
| 463 |
| 464 | bssl::UniquePtr<EVP_PKEY> pkey(EVP_PKEY_new()); |
| 465 | if (pkey == nullptr || |
| 466 | !EVP_PKEY_set1_EC_KEY(pkey.get(), const_cast<EC_KEY *>(ec_key))) { |
| 467 | return -1; |
| 468 | } |
| 469 |
| 470 | return i2d_PUBKEY(pkey.get(), outp); |
| 471 | } |
| 472 |