@@ -150,14 +150,31 @@ func parseName(raw cryptobyte.String) (*pkix.RDNSequence, error) {
150
150
for ! raw .Empty () {
151
151
var rdnSet pkix.RelativeDistinguishedNameSET
152
152
var set cryptobyte.String
153
- if ! raw .ReadASN1 (& set , cryptobyte_asn1 .SET ) {
153
+ var rawSet cryptobyte.String
154
+
155
+ if ! raw .ReadASN1Element (& rawSet , cryptobyte_asn1 .SET ) {
156
+ return nil , errors .New ("x509: invalid RDNSequence" )
157
+ }
158
+
159
+ if ! rawSet .ReadASN1 (& set , cryptobyte_asn1 .SET ) {
154
160
return nil , errors .New ("x509: invalid RDNSequence" )
155
161
}
162
+
163
+ var rawAttrs []cryptobyte.String
164
+
156
165
for ! set .Empty () {
157
166
var atav cryptobyte.String
158
- if ! set .ReadASN1 (& atav , cryptobyte_asn1 .SEQUENCE ) {
167
+ var rawAttr cryptobyte.String
168
+
169
+ if ! set .ReadASN1Element (& rawAttr , cryptobyte_asn1 .SEQUENCE ) {
159
170
return nil , errors .New ("x509: invalid RDNSequence: invalid attribute" )
160
171
}
172
+ rawAttrs = append (rawAttrs , rawAttr )
173
+
174
+ if ! rawAttr .ReadASN1 (& atav , cryptobyte_asn1 .SEQUENCE ) {
175
+ return nil , errors .New ("x509: invalid RDNSequence: invalid attribute" )
176
+ }
177
+
161
178
var attr pkix.AttributeTypeAndValue
162
179
if ! atav .ReadASN1ObjectIdentifier (& attr .Type ) {
163
180
return nil , errors .New ("x509: invalid RDNSequence: invalid attribute type" )
@@ -175,6 +192,18 @@ func parseName(raw cryptobyte.String) (*pkix.RDNSequence, error) {
175
192
rdnSet = append (rdnSet , attr )
176
193
}
177
194
195
+ // Verify that the SET values are sorted according to DER encoding rules
196
+ // as required by X.690 section 11.6
197
+ if len (rawAttrs ) > 1 {
198
+ for i := 1 ; i < len (rawAttrs ); i ++ {
199
+ // Compare each attribute with the previous one
200
+ // In DER, they must be in ascending order when compared as octet strings
201
+ if bytes .Compare (rawAttrs [i - 1 ], rawAttrs [i ]) > 0 {
202
+ return nil , errors .New ("x509: invalid RDNSequence: SET values not in ascending order" )
203
+ }
204
+ }
205
+ }
206
+
178
207
rdnSeq = append (rdnSeq , rdnSet )
179
208
}
180
209
0 commit comments