github.com/zmap/zlint@v1.1.0/util/names.go (about)

     1  /*
     2   * ZLint Copyright 2018 Regents of the University of Michigan
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License"); you may not
     5   * use this file except in compliance with the License. You may obtain a copy
     6   * of the License at http://www.apache.org/licenses/LICENSE-2.0
     7   *
     8   * Unless required by applicable law or agreed to in writing, software
     9   * distributed under the License is distributed on an "AS IS" BASIS,
    10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
    11   * implied. See the License for the specific language governing
    12   * permissions and limitations under the License.
    13   */
    14  
    15  package util
    16  
    17  import (
    18  	"encoding/asn1"
    19  
    20  	"github.com/zmap/zcrypto/x509/pkix"
    21  )
    22  
    23  type empty struct{}
    24  
    25  var nameAttributePrefix = asn1.ObjectIdentifier{2, 5, 4}
    26  var nameAttributeLeaves = map[int]empty{
    27  	// Name attributes defined in RFC 5280 appendix A
    28  	3:  {}, // id-at-commonName	AttributeType ::= { id-at 3 }
    29  	4:  {}, // id-at-surname	AttributeType ::= { id-at  4 }
    30  	5:  {}, // id-at-serialNumber	AttributeType ::= { id-at 5 }
    31  	6:  {}, // id-at-countryName	AttributeType ::= { id-at 6 }
    32  	7:  {}, // id-at-localityName	AttributeType ::= { id-at 7 }
    33  	8:  {}, // id-at-stateOrProvinceName	AttributeType ::= { id-at 8 }
    34  	10: {}, // id-at-organizationName	AttributeType ::= { id-at 10 }
    35  	11: {}, // id-at-organizationalUnitName	AttributeType ::= { id-at 11 }
    36  	12: {}, // id-at-title	AttributeType ::= { id-at 12 }
    37  	41: {}, // id-at-name	AttributeType ::= { id-at 41 }
    38  	42: {}, // id-at-givenName	AttributeType ::= { id-at 42 }
    39  	43: {}, // id-at-initials	AttributeType ::= { id-at 43 }
    40  	44: {}, // id-at-generationQualifier	AttributeType ::= { id-at 44 }
    41  	46: {}, // id-at-dnQualifier	AttributeType ::= { id-at 46 }
    42  
    43  	// Name attributes not present in RFC 5280, but appeared in Go's crypto/x509/pkix.go
    44  	9:  {}, // id-at-streetName	AttributeType ::= { id-at 9 }
    45  	17: {}, // id-at-postalCodeName	AttributeType ::= { id-at 17 }
    46  }
    47  
    48  // IsNameAttribute returns true if the given ObjectIdentifier corresponds with
    49  // the type of any name attribute for PKIX.
    50  func IsNameAttribute(oid asn1.ObjectIdentifier) bool {
    51  	if len(oid) != 4 {
    52  		return false
    53  	}
    54  	if !nameAttributePrefix.Equal(oid[0:3]) {
    55  		return false
    56  	}
    57  	_, ok := nameAttributeLeaves[oid[3]]
    58  	return ok
    59  }
    60  
    61  func NotAllNameFieldsAreEmpty(name *pkix.Name) bool {
    62  	//Return true if at least one field is non-empty
    63  	return len(name.Names) >= 1
    64  }