| 1 | // |
| 2 | // Copyright (c) SAS Institute Inc. |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // 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 implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
| 16 | |
| 17 | package x509tools |
| 18 | |
| 19 | import ( |
| 20 | "crypto/x509" |
| 21 | "crypto/x509/pkix" |
| 22 | "encoding/asn1" |
| 23 | "encoding/binary" |
| 24 | "fmt" |
| 25 | "strings" |
| 26 | "unicode/utf16" |
| 27 | ) |
| 28 | |
| 29 | type NameStyle int |
| 30 | |
| 31 | const ( |
| 32 | NameStyleOpenSsl NameStyle = iota |
| 33 | NameStyleLdap |
| 34 | NameStyleMsOsco |
| 35 | ) |
| 36 | |
| 37 | type attrName struct { |
| 38 | Type asn1.ObjectIdentifier |
| 39 | Name string |
| 40 | } |
| 41 | |
| 42 | var nameStyleLdap = []attrName{ |
| 43 | {asn1.ObjectIdentifier{2, 5, 4, 3}, "CN"}, |
| 44 | {asn1.ObjectIdentifier{2, 5, 4, 4}, "surname"}, |
| 45 | {asn1.ObjectIdentifier{2, 5, 4, 5}, "serialNumber"}, |
| 46 | {asn1.ObjectIdentifier{2, 5, 4, 6}, "C"}, |
| 47 | {asn1.ObjectIdentifier{2, 5, 4, 7}, "L"}, |
| 48 | {asn1.ObjectIdentifier{2, 5, 4, 8}, "ST"}, |
| 49 | {asn1.ObjectIdentifier{2, 5, 4, 9}, "street"}, |
| 50 | {asn1.ObjectIdentifier{2, 5, 4, 10}, "O"}, |
| 51 | {asn1.ObjectIdentifier{2, 5, 4, 11}, "OU"}, |
| 52 | {asn1.ObjectIdentifier{2, 5, 4, 12}, "title"}, |
| 53 | {asn1.ObjectIdentifier{2, 5, 4, 13}, "description"}, |
| 54 | {asn1.ObjectIdentifier{2, 5, 4, 17}, "postalCode"}, |
| 55 | {asn1.ObjectIdentifier{2, 5, 4, 18}, "postOfficeBox"}, |
| 56 | {asn1.ObjectIdentifier{2, 5, 4, 20}, "telephoneNumber"}, |
| 57 | {asn1.ObjectIdentifier{2, 5, 4, 42}, "givenName"}, |
| 58 | {asn1.ObjectIdentifier{2, 5, 4, 43}, "initials"}, |
| 59 | {asn1.ObjectIdentifier{0, 9, 2342, 19200300, 100, 1, 1}, "UID"}, |
| 60 | {asn1.ObjectIdentifier{0, 9, 2342, 19200300, 100, 1, 25}, "dc"}, |
| 61 | {asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 9, 1}, "emailAddress"}, |
| 62 | } |
| 63 | |
| 64 | // Per [MS-OSCO] |
| 65 | // https://msdn.microsoft.com/en-us/library/dd947276(v=office.12).aspx |
| 66 | var nameStyleMsOsco = []attrName{ |
| 67 | {asn1.ObjectIdentifier{2, 5, 4, 3}, "CN"}, |
| 68 | {asn1.ObjectIdentifier{2, 5, 4, 7}, "L"}, |
| 69 | {asn1.ObjectIdentifier{2, 5, 4, 10}, "O"}, |
| 70 | {asn1.ObjectIdentifier{2, 5, 4, 11}, "OU"}, |
| 71 | {asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 9, 1}, "E"}, |
| 72 | {asn1.ObjectIdentifier{2, 5, 4, 6}, "C"}, |
| 73 | {asn1.ObjectIdentifier{2, 5, 4, 8}, "S"}, |
| 74 | {asn1.ObjectIdentifier{2, 5, 4, 9}, "STREET"}, |
| 75 | {asn1.ObjectIdentifier{2, 5, 4, 12}, "T"}, |
| 76 | {asn1.ObjectIdentifier{2, 5, 4, 42}, "G"}, |
| 77 | {asn1.ObjectIdentifier{2, 5, 4, 43}, "I"}, |
| 78 | {asn1.ObjectIdentifier{2, 5, 4, 4}, "SN"}, |
| 79 | {asn1.ObjectIdentifier{2, 5, 4, 5}, "SERIALNUMBER"}, |
| 80 | {asn1.ObjectIdentifier{0, 9, 2342, 19200300, 100, 1, 1}, "UID"}, |
| 81 | {asn1.ObjectIdentifier{0, 9, 2342, 19200300, 100, 1, 25}, "DC"}, |
| 82 | {asn1.ObjectIdentifier{2, 5, 4, 13}, "Description"}, |
| 83 | {asn1.ObjectIdentifier{2, 5, 4, 17}, "PostalCode"}, |
| 84 | {asn1.ObjectIdentifier{2, 5, 4, 18}, "POBox"}, |
| 85 | {asn1.ObjectIdentifier{2, 5, 4, 20}, "Phone"}, |
| 86 | } |
| 87 | |
| 88 | // returned by the Format* functions in case there's something cripplingly |
| 89 | // wrong with it |
| 90 | const InvalidName = "<invalid>" |
| 91 | |
| 92 | // Format the name (RDN sequence) from its raw DER to a readable style. |
| 93 | func FormatPkixName(der []byte, style NameStyle) string { |
| 94 | var seq pkix.RDNSequence |
| 95 | if _, err := asn1.Unmarshal(der, &seq); err != nil { |
| 96 | return InvalidName |
| 97 | } |
| 98 | var b strings.Builder |
| 99 | if style == NameStyleOpenSsl { |
| 100 | for _, rdnSet := range seq { |
| 101 | for _, attr := range rdnSet { |
| 102 | b.WriteByte('/') |
| 103 | b.WriteString(attName(attr.Type, style)) |
| 104 | b.WriteByte('=') |
| 105 | b.WriteString(attValue(attr.Value, style)) |
| 106 | } |
| 107 | } |
| 108 | } else { |
| 109 | // Per RFC 2253 2.1, reverse the order |
| 110 | for i := len(seq) - 1; i >= 0; i-- { |
| 111 | if i < len(seq)-1 { |
| 112 | b.WriteString(", ") |
| 113 | } |
| 114 | rdnSet := seq[i] |
| 115 | for j, attr := range rdnSet { |
| 116 | if j > 0 { |
| 117 | b.WriteString(" + ") |
| 118 | } |
| 119 | b.WriteString(attName(attr.Type, style)) |
| 120 | b.WriteByte('=') |
| 121 | b.WriteString(attValue(attr.Value, style)) |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | return b.String() |
| 126 | } |
| 127 | |
| 128 | func attName(t asn1.ObjectIdentifier, style NameStyle) string { |
| 129 | var names []attrName |
| 130 | var defaultPrefix string |
| 131 | switch style { |
| 132 | case NameStyleLdap, NameStyleOpenSsl: |
| 133 | names = nameStyleLdap |
| 134 | case NameStyleMsOsco: |
| 135 | names = nameStyleMsOsco |
| 136 | defaultPrefix = "OID." |
| 137 | default: |
| 138 | panic("invalid style argument") |
| 139 | } |
| 140 | for _, name := range names { |
| 141 | if name.Type.Equal(t) { |
| 142 | return name.Name |
| 143 | } |
| 144 | } |
| 145 | return defaultPrefix + t.String() |
| 146 | } |
| 147 | |
| 148 | func attValue(raw interface{}, style NameStyle) string { |
| 149 | var value string |
| 150 | switch v := raw.(type) { |
| 151 | case string: |
| 152 | value = v |
| 153 | case fmt.Stringer: |
| 154 | value = v.String() |
| 155 | case int64: |
| 156 | value = fmt.Sprint(v) |
| 157 | default: |
| 158 | return InvalidName |
| 159 | } |
| 160 | switch style { |
| 161 | case NameStyleOpenSsl: |
| 162 | value = strings.ReplaceAll(value, "/", "\\/") |
| 163 | case NameStyleLdap, NameStyleMsOsco: |
| 164 | quote := false |
| 165 | if len(value) == 0 { |
| 166 | quote = true |
| 167 | } |
| 168 | if strings.HasPrefix(value, " ") || strings.HasSuffix(value, " ") { |
| 169 | quote = true |
| 170 | } |
| 171 | if i := strings.IndexAny(value, ",+=\n<>#;'\""); i >= 0 { |
| 172 | quote = true |
| 173 | } |
| 174 | value = strings.ReplaceAll(value, "\"", "\"\"") |
| 175 | if quote { |
| 176 | value = "\"" + value + "\"" |
| 177 | } |
| 178 | } |
| 179 | return value |
| 180 | } |
| 181 | |
| 182 | func ToBMPString(value string) asn1.RawValue { |
| 183 | runes := utf16.Encode([]rune(value)) |
| 184 | raw := make([]byte, 2*len(runes)) |
| 185 | for i, r := range runes { |
| 186 | binary.BigEndian.PutUint16(raw[i*2:], r) |
| 187 | } |
| 188 | return asn1.RawValue{Tag: asn1.TagBMPString, Bytes: raw} |
| 189 | } |
| 190 | |
| 191 | // Format the certificate subject name in LDAP style |
| 192 | func FormatSubject(cert *x509.Certificate) string { |
| 193 | return FormatPkixName(cert.RawSubject, NameStyleLdap) |
| 194 | } |
| 195 | |
| 196 | // Format the certificate issuer name in LDAP style |
| 197 | func FormatIssuer(cert *x509.Certificate) string { |
| 198 | return FormatPkixName(cert.RawIssuer, NameStyleLdap) |
| 199 | } |