Skip to content

Commit c8eb445

Browse files
authored
Upgrade OpenSSL source selection to 4.x (crazywhalecc#1298)
2 parents 9e7ee6e + f2111f2 commit c8eb445

6 files changed

Lines changed: 547 additions & 3 deletions

File tree

‎config/pkg/lib/openssl.yml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ openssl:
44
source:
55
type: ghrel
66
repo: openssl/openssl
7-
match: openssl-3.+\.tar\.gz
7+
match: ^openssl-4\.\d+\.\d+\.tar\.gz$
88
prefer-stable: true
99
source-mirror:
1010
type: filelist
1111
url: 'https://www.openssl.org/source/'
12-
regex: '/href="(?<file>openssl-(?<version>3\.[^"]+)\.tar\.gz)"/'
12+
regex: '/href="(?<file>openssl-(?<version>4\.\d+\.\d+)\.tar\.gz)"/'
1313
metadata:
1414
license-files: [LICENSE.txt]
1515
license: OpenSSL

‎src/Package/Library/grpc.php‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,17 @@
1212
use StaticPHP\Runtime\Executor\UnixCMakeExecutor;
1313
use StaticPHP\Toolchain\Interface\ToolchainInterface;
1414
use StaticPHP\Util\FileSystem;
15+
use StaticPHP\Util\SourcePatcher;
1516
use StaticPHP\Util\System\LinuxUtil;
1617

1718
#[Library('grpc')]
1819
class grpc
1920
{
2021
#[PatchBeforeBuild]
21-
#[PatchDescription('Fix re2 pcre.h include order for compatibility')]
22+
#[PatchDescription('Fix OpenSSL 4 accessors and re2 pcre.h include order')]
2223
public function patchBeforeBuild(LibraryPackage $lib): bool
2324
{
25+
SourcePatcher::patchFile('grpc-openssl4.patch', $lib->getSourceDir());
2426
FileSystem::replaceFileStr(
2527
"{$lib->getSourceDir()}/third_party/re2/util/pcre.h",
2628
["#define UTIL_PCRE_H_\n#include <stdint.h>", '#define UTIL_PCRE_H_'],

‎src/Package/Library/krb5.php‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,27 @@
66

77
use StaticPHP\Attribute\Package\BuildFor;
88
use StaticPHP\Attribute\Package\Library;
9+
use StaticPHP\Attribute\Package\PatchBeforeBuild;
10+
use StaticPHP\Attribute\PatchDescription;
911
use StaticPHP\Package\LibraryPackage;
1012
use StaticPHP\Package\PackageInstaller;
1113
use StaticPHP\Runtime\Executor\UnixAutoconfExecutor;
1214
use StaticPHP\Runtime\SystemTarget;
1315
use StaticPHP\Toolchain\Interface\ToolchainInterface;
1416
use StaticPHP\Toolchain\ZigToolchain;
17+
use StaticPHP\Util\SourcePatcher;
1518
use StaticPHP\Util\SPCConfigUtil;
1619

1720
#[Library('krb5')]
1821
class krb5
1922
{
23+
#[PatchBeforeBuild]
24+
#[PatchDescription('Support OpenSSL 4 accessors in Kerberos 1.22')]
25+
public function patchBeforeBuild(LibraryPackage $lib): bool
26+
{
27+
return SourcePatcher::patchFile('krb5-openssl4.patch', $lib->getSourceDir());
28+
}
29+
2030
#[BuildFor('Linux')]
2131
#[BuildFor('Darwin')]
2232
public function build(LibraryPackage $lib, PackageInstaller $installer, ToolchainInterface $toolchain): void
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
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

Comments
 (0)