#1 2026-11-18 12:01:34

sakura
Member
From: Germany
Registered: 2018-02-21
Posts: 245
Website

mormot.crypt.x509: IP addresses in Subject Alternative Name

Summary

mormot.crypt.x509 writes every entry of the Subject Alternative Name (and Issuer Alternative Name) extension as dNSName ([2]), including IPv4 literals. RFC 5280 §4.2.1.6 requires IP addresses to be encoded as iPAddress ([7], OCTET STRING of 4 bytes in network order for IPv4). Browsers and TLS clients (Chrome, Safari/iOS, Firefox, .NET, Go) do not match an IP address against a dNSName entry, so a server certificate generated by mORMot for e.g. 192.168.1.20 is rejected when the server is accessed by IP address (Chrome: NET::ERR_CERT_COMMON_NAME_INVALID).

Where

src/crypt/mormot.crypt.x509.pas, function CsvToDns():

function CsvToDns(p: PUtf8Char): RawByteString;
begin
  result := '';
  while p <> nil do
    Append(result, AsnTyped(TrimU(GetNextItem(p)), ASN1_CTX2));
end;

It is called from CertInfoCompute() for xeSubjectAlternativeName and xeIssuerAlternativeName, and from TX509Crl for xceIssuerAlternativeName.

Reading is already correct: TXTbsCertificate.AddNextExtensions decodes ASN1_CTX7 via AsnDecIp(). So a certificate with a proper iPAddress entry loads fine and GetSubjects returns the IP as text; only the writing side is affected.

How to reproduce

var
  c: ICryptCert;
begin
  c := Cert('x509-es256').Generate([cuTlsServer, cuDigitalSignature], 'myhost,192.168.10.20');
  // DER contains  82 0D '192.168.10.20'  (dNSName)
  // expected      87 04 C0 A8 0A 14      (iPAddress)
  FileFromString(c.Save(cccCertOnly, '', ccfBinary), 'test.cer');
end;

openssl x509 -in test.cer -inform der -noout -text shows
X509v3 Subject Alternative Name: DNS:myhost, DNS:192.168.10.20 instead of
DNS:myhost, IP Address:192.168.10.20.

Suggested fix

Encode IPv4 literals as iPAddress, everything else as dNSName:

function CsvToDns(p: PUtf8Char): RawByteString;
var
  item, ip: RawUtf8;
  ip4: cardinal;
begin
  result := '';
  while p <> nil do
  begin
    item := TrimU(GetNextItem(p));
    if IPToCardinal(item, ip4) then
    begin
      // RFC 5280 #4.2.1.6: iPAddress = 4 bytes in network order for IPv4
      FastSetRawByteString(ip, @ip4, 4); // IPToCardinal() stores b[0..3] in text order
      Append(result, AsnTyped(ip, ASN1_CTX7));
    end
    else
      Append(result, AsnTyped(item, ASN1_CTX2));
  end;
end;

Notes on the fix:

  • IPToCardinal() (mormot.core.text) deliberately rejects 127.0.0.1, so loopback would still be written as dNSName. A small dedicated parser (like NetIsIP4() in mormot.net.sock, which cannot be used from mormot.crypt.x509 because of the unit dependency) would also cover loopback.

  • IPv6 literals would need the same treatment (16-byte OCTET STRING); not needed in our case.

  • Since CsvToDns() is shared with Issuer Alternative Name and the CRL extension, the fix applies there too, which is also correct per RFC 5280.

Last edited by sakura (2026-11-19 05:09:23)

Offline

Board footer

Powered by FluxBB