Skip to content

Commit 0ff40a7

Browse files
odeke-emagl
authored andcommitted
crypto/x509: check that the issuer name matches the issuer's subject name.
Fixes #14955. Change-Id: I157432584bb51088bec565f6bb9e64348345cff9 Reviewed-on: https://go-review.googlesource.com/23571 Run-TryBot: Adam Langley <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Adam Langley <[email protected]>
1 parent 13c829e commit 0ff40a7

File tree

2 files changed

+152
-0
lines changed

2 files changed

+152
-0
lines changed

src/crypto/x509/verify.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package x509
66

77
import (
8+
"bytes"
89
"errors"
910
"fmt"
1011
"net"
@@ -33,6 +34,9 @@ const (
3334
// IncompatibleUsage results when the certificate's key usage indicates
3435
// that it may only be used for a different purpose.
3536
IncompatibleUsage
37+
// NameMismatch results when the subject name of a parent certificate
38+
// does not match the issuer name in the child.
39+
NameMismatch
3640
)
3741

3842
// CertificateInvalidError results when an odd error occurs. Users of this
@@ -54,6 +58,8 @@ func (e CertificateInvalidError) Error() string {
5458
return "x509: too many intermediates for path length constraint"
5559
case IncompatibleUsage:
5660
return "x509: certificate specifies an incompatible key usage"
61+
case NameMismatch:
62+
return "x509: issuer name does not match subject from issuing certificate"
5763
}
5864
return "x509: unknown error"
5965
}
@@ -185,6 +191,13 @@ func matchNameConstraint(domain, constraint string) bool {
185191

186192
// isValid performs validity checks on the c.
187193
func (c *Certificate) isValid(certType int, currentChain []*Certificate, opts *VerifyOptions) error {
194+
if len(currentChain) > 0 {
195+
child := currentChain[len(currentChain)-1]
196+
if !bytes.Equal(child.RawIssuer, c.RawSubject) {
197+
return CertificateInvalidError{c, NameMismatch}
198+
}
199+
}
200+
188201
now := opts.CurrentTime
189202
if now.IsZero() {
190203
now = time.Now()

src/crypto/x509/verify_test.go

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,17 @@ var verifyTests = []verifyTest{
260260

261261
errorCallback: expectHostnameError,
262262
},
263+
{
264+
// The issuer name in the leaf doesn't exactly match the
265+
// subject name in the root. Go does not perform
266+
// canonicalization and so should reject this. See issue 14955.
267+
leaf: issuerSubjectMatchLeaf,
268+
roots: []string{issuerSubjectMatchRoot},
269+
currentTime: 1475787715,
270+
systemSkip: true,
271+
272+
errorCallback: expectSubjectIssuerMismatcthError,
273+
},
263274
}
264275

265276
func expectHostnameError(t *testing.T, i int, err error) (ok bool) {
@@ -314,6 +325,14 @@ func expectHashError(t *testing.T, i int, err error) bool {
314325
return true
315326
}
316327

328+
func expectSubjectIssuerMismatcthError(t *testing.T, i int, err error) (ok bool) {
329+
if inval, ok := err.(CertificateInvalidError); !ok || inval.Reason != NameMismatch {
330+
t.Errorf("#%d: error was not a NameMismatch: %s", i, err)
331+
return false
332+
}
333+
return true
334+
}
335+
317336
func certificateFromPEM(pemBytes string) (*Certificate, error) {
318337
block, _ := pem.Decode([]byte(pemBytes))
319338
if block == nil {
@@ -1133,6 +1152,126 @@ vRAvOtNiKtPzFeQVdbRPOskC4rcHyPeiDAMAMixeLi63+CFty4da3r5lRezeedCE
11331152
cw3ESZzThBwWqvPOtJdpXdm+r57pDW8qD+/0lY8wfImMNkQAyCUCLg/1Lxt/hrBj
11341153
-----END CERTIFICATE-----`
11351154

1155+
const issuerSubjectMatchRoot = `
1156+
Certificate:
1157+
Data:
1158+
Version: 3 (0x2)
1159+
Serial Number: 161640039802297062 (0x23e42c281e55ae6)
1160+
Signature Algorithm: sha256WithRSAEncryption
1161+
Issuer: O=Golang, CN=Root ca
1162+
Validity
1163+
Not Before: Jan 1 00:00:00 2015 GMT
1164+
Not After : Jan 1 00:00:00 2025 GMT
1165+
Subject: O=Golang, CN=Root ca
1166+
Subject Public Key Info:
1167+
Public Key Algorithm: rsaEncryption
1168+
Public-Key: (1024 bit)
1169+
Modulus:
1170+
00:e9:0e:7f:11:0c:e6:5a:e6:86:83:70:f6:51:07:
1171+
2e:02:78:11:f5:b2:24:92:38:ee:26:62:02:c7:94:
1172+
f1:3e:a1:77:6a:c0:8f:d5:22:68:b6:5d:e2:4c:da:
1173+
e0:85:11:35:c2:92:72:49:8d:81:b4:88:97:6b:b7:
1174+
fc:b2:44:5b:d9:4d:06:70:f9:0c:c6:8f:e9:b3:df:
1175+
a3:6a:84:6c:43:59:be:9d:b2:d0:76:9b:c3:d7:fa:
1176+
99:59:c3:b8:e5:f3:53:03:bd:49:d6:b3:cc:a2:43:
1177+
fe:ad:c2:0b:b9:01:b8:56:29:94:03:24:a7:0d:28:
1178+
21:29:a9:ae:94:5b:4a:f9:9f
1179+
Exponent: 65537 (0x10001)
1180+
X509v3 extensions:
1181+
X509v3 Key Usage: critical
1182+
Certificate Sign
1183+
X509v3 Extended Key Usage:
1184+
TLS Web Server Authentication, TLS Web Client Authentication
1185+
X509v3 Basic Constraints: critical
1186+
CA:TRUE
1187+
X509v3 Subject Key Identifier:
1188+
40:37:D7:01:FB:40:2F:B8:1C:7E:54:04:27:8C:59:01
1189+
Signature Algorithm: sha256WithRSAEncryption
1190+
6f:84:df:49:e0:99:d4:71:66:1d:32:86:56:cb:ea:5a:6b:0e:
1191+
00:6a:d1:5a:6e:1f:06:23:07:ff:cb:d1:1a:74:e4:24:43:0b:
1192+
aa:2a:a0:73:75:25:82:bc:bf:3f:a9:f8:48:88:ac:ed:3a:94:
1193+
3b:0d:d3:88:c8:67:44:61:33:df:71:6c:c5:af:ed:16:8c:bf:
1194+
82:f9:49:bb:e3:2a:07:53:36:37:25:77:de:91:a4:77:09:7f:
1195+
6f:b2:91:58:c4:05:89:ea:8e:fa:e1:3b:19:ef:f8:f6:94:b7:
1196+
7b:27:e6:e4:84:dd:2b:f5:93:f5:3c:d8:86:c5:38:01:56:5c:
1197+
9f:6d
1198+
-----BEGIN CERTIFICATE-----
1199+
MIICIDCCAYmgAwIBAgIIAj5CwoHlWuYwDQYJKoZIhvcNAQELBQAwIzEPMA0GA1UE
1200+
ChMGR29sYW5nMRAwDgYDVQQDEwdSb290IGNhMB4XDTE1MDEwMTAwMDAwMFoXDTI1
1201+
MDEwMTAwMDAwMFowIzEPMA0GA1UEChMGR29sYW5nMRAwDgYDVQQDEwdSb290IGNh
1202+
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDpDn8RDOZa5oaDcPZRBy4CeBH1
1203+
siSSOO4mYgLHlPE+oXdqwI/VImi2XeJM2uCFETXCknJJjYG0iJdrt/yyRFvZTQZw
1204+
+QzGj+mz36NqhGxDWb6dstB2m8PX+plZw7jl81MDvUnWs8yiQ/6twgu5AbhWKZQD
1205+
JKcNKCEpqa6UW0r5nwIDAQABo10wWzAOBgNVHQ8BAf8EBAMCAgQwHQYDVR0lBBYw
1206+
FAYIKwYBBQUHAwEGCCsGAQUFBwMCMA8GA1UdEwEB/wQFMAMBAf8wGQYDVR0OBBIE
1207+
EEA31wH7QC+4HH5UBCeMWQEwDQYJKoZIhvcNAQELBQADgYEAb4TfSeCZ1HFmHTKG
1208+
VsvqWmsOAGrRWm4fBiMH/8vRGnTkJEMLqiqgc3Ulgry/P6n4SIis7TqUOw3TiMhn
1209+
RGEz33Fsxa/tFoy/gvlJu+MqB1M2NyV33pGkdwl/b7KRWMQFieqO+uE7Ge/49pS3
1210+
eyfm5ITdK/WT9TzYhsU4AVZcn20=
1211+
-----END CERTIFICATE-----`
1212+
1213+
const issuerSubjectMatchLeaf = `
1214+
Certificate:
1215+
Data:
1216+
Version: 3 (0x2)
1217+
Serial Number: 16785088708916013734 (0xe8f09d3fe25beaa6)
1218+
Signature Algorithm: sha256WithRSAEncryption
1219+
Issuer: O=Golang, CN=Root CA
1220+
Validity
1221+
Not Before: Jan 1 00:00:00 2015 GMT
1222+
Not After : Jan 1 00:00:00 2025 GMT
1223+
Subject: O=Golang, CN=Leaf
1224+
Subject Public Key Info:
1225+
Public Key Algorithm: rsaEncryption
1226+
Public-Key: (1024 bit)
1227+
Modulus:
1228+
00:db:46:7d:93:2e:12:27:06:48:bc:06:28:21:ab:
1229+
7e:c4:b6:a2:5d:fe:1e:52:45:88:7a:36:47:a5:08:
1230+
0d:92:42:5b:c2:81:c0:be:97:79:98:40:fb:4f:6d:
1231+
14:fd:2b:13:8b:c2:a5:2e:67:d8:d4:09:9e:d6:22:
1232+
38:b7:4a:0b:74:73:2b:c2:34:f1:d1:93:e5:96:d9:
1233+
74:7b:f3:58:9f:6c:61:3c:c0:b0:41:d4:d9:2b:2b:
1234+
24:23:77:5b:1c:3b:bd:75:5d:ce:20:54:cf:a1:63:
1235+
87:1d:1e:24:c4:f3:1d:1a:50:8b:aa:b6:14:43:ed:
1236+
97:a7:75:62:f4:14:c8:52:d7
1237+
Exponent: 65537 (0x10001)
1238+
X509v3 extensions:
1239+
X509v3 Key Usage: critical
1240+
Digital Signature, Key Encipherment
1241+
X509v3 Extended Key Usage:
1242+
TLS Web Server Authentication, TLS Web Client Authentication
1243+
X509v3 Basic Constraints: critical
1244+
CA:FALSE
1245+
X509v3 Subject Key Identifier:
1246+
9F:91:16:1F:43:43:3E:49:A6:DE:6D:B6:80:D7:9F:60
1247+
X509v3 Authority Key Identifier:
1248+
keyid:40:37:D7:01:FB:40:2F:B8:1C:7E:54:04:27:8C:59:01
1249+
1250+
Signature Algorithm: sha256WithRSAEncryption
1251+
8d:86:05:da:89:f5:1d:c5:16:14:41:b9:34:87:2b:5c:38:99:
1252+
e3:d9:5a:5b:7a:5b:de:0b:5c:08:45:09:6f:1c:9d:31:5f:08:
1253+
ca:7a:a3:99:da:83:0b:22:be:4f:02:35:91:4e:5d:5c:37:bf:
1254+
89:22:58:7d:30:76:d2:2f:d0:a0:ee:77:9e:77:c0:d6:19:eb:
1255+
ec:a0:63:35:6a:80:9b:80:1a:80:de:64:bc:40:38:3c:22:69:
1256+
ad:46:26:a2:3d:ea:f4:c2:92:49:16:03:96:ae:64:21:b9:7c:
1257+
ee:64:91:47:81:aa:b4:0c:09:2b:12:1a:b2:f3:af:50:b3:b1:
1258+
ce:24
1259+
-----BEGIN CERTIFICATE-----
1260+
MIICODCCAaGgAwIBAgIJAOjwnT/iW+qmMA0GCSqGSIb3DQEBCwUAMCMxDzANBgNV
1261+
BAoTBkdvbGFuZzEQMA4GA1UEAxMHUm9vdCBDQTAeFw0xNTAxMDEwMDAwMDBaFw0y
1262+
NTAxMDEwMDAwMDBaMCAxDzANBgNVBAoTBkdvbGFuZzENMAsGA1UEAxMETGVhZjCB
1263+
nzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEA20Z9ky4SJwZIvAYoIat+xLaiXf4e
1264+
UkWIejZHpQgNkkJbwoHAvpd5mED7T20U/SsTi8KlLmfY1Ame1iI4t0oLdHMrwjTx
1265+
0ZPlltl0e/NYn2xhPMCwQdTZKyskI3dbHDu9dV3OIFTPoWOHHR4kxPMdGlCLqrYU
1266+
Q+2Xp3Vi9BTIUtcCAwEAAaN3MHUwDgYDVR0PAQH/BAQDAgWgMB0GA1UdJQQWMBQG
1267+
CCsGAQUFBwMBBggrBgEFBQcDAjAMBgNVHRMBAf8EAjAAMBkGA1UdDgQSBBCfkRYf
1268+
Q0M+SabebbaA159gMBsGA1UdIwQUMBKAEEA31wH7QC+4HH5UBCeMWQEwDQYJKoZI
1269+
hvcNAQELBQADgYEAjYYF2on1HcUWFEG5NIcrXDiZ49laW3pb3gtcCEUJbxydMV8I
1270+
ynqjmdqDCyK+TwI1kU5dXDe/iSJYfTB20i/QoO53nnfA1hnr7KBjNWqAm4AagN5k
1271+
vEA4PCJprUYmoj3q9MKSSRYDlq5kIbl87mSRR4GqtAwJKxIasvOvULOxziQ=
1272+
-----END CERTIFICATE-----
1273+
`
1274+
11361275
var unknownAuthorityErrorTests = []struct {
11371276
cert string
11381277
expected string

0 commit comments

Comments
 (0)