Skip to content

Commit fcbbb52

Browse files
committed
deps: openssl: cherry-pick 59ccb72 and 1d28ada
openssl/openssl@59ccb72: ``` commit 59ccb72cd5cec3b4e312853621e12a68dacdbc7e Author: Darshan Sen <raisinten@gmail.com> Date: Fri Jan 14 16:22:41 2022 +0530 Fix invalid malloc failures in PEM_write_bio_PKCS8PrivateKey() When `PEM_write_bio_PKCS8PrivateKey()` was passed an empty passphrase string, `OPENSSL_memdup()` was incorrectly getting used for 0 bytes size allocation, which resulted in malloc failures. Fixes: openssl/openssl#17506 Signed-off-by: Darshan Sen <raisinten@gmail.com> Reviewed-by: Bernd Edlinger <bernd.edlinger@hotmail.de> Reviewed-by: Paul Dale <pauli@openssl.org> Reviewed-by: Tomas Mraz <tomas@openssl.org> (Merged from openssl/openssl#17507) ``` openssl/openssl@1d28ada: ``` commit 1d28ada1c39997c10fe5392f4235bbd2bc44b40f Author: Darshan Sen <raisinten@gmail.com> Date: Sat Jan 22 17:56:05 2022 +0530 Allow empty passphrase in PEM_write_bio_PKCS8PrivateKey_nid() Signed-off-by: Darshan Sen <raisinten@gmail.com> Reviewed-by: Bernd Edlinger <bernd.edlinger@hotmail.de> Reviewed-by: Paul Dale <pauli@openssl.org> Reviewed-by: Tomas Mraz <tomas@openssl.org> (Merged from openssl/openssl#17507) ``` Refs: openssl/openssl#17507 Fixes: #41428 Signed-off-by: Darshan Sen <raisinten@gmail.com>
1 parent dde2f78 commit fcbbb52

5 files changed

Lines changed: 50 additions & 3 deletions

File tree

deps/openssl/openssl/CHANGES.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ breaking changes, and mappings for the large list of deprecated functions.
2828

2929
[Migration guide]: https://github.com/openssl/openssl/tree/master/doc/man7/migration_guide.pod
3030

31+
### Changes between 3.0.0 and 3.0.0+quic [xx XXX xxxx]
32+
33+
* Fixed PEM_write_bio_PKCS8PrivateKey() and PEM_write_bio_PKCS8PrivateKey_nid()
34+
to make it possible to use empty passphrase strings.
35+
36+
*Darshan Sen*
37+
3138
### Changes between 3.0.0 and 3.0.0+quic [7 Sun 2021]
3239

3340
* Add QUIC API support from BoringSSL.

deps/openssl/openssl/crypto/passphrase.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ int ossl_pw_set_passphrase(struct ossl_passphrase_data_st *data,
4141
ossl_pw_clear_passphrase_data(data);
4242
data->type = is_expl_passphrase;
4343
data->_.expl_passphrase.passphrase_copy =
44-
OPENSSL_memdup(passphrase, passphrase_len);
44+
passphrase_len != 0 ? OPENSSL_memdup(passphrase, passphrase_len)
45+
: OPENSSL_malloc(1);
4546
if (data->_.expl_passphrase.passphrase_copy == NULL) {
4647
ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
4748
return 0;

deps/openssl/openssl/crypto/pem/pem_pk8.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ static int do_pk8pkey(BIO *bp, const EVP_PKEY *x, int isder, int nid,
136136
if (enc || (nid != -1)) {
137137
if (kstr == NULL) {
138138
klen = cb(buf, PEM_BUFSIZE, 1, u);
139-
if (klen <= 0) {
139+
if (klen < 0) {
140140
ERR_raise(ERR_LIB_PEM, PEM_R_READ_KEY);
141141
goto legacy_end;
142142
}

deps/openssl/openssl/crypto/ui/ui_util.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ static int ui_read(UI *ui, UI_STRING *uis)
114114

115115
if (len >= 0)
116116
result[len] = '\0';
117-
if (len <= 0)
117+
if (len < 0)
118118
return len;
119119
if (UI_set_result_ex(ui, uis, result, len) >= 0)
120120
return 1;

deps/openssl/openssl/test/evp_pkey_provided_test.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,16 @@ static int compare_with_file(const char *alg, int type, BIO *membio)
128128
return ret;
129129
}
130130

131+
static int pass_cb(char *buf, int size, int rwflag, void *u)
132+
{
133+
return 0;
134+
}
135+
136+
static int pass_cb_error(char *buf, int size, int rwflag, void *u)
137+
{
138+
return -1;
139+
}
140+
131141
static int test_print_key_using_pem(const char *alg, const EVP_PKEY *pk)
132142
{
133143
BIO *membio = BIO_new(BIO_s_mem());
@@ -140,6 +150,35 @@ static int test_print_key_using_pem(const char *alg, const EVP_PKEY *pk)
140150
!TEST_true(PEM_write_bio_PrivateKey(bio_out, pk, EVP_aes_256_cbc(),
141151
(unsigned char *)"pass", 4,
142152
NULL, NULL))
153+
/* Output zero-length passphrase encrypted private key in PEM form */
154+
|| !TEST_true(PEM_write_bio_PKCS8PrivateKey(bio_out, pk,
155+
EVP_aes_256_cbc(),
156+
(const char *)~0, 0,
157+
NULL, NULL))
158+
|| !TEST_true(PEM_write_bio_PKCS8PrivateKey(bio_out, pk,
159+
EVP_aes_256_cbc(),
160+
NULL, 0, NULL, ""))
161+
|| !TEST_true(PEM_write_bio_PKCS8PrivateKey(bio_out, pk,
162+
EVP_aes_256_cbc(),
163+
NULL, 0, pass_cb, NULL))
164+
|| !TEST_false(PEM_write_bio_PKCS8PrivateKey(bio_out, pk,
165+
EVP_aes_256_cbc(),
166+
NULL, 0, pass_cb_error,
167+
NULL))
168+
#ifndef OPENSSL_NO_DES
169+
|| !TEST_true(PEM_write_bio_PKCS8PrivateKey_nid(
170+
bio_out, pk, NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
171+
(const char *)~0, 0, NULL, NULL))
172+
|| !TEST_true(PEM_write_bio_PKCS8PrivateKey_nid(
173+
bio_out, pk, NID_pbe_WithSHA1And3_Key_TripleDES_CBC, NULL, 0,
174+
NULL, ""))
175+
|| !TEST_true(PEM_write_bio_PKCS8PrivateKey_nid(
176+
bio_out, pk, NID_pbe_WithSHA1And3_Key_TripleDES_CBC, NULL, 0,
177+
pass_cb, NULL))
178+
|| !TEST_false(PEM_write_bio_PKCS8PrivateKey_nid(
179+
bio_out, pk, NID_pbe_WithSHA1And3_Key_TripleDES_CBC, NULL, 0,
180+
pass_cb_error, NULL))
181+
#endif
143182
/* Private key in text form */
144183
|| !TEST_int_gt(EVP_PKEY_print_private(membio, pk, 0, NULL), 0)
145184
|| !TEST_true(compare_with_file(alg, PRIV_TEXT, membio))

0 commit comments

Comments
 (0)