Debian Codegraph @

x509/x509ext.go

raw
1 // Package x509ext provides functions for (un)marshalling X.509 extensions not
2 // supported by the crypto/x509 package.
3 package x509ext
4
5 import (
6 "crypto/x509/pkix"
7 "encoding/asn1"
8 "errors"
9 "fmt"
10
11 "github.com/google/go-attestation/oid"
12 )
13
14 // RFC 4043
15 //
16 // https://tools.ietf.org/html/rfc4043
17 var (
18 oidPermanentIdentifier = []int{1, 3, 6, 1, 5, 5, 7, 8, 3}
19 )
20
21 // OtherName ::= SEQUENCE {
22 // type-id OBJECT IDENTIFIER,
23 // value [0] EXPLICIT ANY DEFINED BY type-id }
24 type otherName struct {
25 TypeID asn1.ObjectIdentifier
26 Value asn1.RawValue
27 }
28
29 func marshalOtherName(typeID asn1.ObjectIdentifier, value any) (asn1.RawValue, error) {
30 valueBytes, err := asn1.MarshalWithParams(value, "explicit,tag:0")
31 if err != nil {
32 return asn1.RawValue{}, err
33 }
34 otherName := otherName{
35 TypeID: typeID,
36 Value: asn1.RawValue{FullBytes: valueBytes},
37 }
38 bytes, err := asn1.MarshalWithParams(otherName, "tag:0")
39 if err != nil {
40 return asn1.RawValue{}, err
41 }
42 return asn1.RawValue{FullBytes: bytes}, nil
43 }
44
45 // PermanentIdentifier represents an ASN.1 encoded "permanent identifier" as
46 // defined by RFC4043.
47 //
48 // PermanentIdentifier ::= SEQUENCE {
49 // identifierValue UTF8String OPTIONAL,
50 // assigner OBJECT IDENTIFIER OPTIONAL
51 // }
52 //
53 // https://datatracker.ietf.org/doc/html/rfc4043
54 type PermanentIdentifier struct {
55 IdentifierValue string `asn1:"utf8,optional"`
56 Assigner asn1.ObjectIdentifier `asn1:"optional"`
57 }
58
59 func parsePermanentIdentifier(der []byte) (PermanentIdentifier, error) {
60 var permID PermanentIdentifier
61 if _, err := asn1.UnmarshalWithParams(der, &permID, "explicit,tag:0"); err != nil {
62 return PermanentIdentifier{}, err
63 }
64 return permID, nil
65 }
66
67 // SubjectAltName contains GeneralName variations not supported by the
68 // crypto/x509 package.
69 //
70 // https://datatracker.ietf.org/doc/html/rfc5280
71 type SubjectAltName struct {
72 DirectoryNames []pkix.Name
73 PermanentIdentifiers []PermanentIdentifier
74 }
75
76 // ParseSubjectAltName parses a pkix.Extension into a SubjectAltName struct.
77 func ParseSubjectAltName(ext pkix.Extension) (*SubjectAltName, error) {
78 var out SubjectAltName
79 dirNames, otherNames, err := parseSubjectAltName(ext)
80 if err != nil {
81 return nil, fmt.Errorf("parseSubjectAltName: %v", err)
82 }
83 out.DirectoryNames = dirNames
84
85 for _, otherName := range otherNames {
86 if otherName.TypeID.Equal(oidPermanentIdentifier) {
87 permID, err := parsePermanentIdentifier(otherName.Value.FullBytes)
88 if err != nil {
89 return nil, fmt.Errorf("parsePermanentIdentifier: %v", err)
90 }
91 out.PermanentIdentifiers = append(out.PermanentIdentifiers, permID)
92 } else {
93 return nil, fmt.Errorf("expected type id %v, got %v", oidPermanentIdentifier, otherName.TypeID)
94 }
95 }
96 return &out, nil
97 }
98
99 // https://datatracker.ietf.org/doc/html/rfc5280#page-35
100 func parseSubjectAltName(ext pkix.Extension) (dirNames []pkix.Name, otherNames []otherName, err error) {
101 err = forEachSAN(ext.Value, func(generalName asn1.RawValue) error {
102 switch generalName.Tag {
103 case 0: // otherName
104 var otherName otherName
105 if _, err := asn1.UnmarshalWithParams(generalName.FullBytes, &otherName, "tag:0"); err != nil {
106 return fmt.Errorf("OtherName: asn1.UnmarshalWithParams: %v", err)
107 }
108 otherNames = append(otherNames, otherName)
109 case 4: // directoryName
110 var rdns pkix.RDNSequence
111 if _, err := asn1.Unmarshal(generalName.Bytes, &rdns); err != nil {
112 return fmt.Errorf("DirectoryName: asn1.Unmarshal: %v", err)
113 }
114 var dirName pkix.Name
115 dirName.FillFromRDNSequence(&rdns)
116 dirNames = append(dirNames, dirName)
117 default:
118 return fmt.Errorf("expected tag %d", generalName.Tag)
119 }
120 return nil
121 })
122 return
123 }
124
125 // Borrowed from the x509 package.
126 func forEachSAN(extension []byte, callback func(ext asn1.RawValue) error) error {
127 var seq asn1.RawValue
128 rest, err := asn1.Unmarshal(extension, &seq)
129 if err != nil {
130 return err
131 }
132 if len(rest) != 0 {
133 return errors.New("x509: trailing data after X.509 extension")
134 }
135 if !seq.IsCompound || seq.Tag != 16 || seq.Class != 0 {
136 return asn1.StructuralError{Msg: "bad SAN sequence"}
137 }
138
139 rest = seq.Bytes
140 for len(rest) > 0 {
141 var v asn1.RawValue
142 rest, err = asn1.Unmarshal(rest, &v)
143 if err != nil {
144 return err
145 }
146
147 if err := callback(v); err != nil {
148 return err
149 }
150 }
151
152 return nil
153 }
154
155 // MarshalSubjectAltName converts a SubjectAltName struct into a pkix.Extension,
156 // allowing callers to specify if the extension is critical.
157 func MarshalSubjectAltName(san *SubjectAltName, critical bool) (pkix.Extension, error) {
158 var generalNames []asn1.RawValue
159 for _, permID := range san.PermanentIdentifiers {
160 val, err := marshalOtherName(oidPermanentIdentifier, permID)
161 if err != nil {
162 return pkix.Extension{}, err
163 }
164 generalNames = append(generalNames, val)
165 }
166 for _, dirName := range san.DirectoryNames {
167 bytes, err := asn1.MarshalWithParams(dirName.ToRDNSequence(), "explicit,tag:4")
168 if err != nil {
169 return pkix.Extension{}, err
170 }
171 generalNames = append(generalNames, asn1.RawValue{FullBytes: bytes})
172 }
173 val, err := asn1.Marshal(generalNames)
174 if err != nil {
175 return pkix.Extension{}, err
176 }
177 return pkix.Extension{
178 Id: oid.SubjectAltName,
179 Critical: critical,
180 Value: val,
181 }, nil
182 }