| 1 | // Copyright 2023 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package legacyx509 |
| 6 | |
| 7 | import ( |
| 8 | "bytes" |
| 9 | "encoding/asn1" |
| 10 | "errors" |
| 11 | "math" |
| 12 | "math/big" |
| 13 | "math/bits" |
| 14 | "strconv" |
| 15 | "strings" |
| 16 | ) |
| 17 | |
| 18 | var ( |
| 19 | errInvalidOID = errors.New("invalid oid") |
| 20 | ) |
| 21 | |
| 22 | // An OID represents an ASN.1 OBJECT IDENTIFIER. |
| 23 | type OID struct { |
| 24 | der []byte |
| 25 | } |
| 26 | |
| 27 | // ParseOID parses a Object Identifier string, represented by ASCII numbers separated by dots. |
| 28 | func ParseOID(oid string) (OID, error) { |
| 29 | var o OID |
| 30 | return o, o.unmarshalOIDText(oid) |
| 31 | } |
| 32 | |
| 33 | func newOIDFromDER(der []byte) (OID, bool) { |
| 34 | if len(der) == 0 || der[len(der)-1]&0x80 != 0 { |
| 35 | return OID{}, false |
| 36 | } |
| 37 | |
| 38 | start := 0 |
| 39 | for i, v := range der { |
| 40 | // ITU-T X.690, section 8.19.2: |
| 41 | // The subidentifier shall be encoded in the fewest possible octets, |
| 42 | // that is, the leading octet of the subidentifier shall not have the value 0x80. |
| 43 | if i == start && v == 0x80 { |
| 44 | return OID{}, false |
| 45 | } |
| 46 | if v&0x80 == 0 { |
| 47 | start = i + 1 |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | return OID{der}, true |
| 52 | } |
| 53 | |
| 54 | // OIDFromInts creates a new OID using ints, each integer is a separate component. |
| 55 | func OIDFromInts(oid []uint64) (OID, error) { |
| 56 | if len(oid) < 2 || oid[0] > 2 || (oid[0] < 2 && oid[1] >= 40) { |
| 57 | return OID{}, errInvalidOID |
| 58 | } |
| 59 | |
| 60 | length := base128IntLength(oid[0]*40 + oid[1]) |
| 61 | for _, v := range oid[2:] { |
| 62 | length += base128IntLength(v) |
| 63 | } |
| 64 | |
| 65 | der := make([]byte, 0, length) |
| 66 | der = appendBase128Int(der, oid[0]*40+oid[1]) |
| 67 | for _, v := range oid[2:] { |
| 68 | der = appendBase128Int(der, v) |
| 69 | } |
| 70 | return OID{der}, nil |
| 71 | } |
| 72 | |
| 73 | func base128IntLength(n uint64) int { |
| 74 | if n == 0 { |
| 75 | return 1 |
| 76 | } |
| 77 | return (bits.Len64(n) + 6) / 7 |
| 78 | } |
| 79 | |
| 80 | func appendBase128Int(dst []byte, n uint64) []byte { |
| 81 | for i := base128IntLength(n) - 1; i >= 0; i-- { |
| 82 | o := byte(n >> uint(i*7)) |
| 83 | o &= 0x7f |
| 84 | if i != 0 { |
| 85 | o |= 0x80 |
| 86 | } |
| 87 | dst = append(dst, o) |
| 88 | } |
| 89 | return dst |
| 90 | } |
| 91 | |
| 92 | func base128BigIntLength(n *big.Int) int { |
| 93 | if n.Cmp(big.NewInt(0)) == 0 { |
| 94 | return 1 |
| 95 | } |
| 96 | return (n.BitLen() + 6) / 7 |
| 97 | } |
| 98 | |
| 99 | func appendBase128BigInt(dst []byte, n *big.Int) []byte { |
| 100 | if n.Cmp(big.NewInt(0)) == 0 { |
| 101 | return append(dst, 0) |
| 102 | } |
| 103 | |
| 104 | for i := base128BigIntLength(n) - 1; i >= 0; i-- { |
| 105 | o := byte(big.NewInt(0).Rsh(n, uint(i)*7).Bits()[0]) |
| 106 | o &= 0x7f |
| 107 | if i != 0 { |
| 108 | o |= 0x80 |
| 109 | } |
| 110 | dst = append(dst, o) |
| 111 | } |
| 112 | return dst |
| 113 | } |
| 114 | |
| 115 | // AppendText implements [encoding.TextAppender] |
| 116 | func (o OID) AppendText(b []byte) ([]byte, error) { |
| 117 | return append(b, o.String()...), nil |
| 118 | } |
| 119 | |
| 120 | // MarshalText implements [encoding.TextMarshaler] |
| 121 | func (o OID) MarshalText() ([]byte, error) { |
| 122 | return o.AppendText(nil) |
| 123 | } |
| 124 | |
| 125 | // UnmarshalText implements [encoding.TextUnmarshaler] |
| 126 | func (o *OID) UnmarshalText(text []byte) error { |
| 127 | return o.unmarshalOIDText(string(text)) |
| 128 | } |
| 129 | |
| 130 | // cutString slices s around the first instance of sep, |
| 131 | // returning the text before and after sep. |
| 132 | // The found result reports whether sep appears in s. |
| 133 | // If sep does not appear in s, cut returns s, "", false. |
| 134 | func cutString(s, sep string) (before, after string, found bool) { |
| 135 | if i := strings.Index(s, sep); i >= 0 { |
| 136 | return s[:i], s[i+len(sep):], true |
| 137 | } |
| 138 | return s, "", false |
| 139 | } |
| 140 | |
| 141 | func (o *OID) unmarshalOIDText(oid string) error { |
| 142 | // (*big.Int).SetString allows +/- signs, but we don't want |
| 143 | // to allow them in the string representation of Object Identifier, so |
| 144 | // reject such encodings. |
| 145 | for _, c := range oid { |
| 146 | isDigit := c >= '0' && c <= '9' |
| 147 | if !isDigit && c != '.' { |
| 148 | return errInvalidOID |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | var ( |
| 153 | firstNum string |
| 154 | secondNum string |
| 155 | ) |
| 156 | |
| 157 | var nextComponentExists bool |
| 158 | firstNum, oid, nextComponentExists = cutString(oid, ".") |
| 159 | if !nextComponentExists { |
| 160 | return errInvalidOID |
| 161 | } |
| 162 | secondNum, oid, nextComponentExists = cutString(oid, ".") |
| 163 | |
| 164 | var ( |
| 165 | first = big.NewInt(0) |
| 166 | second = big.NewInt(0) |
| 167 | ) |
| 168 | |
| 169 | if _, ok := first.SetString(firstNum, 10); !ok { |
| 170 | return errInvalidOID |
| 171 | } |
| 172 | if _, ok := second.SetString(secondNum, 10); !ok { |
| 173 | return errInvalidOID |
| 174 | } |
| 175 | |
| 176 | if first.Cmp(big.NewInt(2)) > 0 || (first.Cmp(big.NewInt(2)) < 0 && second.Cmp(big.NewInt(40)) >= 0) { |
| 177 | return errInvalidOID |
| 178 | } |
| 179 | |
| 180 | firstComponent := first.Mul(first, big.NewInt(40)) |
| 181 | firstComponent.Add(firstComponent, second) |
| 182 | |
| 183 | der := appendBase128BigInt(make([]byte, 0, 32), firstComponent) |
| 184 | |
| 185 | for nextComponentExists { |
| 186 | var strNum string |
| 187 | strNum, oid, nextComponentExists = cutString(oid, ".") |
| 188 | b, ok := big.NewInt(0).SetString(strNum, 10) |
| 189 | if !ok { |
| 190 | return errInvalidOID |
| 191 | } |
| 192 | der = appendBase128BigInt(der, b) |
| 193 | } |
| 194 | |
| 195 | o.der = der |
| 196 | return nil |
| 197 | } |
| 198 | |
| 199 | // AppendBinary implements [encoding.BinaryAppender] |
| 200 | func (o OID) AppendBinary(b []byte) ([]byte, error) { |
| 201 | return append(b, o.der...), nil |
| 202 | } |
| 203 | |
| 204 | // MarshalBinary implements [encoding.BinaryMarshaler] |
| 205 | func (o OID) MarshalBinary() ([]byte, error) { |
| 206 | return o.AppendBinary(nil) |
| 207 | } |
| 208 | |
| 209 | // cloneBytes returns a copy of b[:len(b)]. |
| 210 | // The result may have additional unused capacity. |
| 211 | // Clone(nil) returns nil. |
| 212 | func cloneBytes(b []byte) []byte { |
| 213 | if b == nil { |
| 214 | return nil |
| 215 | } |
| 216 | return append([]byte{}, b...) |
| 217 | } |
| 218 | |
| 219 | // UnmarshalBinary implements [encoding.BinaryUnmarshaler] |
| 220 | func (o *OID) UnmarshalBinary(b []byte) error { |
| 221 | oid, ok := newOIDFromDER(cloneBytes(b)) |
| 222 | if !ok { |
| 223 | return errInvalidOID |
| 224 | } |
| 225 | *o = oid |
| 226 | return nil |
| 227 | } |
| 228 | |
| 229 | // Equal returns true when oid and other represents the same Object Identifier. |
| 230 | func (oid OID) Equal(other OID) bool { |
| 231 | // There is only one possible DER encoding of |
| 232 | // each unique Object Identifier. |
| 233 | return bytes.Equal(oid.der, other.der) |
| 234 | } |
| 235 | |
| 236 | func parseBase128Int(bytes []byte, initOffset int) (ret, offset int, failed bool) { |
| 237 | offset = initOffset |
| 238 | var ret64 int64 |
| 239 | for shifted := 0; offset < len(bytes); shifted++ { |
| 240 | // 5 * 7 bits per byte == 35 bits of data |
| 241 | // Thus the representation is either non-minimal or too large for an int32 |
| 242 | if shifted == 5 { |
| 243 | failed = true |
| 244 | return |
| 245 | } |
| 246 | ret64 <<= 7 |
| 247 | b := bytes[offset] |
| 248 | // integers should be minimally encoded, so the leading octet should |
| 249 | // never be 0x80 |
| 250 | if shifted == 0 && b == 0x80 { |
| 251 | failed = true |
| 252 | return |
| 253 | } |
| 254 | ret64 |= int64(b & 0x7f) |
| 255 | offset++ |
| 256 | if b&0x80 == 0 { |
| 257 | ret = int(ret64) |
| 258 | // Ensure that the returned value fits in an int on all platforms |
| 259 | if ret64 > math.MaxInt32 { |
| 260 | failed = true |
| 261 | } |
| 262 | return |
| 263 | } |
| 264 | } |
| 265 | failed = true |
| 266 | return |
| 267 | } |
| 268 | |
| 269 | // EqualASN1OID returns whether an OID equals an asn1.ObjectIdentifier. If |
| 270 | // asn1.ObjectIdentifier cannot represent the OID specified by oid, because |
| 271 | // a component of OID requires more than 31 bits, it returns false. |
| 272 | func (oid OID) EqualASN1OID(other asn1.ObjectIdentifier) bool { |
| 273 | if len(other) < 2 { |
| 274 | return false |
| 275 | } |
| 276 | v, offset, failed := parseBase128Int(oid.der, 0) |
| 277 | if failed { |
| 278 | // This should never happen, since we've already parsed the OID, |
| 279 | // but just in case. |
| 280 | return false |
| 281 | } |
| 282 | if v < 80 { |
| 283 | a, b := v/40, v%40 |
| 284 | if other[0] != a || other[1] != b { |
| 285 | return false |
| 286 | } |
| 287 | } else { |
| 288 | a, b := 2, v-80 |
| 289 | if other[0] != a || other[1] != b { |
| 290 | return false |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | i := 2 |
| 295 | for ; offset < len(oid.der); i++ { |
| 296 | v, offset, failed = parseBase128Int(oid.der, offset) |
| 297 | if failed { |
| 298 | // Again, shouldn't happen, since we've already parsed |
| 299 | // the OID, but better safe than sorry. |
| 300 | return false |
| 301 | } |
| 302 | if i >= len(other) || v != other[i] { |
| 303 | return false |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | return i == len(other) |
| 308 | } |
| 309 | |
| 310 | // Strings returns the string representation of the Object Identifier. |
| 311 | func (oid OID) String() string { |
| 312 | var b strings.Builder |
| 313 | b.Grow(32) |
| 314 | const ( |
| 315 | valSize = 64 // size in bits of val. |
| 316 | bitsPerByte = 7 |
| 317 | maxValSafeShift = (1 << (valSize - bitsPerByte)) - 1 |
| 318 | ) |
| 319 | var ( |
| 320 | start = 0 |
| 321 | val = uint64(0) |
| 322 | numBuf = make([]byte, 0, 21) |
| 323 | bigVal *big.Int |
| 324 | overflow bool |
| 325 | ) |
| 326 | for i, v := range oid.der { |
| 327 | curVal := v & 0x7F |
| 328 | valEnd := v&0x80 == 0 |
| 329 | if valEnd { |
| 330 | if start != 0 { |
| 331 | b.WriteByte('.') |
| 332 | } |
| 333 | } |
| 334 | if !overflow && val > maxValSafeShift { |
| 335 | if bigVal == nil { |
| 336 | bigVal = new(big.Int) |
| 337 | } |
| 338 | bigVal = bigVal.SetUint64(val) |
| 339 | overflow = true |
| 340 | } |
| 341 | if overflow { |
| 342 | bigVal = bigVal.Lsh(bigVal, bitsPerByte).Or(bigVal, big.NewInt(int64(curVal))) |
| 343 | if valEnd { |
| 344 | if start == 0 { |
| 345 | b.WriteString("2.") |
| 346 | bigVal = bigVal.Sub(bigVal, big.NewInt(80)) |
| 347 | } |
| 348 | numBuf = bigVal.Append(numBuf, 10) |
| 349 | b.Write(numBuf) |
| 350 | numBuf = numBuf[:0] |
| 351 | val = 0 |
| 352 | start = i + 1 |
| 353 | overflow = false |
| 354 | } |
| 355 | continue |
| 356 | } |
| 357 | val <<= bitsPerByte |
| 358 | val |= uint64(curVal) |
| 359 | if valEnd { |
| 360 | if start == 0 { |
| 361 | if val < 80 { |
| 362 | b.Write(strconv.AppendUint(numBuf, val/40, 10)) |
| 363 | b.WriteByte('.') |
| 364 | b.Write(strconv.AppendUint(numBuf, val%40, 10)) |
| 365 | } else { |
| 366 | b.WriteString("2.") |
| 367 | b.Write(strconv.AppendUint(numBuf, val-80, 10)) |
| 368 | } |
| 369 | } else { |
| 370 | b.Write(strconv.AppendUint(numBuf, val, 10)) |
| 371 | } |
| 372 | val = 0 |
| 373 | start = i + 1 |
| 374 | } |
| 375 | } |
| 376 | return b.String() |
| 377 | } |