|
| 1 | +From 03b3941d60c4bce58fab69a0c22377ab439bc0e8 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Matt Caswell <matt@openssl.org> |
| 3 | +Date: Fri, 19 Jan 2024 11:28:58 +0000 |
| 4 | +Subject: [PATCH] Add NULL checks where ContentInfo data can be NULL |
| 5 | + |
| 6 | +PKCS12 structures contain PKCS7 ContentInfo fields. These fields are |
| 7 | +optional and can be NULL even if the "type" is a valid value. OpenSSL |
| 8 | +was not properly accounting for this and a NULL dereference can occur |
| 9 | +causing a crash. |
| 10 | + |
| 11 | +Reviewed-by: Tomas Mraz <tomas@openssl.org> |
| 12 | +Reviewed-by: Hugo Landau <hlandau@openssl.org> |
| 13 | +Reviewed-by: Neil Horman <nhorman@openssl.org> |
| 14 | +--- |
| 15 | + crypto/err/openssl.txt | 3 ++- |
| 16 | + crypto/pkcs12/p12_add.c | 19 +++++++++++++++++++ |
| 17 | + crypto/pkcs12/p12_mutl.c | 5 +++++ |
| 18 | + crypto/pkcs12/p12_npas.c | 5 +++-- |
| 19 | + crypto/pkcs12/pk12err.c | 4 +++- |
| 20 | + crypto/pkcs7/pk7_mime.c | 8 ++++++-- |
| 21 | + include/openssl/pkcs12err.h | 3 ++- |
| 22 | + 7 files changed, 40 insertions(+), 7 deletions(-) |
| 23 | + |
| 24 | +diff --git a/crypto/err/openssl.txt b/crypto/err/openssl.txt |
| 25 | +index f302dbc06..900b11ee9 100644 |
| 26 | +--- a/crypto/err/openssl.txt |
| 27 | ++++ b/crypto/err/openssl.txt |
| 28 | +@@ -1,4 +1,4 @@ |
| 29 | +-# Copyright 1999-2023 The OpenSSL Project Authors. All Rights Reserved. |
| 30 | ++# Copyright 1999-2024 The OpenSSL Project Authors. All Rights Reserved. |
| 31 | + # |
| 32 | + # Licensed under the OpenSSL license (the "License"). You may not use |
| 33 | + # this file except in compliance with the License. You can obtain a copy |
| 34 | +@@ -970,6 +970,7 @@ PKCS12_F_PKCS12_SETUP_MAC:122:PKCS12_setup_mac |
| 35 | + PKCS12_F_PKCS12_SET_MAC:123:PKCS12_set_mac |
| 36 | + PKCS12_F_PKCS12_UNPACK_AUTHSAFES:130:PKCS12_unpack_authsafes |
| 37 | + PKCS12_F_PKCS12_UNPACK_P7DATA:131:PKCS12_unpack_p7data |
| 38 | ++PKCS12_F_PKCS12_UNPACK_P7ENCDATA:134:PKCS12_unpack_p7encdata |
| 39 | + PKCS12_F_PKCS12_VERIFY_MAC:126:PKCS12_verify_mac |
| 40 | + PKCS12_F_PKCS8_ENCRYPT:125:PKCS8_encrypt |
| 41 | + PKCS12_F_PKCS8_SET0_PBE:132:PKCS8_set0_pbe |
| 42 | +diff --git a/crypto/pkcs12/p12_add.c b/crypto/pkcs12/p12_add.c |
| 43 | +index af184c86a..f2fbe9660 100644 |
| 44 | +--- a/crypto/pkcs12/p12_add.c |
| 45 | ++++ b/crypto/pkcs12/p12_add.c |
| 46 | +@@ -76,6 +76,12 @@ STACK_OF(PKCS12_SAFEBAG) *PKCS12_unpack_p7data(PKCS7 *p7) |
| 47 | + PKCS12_R_CONTENT_TYPE_NOT_DATA); |
| 48 | + return NULL; |
| 49 | + } |
| 50 | ++ |
| 51 | ++ if (p7->d.data == NULL) { |
| 52 | ++ PKCS12err(PKCS12_F_PKCS12_UNPACK_P7DATA, PKCS12_R_DECODE_ERROR); |
| 53 | ++ return NULL; |
| 54 | ++ } |
| 55 | ++ |
| 56 | + return ASN1_item_unpack(p7->d.data, ASN1_ITEM_rptr(PKCS12_SAFEBAGS)); |
| 57 | + } |
| 58 | + |
| 59 | +@@ -132,6 +138,12 @@ STACK_OF(PKCS12_SAFEBAG) *PKCS12_unpack_p7encdata(PKCS7 *p7, const char *pass, |
| 60 | + { |
| 61 | + if (!PKCS7_type_is_encrypted(p7)) |
| 62 | + return NULL; |
| 63 | ++ |
| 64 | ++ if (p7->d.encrypted == NULL) { |
| 65 | ++ PKCS12err(PKCS12_F_PKCS12_UNPACK_P7ENCDATA, PKCS12_R_DECODE_ERROR); |
| 66 | ++ return NULL; |
| 67 | ++ } |
| 68 | ++ |
| 69 | + return PKCS12_item_decrypt_d2i(p7->d.encrypted->enc_data->algorithm, |
| 70 | + ASN1_ITEM_rptr(PKCS12_SAFEBAGS), |
| 71 | + pass, passlen, |
| 72 | +@@ -159,6 +171,13 @@ STACK_OF(PKCS7) *PKCS12_unpack_authsafes(const PKCS12 *p12) |
| 73 | + PKCS12_R_CONTENT_TYPE_NOT_DATA); |
| 74 | + return NULL; |
| 75 | + } |
| 76 | ++ |
| 77 | ++ if (p12->authsafes->d.data == NULL) { |
| 78 | ++ PKCS12err(PKCS12_F_PKCS12_UNPACK_AUTHSAFES, |
| 79 | ++ PKCS12_R_DECODE_ERROR); |
| 80 | ++ return NULL; |
| 81 | ++ } |
| 82 | ++ |
| 83 | + return ASN1_item_unpack(p12->authsafes->d.data, |
| 84 | + ASN1_ITEM_rptr(PKCS12_AUTHSAFES)); |
| 85 | + } |
| 86 | +diff --git a/crypto/pkcs12/p12_mutl.c b/crypto/pkcs12/p12_mutl.c |
| 87 | +index 3658003fe..766c9c1e9 100644 |
| 88 | +--- a/crypto/pkcs12/p12_mutl.c |
| 89 | ++++ b/crypto/pkcs12/p12_mutl.c |
| 90 | +@@ -93,6 +93,11 @@ static int pkcs12_gen_mac(PKCS12 *p12, const char *pass, int passlen, |
| 91 | + return 0; |
| 92 | + } |
| 93 | + |
| 94 | ++ if (p12->authsafes->d.data == NULL) { |
| 95 | ++ PKCS12err(PKCS12_F_PKCS12_GEN_MAC, PKCS12_R_DECODE_ERROR); |
| 96 | ++ return 0; |
| 97 | ++ } |
| 98 | ++ |
| 99 | + salt = p12->mac->salt->data; |
| 100 | + saltlen = p12->mac->salt->length; |
| 101 | + if (!p12->mac->iter) |
| 102 | +diff --git a/crypto/pkcs12/p12_npas.c b/crypto/pkcs12/p12_npas.c |
| 103 | +index 0334289a8..130337638 100644 |
| 104 | +--- a/crypto/pkcs12/p12_npas.c |
| 105 | ++++ b/crypto/pkcs12/p12_npas.c |
| 106 | +@@ -78,8 +78,9 @@ static int newpass_p12(PKCS12 *p12, const char *oldpass, const char *newpass) |
| 107 | + bags = PKCS12_unpack_p7data(p7); |
| 108 | + } else if (bagnid == NID_pkcs7_encrypted) { |
| 109 | + bags = PKCS12_unpack_p7encdata(p7, oldpass, -1); |
| 110 | +- if (!alg_get(p7->d.encrypted->enc_data->algorithm, |
| 111 | +- &pbe_nid, &pbe_iter, &pbe_saltlen)) |
| 112 | ++ if (p7->d.encrypted == NULL |
| 113 | ++ || !alg_get(p7->d.encrypted->enc_data->algorithm, |
| 114 | ++ &pbe_nid, &pbe_iter, &pbe_saltlen)) |
| 115 | + goto err; |
| 116 | + } else { |
| 117 | + continue; |
| 118 | +diff --git a/crypto/pkcs12/pk12err.c b/crypto/pkcs12/pk12err.c |
| 119 | +index 38ce5197e..224941d74 100644 |
| 120 | +--- a/crypto/pkcs12/pk12err.c |
| 121 | ++++ b/crypto/pkcs12/pk12err.c |
| 122 | +@@ -1,6 +1,6 @@ |
| 123 | + /* |
| 124 | + * Generated by util/mkerr.pl DO NOT EDIT |
| 125 | +- * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. |
| 126 | ++ * Copyright 1995-2024 The OpenSSL Project Authors. All Rights Reserved. |
| 127 | + * |
| 128 | + * Licensed under the OpenSSL license (the "License"). You may not use |
| 129 | + * this file except in compliance with the License. You can obtain a copy |
| 130 | +@@ -58,6 +58,8 @@ static const ERR_STRING_DATA PKCS12_str_functs[] = { |
| 131 | + "PKCS12_unpack_authsafes"}, |
| 132 | + {ERR_PACK(ERR_LIB_PKCS12, PKCS12_F_PKCS12_UNPACK_P7DATA, 0), |
| 133 | + "PKCS12_unpack_p7data"}, |
| 134 | ++ {ERR_PACK(ERR_LIB_PKCS12, PKCS12_F_PKCS12_UNPACK_P7ENCDATA, 0), |
| 135 | ++ "PKCS12_unpack_p7encdata"}, |
| 136 | + {ERR_PACK(ERR_LIB_PKCS12, PKCS12_F_PKCS12_VERIFY_MAC, 0), |
| 137 | + "PKCS12_verify_mac"}, |
| 138 | + {ERR_PACK(ERR_LIB_PKCS12, PKCS12_F_PKCS8_ENCRYPT, 0), "PKCS8_encrypt"}, |
| 139 | +diff --git a/crypto/pkcs7/pk7_mime.c b/crypto/pkcs7/pk7_mime.c |
| 140 | +index 19e686814..b457108c9 100644 |
| 141 | +--- a/crypto/pkcs7/pk7_mime.c |
| 142 | ++++ b/crypto/pkcs7/pk7_mime.c |
| 143 | +@@ -30,10 +30,14 @@ int SMIME_write_PKCS7(BIO *bio, PKCS7 *p7, BIO *data, int flags) |
| 144 | + { |
| 145 | + STACK_OF(X509_ALGOR) *mdalgs; |
| 146 | + int ctype_nid = OBJ_obj2nid(p7->type); |
| 147 | +- if (ctype_nid == NID_pkcs7_signed) |
| 148 | ++ |
| 149 | ++ if (ctype_nid == NID_pkcs7_signed) { |
| 150 | ++ if (p7->d.sign == NULL) |
| 151 | ++ return 0; |
| 152 | + mdalgs = p7->d.sign->md_algs; |
| 153 | +- else |
| 154 | ++ } else { |
| 155 | + mdalgs = NULL; |
| 156 | ++ } |
| 157 | + |
| 158 | + flags ^= SMIME_OLDMIME; |
| 159 | + |
| 160 | +diff --git a/include/openssl/pkcs12err.h b/include/openssl/pkcs12err.h |
| 161 | +index eff5eb260..b0f5446c0 100644 |
| 162 | +--- a/include/openssl/pkcs12err.h |
| 163 | ++++ b/include/openssl/pkcs12err.h |
| 164 | +@@ -1,6 +1,6 @@ |
| 165 | + /* |
| 166 | + * Generated by util/mkerr.pl DO NOT EDIT |
| 167 | +- * Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved. |
| 168 | ++ * Copyright 1995-2024 The OpenSSL Project Authors. All Rights Reserved. |
| 169 | + * |
| 170 | + * Licensed under the OpenSSL license (the "License"). You may not use |
| 171 | + * this file except in compliance with the License. You can obtain a copy |
| 172 | +@@ -49,6 +49,7 @@ int ERR_load_PKCS12_strings(void); |
| 173 | + # define PKCS12_F_PKCS12_SET_MAC 123 |
| 174 | + # define PKCS12_F_PKCS12_UNPACK_AUTHSAFES 130 |
| 175 | + # define PKCS12_F_PKCS12_UNPACK_P7DATA 131 |
| 176 | ++# define PKCS12_F_PKCS12_UNPACK_P7ENCDATA 134 |
| 177 | + # define PKCS12_F_PKCS12_VERIFY_MAC 126 |
| 178 | + # define PKCS12_F_PKCS8_ENCRYPT 125 |
| 179 | + # define PKCS12_F_PKCS8_SET0_PBE 132 |
| 180 | +-- |
| 181 | +2.33.8 |
| 182 | + |
0 commit comments