tls/internal/Identity.protos

Repository
guillermomolina/protos
Commit
2723a55555e7beecee0816bf99d16e3c8c1ae635
Tree
c36054e4e5abd7fc29a039b4e096cac2e8706492
Blob
6fcb6dffd593a223148afe08e397b64ba3cc3dd3
Source
View exact source on GitHub
// THE LICENSED WORK IS PROVIDED UNDER THE TERMS OF THE ADAPTIVE PUBLIC LICENSE
// ("LICENSE") AS FIRST COMPLETED BY: Guillermo Adrián Molina. ANY USE, PUBLIC
// DISPLAY, PUBLIC PERFORMANCE, REPRODUCTION OR DISTRIBUTION OF, OR PREPARATION OF
// DERIVATIVE WORKS BASED ON, THE LICENSED WORK CONSTITUTES RECIPIENT'S ACCEPTANCE
// OF THIS LICENSE AND ITS TERMS, WHETHER OR NOT SUCH RECIPIENT READS THE TERMS OF
// THE LICENSE. "LICENSED WORK" AND "RECIPIENT" ARE DEFINED IN THE LICENSE. A COPY
// OF THE LICENSE IS LOCATED IN THE TEXT FILE ENTITLED "LICENSE.TXT" ACCOMPANYING
// THE CONTENTS OF THIS FILE. IF A COPY OF THE LICENSE DOES NOT ACCOMPANY THIS
// FILE, A COPY OF THE LICENSE MAY ALSO BE OBTAINED AT THE FOLLOWING WEB SITE:
// https://github.com/guillermomolina/protos
//
// Software distributed under the License is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
// the specific language governing rights and limitations under the License.

//! Internal reference identity of std:tls/client (D220–S12, TLS.md §4,
//! RFC 9525). This module is an unpublished implementation artifact, not an
//! approved Standard Library API.
//!
//! Matching is subjectAltName-only: a DNS reference against DNS-IDs, an IP
//! reference against exact iPAddress octets. The subject Common Name is never
//! consulted; the backend does not even supply it.
//!
//! DNS names are ASCII wire names: NR-LDH labels and strict IDNA2008 A-labels
//! (RFC 5890, 5891 §5.4, 5892, 5893, Punycode RFC 3492). Every label rule is
//! decided by the shared std:network/internal/DnsNames module; this module adds
//! the two-label minimum, wildcard matching and IP references. The `unicode`
//! argument is the private TLS backend facility, which
//! supplies only raw Unicode facts of one scalar (`codePoint`) and the NFC
//! test (`isNfc`); it is consulted only for labels beginning "xn--". There is
//! no Unicode-to-ASCII conversion, mapping or fallback.

IpAddresses: import("std:network/ip_addresses")
IpAddress: IpAddresses.IpAddress
DnsNames: import("../../network/internal/DnsNames.protos")

// The shared RFC 3492/5892/5893 mechanisms of std:network/internal/DnsNames,
// forwarded unchanged.
punycodeDecode: (payload) => { DnsNames.punycodeDecode(payload) }
punycodeEncode: (scalars) => { DnsNames.punycodeEncode(scalars) }
derivedProperty: (scalar, facts) => { DnsNames.derivedProperty(scalar, facts) }
contextJ: (scalars, facts, index) => { DnsNames.contextJ(scalars, facts, index) }
contextO: (scalars, facts, index) => { DnsNames.contextO(scalars, facts, index) }
bidiRule: (classes) => { DnsNames.bidiRule(classes) }
numericLiteral: (text) => { DnsNames.numericLiteral(text) }

// Answers {labels, text} for an ASCII DNS name of at least two labels (TLS
// references and DNS-IDs), lowercased, with one optional terminal root dot
// removed; or null. allowWildcard admits only "*" as the entire leftmost
// label (RFC 9525 §6.3). The reference it is compared with is always a fully
// validated name (reference), so a wildcard cannot admit an invalid label.
dnsName: (unicode, text, allowWildcard) => {
    DnsNames.admit(unicode, text, 2, allowWildcard)
}

// The 4 or 16 network-order Integer octets of a recognized IpAddress.
ipOctets: (address) => {
    width: 4
    if(address.version == 6) {
        width = 16
    }
    octets: []
    remaining: address.bits
    while(octets.size() < width) {
        octets = Array(remaining.mod(256), ...octets)
        remaining = remaining.div(256)
    }
    octets.freeze()
}

/// Answers the frozen normalized reference for serverIdentity, or null when it
/// is not admissible: {labels, octets, sni}. A DNS reference has labels and
/// sends its normalized name, without the terminal dot, as SNI; an IpAddress
/// reference has octets and sends no SNI. unicode is the private backend
/// facility (see the module documentation).
reference: (unicode, serverIdentity) => {
    answer: null
    if(String.recognizes(serverIdentity)) {
        name: dnsName(unicode, serverIdentity, false)
        if(name !== null) {
            if(!numericLiteral(name.text)) {
                answer = {
                    labels: name.labels
                    octets: null
                    sni: name.text
                }
            }
        }
    } else {
        if(IpAddress.recognizes(serverIdentity)) {
            answer = {
                labels: null
                octets: ipOctets(serverIdentity)
                sni: null
            }
        }
    }
    if(answer !== null) {
        answer.freeze()
    }
    answer
}

// A presented wildcard label matches exactly one (always nonempty) reference
// label; every other label compares equal after ASCII lowercasing.
sameName: (referenceLabels, presentedLabels) => {
    same: referenceLabels.size() == presentedLabels.size()
    index: 0
    while(same && (index < referenceLabels.size())) {
        if((index > 0) || (presentedLabels[0] != "*")) {
            same = referenceLabels[index] == presentedLabels[index]
        }
        index = index + 1
    }
    same
}

sameOctets: (referenceOctets, presented) => {
    same: referenceOctets.size() == presented.size()
    index: 0
    while(same && (index < referenceOctets.size())) {
        same = referenceOctets[index] == presented[index]
        index = index + 1
    }
    same
}

/// Whether the verified peer presents reference: dnsNames are the raw
/// subjectAltName dNSName Strings, ipAddresses the raw iPAddress Bytes.
/// Presented names that are not valid ASCII DNS-IDs under the same NR-LDH
/// and strict A-label rules never match.
matches: (unicode, reference, dnsNames, ipAddresses) => {
    found: false
    if(reference.labels !== null) {
        for(presented in dnsNames) {
            if(!found) {
                candidate: dnsName(unicode, presented, true)
                if(candidate !== null) {
                    found = sameName(reference.labels, candidate.labels)
                }
            }
        }
    } else {
        for(presented in ipAddresses) {
            if(!found) {
                found = sameOctets(reference.octets, presented)
            }
        }
    }
    found
}