]>
Commit | Line | Data |
---|---|---|
1017b8e4 RL |
1 | /* |
2 | * Copyright 2020 The OpenSSL Project Authors. All Rights Reserved. | |
3 | * | |
4 | * Licensed under the Apache License 2.0 (the "License"). You may not use | |
5 | * this file except in compliance with the License. You can obtain a copy | |
6 | * in the file LICENSE in the source distribution or at | |
7 | * https://www.openssl.org/source/license.html | |
8 | */ | |
9 | ||
37d398c1 | 10 | #include <openssl/core_names.h> |
1017b8e4 | 11 | #include <openssl/bio.h> |
38b14f47 | 12 | #include <openssl/err.h> |
1017b8e4 | 13 | #include <openssl/buffer.h> |
37d398c1 | 14 | #include <openssl/pem.h> /* For public PEM and PVK functions */ |
38b14f47 | 15 | #include <openssl/pkcs12.h> |
37d398c1 | 16 | #include "internal/pem.h" /* For internal PVK and "blob" functions */ |
38b14f47 | 17 | #include "internal/cryptlib.h" |
16feca71 | 18 | #include "internal/asn1.h" |
a517edec | 19 | #include "internal/passphrase.h" |
38b14f47 RL |
20 | #include "prov/bio.h" /* ossl_prov_bio_printf() */ |
21 | #include "prov/providercommonerr.h" /* PROV_R_READ_KEY */ | |
ece9304c | 22 | #include "encoder_local.h" |
1017b8e4 RL |
23 | |
24 | int ossl_prov_read_der(PROV_CTX *provctx, OSSL_CORE_BIO *cin, | |
25 | unsigned char **data, long *len) | |
26 | { | |
27 | BUF_MEM *mem = NULL; | |
28 | BIO *in = bio_new_from_core_bio(provctx, cin); | |
29 | int ok = (asn1_d2i_read_bio(in, &mem) >= 0); | |
30 | ||
31 | if (ok) { | |
32 | *data = (unsigned char *)mem->data; | |
33 | *len = (long)mem->length; | |
34 | OPENSSL_free(mem); | |
35 | } | |
36 | BIO_free(in); | |
37 | return ok; | |
38 | } | |
dcfacbbf RL |
39 | |
40 | int ossl_prov_read_pem(PROV_CTX *provctx, OSSL_CORE_BIO *cin, | |
41 | char **pem_name, char **pem_header, | |
42 | unsigned char **data, long *len) | |
43 | { | |
44 | BIO *in = bio_new_from_core_bio(provctx, cin); | |
45 | int ok = (PEM_read_bio(in, pem_name, pem_header, data, len) > 0); | |
46 | ||
47 | BIO_free(in); | |
48 | return ok; | |
49 | } | |
38b14f47 | 50 | |
37d398c1 RL |
51 | #ifndef OPENSSL_NO_DSA |
52 | EVP_PKEY *ossl_prov_read_msblob(PROV_CTX *provctx, OSSL_CORE_BIO *cin, | |
53 | int *ispub) | |
54 | { | |
55 | BIO *in = bio_new_from_core_bio(provctx, cin); | |
56 | EVP_PKEY *pkey = ossl_b2i_bio(in, ispub); | |
57 | ||
58 | BIO_free(in); | |
59 | return pkey; | |
60 | } | |
61 | ||
37d398c1 RL |
62 | # ifndef OPENSSL_NO_RC4 |
63 | EVP_PKEY *ossl_prov_read_pvk(PROV_CTX *provctx, OSSL_CORE_BIO *cin, | |
64 | OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg) | |
65 | { | |
a517edec | 66 | BIO *in = NULL; |
37d398c1 | 67 | EVP_PKEY *pkey = NULL; |
a517edec | 68 | struct ossl_passphrase_data_st pwdata; |
37d398c1 | 69 | |
a517edec RL |
70 | memset(&pwdata, 0, sizeof(pwdata)); |
71 | if (!ossl_pw_set_ossl_passphrase_cb(&pwdata, pw_cb, pw_cbarg)) | |
72 | return NULL; | |
37d398c1 | 73 | |
a517edec RL |
74 | in = bio_new_from_core_bio(provctx, cin); |
75 | pkey = b2i_PVK_bio(in, ossl_pw_pem_password, &pwdata); | |
37d398c1 | 76 | BIO_free(in); |
a517edec | 77 | |
37d398c1 RL |
78 | return pkey; |
79 | } | |
80 | # endif | |
81 | #endif | |
82 | ||
38b14f47 RL |
83 | int ossl_prov_der_from_p8(unsigned char **new_der, long *new_der_len, |
84 | unsigned char *input_der, long input_der_len, | |
4701f0a9 | 85 | OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg) |
38b14f47 RL |
86 | { |
87 | const unsigned char *derp; | |
88 | X509_SIG *p8 = NULL; | |
89 | int ok = 0; | |
90 | ||
91 | if (!ossl_assert(new_der != NULL && *new_der == NULL) | |
92 | || !ossl_assert(new_der_len != NULL)) | |
93 | return 0; | |
94 | ||
38b14f47 RL |
95 | derp = input_der; |
96 | if ((p8 = d2i_X509_SIG(NULL, &derp, input_der_len)) != NULL) { | |
97 | char pbuf[PEM_BUFSIZE]; | |
4701f0a9 | 98 | size_t plen = 0; |
38b14f47 | 99 | |
4701f0a9 RL |
100 | if (!pw_cb(pbuf, sizeof(pbuf), &plen, NULL, pw_cbarg)) { |
101 | ERR_raise(ERR_LIB_PROV, PROV_R_READ_KEY); | |
102 | } else { | |
38b14f47 RL |
103 | const X509_ALGOR *alg = NULL; |
104 | const ASN1_OCTET_STRING *oct = NULL; | |
105 | int len = 0; | |
106 | ||
107 | X509_SIG_get0(p8, &alg, &oct); | |
4701f0a9 | 108 | if (PKCS12_pbe_crypt(alg, pbuf, plen, oct->data, oct->length, |
38b14f47 RL |
109 | new_der, &len, 0) != NULL) |
110 | ok = 1; | |
111 | *new_der_len = len; | |
112 | } | |
113 | } | |
114 | X509_SIG_free(p8); | |
115 | return ok; | |
116 | } |