|
| 1 | +// Copyright 2022 The Sigstore Authors. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package username |
| 16 | + |
| 17 | +import ( |
| 18 | + "crypto/x509/pkix" |
| 19 | + "encoding/asn1" |
| 20 | + "errors" |
| 21 | + "fmt" |
| 22 | + |
| 23 | + "github.com/sigstore/fulcio/pkg/certificate" |
| 24 | +) |
| 25 | + |
| 26 | +// OtherName describes a name related to a certificate which is not in one |
| 27 | +// of the standard name formats. RFC 5280, 4.2.1.6: |
| 28 | +// |
| 29 | +// OtherName ::= SEQUENCE { |
| 30 | +// type-id OBJECT IDENTIFIER, |
| 31 | +// value [0] EXPLICIT ANY DEFINED BY type-id } |
| 32 | +// |
| 33 | +// OtherName for Fulcio-issued certificates only supports UTF-8 strings as values. |
| 34 | +type OtherName struct { |
| 35 | + ID asn1.ObjectIdentifier |
| 36 | + Value string `asn1:"utf8,explicit,tag:0"` |
| 37 | +} |
| 38 | + |
| 39 | +// MarshalSANS creates a Subject Alternative Name extension |
| 40 | +// with an OtherName sequence. RFC 5280, 4.2.1.6: |
| 41 | +// |
| 42 | +// SubjectAltName ::= GeneralNames |
| 43 | +// GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName |
| 44 | +// GeneralName ::= CHOICE { |
| 45 | +// |
| 46 | +// otherName [0] OtherName, |
| 47 | +// ... } |
| 48 | +func MarshalSANS(name string, critical bool) (*pkix.Extension, error) { |
| 49 | + var rawValues []asn1.RawValue |
| 50 | + o := OtherName{ |
| 51 | + ID: certificate.OIDOtherName, |
| 52 | + Value: name, |
| 53 | + } |
| 54 | + bytes, err := asn1.MarshalWithParams(o, "tag:0") |
| 55 | + if err != nil { |
| 56 | + return nil, err |
| 57 | + } |
| 58 | + rawValues = append(rawValues, asn1.RawValue{FullBytes: bytes}) |
| 59 | + |
| 60 | + sans, err := asn1.Marshal(rawValues) |
| 61 | + if err != nil { |
| 62 | + return nil, err |
| 63 | + } |
| 64 | + return &pkix.Extension{ |
| 65 | + Id: asn1.ObjectIdentifier{2, 5, 29, 17}, |
| 66 | + Critical: critical, |
| 67 | + Value: sans, |
| 68 | + }, nil |
| 69 | +} |
| 70 | + |
| 71 | +// UnmarshalSANs extracts a UTF-8 string from the OtherName |
| 72 | +// field in the Subject Alternative Name extension. |
| 73 | +func UnmarshalSANS(exts []pkix.Extension) (string, error) { |
| 74 | + var otherNames []string |
| 75 | + |
| 76 | + for _, e := range exts { |
| 77 | + if !e.Id.Equal(asn1.ObjectIdentifier{2, 5, 29, 17}) { |
| 78 | + continue |
| 79 | + } |
| 80 | + |
| 81 | + var seq asn1.RawValue |
| 82 | + rest, err := asn1.Unmarshal(e.Value, &seq) |
| 83 | + if err != nil { |
| 84 | + return "", err |
| 85 | + } else if len(rest) != 0 { |
| 86 | + return "", fmt.Errorf("trailing data after X.509 extension") |
| 87 | + } |
| 88 | + if !seq.IsCompound || seq.Tag != 16 || seq.Class != 0 { |
| 89 | + return "", asn1.StructuralError{Msg: "bad SAN sequence"} |
| 90 | + } |
| 91 | + |
| 92 | + rest = seq.Bytes |
| 93 | + for len(rest) > 0 { |
| 94 | + var v asn1.RawValue |
| 95 | + rest, err = asn1.Unmarshal(rest, &v) |
| 96 | + if err != nil { |
| 97 | + return "", err |
| 98 | + } |
| 99 | + |
| 100 | + // skip all GeneralName fields except OtherName |
| 101 | + if v.Tag != 0 { |
| 102 | + continue |
| 103 | + } |
| 104 | + |
| 105 | + var other OtherName |
| 106 | + _, err := asn1.UnmarshalWithParams(v.FullBytes, &other, "tag:0") |
| 107 | + if err != nil { |
| 108 | + return "", fmt.Errorf("could not parse requested OtherName SAN: %v", err) |
| 109 | + } |
| 110 | + if !other.ID.Equal(certificate.OIDOtherName) { |
| 111 | + return "", fmt.Errorf("unexpected OID for OtherName, expected %v, got %v", certificate.OIDOtherName, other.ID) |
| 112 | + } |
| 113 | + otherNames = append(otherNames, other.Value) |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + if len(otherNames) == 0 { |
| 118 | + return "", errors.New("no OtherName found") |
| 119 | + } |
| 120 | + if len(otherNames) != 1 { |
| 121 | + return "", errors.New("expected only one OtherName") |
| 122 | + } |
| 123 | + |
| 124 | + return otherNames[0], nil |
| 125 | +} |
0 commit comments