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