Skip to content

Remove deprecated X509_NAME_get_text_by_NID()#2389

Open
yadij wants to merge 1 commit intosquid-cache:masterfrom
yadij:openssl-v4-support-2
Open

Remove deprecated X509_NAME_get_text_by_NID()#2389
yadij wants to merge 1 commit intosquid-cache:masterfrom
yadij:openssl-v4-support-2

Conversation

@yadij
Copy link
Contributor

@yadij yadij commented Mar 15, 2026

OpenSSL v4 deprecates this function and produces build errors
when combined with the Squid default -Wall -Werror.

Replace with the recommended X509_NAME_get_index_by_NID()
function which is also supported by older OpenSSL.

@yadij
Copy link
Contributor Author

yadij commented Mar 15, 2026

Build tested, but not yet run-time tested. There are many other OpenSSL 4 issues to fix.

@rousskov rousskov self-requested a review March 15, 2026 14:05
Copy link
Contributor

@rousskov rousskov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Build tested, but not yet run-time tested.

Please do not post untested code for review unless you want to discuss some code design issues or some such. In the latter case, please post a draft PR and ask specific questions to guide that discussion.

There are many other OpenSSL 4 issues to fix.

Please do not post more related PRs until this PR is merged and the current backlog is dealt with.

int pos = -1;
pos = X509_NAME_get_index_by_NID(nm, nid, pos);
if (pos < 0) {
debugs(83, 3, (pos == -2 ? "Invalid" : "Missing") << " SSL certificate subject name");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In current PR code, negative pos does not always imply that the certificate has an invalid or missing "subject name". The name may be there, but it may be missing the requested name entry or field (e.g., CN). When other change requests are addressed, it will never imply that. The suggestion below reflects other change requests:

Suggested change
debugs(83, 3, (pos == -2 ? "Invalid" : "Missing") << " SSL certificate subject name");
debugs(83, 3, (pos == -2 ? "invalid" : "missing") << " certificate subject name entry");

Converting nid into human-friendly text inside this error message would be nice, but I do not insist on that.


if (nameLen > 0)
return name;
const auto nm = X509_get_subject_name(x509);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please test nm and exit on lookup errors (with a debugs() message) instead of using nil nm in lower code, even if the current code can cope with nil nm. Doing so helps in triage and makes this code safer long-term.

Comment on lines +1065 to +1066
int pos = -1;
pos = X509_NAME_get_index_by_NID(nm, nid, pos);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If possible, please make pos constant and apply AAA:

Suggested change
int pos = -1;
pos = X509_NAME_get_index_by_NID(nm, nid, pos);
const auto pos = X509_NAME_get_index_by_NID(nm, nid, -1);


return nullptr;
const auto str = X509_NAME_ENTRY_get_data(X509_NAME_get_entry(nm, pos));
xstrncpy(name, reinterpret_cast<const char *>(ASN1_STRING_get0_data(str)), sizeof(name));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ASN1_STRING_get0_data() manual page says that "In general it cannot be assumed that the data returned by ASN1_STRING_get0_data() is null terminated or does not contain embedded nulls.`

To avoid out of bound reads, we should at least be using ASN1_STRING_length() to limit what we read/copy (in addition to sizeof(name), of course). To avoid misleading code readers, we should not use c-string functions for copying this raw data.

We should return nil instead of truncated names when the name buffer is too small.

N.B. AFAICT, "get0" in OpenSSL function names means something like "raw access to an internal buffer" rather than a "c-string".

}

return nullptr;
const auto str = X509_NAME_ENTRY_get_data(X509_NAME_get_entry(nm, pos));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar lack of error checking concerns apply here, especially since ASN1_STRING_get0_data() is going to crash if given a nil str. Please check every OpenSSL call that this PR adds or modifies.

Comment on lines +877 to +884
int pos = -1;
pos = X509_NAME_get_index_by_NID(name, nid, pos);
if (pos < 0) {
debugs(83, 3, (pos == -2 ? "Invalid" : "Missing") << " SSL attribute name '" << attribute_name << "'");
return nullptr;
}
auto str = X509_NAME_ENTRY_get_data(X509_NAME_get_entry(name, pos));
xstrncpy(buffer, reinterpret_cast<const char *>(ASN1_STRING_get0_data(str)), sizeof(buffer));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do not duplicate this non-trivial and error-prone code! Add and reuse an ssl_get_attribute_by_NID() or similar function instead.

@@ -1060,14 +1060,18 @@ static const char *getSubjectEntry(X509 *x509, int nid)
return nullptr;

// TODO: What if the entry is a UTF8String? See X509_NAME_get_index_by_NID(3ssl).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAICT, this comment (and the matching manual page text) were written for X509_NAME_get_text_by_NID() call that this PR upgrades. The comment becomes misplaced in PR code -- it looks like it applies to X509_get_subject_name(), but it does not.

The comment mentions but does not apply to X509_NAME_get_index_by_NID() that official code did not have, but PR code has, causing additional confusion.

The comment now applies to ASN1_STRING_get0_data() call further below, I guess.

Please move and adjust to address all these problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants