|
| 1 | +Backport the OpenSSL 4 accessor fixes from https://github.com/grpc/grpc/pull/41932 |
| 2 | +for the pinned gRPC 1.75 branch. |
| 3 | + |
| 4 | +diff --git a/src/core/credentials/transport/tls/grpc_tls_crl_provider.cc b/src/core/credentials/transport/tls/grpc_tls_crl_provider.cc |
| 5 | +--- a/src/core/credentials/transport/tls/grpc_tls_crl_provider.cc |
| 6 | ++++ b/src/core/credentials/transport/tls/grpc_tls_crl_provider.cc |
| 7 | +@@ -56,12 +56,12 @@ |
| 8 | + if (crl == nullptr) { |
| 9 | + return absl::InvalidArgumentError("crl cannot be null"); |
| 10 | + } |
| 11 | +- X509_NAME* issuer = X509_CRL_get_issuer(crl); |
| 12 | ++ auto* issuer = X509_CRL_get_issuer(crl); |
| 13 | + if (issuer == nullptr) { |
| 14 | + return absl::InvalidArgumentError("crl cannot have null issuer"); |
| 15 | + } |
| 16 | + unsigned char* buf = nullptr; |
| 17 | +- int len = i2d_X509_NAME(issuer, &buf); |
| 18 | ++ int len = i2d_X509_NAME(const_cast<X509_NAME*>(issuer), &buf); |
| 19 | + if (len < 0 || buf == nullptr) { |
| 20 | + return absl::InvalidArgumentError("crl cannot have null issuer"); |
| 21 | + } |
| 22 | +diff --git a/src/core/tsi/ssl_transport_security.cc b/src/core/tsi/ssl_transport_security.cc |
| 23 | +--- a/src/core/tsi/ssl_transport_security.cc |
| 24 | ++++ b/src/core/tsi/ssl_transport_security.cc |
| 25 | +@@ -42,7 +42,9 @@ |
| 26 | + #include <grpc/support/thd_id.h> |
| 27 | + #include <openssl/bio.h> |
| 28 | + #include <openssl/crypto.h> // For OPENSSL_free |
| 29 | ++#if !defined(OPENSSL_NO_ENGINE) |
| 30 | + #include <openssl/engine.h> |
| 31 | ++#endif |
| 32 | + #include <openssl/err.h> |
| 33 | + #include <openssl/ssl.h> |
| 34 | + #include <openssl/tls1.h> |
| 35 | +@@ -406,9 +408,7 @@ |
| 36 | + static tsi_result ssl_get_x509_common_name(X509* cert, unsigned char** utf8, |
| 37 | + size_t* utf8_size) { |
| 38 | + int common_name_index = -1; |
| 39 | +- X509_NAME_ENTRY* common_name_entry = nullptr; |
| 40 | +- ASN1_STRING* common_name_asn1 = nullptr; |
| 41 | +- X509_NAME* subject_name = X509_get_subject_name(cert); |
| 42 | ++ auto* subject_name = X509_get_subject_name(cert); |
| 43 | + int utf8_returned_size = 0; |
| 44 | + if (subject_name == nullptr) { |
| 45 | + VLOG(2) << "Could not get subject name from certificate."; |
| 46 | +@@ -420,12 +420,13 @@ |
| 47 | + VLOG(2) << "Could not get common name of subject from certificate."; |
| 48 | + return TSI_NOT_FOUND; |
| 49 | + } |
| 50 | +- common_name_entry = X509_NAME_get_entry(subject_name, common_name_index); |
| 51 | ++ auto* common_name_entry = |
| 52 | ++ X509_NAME_get_entry(subject_name, common_name_index); |
| 53 | + if (common_name_entry == nullptr) { |
| 54 | + LOG(ERROR) << "Could not get common name entry from certificate."; |
| 55 | + return TSI_INTERNAL_ERROR; |
| 56 | + } |
| 57 | +- common_name_asn1 = X509_NAME_ENTRY_get_data(common_name_entry); |
| 58 | ++ auto* common_name_asn1 = X509_NAME_ENTRY_get_data(common_name_entry); |
| 59 | + if (common_name_asn1 == nullptr) { |
| 60 | + LOG(ERROR) << "Could not get common name entry asn1 from certificate."; |
| 61 | + return TSI_INTERNAL_ERROR; |
| 62 | +@@ -466,7 +467,7 @@ |
| 63 | + static tsi_result peer_property_from_x509_subject(X509* cert, |
| 64 | + tsi_peer_property* property, |
| 65 | + bool is_verified_root_cert) { |
| 66 | +- X509_NAME* subject_name = X509_get_subject_name(cert); |
| 67 | ++ auto* subject_name = X509_get_subject_name(cert); |
| 68 | + if (subject_name == nullptr) { |
| 69 | + GRPC_TRACE_LOG(tsi, INFO) << "Could not get subject name from certificate."; |
| 70 | + return TSI_NOT_FOUND; |
| 71 | +@@ -562,17 +563,30 @@ |
| 72 | + char ntop_buf[INET6_ADDRSTRLEN]; |
| 73 | + int af; |
| 74 | + |
| 75 | ++#if OPENSSL_VERSION_NUMBER >= 0x10100000L |
| 76 | ++ if (ASN1_STRING_length(subject_alt_name->d.iPAddress) == 4) { |
| 77 | ++ af = AF_INET; |
| 78 | ++ } else if (ASN1_STRING_length(subject_alt_name->d.iPAddress) == 16) { |
| 79 | ++ af = AF_INET6; |
| 80 | ++#else |
| 81 | + if (subject_alt_name->d.iPAddress->length == 4) { |
| 82 | + af = AF_INET; |
| 83 | + } else if (subject_alt_name->d.iPAddress->length == 16) { |
| 84 | + af = AF_INET6; |
| 85 | ++#endif |
| 86 | + } else { |
| 87 | + LOG(ERROR) << "SAN IP Address contained invalid IP"; |
| 88 | + result = TSI_INTERNAL_ERROR; |
| 89 | + break; |
| 90 | + } |
| 91 | ++#if OPENSSL_VERSION_NUMBER >= 0x10100000L |
| 92 | ++ const char* name = |
| 93 | ++ inet_ntop(af, ASN1_STRING_get0_data(subject_alt_name->d.iPAddress), |
| 94 | ++ ntop_buf, INET6_ADDRSTRLEN); |
| 95 | ++#else |
| 96 | + const char* name = inet_ntop(af, subject_alt_name->d.iPAddress->data, |
| 97 | + ntop_buf, INET6_ADDRSTRLEN); |
| 98 | ++#endif |
| 99 | + if (name == nullptr) { |
| 100 | + LOG(ERROR) << "Could not get IP string from asn1 octet."; |
| 101 | + result = TSI_INTERNAL_ERROR; |
| 102 | +@@ -855,13 +869,13 @@ |
| 103 | + break; // We're at the end of stream. |
| 104 | + } |
| 105 | + if (root_names != nullptr) { |
| 106 | +- root_name = X509_get_subject_name(root); |
| 107 | +- if (root_name == nullptr) { |
| 108 | ++ auto* root_subject = X509_get_subject_name(root); |
| 109 | ++ if (root_subject == nullptr) { |
| 110 | + LOG(ERROR) << "Could not get name from root certificate."; |
| 111 | + result = TSI_INVALID_ARGUMENT; |
| 112 | + break; |
| 113 | + } |
| 114 | +- root_name = X509_NAME_dup(root_name); |
| 115 | ++ root_name = X509_NAME_dup(const_cast<X509_NAME*>(root_subject)); |
| 116 | + if (root_name == nullptr) { |
| 117 | + result = TSI_OUT_OF_RESOURCES; |
| 118 | + break; |
| 119 | +diff --git a/src/core/tsi/ssl_transport_security_utils.cc b/src/core/tsi/ssl_transport_security_utils.cc |
| 120 | +--- a/src/core/tsi/ssl_transport_security_utils.cc |
| 121 | ++++ b/src/core/tsi/ssl_transport_security_utils.cc |
| 122 | +@@ -281,11 +281,11 @@ |
| 123 | + if (cert == nullptr || crl == nullptr) { |
| 124 | + return false; |
| 125 | + } |
| 126 | +- X509_NAME* cert_issuer_name = X509_get_issuer_name(cert); |
| 127 | ++ auto* cert_issuer_name = X509_get_issuer_name(cert); |
| 128 | + if (cert == nullptr) { |
| 129 | + return false; |
| 130 | + } |
| 131 | +- X509_NAME* crl_issuer_name = X509_CRL_get_issuer(crl); |
| 132 | ++ auto* crl_issuer_name = X509_CRL_get_issuer(crl); |
| 133 | + if (crl_issuer_name == nullptr) { |
| 134 | + return false; |
| 135 | + } |
| 136 | +@@ -316,9 +316,9 @@ |
| 137 | + if (cert == nullptr) { |
| 138 | + return absl::InvalidArgumentError("cert cannot be null"); |
| 139 | + } |
| 140 | +- X509_NAME* issuer = X509_get_issuer_name(cert); |
| 141 | ++ auto* issuer = X509_get_issuer_name(cert); |
| 142 | + unsigned char* buf = nullptr; |
| 143 | +- int len = i2d_X509_NAME(issuer, &buf); |
| 144 | ++ int len = i2d_X509_NAME(const_cast<X509_NAME*>(issuer), &buf); |
| 145 | + if (len < 0 || buf == nullptr) { |
| 146 | + return absl::InvalidArgumentError("could not read issuer name from cert"); |
| 147 | + } |
| 148 | +@@ -338,7 +338,8 @@ |
| 149 | + if (X509_get_ext_by_NID(cert, NID_authority_key_identifier, j) != -1) { |
| 150 | + return absl::InvalidArgumentError("Could not get AKID from certificate."); |
| 151 | + } |
| 152 | +- akid = X509_EXTENSION_get_data(X509_get_ext(cert, j)); |
| 153 | ++ akid = const_cast<ASN1_OCTET_STRING*>( |
| 154 | ++ X509_EXTENSION_get_data(X509_get_ext(cert, j))); |
| 155 | + } else { |
| 156 | + return absl::InvalidArgumentError("Could not get AKID from certificate."); |
| 157 | + } |
| 158 | +@@ -363,7 +364,8 @@ |
| 159 | + if (X509_CRL_get_ext_by_NID(crl, NID_authority_key_identifier, j) != -1) { |
| 160 | + return absl::InvalidArgumentError("Could not get AKID from crl."); |
| 161 | + } |
| 162 | +- akid = X509_EXTENSION_get_data(X509_CRL_get_ext(crl, j)); |
| 163 | ++ akid = const_cast<ASN1_OCTET_STRING*>( |
| 164 | ++ X509_EXTENSION_get_data(X509_CRL_get_ext(crl, j))); |
| 165 | + } else { |
| 166 | + return absl::InvalidArgumentError("Could not get AKID from crl."); |
| 167 | + } |
0 commit comments