25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

366 lines
13 KiB

  1. // Copyright 2018 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. package tls
  5. import (
  6. "crypto"
  7. "crypto/x509"
  8. "encoding/asn1"
  9. "encoding/pem"
  10. "errors"
  11. "fmt"
  12. "testing"
  13. "time"
  14. )
  15. // A PEM-encoded "delegation certificate", an X.509 certificate with the
  16. // DelegationUsage extension. The extension is defined in
  17. // specified in https://tools.ietf.org/html/draft-ietf-tls-subcerts-02.
  18. const DcCertWithDelegationUsage = `-----BEGIN CERTIFICATE-----
  19. MIIBejCCASGgAwIBAgIQXXtl0v50W2OadoW0QwLUlzAKBggqhkjOPQQDAjAUMRIw
  20. EAYDVQQKEwlBY21lIEluYy4wHhcNMTgwNzMwMjAxMTE5WhcNMTgwODA2MjAxMTE5
  21. WjAUMRIwEAYDVQQKEwlBY21lIEluYy4wWTATBgcqhkjOPQIBBggqhkjOPQMBBwNC
  22. AATcQuuaUNJ3kqKGs4DBdJVd7zWzyGANT4uBNGVkZ2cgaDsdFnx99fGibfgoWer8
  23. HLt9Z+S6Hs+8bDPBHNgTR/Lfo1UwUzAOBgNVHQ8BAf8EBAMCB4AwEwYDVR0lBAww
  24. CgYIKwYBBQUHAwEwDAYDVR0TAQH/BAIwADAPBgNVHREECDAGhwR/AAABMA0GCSsG
  25. AQQBgtpLLAQAMAoGCCqGSM49BAMCA0cAMEQCIEMdIkwwmzQAJ6RSDT3wcrsySx2B
  26. 5Lvx5HGzc43Fgu9eAiAi4sFXnizFBVUL43qXZBq4ARw17o0JW3/7eec1xttQhw==
  27. -----END CERTIFICATE-----
  28. `
  29. // The PEM-encoded "delegation key", the secret key associated with the
  30. // delegation certificate. This is a key for ECDSA with P256 and SHA256.
  31. const DcKeyWithDelegationUsage = `-----BEGIN EC PRIVATE KEY-----
  32. MHcCAQEEIAS/pGktmxK1hlt3gF4N2nkMrJnoZihvOO63nnNcxXQroAoGCCqGSM49
  33. AwEHoUQDQgAE3ELrmlDSd5KihrOAwXSVXe81s8hgDU+LgTRlZGdnIGg7HRZ8ffXx
  34. om34KFnq/By7fWfkuh7PvGwzwRzYE0fy3w==
  35. -----END EC PRIVATE KEY-----
  36. `
  37. // A certificate without the DelegationUsage extension.
  38. const DcCertWithoutDelegationUsage = `-----BEGIN CERTIFICATE-----
  39. MIIBajCCAQ+gAwIBAgIRAMUg/VFqJaWWJwZ9iHoMjqIwCgYIKoZIzj0EAwIwEjEQ
  40. MA4GA1UEChMHQWNtZSBDbzAeFw0xODA3MzAyMDExMTlaFw0xOTA3MzAyMDExMTla
  41. MBIxEDAOBgNVBAoTB0FjbWUgQ28wWTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAATA
  42. n+oeWSvSNHhEskSRgkkerCQDoV/NA+r3S5AtCOFT5AYLt8xltSTWerFI/YlZLIcL
  43. xlJPT7T+XpBnfS6xaAuxo0YwRDAOBgNVHQ8BAf8EBAMCBaAwEwYDVR0lBAwwCgYI
  44. KwYBBQUHAwEwDAYDVR0TAQH/BAIwADAPBgNVHREECDAGhwR/AAABMAoGCCqGSM49
  45. BAMCA0kAMEYCIQCFGWnoJmwH1rxNCKBJWVDBKDTSsYhySRk4h9RPyR8bUwIhAJxc
  46. KFyrowMTan791RJnyANH/4uYhmvkfhfrFGSTXUli
  47. -----END CERTIFICATE-----
  48. `
  49. // The secret key associatted with DcCertWithoutDelegationUsage.
  50. const DcKeyWithoutDelegationUsage = `-----BEGIN EC PRIVATE KEY-----
  51. MHcCAQEEIEP82pOhzx0tKkky9t0OmUo9MHgmfdAHxDN2cHmWGqOhoAoGCCqGSM49
  52. AwEHoUQDQgAEwJ/qHlkr0jR4RLJEkYJJHqwkA6FfzQPq90uQLQjhU+QGC7fMZbUk
  53. 1nqxSP2JWSyHC8ZST0+0/l6QZ30usWgLsQ==
  54. -----END EC PRIVATE KEY-----
  55. `
  56. // dcTestDC stores delegated credentials and their secret keys.
  57. type dcTestDC struct {
  58. Name string
  59. Version int
  60. Scheme int
  61. DC []byte
  62. PrivateKey []byte
  63. }
  64. // Use with maxVersion == VersionTLS13.
  65. const DcTestDataTLS13PEM = `-----BEGIN DC TEST DATA-----
  66. MIIIQzCCAUMTCXRsczEzcDI1NgICAwQCAgQDBIGyAAk6gAQDAwQAAFswWTATBgcq
  67. hkjOPQIBBggqhkjOPQMBBwNCAAQpQtUm8AWOzCN+aGUVsoKH9lZWNqkQCBGhpVtT
  68. u3ye6ACcwgNf81AYQ1ROb3EbWrnbvq9ap4a5QJ8AcrhZ9u0dBAMASDBGAiEA7LHb
  69. Fh+RDi9RTRjlP0+b2eP+4CDtuK0qKSjf4kFbJ9ICIQDB/XIXkLV6qLW70MhFWCUi
  70. 2eqyhwtvTuMyATEJnyHKvwR5MHcCAQEEILHC94EWZnuVJqrbq3U+BnEU8BQPGfk6
  71. pkB7mD8wqhl/oAoGCCqGSM49AwEHoUQDQgAEKULVJvAFjswjfmhlFbKCh/ZWVjap
  72. EAgRoaVbU7t8nugAnMIDX/NQGENUTm9xG1q5276vWqeGuUCfAHK4WfbtHTCCAesT
  73. CXRsczEzcDUyMQICAwQCAgYDBIHzAAk6gAYDAwQAAJ4wgZswEAYHKoZIzj0CAQYF
  74. K4EEACMDgYYABAHgWg5NSn/t/BBxU9uWVBwIz3NWfq2xo1eQMsJY1ui9ILtmFsLn
  75. QF1jbGrjlBZoh2sbHPFPl7yMOSYyVBFryhTaiQG7x11/Xs9fNC6AUm/6wROLMHTr
  76. qCkiqCjIKVtBaM8FCAfPLoJHzPUu/h79Q0IdBlVhl4nEa4cWVW34cECfT+YdjgQD
  77. AEYwRAIge+tF+cai/jfZtzUaVTcVuZfdIcGpRy4CfI2tKLipDCQCIAVigOh2jOFh
  78. QWbX4h4Vz3ULoIuM+3wsFad0S0oH1v9HBIHfMIHcAgEBBEIAzNpPpiTsrv+0a3oA
  79. CaGGr83/2Z632tygYjEOs919YrLR1Xe83hf5AvJLUz6u3RRlQdqwyPGQ1wm8baQ6
  80. E0Pf6j+gBwYFK4EEACOhgYkDgYYABAHgWg5NSn/t/BBxU9uWVBwIz3NWfq2xo1eQ
  81. MsJY1ui9ILtmFsLnQF1jbGrjlBZoh2sbHPFPl7yMOSYyVBFryhTaiQG7x11/Xs9f
  82. NC6AUm/6wROLMHTrqCkiqCjIKVtBaM8FCAfPLoJHzPUu/h79Q0IdBlVhl4nEa4cW
  83. VW34cECfT+YdjjCCAUITB2JhZHZlcnMCAwD/AAICBAMEgbIACTqABAP/AAAAWzBZ
  84. MBMGByqGSM49AgEGCCqGSM49AwEHA0IABCPo5FSmarRgC/15bymE+3s4TXyQH9Oh
  85. nlcKbAR70jqWLr9jbyjT7dy09sr5B6cVlw8AU2TeojdRUNG7y4nKnLsEAwBIMEYC
  86. IQDZiMm7SoNMMvvrlxOF0OMSt1/hMOras702RDI2wvT92gIhAKgCmYucgBUIqMJd
  87. d6g2FcY9UZnPzvnSuX9uBm38RMLMBHkwdwIBAQQgnx2Os1Z5kbZo61ItkpwJ0khL
  88. 7zgzLcc1X4unR3R56q+gCgYIKoZIzj0DAQehRANCAAQj6ORUpmq0YAv9eW8phPt7
  89. OE18kB/ToZ5XCmwEe9I6li6/Y28o0+3ctPbK+QenFZcPAFNk3qI3UVDRu8uJypy7
  90. MIIBPxMGYmFka2V5AgIDBAICBAMEgbEACTqABAMDBAAAWzBZMBMGByqGSM49AgEG
  91. CCqGSM49AwEHA0IABGGXD4Td3D7im9y0S1wGoFgL4afAiklkSlQcNus2XfGUJS4c
  92. io+gm4NBMcXby6LpN4lg5/0+K0i448WrIdd2eBYEAwBHMEUCIBMirxmjL9Yeigpl
  93. aeqHncrT4V2u+sYBqa+dUUCXDTaqAiEAuR2geInXmNRtGWVltZh1pnohvwloPVvu
  94. XK5qUb9g6/gEeTB3AgEBBCDk7f6Fto9m6vEDYiZapi2Hm8ranfS0AOgfnDfsRQa5
  95. PKAKBggqhkjOPQMBB6FEA0IABFmA7YsXewnCF0R5eHLBwn4RsF1F5IwB8ZLpL2v4
  96. GBD6YHmZDPBZ2/SZ3LxLGgT5yiO1/5y2ujDXsQ9X78ucHn8wggE+EwZiYWRzaWcC
  97. AgMEAgIEAwSBsAAJOoAEAwMEAABbMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE
  98. W2eqjqibupKlU/BwVWwfNE1qUdxqhF3cen0aKl8in24PcEi3AH1Y/zubsjoKah/q
  99. YUfcmgAvhvsSFqohWzMa5gQDAEYwRAIgT4Tm7648J1OuTrn+HAJXVfzoXbcL/QUx
  100. YxVDcpxytkoCIDulABj6w3EoQLoq8b1V781oPHKkUR7+L/SUPj/DxKQ2BHkwdwIB
  101. AQQgIAwscB81XCsAujU+tr75y7yMFfSLtFkPAzn3/GiXpoWgCgYIKoZIzj0DAQeh
  102. RANCAARbZ6qOqJu6kqVT8HBVbB80TWpR3GqEXdx6fRoqXyKfbg9wSLcAfVj/O5uy
  103. OgpqH+phR9yaAC+G+xIWqiFbMxrmMIIBPhMFdGxzMTICAgMDAgIEAwSBsQAJOoAE
  104. AwMDAABbMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEnsChIIFXdvdOTFnf3cyv
  105. MsHKpSy68X+SbepvhPg+MBrn+ly9mb+hWPp2j0UJKiXwQmMf4vicNOYyjreml8Hf
  106. VQQDAEcwRQIhANfDJ57MDLZqtye+uolguWx39vhfkvB9svEjYZwWTcoKAiALBgkH
  107. OoRxcalH9qbE2p6LHLszqYyYW312aTHHYF0/6QR5MHcCAQEEILFX1gHwKwJwAQI+
  108. GNisTdlwN0clslAccLogW0ON0gAZoAoGCCqGSM49AwEHoUQDQgAEnsChIIFXdvdO
  109. TFnf3cyvMsHKpSy68X+SbepvhPg+MBrn+ly9mb+hWPp2j0UJKiXwQmMf4vicNOYy
  110. jreml8HfVQ==
  111. -----END DC TEST DATA-----`
  112. // Parses the input PEM block containing the test DCs.
  113. func dcLoadTestData(in []byte, out *[]dcTestDC) error {
  114. block, _ := pem.Decode(in)
  115. if block == nil {
  116. return errors.New("failed to decode DC tests PEM block")
  117. }
  118. // Parse the DER-encoded test DCs.
  119. _, err := asn1.Unmarshal(block.Bytes, out)
  120. if err != nil {
  121. return errors.New("failed to unmarshal DC test ASN.1 data")
  122. }
  123. // Check that the test data is for the right version. This should be
  124. // maxVersion, defined in common.go.
  125. for _, test := range *out {
  126. dc, err := unmarshalDelegatedCredential(test.DC)
  127. if err != nil {
  128. return err
  129. }
  130. // Sanity check that test version matches the version encoded by the DC.
  131. testVersion := uint16(test.Version)
  132. if dc.cred.expectedVersion != testVersion {
  133. return fmt.Errorf(
  134. "test version doesn't match credential version: got: 0x0%04x; want: 0x%04x",
  135. testVersion, dc.cred.expectedVersion)
  136. }
  137. // With the exception of "badvers" and "tsl12", all test DCs should have
  138. // the expected verison.
  139. if test.Name != "badvers" && test.Name != "tls12" && testVersion != maxVersion {
  140. return fmt.Errorf(
  141. "encountered test with wrong version: got: 0x0%04x; want: 0x%04x",
  142. test.Version, maxVersion)
  143. }
  144. }
  145. return nil
  146. }
  147. var dcTestDCs []dcTestDC
  148. var dcTestConfig *Config
  149. var dcTestDelegationCert Certificate
  150. var dcTestCert Certificate
  151. var dcTestNow time.Time
  152. func init() {
  153. // Load the DC test data.
  154. var testData []byte
  155. if maxVersion != 0x0304 {
  156. panic(fmt.Errorf("no test data for version %04x", maxVersion))
  157. }
  158. testData = []byte(DcTestDataTLS13PEM)
  159. err := dcLoadTestData(testData, &dcTestDCs)
  160. if err != nil {
  161. panic(err)
  162. }
  163. // The base configuration for the client and server.
  164. dcTestConfig = &Config{
  165. Time: func() time.Time {
  166. return dcTestNow
  167. },
  168. Rand: zeroSource{},
  169. Certificates: nil,
  170. MinVersion: VersionTLS10,
  171. MaxVersion: VersionTLS13,
  172. CipherSuites: allCipherSuites(),
  173. }
  174. // The delegation certificate.
  175. dcTestDelegationCert, err = X509KeyPair([]byte(DcCertWithDelegationUsage), []byte(DcKeyWithDelegationUsage))
  176. if err != nil {
  177. panic(err)
  178. }
  179. dcTestDelegationCert.Leaf, err = x509.ParseCertificate(dcTestDelegationCert.Certificate[0])
  180. if err != nil {
  181. panic(err)
  182. }
  183. // A certificate without the the DelegationUsage extension for X.509.
  184. dcTestCert, err = X509KeyPair([]byte(DcCertWithoutDelegationUsage), []byte(DcKeyWithoutDelegationUsage))
  185. if err != nil {
  186. panic(err)
  187. }
  188. dcTestCert.Leaf, err = x509.ParseCertificate(dcTestCert.Certificate[0])
  189. if err != nil {
  190. panic(err)
  191. }
  192. // For testing purposes, use the point at which the test DCs were generated
  193. // as the current time. This is the same as the time at which the
  194. // delegation certificate was generated.
  195. dcTestNow = dcTestDelegationCert.Leaf.NotBefore
  196. // Make these roots of these certificates the client's trusted CAs.
  197. dcTestConfig.RootCAs = x509.NewCertPool()
  198. raw := dcTestDelegationCert.Certificate[len(dcTestDelegationCert.Certificate)-1]
  199. root, err := x509.ParseCertificate(raw)
  200. if err != nil {
  201. panic(err)
  202. }
  203. dcTestConfig.RootCAs.AddCert(root)
  204. raw = dcTestCert.Certificate[len(dcTestCert.Certificate)-1]
  205. root, err = x509.ParseCertificate(raw)
  206. if err != nil {
  207. panic(err)
  208. }
  209. dcTestConfig.RootCAs.AddCert(root)
  210. }
  211. // Executes the handshake with the given configuration and returns true if the
  212. // delegated credential extension was successfully negotiated.
  213. func testConnWithDC(t *testing.T, clientConfig, serverConfig *Config) (bool, error) {
  214. ln := newLocalListener(t)
  215. defer ln.Close()
  216. // Listen for and serve a single client connection.
  217. srvCh := make(chan *Conn, 1)
  218. var serr error
  219. go func() {
  220. sconn, err := ln.Accept()
  221. if err != nil {
  222. serr = err
  223. srvCh <- nil
  224. return
  225. }
  226. srv := Server(sconn, serverConfig)
  227. if err := srv.Handshake(); err != nil {
  228. serr = fmt.Errorf("handshake: %v", err)
  229. srvCh <- nil
  230. return
  231. }
  232. srvCh <- srv
  233. }()
  234. // Dial the server.
  235. cli, err := Dial("tcp", ln.Addr().String(), clientConfig)
  236. if err != nil {
  237. return false, err
  238. }
  239. defer cli.Close()
  240. srv := <-srvCh
  241. if srv == nil {
  242. return false, serr
  243. }
  244. // Return true if the client's conn.dc structure was instantiated.
  245. st := cli.ConnectionState()
  246. return (st.DelegatedCredential != nil), nil
  247. }
  248. // Checks that the client suppports a version >= 1.2 and accepts delegated
  249. // credentials. If so, it returns the delegation certificate; otherwise it
  250. // returns a plain certificate.
  251. func testServerGetCertificate(ch *ClientHelloInfo) (*Certificate, error) {
  252. versOk := false
  253. for _, vers := range ch.SupportedVersions {
  254. versOk = versOk || (vers >= uint16(VersionTLS12))
  255. }
  256. if versOk && ch.AcceptsDelegatedCredential {
  257. return &dcTestDelegationCert, nil
  258. }
  259. return &dcTestCert, nil
  260. }
  261. // Various test cases for handshakes involving DCs.
  262. var dcTesters = []struct {
  263. clientDC bool
  264. serverDC bool
  265. clientSkipVerify bool
  266. clientMaxVers uint16
  267. serverMaxVers uint16
  268. nowOffset time.Duration
  269. dcTestName string
  270. expectSuccess bool
  271. expectDC bool
  272. name string
  273. }{
  274. {true, true, false, VersionTLS13, VersionTLS13, 0, "tls13p256", true, true, "tls13"},
  275. {true, true, false, VersionTLS13, VersionTLS13, 0, "tls13p521", true, true, "tls13"},
  276. {true, false, false, VersionTLS13, VersionTLS13, 0, "tls13p256", true, false, "server no dc"},
  277. {true, true, false, VersionTLS12, VersionTLS13, 0, "tls13p256", true, false, "client old"},
  278. {true, true, false, VersionTLS13, VersionTLS12, 0, "tls13p256", true, false, "server old"},
  279. {true, true, false, VersionTLS13, VersionTLS13, 0, "badkey", false, false, "bad key"},
  280. {true, true, true, VersionTLS13, VersionTLS13, 0, "badsig", true, true, "bad key, skip verify"},
  281. {true, true, false, VersionTLS13, VersionTLS13, dcMaxTTL, "tls13", false, false, "expired dc"},
  282. {true, true, false, VersionTLS13, VersionTLS13, 0, "badvers", false, false, "dc wrong version"},
  283. {true, true, false, VersionTLS12, VersionTLS12, 0, "tls12", true, false, "tls12"},
  284. }
  285. // Tests the handshake with the delegated credential extension for each test
  286. // case in dcTests.
  287. func TestDCHandshake(t *testing.T) {
  288. clientConfig := dcTestConfig.Clone()
  289. serverConfig := dcTestConfig.Clone()
  290. serverConfig.GetCertificate = testServerGetCertificate
  291. for i, tester := range dcTesters {
  292. clientConfig.MaxVersion = tester.clientMaxVers
  293. serverConfig.MaxVersion = tester.serverMaxVers
  294. clientConfig.InsecureSkipVerify = tester.clientSkipVerify
  295. clientConfig.AcceptDelegatedCredential = tester.clientDC
  296. clientConfig.Time = func() time.Time {
  297. return dcTestNow.Add(time.Duration(tester.nowOffset))
  298. }
  299. if tester.serverDC {
  300. serverConfig.GetDelegatedCredential = func(
  301. ch *ClientHelloInfo, vers uint16) ([]byte, crypto.PrivateKey, error) {
  302. if vers < VersionTLS13 {
  303. return nil, nil, nil
  304. }
  305. for _, test := range dcTestDCs {
  306. if test.Name == tester.dcTestName {
  307. sk, err := x509.ParseECPrivateKey(test.PrivateKey)
  308. if err != nil {
  309. return nil, nil, err
  310. }
  311. return test.DC, sk, nil
  312. }
  313. }
  314. return nil, nil, fmt.Errorf("Test DC with name '%s' not found", tester.dcTestName)
  315. }
  316. } else {
  317. serverConfig.GetDelegatedCredential = nil
  318. }
  319. usedDC, err := testConnWithDC(t, clientConfig, serverConfig)
  320. if err != nil && tester.expectSuccess {
  321. t.Errorf("test #%d (%s) fails: %s", i+1, tester.name, err)
  322. } else if err == nil && !tester.expectSuccess {
  323. t.Errorf("test #%d (%s) succeeds; expected failure", i+1, tester.name)
  324. }
  325. if usedDC != tester.expectDC {
  326. t.Errorf("test #%d (%s) usedDC = %v; expected %v", i+1, tester.name, usedDC, tester.expectDC)
  327. }
  328. }
  329. }