diff options
Diffstat (limited to '')
| -rw-r--r-- | src/lib/libcrypto/pem/pvkfmt.c | 68 |
1 files changed, 32 insertions, 36 deletions
diff --git a/src/lib/libcrypto/pem/pvkfmt.c b/src/lib/libcrypto/pem/pvkfmt.c index 0a33a24b9f..5ed8df585f 100644 --- a/src/lib/libcrypto/pem/pvkfmt.c +++ b/src/lib/libcrypto/pem/pvkfmt.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: pvkfmt.c,v 1.17 2017/01/21 09:38:59 beck Exp $ */ | 1 | /* $OpenBSD: pvkfmt.c,v 1.18 2017/01/29 17:49:23 beck Exp $ */ |
| 2 | /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL | 2 | /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL |
| 3 | * project 2005. | 3 | * project 2005. |
| 4 | */ | 4 | */ |
| @@ -157,15 +157,13 @@ do_blob_header(const unsigned char **in, unsigned int length, | |||
| 157 | /* bType */ | 157 | /* bType */ |
| 158 | if (*p == MS_PUBLICKEYBLOB) { | 158 | if (*p == MS_PUBLICKEYBLOB) { |
| 159 | if (*pispub == 0) { | 159 | if (*pispub == 0) { |
| 160 | PEMerr(PEM_F_DO_BLOB_HEADER, | 160 | PEMerror(PEM_R_EXPECTING_PRIVATE_KEY_BLOB); |
| 161 | PEM_R_EXPECTING_PRIVATE_KEY_BLOB); | ||
| 162 | return 0; | 161 | return 0; |
| 163 | } | 162 | } |
| 164 | *pispub = 1; | 163 | *pispub = 1; |
| 165 | } else if (*p == MS_PRIVATEKEYBLOB) { | 164 | } else if (*p == MS_PRIVATEKEYBLOB) { |
| 166 | if (*pispub == 1) { | 165 | if (*pispub == 1) { |
| 167 | PEMerr(PEM_F_DO_BLOB_HEADER, | 166 | PEMerror(PEM_R_EXPECTING_PUBLIC_KEY_BLOB); |
| 168 | PEM_R_EXPECTING_PUBLIC_KEY_BLOB); | ||
| 169 | return 0; | 167 | return 0; |
| 170 | } | 168 | } |
| 171 | *pispub = 0; | 169 | *pispub = 0; |
| @@ -174,7 +172,7 @@ do_blob_header(const unsigned char **in, unsigned int length, | |||
| 174 | p++; | 172 | p++; |
| 175 | /* Version */ | 173 | /* Version */ |
| 176 | if (*p++ != 0x2) { | 174 | if (*p++ != 0x2) { |
| 177 | PEMerr(PEM_F_DO_BLOB_HEADER, PEM_R_BAD_VERSION_NUMBER); | 175 | PEMerror(PEM_R_BAD_VERSION_NUMBER); |
| 178 | return 0; | 176 | return 0; |
| 179 | } | 177 | } |
| 180 | /* Ignore reserved, aiKeyAlg */ | 178 | /* Ignore reserved, aiKeyAlg */ |
| @@ -182,7 +180,7 @@ do_blob_header(const unsigned char **in, unsigned int length, | |||
| 182 | *pmagic = read_ledword(&p); | 180 | *pmagic = read_ledword(&p); |
| 183 | *pbitlen = read_ledword(&p); | 181 | *pbitlen = read_ledword(&p); |
| 184 | if (*pbitlen > 65536) { | 182 | if (*pbitlen > 65536) { |
| 185 | PEMerr(PEM_F_DO_BLOB_HEADER, PEM_R_INCONSISTENT_HEADER); | 183 | PEMerror(PEM_R_INCONSISTENT_HEADER); |
| 186 | return 0; | 184 | return 0; |
| 187 | } | 185 | } |
| 188 | *pisdss = 0; | 186 | *pisdss = 0; |
| @@ -192,8 +190,7 @@ do_blob_header(const unsigned char **in, unsigned int length, | |||
| 192 | *pisdss = 1; | 190 | *pisdss = 1; |
| 193 | case MS_RSA1MAGIC: | 191 | case MS_RSA1MAGIC: |
| 194 | if (*pispub == 0) { | 192 | if (*pispub == 0) { |
| 195 | PEMerr(PEM_F_DO_BLOB_HEADER, | 193 | PEMerror(PEM_R_EXPECTING_PRIVATE_KEY_BLOB); |
| 196 | PEM_R_EXPECTING_PRIVATE_KEY_BLOB); | ||
| 197 | return 0; | 194 | return 0; |
| 198 | } | 195 | } |
| 199 | break; | 196 | break; |
| @@ -202,14 +199,13 @@ do_blob_header(const unsigned char **in, unsigned int length, | |||
| 202 | *pisdss = 1; | 199 | *pisdss = 1; |
| 203 | case MS_RSA2MAGIC: | 200 | case MS_RSA2MAGIC: |
| 204 | if (*pispub == 1) { | 201 | if (*pispub == 1) { |
| 205 | PEMerr(PEM_F_DO_BLOB_HEADER, | 202 | PEMerror(PEM_R_EXPECTING_PUBLIC_KEY_BLOB); |
| 206 | PEM_R_EXPECTING_PUBLIC_KEY_BLOB); | ||
| 207 | return 0; | 203 | return 0; |
| 208 | } | 204 | } |
| 209 | break; | 205 | break; |
| 210 | 206 | ||
| 211 | default: | 207 | default: |
| 212 | PEMerr(PEM_F_DO_BLOB_HEADER, PEM_R_BAD_MAGIC_NUMBER); | 208 | PEMerror(PEM_R_BAD_MAGIC_NUMBER); |
| 213 | return -1; | 209 | return -1; |
| 214 | } | 210 | } |
| 215 | *in = p; | 211 | *in = p; |
| @@ -256,12 +252,12 @@ do_b2i(const unsigned char **in, unsigned int length, int ispub) | |||
| 256 | int isdss; | 252 | int isdss; |
| 257 | 253 | ||
| 258 | if (do_blob_header(&p, length, &magic, &bitlen, &isdss, &ispub) <= 0) { | 254 | if (do_blob_header(&p, length, &magic, &bitlen, &isdss, &ispub) <= 0) { |
| 259 | PEMerr(PEM_F_DO_B2I, PEM_R_KEYBLOB_HEADER_PARSE_ERROR); | 255 | PEMerror(PEM_R_KEYBLOB_HEADER_PARSE_ERROR); |
| 260 | return NULL; | 256 | return NULL; |
| 261 | } | 257 | } |
| 262 | length -= 16; | 258 | length -= 16; |
| 263 | if (length < blob_length(bitlen, isdss, ispub)) { | 259 | if (length < blob_length(bitlen, isdss, ispub)) { |
| 264 | PEMerr(PEM_F_DO_B2I, PEM_R_KEYBLOB_TOO_SHORT); | 260 | PEMerror(PEM_R_KEYBLOB_TOO_SHORT); |
| 265 | return NULL; | 261 | return NULL; |
| 266 | } | 262 | } |
| 267 | if (isdss) | 263 | if (isdss) |
| @@ -280,7 +276,7 @@ do_b2i_bio(BIO *in, int ispub) | |||
| 280 | EVP_PKEY *ret = NULL; | 276 | EVP_PKEY *ret = NULL; |
| 281 | 277 | ||
| 282 | if (BIO_read(in, hdr_buf, 16) != 16) { | 278 | if (BIO_read(in, hdr_buf, 16) != 16) { |
| 283 | PEMerr(PEM_F_DO_B2I_BIO, PEM_R_KEYBLOB_TOO_SHORT); | 279 | PEMerror(PEM_R_KEYBLOB_TOO_SHORT); |
| 284 | return NULL; | 280 | return NULL; |
| 285 | } | 281 | } |
| 286 | p = hdr_buf; | 282 | p = hdr_buf; |
| @@ -290,12 +286,12 @@ do_b2i_bio(BIO *in, int ispub) | |||
| 290 | length = blob_length(bitlen, isdss, ispub); | 286 | length = blob_length(bitlen, isdss, ispub); |
| 291 | buf = malloc(length); | 287 | buf = malloc(length); |
| 292 | if (!buf) { | 288 | if (!buf) { |
| 293 | PEMerr(PEM_F_DO_B2I_BIO, ERR_R_MALLOC_FAILURE); | 289 | PEMerror(ERR_R_MALLOC_FAILURE); |
| 294 | goto err; | 290 | goto err; |
| 295 | } | 291 | } |
| 296 | p = buf; | 292 | p = buf; |
| 297 | if (BIO_read(in, buf, length) != (int)length) { | 293 | if (BIO_read(in, buf, length) != (int)length) { |
| 298 | PEMerr(PEM_F_DO_B2I_BIO, PEM_R_KEYBLOB_TOO_SHORT); | 294 | PEMerror(PEM_R_KEYBLOB_TOO_SHORT); |
| 299 | goto err; | 295 | goto err; |
| 300 | } | 296 | } |
| 301 | 297 | ||
| @@ -354,7 +350,7 @@ b2i_dss(const unsigned char **in, unsigned int length, unsigned int bitlen, | |||
| 354 | return ret; | 350 | return ret; |
| 355 | 351 | ||
| 356 | memerr: | 352 | memerr: |
| 357 | PEMerr(PEM_F_B2I_DSS, ERR_R_MALLOC_FAILURE); | 353 | PEMerror(ERR_R_MALLOC_FAILURE); |
| 358 | DSA_free(dsa); | 354 | DSA_free(dsa); |
| 359 | EVP_PKEY_free(ret); | 355 | EVP_PKEY_free(ret); |
| 360 | BN_CTX_free(ctx); | 356 | BN_CTX_free(ctx); |
| @@ -404,7 +400,7 @@ b2i_rsa(const unsigned char **in, unsigned int length, unsigned int bitlen, | |||
| 404 | return ret; | 400 | return ret; |
| 405 | 401 | ||
| 406 | memerr: | 402 | memerr: |
| 407 | PEMerr(PEM_F_B2I_RSA, ERR_R_MALLOC_FAILURE); | 403 | PEMerror(ERR_R_MALLOC_FAILURE); |
| 408 | RSA_free(rsa); | 404 | RSA_free(rsa); |
| 409 | EVP_PKEY_free(ret); | 405 | EVP_PKEY_free(ret); |
| 410 | return NULL; | 406 | return NULL; |
| @@ -566,7 +562,7 @@ check_bitlen_dsa(DSA *dsa, int ispub, unsigned int *pmagic) | |||
| 566 | return bitlen; | 562 | return bitlen; |
| 567 | 563 | ||
| 568 | badkey: | 564 | badkey: |
| 569 | PEMerr(PEM_F_CHECK_BITLEN_DSA, PEM_R_UNSUPPORTED_KEY_COMPONENTS); | 565 | PEMerror(PEM_R_UNSUPPORTED_KEY_COMPONENTS); |
| 570 | return 0; | 566 | return 0; |
| 571 | } | 567 | } |
| 572 | 568 | ||
| @@ -600,7 +596,7 @@ check_bitlen_rsa(RSA *rsa, int ispub, unsigned int *pmagic) | |||
| 600 | return bitlen; | 596 | return bitlen; |
| 601 | 597 | ||
| 602 | badkey: | 598 | badkey: |
| 603 | PEMerr(PEM_F_CHECK_BITLEN_RSA, PEM_R_UNSUPPORTED_KEY_COMPONENTS); | 599 | PEMerror(PEM_R_UNSUPPORTED_KEY_COMPONENTS); |
| 604 | return 0; | 600 | return 0; |
| 605 | } | 601 | } |
| 606 | 602 | ||
| @@ -665,19 +661,19 @@ do_PVK_header(const unsigned char **in, unsigned int length, int skip_magic, | |||
| 665 | 661 | ||
| 666 | if (skip_magic) { | 662 | if (skip_magic) { |
| 667 | if (length < 20) { | 663 | if (length < 20) { |
| 668 | PEMerr(PEM_F_DO_PVK_HEADER, PEM_R_PVK_TOO_SHORT); | 664 | PEMerror(PEM_R_PVK_TOO_SHORT); |
| 669 | return 0; | 665 | return 0; |
| 670 | } | 666 | } |
| 671 | length -= 20; | 667 | length -= 20; |
| 672 | } else { | 668 | } else { |
| 673 | if (length < 24) { | 669 | if (length < 24) { |
| 674 | PEMerr(PEM_F_DO_PVK_HEADER, PEM_R_PVK_TOO_SHORT); | 670 | PEMerror(PEM_R_PVK_TOO_SHORT); |
| 675 | return 0; | 671 | return 0; |
| 676 | } | 672 | } |
| 677 | length -= 24; | 673 | length -= 24; |
| 678 | pvk_magic = read_ledword(&p); | 674 | pvk_magic = read_ledword(&p); |
| 679 | if (pvk_magic != MS_PVKMAGIC) { | 675 | if (pvk_magic != MS_PVKMAGIC) { |
| 680 | PEMerr(PEM_F_DO_PVK_HEADER, PEM_R_BAD_MAGIC_NUMBER); | 676 | PEMerror(PEM_R_BAD_MAGIC_NUMBER); |
| 681 | return 0; | 677 | return 0; |
| 682 | } | 678 | } |
| 683 | } | 679 | } |
| @@ -688,12 +684,12 @@ do_PVK_header(const unsigned char **in, unsigned int length, int skip_magic, | |||
| 688 | *psaltlen = read_ledword(&p); | 684 | *psaltlen = read_ledword(&p); |
| 689 | *pkeylen = read_ledword(&p); | 685 | *pkeylen = read_ledword(&p); |
| 690 | if (*psaltlen > 65536 || *pkeylen > 65536) { | 686 | if (*psaltlen > 65536 || *pkeylen > 65536) { |
| 691 | PEMerr(PEM_F_DO_PVK_HEADER, PEM_R_ERROR_CONVERTING_PRIVATE_KEY); | 687 | PEMerror(PEM_R_ERROR_CONVERTING_PRIVATE_KEY); |
| 692 | return 0; | 688 | return 0; |
| 693 | } | 689 | } |
| 694 | 690 | ||
| 695 | if (is_encrypted && !*psaltlen) { | 691 | if (is_encrypted && !*psaltlen) { |
| 696 | PEMerr(PEM_F_DO_PVK_HEADER, PEM_R_INCONSISTENT_HEADER); | 692 | PEMerror(PEM_R_INCONSISTENT_HEADER); |
| 697 | return 0; | 693 | return 0; |
| 698 | } | 694 | } |
| 699 | 695 | ||
| @@ -740,12 +736,12 @@ do_PVK_body(const unsigned char **in, unsigned int saltlen, | |||
| 740 | else | 736 | else |
| 741 | inlen = PEM_def_callback(psbuf, PEM_BUFSIZE, 0, u); | 737 | inlen = PEM_def_callback(psbuf, PEM_BUFSIZE, 0, u); |
| 742 | if (inlen <= 0) { | 738 | if (inlen <= 0) { |
| 743 | PEMerr(PEM_F_DO_PVK_BODY, PEM_R_BAD_PASSWORD_READ); | 739 | PEMerror(PEM_R_BAD_PASSWORD_READ); |
| 744 | goto err; | 740 | goto err; |
| 745 | } | 741 | } |
| 746 | enctmp = malloc(keylen + 8); | 742 | enctmp = malloc(keylen + 8); |
| 747 | if (!enctmp) { | 743 | if (!enctmp) { |
| 748 | PEMerr(PEM_F_DO_PVK_BODY, ERR_R_MALLOC_FAILURE); | 744 | PEMerror(ERR_R_MALLOC_FAILURE); |
| 749 | goto err; | 745 | goto err; |
| 750 | } | 746 | } |
| 751 | if (!derive_pvk_key(keybuf, p, saltlen, (unsigned char *)psbuf, | 747 | if (!derive_pvk_key(keybuf, p, saltlen, (unsigned char *)psbuf, |
| @@ -757,7 +753,7 @@ do_PVK_body(const unsigned char **in, unsigned int saltlen, | |||
| 757 | memcpy(enctmp, p, 8); | 753 | memcpy(enctmp, p, 8); |
| 758 | p += 8; | 754 | p += 8; |
| 759 | if (keylen < 8) { | 755 | if (keylen < 8) { |
| 760 | PEMerr(PEM_F_DO_PVK_BODY, PEM_R_PVK_TOO_SHORT); | 756 | PEMerror(PEM_R_PVK_TOO_SHORT); |
| 761 | goto err; | 757 | goto err; |
| 762 | } | 758 | } |
| 763 | inlen = keylen - 8; | 759 | inlen = keylen - 8; |
| @@ -783,7 +779,7 @@ do_PVK_body(const unsigned char **in, unsigned int saltlen, | |||
| 783 | goto err; | 779 | goto err; |
| 784 | magic = read_ledword((const unsigned char **)&q); | 780 | magic = read_ledword((const unsigned char **)&q); |
| 785 | if (magic != MS_RSA2MAGIC && magic != MS_DSS2MAGIC) { | 781 | if (magic != MS_RSA2MAGIC && magic != MS_DSS2MAGIC) { |
| 786 | PEMerr(PEM_F_DO_PVK_BODY, PEM_R_BAD_DECRYPT); | 782 | PEMerror(PEM_R_BAD_DECRYPT); |
| 787 | goto err; | 783 | goto err; |
| 788 | } | 784 | } |
| 789 | } else | 785 | } else |
| @@ -811,7 +807,7 @@ b2i_PVK_bio(BIO *in, pem_password_cb *cb, void *u) | |||
| 811 | unsigned int saltlen, keylen; | 807 | unsigned int saltlen, keylen; |
| 812 | 808 | ||
| 813 | if (BIO_read(in, pvk_hdr, 24) != 24) { | 809 | if (BIO_read(in, pvk_hdr, 24) != 24) { |
| 814 | PEMerr(PEM_F_B2I_PVK_BIO, PEM_R_PVK_DATA_TOO_SHORT); | 810 | PEMerror(PEM_R_PVK_DATA_TOO_SHORT); |
| 815 | return NULL; | 811 | return NULL; |
| 816 | } | 812 | } |
| 817 | p = pvk_hdr; | 813 | p = pvk_hdr; |
| @@ -821,12 +817,12 @@ b2i_PVK_bio(BIO *in, pem_password_cb *cb, void *u) | |||
| 821 | buflen = keylen + saltlen; | 817 | buflen = keylen + saltlen; |
| 822 | buf = malloc(buflen); | 818 | buf = malloc(buflen); |
| 823 | if (!buf) { | 819 | if (!buf) { |
| 824 | PEMerr(PEM_F_B2I_PVK_BIO, ERR_R_MALLOC_FAILURE); | 820 | PEMerror(ERR_R_MALLOC_FAILURE); |
| 825 | return 0; | 821 | return 0; |
| 826 | } | 822 | } |
| 827 | p = buf; | 823 | p = buf; |
| 828 | if (BIO_read(in, buf, buflen) != buflen) { | 824 | if (BIO_read(in, buf, buflen) != buflen) { |
| 829 | PEMerr(PEM_F_B2I_PVK_BIO, PEM_R_PVK_DATA_TOO_SHORT); | 825 | PEMerror(PEM_R_PVK_DATA_TOO_SHORT); |
| 830 | goto err; | 826 | goto err; |
| 831 | } | 827 | } |
| 832 | ret = do_PVK_body(&p, saltlen, keylen, cb, u); | 828 | ret = do_PVK_body(&p, saltlen, keylen, cb, u); |
| @@ -861,7 +857,7 @@ i2b_PVK(unsigned char **out, EVP_PKEY*pk, int enclevel, pem_password_cb *cb, | |||
| 861 | else { | 857 | else { |
| 862 | p = malloc(outlen); | 858 | p = malloc(outlen); |
| 863 | if (!p) { | 859 | if (!p) { |
| 864 | PEMerr(PEM_F_I2B_PVK, ERR_R_MALLOC_FAILURE); | 860 | PEMerror(ERR_R_MALLOC_FAILURE); |
| 865 | return -1; | 861 | return -1; |
| 866 | } | 862 | } |
| 867 | *out = p; | 863 | *out = p; |
| @@ -893,7 +889,7 @@ i2b_PVK(unsigned char **out, EVP_PKEY*pk, int enclevel, pem_password_cb *cb, | |||
| 893 | else | 889 | else |
| 894 | inlen = PEM_def_callback(psbuf, PEM_BUFSIZE, 1, u); | 890 | inlen = PEM_def_callback(psbuf, PEM_BUFSIZE, 1, u); |
| 895 | if (inlen <= 0) { | 891 | if (inlen <= 0) { |
| 896 | PEMerr(PEM_F_I2B_PVK, PEM_R_BAD_PASSWORD_READ); | 892 | PEMerror(PEM_R_BAD_PASSWORD_READ); |
| 897 | goto error; | 893 | goto error; |
| 898 | } | 894 | } |
| 899 | if (!derive_pvk_key(keybuf, salt, PVK_SALTLEN, | 895 | if (!derive_pvk_key(keybuf, salt, PVK_SALTLEN, |
| @@ -930,7 +926,7 @@ i2b_PVK_bio(BIO *out, EVP_PKEY *pk, int enclevel, pem_password_cb *cb, void *u) | |||
| 930 | wrlen = BIO_write(out, tmp, outlen); | 926 | wrlen = BIO_write(out, tmp, outlen); |
| 931 | free(tmp); | 927 | free(tmp); |
| 932 | if (wrlen == outlen) { | 928 | if (wrlen == outlen) { |
| 933 | PEMerr(PEM_F_I2B_PVK_BIO, PEM_R_BIO_WRITE_FAILURE); | 929 | PEMerror(PEM_R_BIO_WRITE_FAILURE); |
| 934 | return outlen; | 930 | return outlen; |
| 935 | } | 931 | } |
| 936 | return -1; | 932 | return -1; |
