github.com/zmap/zlint@v1.1.0/lints/lint_ext_san_uri_host_not_fqdn_or_ip.go (about)

     1  package lints
     2  
     3  /*
     4   * ZLint Copyright 2018 Regents of the University of Michigan
     5   *
     6   * Licensed under the Apache License, Version 2.0 (the "License"); you may not
     7   * use this file except in compliance with the License. You may obtain a copy
     8   * of the License at http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
    13   * implied. See the License for the specific language governing
    14   * permissions and limitations under the License.
    15   */
    16  
    17  /*********************************************************************
    18  When the subjectAltName extension contains a URI, the name MUST be
    19  stored in the uniformResourceIdentifier (an IA5String).  The name
    20  MUST NOT be a relative URI, and it MUST follow the URI syntax and
    21  encoding rules specified in [RFC3986].  The name MUST include both a
    22  scheme (e.g., "http" or "ftp") and a scheme-specific-part.  URIs that
    23  include an authority ([RFC3986], Section 3.2) MUST include a fully
    24  qualified domain name or IP address as the host.  Rules for encoding
    25  Internationalized Resource Identifiers (IRIs) are specified in
    26  Section 7.4.
    27  *********************************************************************/
    28  
    29  import (
    30  	"github.com/zmap/zcrypto/x509"
    31  	"github.com/zmap/zlint/util"
    32  	"net/url"
    33  )
    34  
    35  type SANURIHost struct{}
    36  
    37  func (l *SANURIHost) Initialize() error {
    38  	return nil
    39  }
    40  
    41  func (l *SANURIHost) CheckApplies(c *x509.Certificate) bool {
    42  	return util.IsExtInCert(c, util.SubjectAlternateNameOID)
    43  }
    44  
    45  func (l *SANURIHost) Execute(c *x509.Certificate) *LintResult {
    46  	for _, uri := range c.URIs {
    47  		if uri != "" {
    48  			parsed, err := url.Parse(uri)
    49  			if err != nil {
    50  				return &LintResult{Status: Error}
    51  			}
    52  			if parsed.Opaque == "" {
    53  				// if Opaque is not empty, that means there is no authority, which means that the URI is vacuously OK
    54  				if parsed.Host == "" {
    55  					return &LintResult{Status: Error}
    56  				}
    57  				if !util.IsFQDNOrIP(parsed.Host) {
    58  					return &LintResult{Status: Error}
    59  				}
    60  			}
    61  		}
    62  	}
    63  
    64  	return &LintResult{Status: Pass}
    65  }
    66  
    67  func init() {
    68  	RegisterLint(&Lint{
    69  		Name:          "e_ext_san_uri_host_not_fqdn_or_ip",
    70  		Description:   "URIs that include an authority ([RFC3986], Section 3.2) MUST include a fully qualified domain name or IP address as the host",
    71  		Citation:      "RFC 5280: 4.2.1.7",
    72  		Source:        RFC5280,
    73  		EffectiveDate: util.RFC5280Date,
    74  		Lint:          &SANURIHost{},
    75  	})
    76  }