| 1 |
// Copyright The Notary Project Authors. |
| 2 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
// you may not use this file except in compliance with the License. |
| 4 |
// You may obtain a copy of the License at |
| 5 |
// |
| 6 |
// 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 implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package pkix |
| 15 |
|
| 16 |
import ( |
| 17 |
"fmt" |
| 18 |
"strings" |
| 19 |
|
| 20 |
ldapv3 "github.com/go-ldap/ldap/v3" |
| 21 |
) |
| 22 |
|
| 23 |
// ParseDistinguishedName parses a DN name and validates Notary Project rules |
| 24 |
func ParseDistinguishedName(name string) (map[string]string, error) { |
| 25 |
if strings.Contains(name, "=#") { |
| 26 |
return nil, fmt.Errorf("unsupported distinguished name (DN) %q: notation does not support x509.subject identities containing \"=#\"", name) |
| 27 |
} |
| 28 |
|
| 29 |
attrKeyValue := make(map[string]string) |
| 30 |
dn, err := ldapv3.ParseDN(name) |
| 31 |
if err != nil { |
| 32 |
return nil, fmt.Errorf("parsing distinguished name (DN) %q failed with err: %v. A valid DN must contain 'C', 'ST' or 'S', and 'O' RDN attributes at a minimum, and follow RFC 4514 standard", name, err) |
| 33 |
} |
| 34 |
|
| 35 |
for _, rdn := range dn.RDNs { |
| 36 |
// multi-valued RDNs are not supported (TODO: add spec reference here) |
| 37 |
if len(rdn.Attributes) > 1 { |
| 38 |
return nil, fmt.Errorf("distinguished name (DN) %q has multi-valued RDN attributes, remove multi-valued RDN attributes as they are not supported", name) |
| 39 |
} |
| 40 |
for _, attribute := range rdn.Attributes { |
| 41 |
// stateOrProvince name 'S' is an alias for 'ST' |
| 42 |
if attribute.Type == "S" { |
| 43 |
attribute.Type = "ST" |
| 44 |
} |
| 45 |
if attrKeyValue[attribute.Type] == "" { |
| 46 |
attrKeyValue[attribute.Type] = attribute.Value |
| 47 |
} else { |
| 48 |
return nil, fmt.Errorf("distinguished name (DN) %q has duplicate RDN attribute for %q, DN can only have unique RDN attributes", name, attribute.Type) |
| 49 |
} |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
// Verify mandatory fields are present |
| 54 |
mandatoryFields := []string{"C", "ST", "O"} |
| 55 |
for _, field := range mandatoryFields { |
| 56 |
if attrKeyValue[field] == "" { |
| 57 |
return nil, fmt.Errorf("distinguished name (DN) %q has no mandatory RDN attribute for %q, it must contain 'C', 'ST' or 'S', and 'O' RDN attributes at a minimum", name, field) |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
// No errors |
| 62 |
return attrKeyValue, nil |
| 63 |
} |
| 64 |
|
| 65 |
// IsSubsetDN returns true if dn1 is a subset of dn2 i.e. every key/value pair |
| 66 |
// of dn1 has a matching key/value pair in dn2, otherwise returns false |
| 67 |
func IsSubsetDN(dn1 map[string]string, dn2 map[string]string) bool { |
| 68 |
for key := range dn1 { |
| 69 |
if dn1[key] != dn2[key] { |
| 70 |
return false |
| 71 |
} |
| 72 |
} |
| 73 |
return true |
| 74 |
} |