Merge commit from fork

When building the byte view of a subject alternative name, NIOSSL reads
the `ASN1_STRING` member of the BoringSSL `GENERAL_NAME` union for every
entry without checking the name's type. The union only holds an
`ASN1_STRING` for the string-shaped GeneralName types (`dNSName`,
`rfc822Name`, `uniformResourceIdentifier`, `iPAddress`). For the other
types (`otherName`, `x400Address`, `directoryName`, `ediPartyName`,
`registeredID`) the union holds a different structure, so this reads the
buffer's base pointer and length out of unrelated memory.

The SAN list is parsed from an X.509 certificate, which is untrusted in
the usual TLS/mTLS case, and `contents` is reachable through the public,
albeit underscored, `_subjectAlternativeNames()` API.

NIOSSL should only interpret the union as an `ASN1_STRING` for the
string-shaped types and expose empty contents for the rest.

- In `_SubjectAlternativeNames.Storage.subscript`, switch on the GeneralName
  type. Build the `ASN1_STRING`-backed byte buffer only for `dNSName`,
  `rfc822Name`, `uniformResourceIdentifier` and `iPAddress`; expose an empty
  buffer for every other type.
- Add a test that parses a certificate carrying an `otherName` SAN followed by a
  `dNSName`, asserting the `otherName` has empty `contents` while the `dNSName`
  is read normally.

Reading the `contents` of a non-string subject alternative name no longer reads
out of bounds; it returns empty. String-typed names are unaffected. NIOSSL's
built-in identity verification already filters by name type, so the default
handshake path was not affected; this fixes the behaviour for callers of
`_subjectAlternativeNames()` that read `contents` for arbitrary name types.
This commit is contained in:
Si Beaumont authored and GitHub committed 2026-07-15 13:05:47 +01:00
1 parent fd221573c7
commit d930168b86
2 files changed
+60 -5

No files matched your search

+20 -5
View File
@@ -51,11 +51,26 @@ public struct _SubjectAlternativeNames {
fatalError("Unexpected null pointer when unwrapping SAN value")
}
let contents = UnsafeBufferPointer(
start: CNIOBoringSSL_ASN1_STRING_get0_data(name.pointee.d.ia5),
count: Int(CNIOBoringSSL_ASN1_STRING_length(name.pointee.d.ia5))
)
return .init(nameType: .init(name.pointee.type), contents: .init(collection: self, buffer: contents))
let type = _SubjectAlternativeName.NameType(name.pointee.type)
let buffer: UnsafeBufferPointer<UInt8>
switch type {
case .dnsName, .ipAddress, .uri, .email:
// These are the string-shaped GeneralName types whose `d` union member is an
// ASN1_STRING (dNSName/rfc822Name/URI are ASN1_IA5STRING, iPAddress is
// ASN1_OCTET_STRING), so reading it as an ASN1_STRING is safe.
buffer = UnsafeBufferPointer(
start: CNIOBoringSSL_ASN1_STRING_get0_data(name.pointee.d.ia5),
count: Int(CNIOBoringSSL_ASN1_STRING_length(name.pointee.d.ia5))
)
default:
// Every other type (otherName, x400Address, directoryName, ediPartyName,
// registeredID) has a non-ASN1_STRING `d` union member. Reinterpreting it as an
// ASN1_STRING would take the base pointer and length from unrelated memory — a heap
// out-of-bounds read — so expose no contents.
buffer = UnsafeBufferPointer(start: nil, count: 0)
}
return _SubjectAlternativeName(nameType: type, contents: .init(collection: self, buffer: buffer))
}
deinit {
@@ -143,6 +143,26 @@ let certWithAllSupportedSANTypes = """
-----END CERTIFICATE-----
"""
// A certificate whose SAN extension carries a non-string GeneralName (an `otherName`)
// followed by a `dNSName`, used to check how NIOSSL surfaces the contents of each. Created with:
// openssl req -x509 -newkey ec -pkeyopt ec_paramgen_curve:prime256v1 -sha256 -days 3650 -nodes \
// -keyout key.pem -out cert.pem -subj '/CN=example.com' \
// -addext 'subjectAltName=otherName:1.3.6.1.4.1.311.20.2.3;UTF8:upn@example.com,DNS:example.com'
let certWithOtherNameSAN = """
-----BEGIN CERTIFICATE-----
MIIBvTCCAWKgAwIBAgIUG5kO7W4Q1Iegri+QiAVHM/U31kEwCgYIKoZIzj0EAwIw
FjEUMBIGA1UEAwwLZXhhbXBsZS5jb20wHhcNMjYwNzA3MTcyNDUxWhcNMzYwNzA0
MTcyNDUxWjAWMRQwEgYDVQQDDAtleGFtcGxlLmNvbTBZMBMGByqGSM49AgEGCCqG
SM49AwEHA0IABLiGGM7RdaZShwg6MY//g7ir6KBD+mdSAurHLKNCk0st89Gto1ab
zzOWNaRFy5D851aF+xDxo6t3tlAXvFhKEW2jgY0wgYowHQYDVR0OBBYEFOCHFhfI
biL4d3CFvgMxL88RiUE5MB8GA1UdIwQYMBaAFOCHFhfIbiL4d3CFvgMxL88RiUE5
MA8GA1UdEwEB/wQFMAMBAf8wNwYDVR0RBDAwLqAfBgorBgEEAYI3FAIDoBEMD3Vw
bkBleGFtcGxlLmNvbYILZXhhbXBsZS5jb20wCgYIKoZIzj0EAwIDSQAwRgIhAPC/
NL85+5HvlzDn6BFkG8MEEfFM+tIi1Bc00qcNWr3lAiEAszKnEc5QUNdoRbagZOGs
b7zWUh0L+LBUmEO6a6XsJp0=
-----END CERTIFICATE-----
"""
func makeTemporaryFile(fileExtension: String = "", customPath: String = "") throws -> String {
var template = "\(FileManager.default.temporaryDirectory.path)/niotestXXXXXXX\(fileExtension)"
// If a custom file path is passed in then a new directory has to also be created. Then the file can be written to that directory.
@@ -421,6 +441,26 @@ class SSLCertificateTest: XCTestCase {
XCTAssertEqual(String(decoding: sans[6].contents, as: UTF8.self), "http://example.org/")
}
func testSubjectAlternativeNameWithNonStringTypeHasEmptyContents() throws {
// NIOSSL exposes the raw bytes of a subject alternative name only for the string-shaped
// GeneralName types (dNSName, rfc822Name, uniformResourceIdentifier, iPAddress). Names of
// any other type (otherName, x400Address, directoryName, ediPartyName, registeredID) have
// empty `contents`. String-typed names in the same certificate are read as normal.
let cert = try NIOSSLCertificate(bytes: .init(certWithOtherNameSAN.utf8), format: .pem)
let sans = cert._subjectAlternativeNames()
XCTAssertEqual(sans.count, 2)
// otherName is not a string-shaped type (its raw GeneralName value is 0), so its contents
// are empty.
XCTAssertEqual(sans[0].nameType.rawValue, 0)
XCTAssertEqual(sans[0].contents.count, 0)
XCTAssertEqual(Array(sans[0].contents), [])
// dNSName is a string-shaped type, so its contents are the name's bytes.
XCTAssertEqual(sans[1].nameType, .dnsName)
XCTAssertEqual(String(decoding: sans[1].contents, as: UTF8.self), "example.com")
}
func testSubjectName() throws {
let cert = try NIOSSLCertificate(bytes: .init(samplePemCert.utf8), format: .pem)
XCTAssertEqual(