| 1 | /* |
| 2 | * Public Key layer for parsing key files and structures |
| 3 | * |
| 4 | * Copyright The Mbed TLS Contributors |
| 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | * not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
| 18 | */ |
| 19 | |
| 20 | #include "common.h" |
| 21 | |
| 22 | #if defined(MBEDTLS_PK_PARSE_C) |
| 23 | |
| 24 | #include "mbedtls/pk.h" |
| 25 | #include "mbedtls/asn1.h" |
| 26 | #include "mbedtls/oid.h" |
| 27 | #include "mbedtls/platform_util.h" |
| 28 | #include "mbedtls/error.h" |
| 29 | |
| 30 | #include <string.h> |
| 31 | |
| 32 | #if defined(MBEDTLS_RSA_C) |
| 33 | #include "mbedtls/rsa.h" |
| 34 | #endif |
| 35 | #if defined(MBEDTLS_ECP_C) |
| 36 | #include "mbedtls/ecp.h" |
| 37 | #endif |
| 38 | #if defined(MBEDTLS_ECDSA_C) |
| 39 | #include "mbedtls/ecdsa.h" |
| 40 | #endif |
| 41 | #if defined(MBEDTLS_PEM_PARSE_C) |
| 42 | #include "mbedtls/pem.h" |
| 43 | #endif |
| 44 | #if defined(MBEDTLS_PKCS5_C) |
| 45 | #include "mbedtls/pkcs5.h" |
| 46 | #endif |
| 47 | #if defined(MBEDTLS_PKCS12_C) |
| 48 | #include "mbedtls/pkcs12.h" |
| 49 | #endif |
| 50 | |
| 51 | #include "mbedtls/platform.h" |
| 52 | |
| 53 | /* Parameter validation macros based on platform_util.h */ |
| 54 | #define PK_VALIDATE_RET(cond) \ |
| 55 | MBEDTLS_INTERNAL_VALIDATE_RET(cond, MBEDTLS_ERR_PK_BAD_INPUT_DATA) |
| 56 | #define PK_VALIDATE(cond) \ |
| 57 | MBEDTLS_INTERNAL_VALIDATE(cond) |
| 58 | |
| 59 | #if defined(MBEDTLS_FS_IO) |
| 60 | /* |
| 61 | * Load all data from a file into a given buffer. |
| 62 | * |
| 63 | * The file is expected to contain either PEM or DER encoded data. |
| 64 | * A terminating null byte is always appended. It is included in the announced |
| 65 | * length only if the data looks like it is PEM encoded. |
| 66 | */ |
| 67 | int mbedtls_pk_load_file(const char *path, unsigned char **buf, size_t *n) |
| 68 | { |
| 69 | FILE *f; |
| 70 | long size; |
| 71 | |
| 72 | PK_VALIDATE_RET(path != NULL); |
| 73 | PK_VALIDATE_RET(buf != NULL); |
| 74 | PK_VALIDATE_RET(n != NULL); |
| 75 | |
| 76 | if ((f = fopen(path, "rb" )) == NULL) { |
| 77 | return MBEDTLS_ERR_PK_FILE_IO_ERROR; |
| 78 | } |
| 79 | |
| 80 | fseek(f, 0, SEEK_END); |
| 81 | if ((size = ftell(f)) == -1) { |
| 82 | fclose(f); |
| 83 | return MBEDTLS_ERR_PK_FILE_IO_ERROR; |
| 84 | } |
| 85 | fseek(f, 0, SEEK_SET); |
| 86 | |
| 87 | *n = (size_t) size; |
| 88 | |
| 89 | if (*n + 1 == 0 || |
| 90 | (*buf = mbedtls_calloc(1, *n + 1)) == NULL) { |
| 91 | fclose(f); |
| 92 | return MBEDTLS_ERR_PK_ALLOC_FAILED; |
| 93 | } |
| 94 | |
| 95 | if (fread(*buf, 1, *n, f) != *n) { |
| 96 | fclose(f); |
| 97 | |
| 98 | mbedtls_platform_zeroize(*buf, *n); |
| 99 | mbedtls_free(*buf); |
| 100 | |
| 101 | return MBEDTLS_ERR_PK_FILE_IO_ERROR; |
| 102 | } |
| 103 | |
| 104 | fclose(f); |
| 105 | |
| 106 | (*buf)[*n] = '\0'; |
| 107 | |
| 108 | if (strstr((const char *) *buf, "-----BEGIN " ) != NULL) { |
| 109 | ++*n; |
| 110 | } |
| 111 | |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | /* |
| 116 | * Load and parse a private key |
| 117 | */ |
| 118 | int mbedtls_pk_parse_keyfile(mbedtls_pk_context *ctx, |
| 119 | const char *path, const char *pwd) |
| 120 | { |
| 121 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 122 | size_t n; |
| 123 | unsigned char *buf; |
| 124 | |
| 125 | PK_VALIDATE_RET(ctx != NULL); |
| 126 | PK_VALIDATE_RET(path != NULL); |
| 127 | |
| 128 | if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) { |
| 129 | return ret; |
| 130 | } |
| 131 | |
| 132 | if (pwd == NULL) { |
| 133 | ret = mbedtls_pk_parse_key(ctx, buf, n, NULL, 0); |
| 134 | } else { |
| 135 | ret = mbedtls_pk_parse_key(ctx, buf, n, |
| 136 | (const unsigned char *) pwd, strlen(pwd)); |
| 137 | } |
| 138 | |
| 139 | mbedtls_platform_zeroize(buf, n); |
| 140 | mbedtls_free(buf); |
| 141 | |
| 142 | return ret; |
| 143 | } |
| 144 | |
| 145 | /* |
| 146 | * Load and parse a public key |
| 147 | */ |
| 148 | int mbedtls_pk_parse_public_keyfile(mbedtls_pk_context *ctx, const char *path) |
| 149 | { |
| 150 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 151 | size_t n; |
| 152 | unsigned char *buf; |
| 153 | |
| 154 | PK_VALIDATE_RET(ctx != NULL); |
| 155 | PK_VALIDATE_RET(path != NULL); |
| 156 | |
| 157 | if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) { |
| 158 | return ret; |
| 159 | } |
| 160 | |
| 161 | ret = mbedtls_pk_parse_public_key(ctx, buf, n); |
| 162 | |
| 163 | mbedtls_platform_zeroize(buf, n); |
| 164 | mbedtls_free(buf); |
| 165 | |
| 166 | return ret; |
| 167 | } |
| 168 | #endif /* MBEDTLS_FS_IO */ |
| 169 | |
| 170 | #if defined(MBEDTLS_ECP_C) |
| 171 | /* Minimally parse an ECParameters buffer to and mbedtls_asn1_buf |
| 172 | * |
| 173 | * ECParameters ::= CHOICE { |
| 174 | * namedCurve OBJECT IDENTIFIER |
| 175 | * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... } |
| 176 | * -- implicitCurve NULL |
| 177 | * } |
| 178 | */ |
| 179 | static int pk_get_ecparams(unsigned char **p, const unsigned char *end, |
| 180 | mbedtls_asn1_buf *params) |
| 181 | { |
| 182 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 183 | |
| 184 | if (end - *p < 1) { |
| 185 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, |
| 186 | MBEDTLS_ERR_ASN1_OUT_OF_DATA); |
| 187 | } |
| 188 | |
| 189 | /* Tag may be either OID or SEQUENCE */ |
| 190 | params->tag = **p; |
| 191 | if (params->tag != MBEDTLS_ASN1_OID |
| 192 | #if defined(MBEDTLS_PK_PARSE_EC_EXTENDED) |
| 193 | && params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE) |
| 194 | #endif |
| 195 | ) { |
| 196 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, |
| 197 | MBEDTLS_ERR_ASN1_UNEXPECTED_TAG); |
| 198 | } |
| 199 | |
| 200 | if ((ret = mbedtls_asn1_get_tag(p, end, ¶ms->len, params->tag)) != 0) { |
| 201 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret); |
| 202 | } |
| 203 | |
| 204 | params->p = *p; |
| 205 | *p += params->len; |
| 206 | |
| 207 | if (*p != end) { |
| 208 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, |
| 209 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
| 210 | } |
| 211 | |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | #if defined(MBEDTLS_PK_PARSE_EC_EXTENDED) |
| 216 | /* |
| 217 | * Parse a SpecifiedECDomain (SEC 1 C.2) and (mostly) fill the group with it. |
| 218 | * WARNING: the resulting group should only be used with |
| 219 | * pk_group_id_from_specified(), since its base point may not be set correctly |
| 220 | * if it was encoded compressed. |
| 221 | * |
| 222 | * SpecifiedECDomain ::= SEQUENCE { |
| 223 | * version SpecifiedECDomainVersion(ecdpVer1 | ecdpVer2 | ecdpVer3, ...), |
| 224 | * fieldID FieldID {{FieldTypes}}, |
| 225 | * curve Curve, |
| 226 | * base ECPoint, |
| 227 | * order INTEGER, |
| 228 | * cofactor INTEGER OPTIONAL, |
| 229 | * hash HashAlgorithm OPTIONAL, |
| 230 | * ... |
| 231 | * } |
| 232 | * |
| 233 | * We only support prime-field as field type, and ignore hash and cofactor. |
| 234 | */ |
| 235 | static int pk_group_from_specified(const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp) |
| 236 | { |
| 237 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 238 | unsigned char *p = params->p; |
| 239 | const unsigned char * const end = params->p + params->len; |
| 240 | const unsigned char *end_field, *end_curve; |
| 241 | size_t len; |
| 242 | int ver; |
| 243 | |
| 244 | /* SpecifiedECDomainVersion ::= INTEGER { 1, 2, 3 } */ |
| 245 | if ((ret = mbedtls_asn1_get_int(&p, end, &ver)) != 0) { |
| 246 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret); |
| 247 | } |
| 248 | |
| 249 | if (ver < 1 || ver > 3) { |
| 250 | return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT; |
| 251 | } |
| 252 | |
| 253 | /* |
| 254 | * FieldID { FIELD-ID:IOSet } ::= SEQUENCE { -- Finite field |
| 255 | * fieldType FIELD-ID.&id({IOSet}), |
| 256 | * parameters FIELD-ID.&Type({IOSet}{@fieldType}) |
| 257 | * } |
| 258 | */ |
| 259 | if ((ret = mbedtls_asn1_get_tag(&p, end, &len, |
| 260 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { |
| 261 | return ret; |
| 262 | } |
| 263 | |
| 264 | end_field = p + len; |
| 265 | |
| 266 | /* |
| 267 | * FIELD-ID ::= TYPE-IDENTIFIER |
| 268 | * FieldTypes FIELD-ID ::= { |
| 269 | * { Prime-p IDENTIFIED BY prime-field } | |
| 270 | * { Characteristic-two IDENTIFIED BY characteristic-two-field } |
| 271 | * } |
| 272 | * prime-field OBJECT IDENTIFIER ::= { id-fieldType 1 } |
| 273 | */ |
| 274 | if ((ret = mbedtls_asn1_get_tag(&p, end_field, &len, MBEDTLS_ASN1_OID)) != 0) { |
| 275 | return ret; |
| 276 | } |
| 277 | |
| 278 | if (len != MBEDTLS_OID_SIZE(MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD) || |
| 279 | memcmp(p, MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD, len) != 0) { |
| 280 | return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE; |
| 281 | } |
| 282 | |
| 283 | p += len; |
| 284 | |
| 285 | /* Prime-p ::= INTEGER -- Field of size p. */ |
| 286 | if ((ret = mbedtls_asn1_get_mpi(&p, end_field, &grp->P)) != 0) { |
| 287 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret); |
| 288 | } |
| 289 | |
| 290 | grp->pbits = mbedtls_mpi_bitlen(&grp->P); |
| 291 | |
| 292 | if (p != end_field) { |
| 293 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, |
| 294 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
| 295 | } |
| 296 | |
| 297 | /* |
| 298 | * Curve ::= SEQUENCE { |
| 299 | * a FieldElement, |
| 300 | * b FieldElement, |
| 301 | * seed BIT STRING OPTIONAL |
| 302 | * -- Shall be present if used in SpecifiedECDomain |
| 303 | * -- with version equal to ecdpVer2 or ecdpVer3 |
| 304 | * } |
| 305 | */ |
| 306 | if ((ret = mbedtls_asn1_get_tag(&p, end, &len, |
| 307 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { |
| 308 | return ret; |
| 309 | } |
| 310 | |
| 311 | end_curve = p + len; |
| 312 | |
| 313 | /* |
| 314 | * FieldElement ::= OCTET STRING |
| 315 | * containing an integer in the case of a prime field |
| 316 | */ |
| 317 | if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 || |
| 318 | (ret = mbedtls_mpi_read_binary(&grp->A, p, len)) != 0) { |
| 319 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret); |
| 320 | } |
| 321 | |
| 322 | p += len; |
| 323 | |
| 324 | if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 || |
| 325 | (ret = mbedtls_mpi_read_binary(&grp->B, p, len)) != 0) { |
| 326 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret); |
| 327 | } |
| 328 | |
| 329 | p += len; |
| 330 | |
| 331 | /* Ignore seed BIT STRING OPTIONAL */ |
| 332 | if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_BIT_STRING)) == 0) { |
| 333 | p += len; |
| 334 | } |
| 335 | |
| 336 | if (p != end_curve) { |
| 337 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, |
| 338 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
| 339 | } |
| 340 | |
| 341 | /* |
| 342 | * ECPoint ::= OCTET STRING |
| 343 | */ |
| 344 | if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) { |
| 345 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret); |
| 346 | } |
| 347 | |
| 348 | if ((ret = mbedtls_ecp_point_read_binary(grp, &grp->G, |
| 349 | (const unsigned char *) p, len)) != 0) { |
| 350 | /* |
| 351 | * If we can't read the point because it's compressed, cheat by |
| 352 | * reading only the X coordinate and the parity bit of Y. |
| 353 | */ |
| 354 | if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE || |
| 355 | (p[0] != 0x02 && p[0] != 0x03) || |
| 356 | len != mbedtls_mpi_size(&grp->P) + 1 || |
| 357 | mbedtls_mpi_read_binary(&grp->G.X, p + 1, len - 1) != 0 || |
| 358 | mbedtls_mpi_lset(&grp->G.Y, p[0] - 2) != 0 || |
| 359 | mbedtls_mpi_lset(&grp->G.Z, 1) != 0) { |
| 360 | return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT; |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | p += len; |
| 365 | |
| 366 | /* |
| 367 | * order INTEGER |
| 368 | */ |
| 369 | if ((ret = mbedtls_asn1_get_mpi(&p, end, &grp->N)) != 0) { |
| 370 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret); |
| 371 | } |
| 372 | |
| 373 | grp->nbits = mbedtls_mpi_bitlen(&grp->N); |
| 374 | |
| 375 | /* |
| 376 | * Allow optional elements by purposefully not enforcing p == end here. |
| 377 | */ |
| 378 | |
| 379 | return 0; |
| 380 | } |
| 381 | |
| 382 | /* |
| 383 | * Find the group id associated with an (almost filled) group as generated by |
| 384 | * pk_group_from_specified(), or return an error if unknown. |
| 385 | */ |
| 386 | static int pk_group_id_from_group(const mbedtls_ecp_group *grp, mbedtls_ecp_group_id *grp_id) |
| 387 | { |
| 388 | int ret = 0; |
| 389 | mbedtls_ecp_group ref; |
| 390 | const mbedtls_ecp_group_id *id; |
| 391 | |
| 392 | mbedtls_ecp_group_init(&ref); |
| 393 | |
| 394 | for (id = mbedtls_ecp_grp_id_list(); *id != MBEDTLS_ECP_DP_NONE; id++) { |
| 395 | /* Load the group associated to that id */ |
| 396 | mbedtls_ecp_group_free(&ref); |
| 397 | MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&ref, *id)); |
| 398 | |
| 399 | /* Compare to the group we were given, starting with easy tests */ |
| 400 | if (grp->pbits == ref.pbits && grp->nbits == ref.nbits && |
| 401 | mbedtls_mpi_cmp_mpi(&grp->P, &ref.P) == 0 && |
| 402 | mbedtls_mpi_cmp_mpi(&grp->A, &ref.A) == 0 && |
| 403 | mbedtls_mpi_cmp_mpi(&grp->B, &ref.B) == 0 && |
| 404 | mbedtls_mpi_cmp_mpi(&grp->N, &ref.N) == 0 && |
| 405 | mbedtls_mpi_cmp_mpi(&grp->G.X, &ref.G.X) == 0 && |
| 406 | mbedtls_mpi_cmp_mpi(&grp->G.Z, &ref.G.Z) == 0 && |
| 407 | /* For Y we may only know the parity bit, so compare only that */ |
| 408 | mbedtls_mpi_get_bit(&grp->G.Y, 0) == mbedtls_mpi_get_bit(&ref.G.Y, 0)) { |
| 409 | break; |
| 410 | } |
| 411 | |
| 412 | } |
| 413 | |
| 414 | cleanup: |
| 415 | mbedtls_ecp_group_free(&ref); |
| 416 | |
| 417 | *grp_id = *id; |
| 418 | |
| 419 | if (ret == 0 && *id == MBEDTLS_ECP_DP_NONE) { |
| 420 | ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE; |
| 421 | } |
| 422 | |
| 423 | return ret; |
| 424 | } |
| 425 | |
| 426 | /* |
| 427 | * Parse a SpecifiedECDomain (SEC 1 C.2) and find the associated group ID |
| 428 | */ |
| 429 | static int pk_group_id_from_specified(const mbedtls_asn1_buf *params, |
| 430 | mbedtls_ecp_group_id *grp_id) |
| 431 | { |
| 432 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 433 | mbedtls_ecp_group grp; |
| 434 | |
| 435 | mbedtls_ecp_group_init(&grp); |
| 436 | |
| 437 | if ((ret = pk_group_from_specified(params, &grp)) != 0) { |
| 438 | goto cleanup; |
| 439 | } |
| 440 | |
| 441 | ret = pk_group_id_from_group(&grp, grp_id); |
| 442 | |
| 443 | cleanup: |
| 444 | mbedtls_ecp_group_free(&grp); |
| 445 | |
| 446 | return ret; |
| 447 | } |
| 448 | #endif /* MBEDTLS_PK_PARSE_EC_EXTENDED */ |
| 449 | |
| 450 | /* |
| 451 | * Use EC parameters to initialise an EC group |
| 452 | * |
| 453 | * ECParameters ::= CHOICE { |
| 454 | * namedCurve OBJECT IDENTIFIER |
| 455 | * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... } |
| 456 | * -- implicitCurve NULL |
| 457 | */ |
| 458 | static int pk_use_ecparams(const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp) |
| 459 | { |
| 460 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 461 | mbedtls_ecp_group_id grp_id; |
| 462 | |
| 463 | if (params->tag == MBEDTLS_ASN1_OID) { |
| 464 | if (mbedtls_oid_get_ec_grp(params, &grp_id) != 0) { |
| 465 | return MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE; |
| 466 | } |
| 467 | } else { |
| 468 | #if defined(MBEDTLS_PK_PARSE_EC_EXTENDED) |
| 469 | if ((ret = pk_group_id_from_specified(params, &grp_id)) != 0) { |
| 470 | return ret; |
| 471 | } |
| 472 | #else |
| 473 | return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT; |
| 474 | #endif |
| 475 | } |
| 476 | |
| 477 | /* |
| 478 | * grp may already be initialized; if so, make sure IDs match |
| 479 | */ |
| 480 | if (grp->id != MBEDTLS_ECP_DP_NONE && grp->id != grp_id) { |
| 481 | return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT; |
| 482 | } |
| 483 | |
| 484 | if ((ret = mbedtls_ecp_group_load(grp, grp_id)) != 0) { |
| 485 | return ret; |
| 486 | } |
| 487 | |
| 488 | return 0; |
| 489 | } |
| 490 | |
| 491 | /* |
| 492 | * EC public key is an EC point |
| 493 | * |
| 494 | * The caller is responsible for clearing the structure upon failure if |
| 495 | * desired. Take care to pass along the possible ECP_FEATURE_UNAVAILABLE |
| 496 | * return code of mbedtls_ecp_point_read_binary() and leave p in a usable state. |
| 497 | */ |
| 498 | static int pk_get_ecpubkey(unsigned char **p, const unsigned char *end, |
| 499 | mbedtls_ecp_keypair *key) |
| 500 | { |
| 501 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 502 | |
| 503 | if ((ret = mbedtls_ecp_point_read_binary(&key->grp, &key->Q, |
| 504 | (const unsigned char *) *p, end - *p)) == 0) { |
| 505 | ret = mbedtls_ecp_check_pubkey(&key->grp, &key->Q); |
| 506 | } |
| 507 | |
| 508 | /* |
| 509 | * We know mbedtls_ecp_point_read_binary consumed all bytes or failed |
| 510 | */ |
| 511 | *p = (unsigned char *) end; |
| 512 | |
| 513 | return ret; |
| 514 | } |
| 515 | #endif /* MBEDTLS_ECP_C */ |
| 516 | |
| 517 | #if defined(MBEDTLS_RSA_C) |
| 518 | /* |
| 519 | * RSAPublicKey ::= SEQUENCE { |
| 520 | * modulus INTEGER, -- n |
| 521 | * publicExponent INTEGER -- e |
| 522 | * } |
| 523 | */ |
| 524 | static int pk_get_rsapubkey(unsigned char **p, |
| 525 | const unsigned char *end, |
| 526 | mbedtls_rsa_context *rsa) |
| 527 | { |
| 528 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 529 | size_t len; |
| 530 | |
| 531 | if ((ret = mbedtls_asn1_get_tag(p, end, &len, |
| 532 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { |
| 533 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret); |
| 534 | } |
| 535 | |
| 536 | if (*p + len != end) { |
| 537 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, |
| 538 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
| 539 | } |
| 540 | |
| 541 | /* Import N */ |
| 542 | if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) { |
| 543 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret); |
| 544 | } |
| 545 | |
| 546 | if ((ret = mbedtls_rsa_import_raw(rsa, *p, len, NULL, 0, NULL, 0, |
| 547 | NULL, 0, NULL, 0)) != 0) { |
| 548 | return MBEDTLS_ERR_PK_INVALID_PUBKEY; |
| 549 | } |
| 550 | |
| 551 | *p += len; |
| 552 | |
| 553 | /* Import E */ |
| 554 | if ((ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_INTEGER)) != 0) { |
| 555 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret); |
| 556 | } |
| 557 | |
| 558 | if ((ret = mbedtls_rsa_import_raw(rsa, NULL, 0, NULL, 0, NULL, 0, |
| 559 | NULL, 0, *p, len)) != 0) { |
| 560 | return MBEDTLS_ERR_PK_INVALID_PUBKEY; |
| 561 | } |
| 562 | |
| 563 | *p += len; |
| 564 | |
| 565 | if (mbedtls_rsa_complete(rsa) != 0 || |
| 566 | mbedtls_rsa_check_pubkey(rsa) != 0) { |
| 567 | return MBEDTLS_ERR_PK_INVALID_PUBKEY; |
| 568 | } |
| 569 | |
| 570 | if (*p != end) { |
| 571 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, |
| 572 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
| 573 | } |
| 574 | |
| 575 | return 0; |
| 576 | } |
| 577 | #endif /* MBEDTLS_RSA_C */ |
| 578 | |
| 579 | /* Get a PK algorithm identifier |
| 580 | * |
| 581 | * AlgorithmIdentifier ::= SEQUENCE { |
| 582 | * algorithm OBJECT IDENTIFIER, |
| 583 | * parameters ANY DEFINED BY algorithm OPTIONAL } |
| 584 | */ |
| 585 | static int pk_get_pk_alg(unsigned char **p, |
| 586 | const unsigned char *end, |
| 587 | mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params) |
| 588 | { |
| 589 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 590 | mbedtls_asn1_buf alg_oid; |
| 591 | |
| 592 | memset(params, 0, sizeof(mbedtls_asn1_buf)); |
| 593 | |
| 594 | if ((ret = mbedtls_asn1_get_alg(p, end, &alg_oid, params)) != 0) { |
| 595 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_ALG, ret); |
| 596 | } |
| 597 | |
| 598 | if (mbedtls_oid_get_pk_alg(&alg_oid, pk_alg) != 0) { |
| 599 | return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG; |
| 600 | } |
| 601 | |
| 602 | /* |
| 603 | * No parameters with RSA (only for EC) |
| 604 | */ |
| 605 | if (*pk_alg == MBEDTLS_PK_RSA && |
| 606 | ((params->tag != MBEDTLS_ASN1_NULL && params->tag != 0) || |
| 607 | params->len != 0)) { |
| 608 | return MBEDTLS_ERR_PK_INVALID_ALG; |
| 609 | } |
| 610 | |
| 611 | return 0; |
| 612 | } |
| 613 | |
| 614 | /* |
| 615 | * SubjectPublicKeyInfo ::= SEQUENCE { |
| 616 | * algorithm AlgorithmIdentifier, |
| 617 | * subjectPublicKey BIT STRING } |
| 618 | */ |
| 619 | int mbedtls_pk_parse_subpubkey(unsigned char **p, const unsigned char *end, |
| 620 | mbedtls_pk_context *pk) |
| 621 | { |
| 622 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 623 | size_t len; |
| 624 | mbedtls_asn1_buf alg_params; |
| 625 | mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE; |
| 626 | const mbedtls_pk_info_t *pk_info; |
| 627 | |
| 628 | PK_VALIDATE_RET(p != NULL); |
| 629 | PK_VALIDATE_RET(*p != NULL); |
| 630 | PK_VALIDATE_RET(end != NULL); |
| 631 | PK_VALIDATE_RET(pk != NULL); |
| 632 | |
| 633 | if ((ret = mbedtls_asn1_get_tag(p, end, &len, |
| 634 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { |
| 635 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret); |
| 636 | } |
| 637 | |
| 638 | end = *p + len; |
| 639 | |
| 640 | if ((ret = pk_get_pk_alg(p, end, &pk_alg, &alg_params)) != 0) { |
| 641 | return ret; |
| 642 | } |
| 643 | |
| 644 | if ((ret = mbedtls_asn1_get_bitstring_null(p, end, &len)) != 0) { |
| 645 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, ret); |
| 646 | } |
| 647 | |
| 648 | if (*p + len != end) { |
| 649 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, |
| 650 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
| 651 | } |
| 652 | |
| 653 | if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) { |
| 654 | return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG; |
| 655 | } |
| 656 | |
| 657 | if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) { |
| 658 | return ret; |
| 659 | } |
| 660 | |
| 661 | #if defined(MBEDTLS_RSA_C) |
| 662 | if (pk_alg == MBEDTLS_PK_RSA) { |
| 663 | ret = pk_get_rsapubkey(p, end, mbedtls_pk_rsa(*pk)); |
| 664 | } else |
| 665 | #endif /* MBEDTLS_RSA_C */ |
| 666 | #if defined(MBEDTLS_ECP_C) |
| 667 | if (pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY) { |
| 668 | ret = pk_use_ecparams(&alg_params, &mbedtls_pk_ec(*pk)->grp); |
| 669 | if (ret == 0) { |
| 670 | ret = pk_get_ecpubkey(p, end, mbedtls_pk_ec(*pk)); |
| 671 | } |
| 672 | } else |
| 673 | #endif /* MBEDTLS_ECP_C */ |
| 674 | ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG; |
| 675 | |
| 676 | if (ret == 0 && *p != end) { |
| 677 | ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, |
| 678 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
| 679 | } |
| 680 | |
| 681 | if (ret != 0) { |
| 682 | mbedtls_pk_free(pk); |
| 683 | } |
| 684 | |
| 685 | return ret; |
| 686 | } |
| 687 | |
| 688 | #if defined(MBEDTLS_RSA_C) |
| 689 | /* |
| 690 | * Wrapper around mbedtls_asn1_get_mpi() that rejects zero. |
| 691 | * |
| 692 | * The value zero is: |
| 693 | * - never a valid value for an RSA parameter |
| 694 | * - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete(). |
| 695 | * |
| 696 | * Since values can't be omitted in PKCS#1, passing a zero value to |
| 697 | * rsa_complete() would be incorrect, so reject zero values early. |
| 698 | */ |
| 699 | static int asn1_get_nonzero_mpi(unsigned char **p, |
| 700 | const unsigned char *end, |
| 701 | mbedtls_mpi *X) |
| 702 | { |
| 703 | int ret; |
| 704 | |
| 705 | ret = mbedtls_asn1_get_mpi(p, end, X); |
| 706 | if (ret != 0) { |
| 707 | return ret; |
| 708 | } |
| 709 | |
| 710 | if (mbedtls_mpi_cmp_int(X, 0) == 0) { |
| 711 | return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT; |
| 712 | } |
| 713 | |
| 714 | return 0; |
| 715 | } |
| 716 | |
| 717 | /* |
| 718 | * Parse a PKCS#1 encoded private RSA key |
| 719 | */ |
| 720 | static int pk_parse_key_pkcs1_der(mbedtls_rsa_context *rsa, |
| 721 | const unsigned char *key, |
| 722 | size_t keylen) |
| 723 | { |
| 724 | int ret, version; |
| 725 | size_t len; |
| 726 | unsigned char *p, *end; |
| 727 | |
| 728 | mbedtls_mpi T; |
| 729 | mbedtls_mpi_init(&T); |
| 730 | |
| 731 | p = (unsigned char *) key; |
| 732 | end = p + keylen; |
| 733 | |
| 734 | /* |
| 735 | * This function parses the RSAPrivateKey (PKCS#1) |
| 736 | * |
| 737 | * RSAPrivateKey ::= SEQUENCE { |
| 738 | * version Version, |
| 739 | * modulus INTEGER, -- n |
| 740 | * publicExponent INTEGER, -- e |
| 741 | * privateExponent INTEGER, -- d |
| 742 | * prime1 INTEGER, -- p |
| 743 | * prime2 INTEGER, -- q |
| 744 | * exponent1 INTEGER, -- d mod (p-1) |
| 745 | * exponent2 INTEGER, -- d mod (q-1) |
| 746 | * coefficient INTEGER, -- (inverse of q) mod p |
| 747 | * otherPrimeInfos OtherPrimeInfos OPTIONAL |
| 748 | * } |
| 749 | */ |
| 750 | if ((ret = mbedtls_asn1_get_tag(&p, end, &len, |
| 751 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { |
| 752 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret); |
| 753 | } |
| 754 | |
| 755 | end = p + len; |
| 756 | |
| 757 | if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) { |
| 758 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret); |
| 759 | } |
| 760 | |
| 761 | if (version != 0) { |
| 762 | return MBEDTLS_ERR_PK_KEY_INVALID_VERSION; |
| 763 | } |
| 764 | |
| 765 | /* Import N */ |
| 766 | if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 || |
| 767 | (ret = mbedtls_rsa_import(rsa, &T, NULL, NULL, |
| 768 | NULL, NULL)) != 0) { |
| 769 | goto cleanup; |
| 770 | } |
| 771 | |
| 772 | /* Import E */ |
| 773 | if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 || |
| 774 | (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL, |
| 775 | NULL, &T)) != 0) { |
| 776 | goto cleanup; |
| 777 | } |
| 778 | |
| 779 | /* Import D */ |
| 780 | if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 || |
| 781 | (ret = mbedtls_rsa_import(rsa, NULL, NULL, NULL, |
| 782 | &T, NULL)) != 0) { |
| 783 | goto cleanup; |
| 784 | } |
| 785 | |
| 786 | /* Import P */ |
| 787 | if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 || |
| 788 | (ret = mbedtls_rsa_import(rsa, NULL, &T, NULL, |
| 789 | NULL, NULL)) != 0) { |
| 790 | goto cleanup; |
| 791 | } |
| 792 | |
| 793 | /* Import Q */ |
| 794 | if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 || |
| 795 | (ret = mbedtls_rsa_import(rsa, NULL, NULL, &T, |
| 796 | NULL, NULL)) != 0) { |
| 797 | goto cleanup; |
| 798 | } |
| 799 | |
| 800 | #if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT) |
| 801 | /* |
| 802 | * The RSA CRT parameters DP, DQ and QP are nominally redundant, in |
| 803 | * that they can be easily recomputed from D, P and Q. However by |
| 804 | * parsing them from the PKCS1 structure it is possible to avoid |
| 805 | * recalculating them which both reduces the overhead of loading |
| 806 | * RSA private keys into memory and also avoids side channels which |
| 807 | * can arise when computing those values, since all of D, P, and Q |
| 808 | * are secret. See https://eprint.iacr.org/2020/055 for a |
| 809 | * description of one such attack. |
| 810 | */ |
| 811 | |
| 812 | /* Import DP */ |
| 813 | if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 || |
| 814 | (ret = mbedtls_mpi_copy(&rsa->DP, &T)) != 0) { |
| 815 | goto cleanup; |
| 816 | } |
| 817 | |
| 818 | /* Import DQ */ |
| 819 | if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 || |
| 820 | (ret = mbedtls_mpi_copy(&rsa->DQ, &T)) != 0) { |
| 821 | goto cleanup; |
| 822 | } |
| 823 | |
| 824 | /* Import QP */ |
| 825 | if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 || |
| 826 | (ret = mbedtls_mpi_copy(&rsa->QP, &T)) != 0) { |
| 827 | goto cleanup; |
| 828 | } |
| 829 | |
| 830 | #else |
| 831 | /* Verify existence of the CRT params */ |
| 832 | if ((ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 || |
| 833 | (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0 || |
| 834 | (ret = asn1_get_nonzero_mpi(&p, end, &T)) != 0) { |
| 835 | goto cleanup; |
| 836 | } |
| 837 | #endif |
| 838 | |
| 839 | /* rsa_complete() doesn't complete anything with the default |
| 840 | * implementation but is still called: |
| 841 | * - for the benefit of alternative implementation that may want to |
| 842 | * pre-compute stuff beyond what's provided (eg Montgomery factors) |
| 843 | * - as is also sanity-checks the key |
| 844 | * |
| 845 | * Furthermore, we also check the public part for consistency with |
| 846 | * mbedtls_pk_parse_pubkey(), as it includes size minima for example. |
| 847 | */ |
| 848 | if ((ret = mbedtls_rsa_complete(rsa)) != 0 || |
| 849 | (ret = mbedtls_rsa_check_pubkey(rsa)) != 0) { |
| 850 | goto cleanup; |
| 851 | } |
| 852 | |
| 853 | if (p != end) { |
| 854 | ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, |
| 855 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
| 856 | } |
| 857 | |
| 858 | cleanup: |
| 859 | |
| 860 | mbedtls_mpi_free(&T); |
| 861 | |
| 862 | if (ret != 0) { |
| 863 | /* Wrap error code if it's coming from a lower level */ |
| 864 | if ((ret & 0xff80) == 0) { |
| 865 | ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret); |
| 866 | } else { |
| 867 | ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT; |
| 868 | } |
| 869 | |
| 870 | mbedtls_rsa_free(rsa); |
| 871 | } |
| 872 | |
| 873 | return ret; |
| 874 | } |
| 875 | #endif /* MBEDTLS_RSA_C */ |
| 876 | |
| 877 | #if defined(MBEDTLS_ECP_C) |
| 878 | /* |
| 879 | * Parse a SEC1 encoded private EC key |
| 880 | */ |
| 881 | static int pk_parse_key_sec1_der(mbedtls_ecp_keypair *eck, |
| 882 | const unsigned char *key, |
| 883 | size_t keylen) |
| 884 | { |
| 885 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 886 | int version, pubkey_done; |
| 887 | size_t len; |
| 888 | mbedtls_asn1_buf params; |
| 889 | unsigned char *p = (unsigned char *) key; |
| 890 | unsigned char *end = p + keylen; |
| 891 | unsigned char *end2; |
| 892 | |
| 893 | /* |
| 894 | * RFC 5915, or SEC1 Appendix C.4 |
| 895 | * |
| 896 | * ECPrivateKey ::= SEQUENCE { |
| 897 | * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1), |
| 898 | * privateKey OCTET STRING, |
| 899 | * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL, |
| 900 | * publicKey [1] BIT STRING OPTIONAL |
| 901 | * } |
| 902 | */ |
| 903 | if ((ret = mbedtls_asn1_get_tag(&p, end, &len, |
| 904 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { |
| 905 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret); |
| 906 | } |
| 907 | |
| 908 | end = p + len; |
| 909 | |
| 910 | if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) { |
| 911 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret); |
| 912 | } |
| 913 | |
| 914 | if (version != 1) { |
| 915 | return MBEDTLS_ERR_PK_KEY_INVALID_VERSION; |
| 916 | } |
| 917 | |
| 918 | if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) { |
| 919 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret); |
| 920 | } |
| 921 | |
| 922 | if ((ret = mbedtls_mpi_read_binary(&eck->d, p, len)) != 0) { |
| 923 | mbedtls_ecp_keypair_free(eck); |
| 924 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret); |
| 925 | } |
| 926 | |
| 927 | p += len; |
| 928 | |
| 929 | pubkey_done = 0; |
| 930 | if (p != end) { |
| 931 | /* |
| 932 | * Is 'parameters' present? |
| 933 | */ |
| 934 | if ((ret = mbedtls_asn1_get_tag(&p, end, &len, |
| 935 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | |
| 936 | 0)) == 0) { |
| 937 | if ((ret = pk_get_ecparams(&p, p + len, ¶ms)) != 0 || |
| 938 | (ret = pk_use_ecparams(¶ms, &eck->grp)) != 0) { |
| 939 | mbedtls_ecp_keypair_free(eck); |
| 940 | return ret; |
| 941 | } |
| 942 | } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) { |
| 943 | mbedtls_ecp_keypair_free(eck); |
| 944 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret); |
| 945 | } |
| 946 | } |
| 947 | |
| 948 | if (p != end) { |
| 949 | /* |
| 950 | * Is 'publickey' present? If not, or if we can't read it (eg because it |
| 951 | * is compressed), create it from the private key. |
| 952 | */ |
| 953 | if ((ret = mbedtls_asn1_get_tag(&p, end, &len, |
| 954 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | |
| 955 | 1)) == 0) { |
| 956 | end2 = p + len; |
| 957 | |
| 958 | if ((ret = mbedtls_asn1_get_bitstring_null(&p, end2, &len)) != 0) { |
| 959 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret); |
| 960 | } |
| 961 | |
| 962 | if (p + len != end2) { |
| 963 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, |
| 964 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
| 965 | } |
| 966 | |
| 967 | if ((ret = pk_get_ecpubkey(&p, end2, eck)) == 0) { |
| 968 | pubkey_done = 1; |
| 969 | } else { |
| 970 | /* |
| 971 | * The only acceptable failure mode of pk_get_ecpubkey() above |
| 972 | * is if the point format is not recognized. |
| 973 | */ |
| 974 | if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE) { |
| 975 | return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT; |
| 976 | } |
| 977 | } |
| 978 | } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) { |
| 979 | mbedtls_ecp_keypair_free(eck); |
| 980 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret); |
| 981 | } |
| 982 | } |
| 983 | |
| 984 | if (!pubkey_done && |
| 985 | (ret = mbedtls_ecp_mul(&eck->grp, &eck->Q, &eck->d, &eck->grp.G, |
| 986 | NULL, NULL)) != 0) { |
| 987 | mbedtls_ecp_keypair_free(eck); |
| 988 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret); |
| 989 | } |
| 990 | |
| 991 | if ((ret = mbedtls_ecp_check_privkey(&eck->grp, &eck->d)) != 0) { |
| 992 | mbedtls_ecp_keypair_free(eck); |
| 993 | return ret; |
| 994 | } |
| 995 | |
| 996 | return 0; |
| 997 | } |
| 998 | #endif /* MBEDTLS_ECP_C */ |
| 999 | |
| 1000 | /* |
| 1001 | * Parse an unencrypted PKCS#8 encoded private key |
| 1002 | * |
| 1003 | * Notes: |
| 1004 | * |
| 1005 | * - This function does not own the key buffer. It is the |
| 1006 | * responsibility of the caller to take care of zeroizing |
| 1007 | * and freeing it after use. |
| 1008 | * |
| 1009 | * - The function is responsible for freeing the provided |
| 1010 | * PK context on failure. |
| 1011 | * |
| 1012 | */ |
| 1013 | static int pk_parse_key_pkcs8_unencrypted_der( |
| 1014 | mbedtls_pk_context *pk, |
| 1015 | const unsigned char *key, |
| 1016 | size_t keylen) |
| 1017 | { |
| 1018 | int ret, version; |
| 1019 | size_t len; |
| 1020 | mbedtls_asn1_buf params; |
| 1021 | unsigned char *p = (unsigned char *) key; |
| 1022 | unsigned char *end = p + keylen; |
| 1023 | mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE; |
| 1024 | const mbedtls_pk_info_t *pk_info; |
| 1025 | |
| 1026 | /* |
| 1027 | * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208) |
| 1028 | * |
| 1029 | * PrivateKeyInfo ::= SEQUENCE { |
| 1030 | * version Version, |
| 1031 | * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier, |
| 1032 | * privateKey PrivateKey, |
| 1033 | * attributes [0] IMPLICIT Attributes OPTIONAL } |
| 1034 | * |
| 1035 | * Version ::= INTEGER |
| 1036 | * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier |
| 1037 | * PrivateKey ::= OCTET STRING |
| 1038 | * |
| 1039 | * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey |
| 1040 | */ |
| 1041 | |
| 1042 | if ((ret = mbedtls_asn1_get_tag(&p, end, &len, |
| 1043 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { |
| 1044 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret); |
| 1045 | } |
| 1046 | |
| 1047 | end = p + len; |
| 1048 | |
| 1049 | if ((ret = mbedtls_asn1_get_int(&p, end, &version)) != 0) { |
| 1050 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret); |
| 1051 | } |
| 1052 | |
| 1053 | if (version != 0) { |
| 1054 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_VERSION, ret); |
| 1055 | } |
| 1056 | |
| 1057 | if ((ret = pk_get_pk_alg(&p, end, &pk_alg, ¶ms)) != 0) { |
| 1058 | return ret; |
| 1059 | } |
| 1060 | |
| 1061 | if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) { |
| 1062 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret); |
| 1063 | } |
| 1064 | |
| 1065 | if (len < 1) { |
| 1066 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, |
| 1067 | MBEDTLS_ERR_ASN1_OUT_OF_DATA); |
| 1068 | } |
| 1069 | |
| 1070 | if ((pk_info = mbedtls_pk_info_from_type(pk_alg)) == NULL) { |
| 1071 | return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG; |
| 1072 | } |
| 1073 | |
| 1074 | if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0) { |
| 1075 | return ret; |
| 1076 | } |
| 1077 | |
| 1078 | #if defined(MBEDTLS_RSA_C) |
| 1079 | if (pk_alg == MBEDTLS_PK_RSA) { |
| 1080 | if ((ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), p, len)) != 0) { |
| 1081 | mbedtls_pk_free(pk); |
| 1082 | return ret; |
| 1083 | } |
| 1084 | } else |
| 1085 | #endif /* MBEDTLS_RSA_C */ |
| 1086 | #if defined(MBEDTLS_ECP_C) |
| 1087 | if (pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH) { |
| 1088 | if ((ret = pk_use_ecparams(¶ms, &mbedtls_pk_ec(*pk)->grp)) != 0 || |
| 1089 | (ret = pk_parse_key_sec1_der(mbedtls_pk_ec(*pk), p, len)) != 0) { |
| 1090 | mbedtls_pk_free(pk); |
| 1091 | return ret; |
| 1092 | } |
| 1093 | } else |
| 1094 | #endif /* MBEDTLS_ECP_C */ |
| 1095 | return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG; |
| 1096 | |
| 1097 | return 0; |
| 1098 | } |
| 1099 | |
| 1100 | /* |
| 1101 | * Parse an encrypted PKCS#8 encoded private key |
| 1102 | * |
| 1103 | * To save space, the decryption happens in-place on the given key buffer. |
| 1104 | * Also, while this function may modify the keybuffer, it doesn't own it, |
| 1105 | * and instead it is the responsibility of the caller to zeroize and properly |
| 1106 | * free it after use. |
| 1107 | * |
| 1108 | */ |
| 1109 | #if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C) |
| 1110 | static int pk_parse_key_pkcs8_encrypted_der( |
| 1111 | mbedtls_pk_context *pk, |
| 1112 | unsigned char *key, size_t keylen, |
| 1113 | const unsigned char *pwd, size_t pwdlen) |
| 1114 | { |
| 1115 | int ret, decrypted = 0; |
| 1116 | size_t len; |
| 1117 | unsigned char *buf; |
| 1118 | unsigned char *p, *end; |
| 1119 | mbedtls_asn1_buf pbe_alg_oid, pbe_params; |
| 1120 | #if defined(MBEDTLS_PKCS12_C) |
| 1121 | mbedtls_cipher_type_t cipher_alg; |
| 1122 | mbedtls_md_type_t md_alg; |
| 1123 | #endif |
| 1124 | |
| 1125 | p = key; |
| 1126 | end = p + keylen; |
| 1127 | |
| 1128 | if (pwdlen == 0) { |
| 1129 | return MBEDTLS_ERR_PK_PASSWORD_REQUIRED; |
| 1130 | } |
| 1131 | |
| 1132 | /* |
| 1133 | * This function parses the EncryptedPrivateKeyInfo object (PKCS#8) |
| 1134 | * |
| 1135 | * EncryptedPrivateKeyInfo ::= SEQUENCE { |
| 1136 | * encryptionAlgorithm EncryptionAlgorithmIdentifier, |
| 1137 | * encryptedData EncryptedData |
| 1138 | * } |
| 1139 | * |
| 1140 | * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier |
| 1141 | * |
| 1142 | * EncryptedData ::= OCTET STRING |
| 1143 | * |
| 1144 | * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo |
| 1145 | * |
| 1146 | */ |
| 1147 | if ((ret = mbedtls_asn1_get_tag(&p, end, &len, |
| 1148 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { |
| 1149 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret); |
| 1150 | } |
| 1151 | |
| 1152 | end = p + len; |
| 1153 | |
| 1154 | if ((ret = mbedtls_asn1_get_alg(&p, end, &pbe_alg_oid, &pbe_params)) != 0) { |
| 1155 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret); |
| 1156 | } |
| 1157 | |
| 1158 | if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0) { |
| 1159 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret); |
| 1160 | } |
| 1161 | |
| 1162 | buf = p; |
| 1163 | |
| 1164 | /* |
| 1165 | * Decrypt EncryptedData with appropriate PBE |
| 1166 | */ |
| 1167 | #if defined(MBEDTLS_PKCS12_C) |
| 1168 | if (mbedtls_oid_get_pkcs12_pbe_alg(&pbe_alg_oid, &md_alg, &cipher_alg) == 0) { |
| 1169 | if ((ret = mbedtls_pkcs12_pbe(&pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT, |
| 1170 | cipher_alg, md_alg, |
| 1171 | pwd, pwdlen, p, len, buf)) != 0) { |
| 1172 | if (ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH) { |
| 1173 | return MBEDTLS_ERR_PK_PASSWORD_MISMATCH; |
| 1174 | } |
| 1175 | |
| 1176 | return ret; |
| 1177 | } |
| 1178 | |
| 1179 | decrypted = 1; |
| 1180 | } else if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid) == 0) { |
| 1181 | if ((ret = mbedtls_pkcs12_pbe_sha1_rc4_128(&pbe_params, |
| 1182 | MBEDTLS_PKCS12_PBE_DECRYPT, |
| 1183 | pwd, pwdlen, |
| 1184 | p, len, buf)) != 0) { |
| 1185 | return ret; |
| 1186 | } |
| 1187 | |
| 1188 | // Best guess for password mismatch when using RC4. If first tag is |
| 1189 | // not MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE |
| 1190 | // |
| 1191 | if (*buf != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) { |
| 1192 | return MBEDTLS_ERR_PK_PASSWORD_MISMATCH; |
| 1193 | } |
| 1194 | |
| 1195 | decrypted = 1; |
| 1196 | } else |
| 1197 | #endif /* MBEDTLS_PKCS12_C */ |
| 1198 | #if defined(MBEDTLS_PKCS5_C) |
| 1199 | if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid) == 0) { |
| 1200 | if ((ret = mbedtls_pkcs5_pbes2(&pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen, |
| 1201 | p, len, buf)) != 0) { |
| 1202 | if (ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH) { |
| 1203 | return MBEDTLS_ERR_PK_PASSWORD_MISMATCH; |
| 1204 | } |
| 1205 | |
| 1206 | return ret; |
| 1207 | } |
| 1208 | |
| 1209 | decrypted = 1; |
| 1210 | } else |
| 1211 | #endif /* MBEDTLS_PKCS5_C */ |
| 1212 | { |
| 1213 | ((void) pwd); |
| 1214 | } |
| 1215 | |
| 1216 | if (decrypted == 0) { |
| 1217 | return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE; |
| 1218 | } |
| 1219 | |
| 1220 | return pk_parse_key_pkcs8_unencrypted_der(pk, buf, len); |
| 1221 | } |
| 1222 | #endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */ |
| 1223 | |
| 1224 | /* |
| 1225 | * Parse a private key |
| 1226 | */ |
| 1227 | int mbedtls_pk_parse_key(mbedtls_pk_context *pk, |
| 1228 | const unsigned char *key, size_t keylen, |
| 1229 | const unsigned char *pwd, size_t pwdlen) |
| 1230 | { |
| 1231 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 1232 | const mbedtls_pk_info_t *pk_info; |
| 1233 | #if defined(MBEDTLS_PEM_PARSE_C) |
| 1234 | size_t len; |
| 1235 | mbedtls_pem_context pem; |
| 1236 | #endif |
| 1237 | |
| 1238 | (void) pk_info; |
| 1239 | |
| 1240 | PK_VALIDATE_RET(pk != NULL); |
| 1241 | if (keylen == 0) { |
| 1242 | return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT; |
| 1243 | } |
| 1244 | PK_VALIDATE_RET(key != NULL); |
| 1245 | |
| 1246 | #if defined(MBEDTLS_PEM_PARSE_C) |
| 1247 | mbedtls_pem_init(&pem); |
| 1248 | |
| 1249 | #if defined(MBEDTLS_RSA_C) |
| 1250 | /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */ |
| 1251 | if (key[keylen - 1] != '\0') { |
| 1252 | ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT; |
| 1253 | } else { |
| 1254 | ret = mbedtls_pem_read_buffer(&pem, |
| 1255 | "-----BEGIN RSA PRIVATE KEY-----" , |
| 1256 | "-----END RSA PRIVATE KEY-----" , |
| 1257 | key, pwd, pwdlen, &len); |
| 1258 | } |
| 1259 | |
| 1260 | if (ret == 0) { |
| 1261 | pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA); |
| 1262 | if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 || |
| 1263 | (ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), |
| 1264 | pem.buf, pem.buflen)) != 0) { |
| 1265 | mbedtls_pk_free(pk); |
| 1266 | } |
| 1267 | |
| 1268 | mbedtls_pem_free(&pem); |
| 1269 | return ret; |
| 1270 | } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) { |
| 1271 | return MBEDTLS_ERR_PK_PASSWORD_MISMATCH; |
| 1272 | } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) { |
| 1273 | return MBEDTLS_ERR_PK_PASSWORD_REQUIRED; |
| 1274 | } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) { |
| 1275 | return ret; |
| 1276 | } |
| 1277 | #endif /* MBEDTLS_RSA_C */ |
| 1278 | |
| 1279 | #if defined(MBEDTLS_ECP_C) |
| 1280 | /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */ |
| 1281 | if (key[keylen - 1] != '\0') { |
| 1282 | ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT; |
| 1283 | } else { |
| 1284 | ret = mbedtls_pem_read_buffer(&pem, |
| 1285 | "-----BEGIN EC PRIVATE KEY-----" , |
| 1286 | "-----END EC PRIVATE KEY-----" , |
| 1287 | key, pwd, pwdlen, &len); |
| 1288 | } |
| 1289 | if (ret == 0) { |
| 1290 | pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY); |
| 1291 | |
| 1292 | if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 || |
| 1293 | (ret = pk_parse_key_sec1_der(mbedtls_pk_ec(*pk), |
| 1294 | pem.buf, pem.buflen)) != 0) { |
| 1295 | mbedtls_pk_free(pk); |
| 1296 | } |
| 1297 | |
| 1298 | mbedtls_pem_free(&pem); |
| 1299 | return ret; |
| 1300 | } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) { |
| 1301 | return MBEDTLS_ERR_PK_PASSWORD_MISMATCH; |
| 1302 | } else if (ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) { |
| 1303 | return MBEDTLS_ERR_PK_PASSWORD_REQUIRED; |
| 1304 | } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) { |
| 1305 | return ret; |
| 1306 | } |
| 1307 | #endif /* MBEDTLS_ECP_C */ |
| 1308 | |
| 1309 | /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */ |
| 1310 | if (key[keylen - 1] != '\0') { |
| 1311 | ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT; |
| 1312 | } else { |
| 1313 | ret = mbedtls_pem_read_buffer(&pem, |
| 1314 | "-----BEGIN PRIVATE KEY-----" , |
| 1315 | "-----END PRIVATE KEY-----" , |
| 1316 | key, NULL, 0, &len); |
| 1317 | } |
| 1318 | if (ret == 0) { |
| 1319 | if ((ret = pk_parse_key_pkcs8_unencrypted_der(pk, |
| 1320 | pem.buf, pem.buflen)) != 0) { |
| 1321 | mbedtls_pk_free(pk); |
| 1322 | } |
| 1323 | |
| 1324 | mbedtls_pem_free(&pem); |
| 1325 | return ret; |
| 1326 | } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) { |
| 1327 | return ret; |
| 1328 | } |
| 1329 | |
| 1330 | #if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C) |
| 1331 | /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */ |
| 1332 | if (key[keylen - 1] != '\0') { |
| 1333 | ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT; |
| 1334 | } else { |
| 1335 | ret = mbedtls_pem_read_buffer(&pem, |
| 1336 | "-----BEGIN ENCRYPTED PRIVATE KEY-----" , |
| 1337 | "-----END ENCRYPTED PRIVATE KEY-----" , |
| 1338 | key, NULL, 0, &len); |
| 1339 | } |
| 1340 | if (ret == 0) { |
| 1341 | if ((ret = pk_parse_key_pkcs8_encrypted_der(pk, |
| 1342 | pem.buf, pem.buflen, |
| 1343 | pwd, pwdlen)) != 0) { |
| 1344 | mbedtls_pk_free(pk); |
| 1345 | } |
| 1346 | |
| 1347 | mbedtls_pem_free(&pem); |
| 1348 | return ret; |
| 1349 | } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) { |
| 1350 | return ret; |
| 1351 | } |
| 1352 | #endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */ |
| 1353 | #else |
| 1354 | ((void) pwd); |
| 1355 | ((void) pwdlen); |
| 1356 | #endif /* MBEDTLS_PEM_PARSE_C */ |
| 1357 | |
| 1358 | /* |
| 1359 | * At this point we only know it's not a PEM formatted key. Could be any |
| 1360 | * of the known DER encoded private key formats |
| 1361 | * |
| 1362 | * We try the different DER format parsers to see if one passes without |
| 1363 | * error |
| 1364 | */ |
| 1365 | #if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C) |
| 1366 | { |
| 1367 | unsigned char *key_copy; |
| 1368 | |
| 1369 | if ((key_copy = mbedtls_calloc(1, keylen)) == NULL) { |
| 1370 | return MBEDTLS_ERR_PK_ALLOC_FAILED; |
| 1371 | } |
| 1372 | |
| 1373 | memcpy(key_copy, key, keylen); |
| 1374 | |
| 1375 | ret = pk_parse_key_pkcs8_encrypted_der(pk, key_copy, keylen, |
| 1376 | pwd, pwdlen); |
| 1377 | |
| 1378 | mbedtls_platform_zeroize(key_copy, keylen); |
| 1379 | mbedtls_free(key_copy); |
| 1380 | } |
| 1381 | |
| 1382 | if (ret == 0) { |
| 1383 | return 0; |
| 1384 | } |
| 1385 | |
| 1386 | mbedtls_pk_free(pk); |
| 1387 | mbedtls_pk_init(pk); |
| 1388 | |
| 1389 | if (ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH) { |
| 1390 | return ret; |
| 1391 | } |
| 1392 | #endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */ |
| 1393 | |
| 1394 | ret = pk_parse_key_pkcs8_unencrypted_der(pk, key, keylen); |
| 1395 | if (ret == 0) { |
| 1396 | return 0; |
| 1397 | } |
| 1398 | |
| 1399 | mbedtls_pk_free(pk); |
| 1400 | mbedtls_pk_init(pk); |
| 1401 | |
| 1402 | #if defined(MBEDTLS_RSA_C) |
| 1403 | |
| 1404 | pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA); |
| 1405 | if (mbedtls_pk_setup(pk, pk_info) == 0 && |
| 1406 | pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk), key, keylen) == 0) { |
| 1407 | return 0; |
| 1408 | } |
| 1409 | |
| 1410 | mbedtls_pk_free(pk); |
| 1411 | mbedtls_pk_init(pk); |
| 1412 | #endif /* MBEDTLS_RSA_C */ |
| 1413 | |
| 1414 | #if defined(MBEDTLS_ECP_C) |
| 1415 | pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY); |
| 1416 | if (mbedtls_pk_setup(pk, pk_info) == 0 && |
| 1417 | pk_parse_key_sec1_der(mbedtls_pk_ec(*pk), |
| 1418 | key, keylen) == 0) { |
| 1419 | return 0; |
| 1420 | } |
| 1421 | mbedtls_pk_free(pk); |
| 1422 | #endif /* MBEDTLS_ECP_C */ |
| 1423 | |
| 1424 | /* If MBEDTLS_RSA_C is defined but MBEDTLS_ECP_C isn't, |
| 1425 | * it is ok to leave the PK context initialized but not |
| 1426 | * freed: It is the caller's responsibility to call pk_init() |
| 1427 | * before calling this function, and to call pk_free() |
| 1428 | * when it fails. If MBEDTLS_ECP_C is defined but MBEDTLS_RSA_C |
| 1429 | * isn't, this leads to mbedtls_pk_free() being called |
| 1430 | * twice, once here and once by the caller, but this is |
| 1431 | * also ok and in line with the mbedtls_pk_free() calls |
| 1432 | * on failed PEM parsing attempts. */ |
| 1433 | |
| 1434 | return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT; |
| 1435 | } |
| 1436 | |
| 1437 | /* |
| 1438 | * Parse a public key |
| 1439 | */ |
| 1440 | int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx, |
| 1441 | const unsigned char *key, size_t keylen) |
| 1442 | { |
| 1443 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 1444 | unsigned char *p; |
| 1445 | #if defined(MBEDTLS_RSA_C) |
| 1446 | const mbedtls_pk_info_t *pk_info; |
| 1447 | #endif |
| 1448 | #if defined(MBEDTLS_PEM_PARSE_C) |
| 1449 | size_t len; |
| 1450 | mbedtls_pem_context pem; |
| 1451 | #endif |
| 1452 | |
| 1453 | PK_VALIDATE_RET(ctx != NULL); |
| 1454 | if (keylen == 0) { |
| 1455 | return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT; |
| 1456 | } |
| 1457 | PK_VALIDATE_RET(key != NULL || keylen == 0); |
| 1458 | |
| 1459 | #if defined(MBEDTLS_PEM_PARSE_C) |
| 1460 | mbedtls_pem_init(&pem); |
| 1461 | #if defined(MBEDTLS_RSA_C) |
| 1462 | /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */ |
| 1463 | if (key[keylen - 1] != '\0') { |
| 1464 | ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT; |
| 1465 | } else { |
| 1466 | ret = mbedtls_pem_read_buffer(&pem, |
| 1467 | "-----BEGIN RSA PUBLIC KEY-----" , |
| 1468 | "-----END RSA PUBLIC KEY-----" , |
| 1469 | key, NULL, 0, &len); |
| 1470 | } |
| 1471 | |
| 1472 | if (ret == 0) { |
| 1473 | p = pem.buf; |
| 1474 | if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) { |
| 1475 | mbedtls_pem_free(&pem); |
| 1476 | return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG; |
| 1477 | } |
| 1478 | |
| 1479 | if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) { |
| 1480 | mbedtls_pem_free(&pem); |
| 1481 | return ret; |
| 1482 | } |
| 1483 | |
| 1484 | if ((ret = pk_get_rsapubkey(&p, p + pem.buflen, mbedtls_pk_rsa(*ctx))) != 0) { |
| 1485 | mbedtls_pk_free(ctx); |
| 1486 | } |
| 1487 | |
| 1488 | mbedtls_pem_free(&pem); |
| 1489 | return ret; |
| 1490 | } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) { |
| 1491 | mbedtls_pem_free(&pem); |
| 1492 | return ret; |
| 1493 | } |
| 1494 | #endif /* MBEDTLS_RSA_C */ |
| 1495 | |
| 1496 | /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */ |
| 1497 | if (key[keylen - 1] != '\0') { |
| 1498 | ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT; |
| 1499 | } else { |
| 1500 | ret = mbedtls_pem_read_buffer(&pem, |
| 1501 | "-----BEGIN PUBLIC KEY-----" , |
| 1502 | "-----END PUBLIC KEY-----" , |
| 1503 | key, NULL, 0, &len); |
| 1504 | } |
| 1505 | |
| 1506 | if (ret == 0) { |
| 1507 | /* |
| 1508 | * Was PEM encoded |
| 1509 | */ |
| 1510 | p = pem.buf; |
| 1511 | |
| 1512 | ret = mbedtls_pk_parse_subpubkey(&p, p + pem.buflen, ctx); |
| 1513 | mbedtls_pem_free(&pem); |
| 1514 | return ret; |
| 1515 | } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) { |
| 1516 | mbedtls_pem_free(&pem); |
| 1517 | return ret; |
| 1518 | } |
| 1519 | mbedtls_pem_free(&pem); |
| 1520 | #endif /* MBEDTLS_PEM_PARSE_C */ |
| 1521 | |
| 1522 | #if defined(MBEDTLS_RSA_C) |
| 1523 | if ((pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)) == NULL) { |
| 1524 | return MBEDTLS_ERR_PK_UNKNOWN_PK_ALG; |
| 1525 | } |
| 1526 | |
| 1527 | if ((ret = mbedtls_pk_setup(ctx, pk_info)) != 0) { |
| 1528 | return ret; |
| 1529 | } |
| 1530 | |
| 1531 | p = (unsigned char *) key; |
| 1532 | ret = pk_get_rsapubkey(&p, p + keylen, mbedtls_pk_rsa(*ctx)); |
| 1533 | if (ret == 0) { |
| 1534 | return ret; |
| 1535 | } |
| 1536 | mbedtls_pk_free(ctx); |
| 1537 | if (ret != (MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PK_INVALID_PUBKEY, |
| 1538 | MBEDTLS_ERR_ASN1_UNEXPECTED_TAG))) { |
| 1539 | return ret; |
| 1540 | } |
| 1541 | #endif /* MBEDTLS_RSA_C */ |
| 1542 | p = (unsigned char *) key; |
| 1543 | |
| 1544 | ret = mbedtls_pk_parse_subpubkey(&p, p + keylen, ctx); |
| 1545 | |
| 1546 | return ret; |
| 1547 | } |
| 1548 | |
| 1549 | #endif /* MBEDTLS_PK_PARSE_C */ |
| 1550 | |