github.com/zmap/zlint@v1.1.0/lints/lint_ext_san_rfc822_format_invalid.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  RFC 5280: 4.2.1.6
    19   When the subjectAltName extension contains an Internet mail address,
    20     the address MUST be stored in the rfc822Name.  The format of an
    21     rfc822Name is a "Mailbox" as defined in Section 4.1.2 of [RFC2821].
    22     A Mailbox has the form "Local-part@Domain".  Note that a Mailbox has
    23     no phrase (such as a common name) before it, has no comment (text
    24     surrounded in parentheses) after it, and is not surrounded by "<" and
    25     ">".  Rules for encoding Internet mail addresses that include
    26     internationalized domain names are specified in Section 7.5.
    27  ************************************************************************/
    28  
    29  import (
    30  	"strings"
    31  
    32  	"github.com/zmap/zcrypto/x509"
    33  	"github.com/zmap/zlint/util"
    34  )
    35  
    36  type invalidEmail struct{}
    37  
    38  func (l *invalidEmail) Initialize() error {
    39  	return nil
    40  }
    41  
    42  func (l *invalidEmail) CheckApplies(c *x509.Certificate) bool {
    43  	return util.IsExtInCert(c, util.SubjectAlternateNameOID)
    44  }
    45  
    46  func (l *invalidEmail) Execute(c *x509.Certificate) *LintResult {
    47  	for _, str := range c.EmailAddresses {
    48  		if str == "" {
    49  			continue
    50  		}
    51  		if strings.Contains(str, " ") {
    52  			return &LintResult{Status: Error}
    53  		} else if str[0] == '<' || str[len(str)-1] == ')' {
    54  			return &LintResult{Status: Error}
    55  		}
    56  	}
    57  	return &LintResult{Status: Pass}
    58  }
    59  
    60  func init() {
    61  	RegisterLint(&Lint{
    62  		Name:          "e_ext_san_rfc822_format_invalid",
    63  		Description:   "Email MUST NOT be surrounded with `<>`, and there must be no trailing comments in `()`",
    64  		Citation:      "RFC 5280: 4.2.1.6",
    65  		Source:        RFC5280,
    66  		EffectiveDate: util.RFC2459Date,
    67  		Lint:          &invalidEmail{},
    68  	})
    69  }