github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/gmtls/prf_test.go (about) 1 // Copyright 2022 s1ren@github.com/hxx258456. 2 3 /* 4 gmtls是基于`golang/go`的`tls`包实现的国密改造版本。 5 对应版权声明: thrid_licenses/github.com/golang/go/LICENSE 6 */ 7 8 package gmtls 9 10 import ( 11 "encoding/hex" 12 "testing" 13 ) 14 15 type testSplitPreMasterSecretTest struct { 16 in, out1, out2 string 17 } 18 19 var testSplitPreMasterSecretTests = []testSplitPreMasterSecretTest{ 20 {"", "", ""}, 21 {"00", "00", "00"}, 22 {"0011", "00", "11"}, 23 {"001122", "0011", "1122"}, 24 {"00112233", "0011", "2233"}, 25 } 26 27 func TestSplitPreMasterSecret(t *testing.T) { 28 for i, test := range testSplitPreMasterSecretTests { 29 in, _ := hex.DecodeString(test.in) 30 out1, out2 := splitPreMasterSecret(in) 31 s1 := hex.EncodeToString(out1) 32 s2 := hex.EncodeToString(out2) 33 if s1 != test.out1 || s2 != test.out2 { 34 t.Errorf("#%d: got: (%s, %s) want: (%s, %s)", i, s1, s2, test.out1, test.out2) 35 } 36 } 37 } 38 39 type testKeysFromTest struct { 40 version uint16 41 suite *cipherSuite 42 preMasterSecret string 43 clientRandom, serverRandom string 44 masterSecret string 45 clientMAC, serverMAC string 46 clientKey, serverKey string 47 macLen, keyLen int 48 contextKeyingMaterial, noContextKeyingMaterial string 49 } 50 51 func TestKeysFromPreMasterSecret(t *testing.T) { 52 for i, test := range testKeysFromTests { 53 in, _ := hex.DecodeString(test.preMasterSecret) 54 clientRandom, _ := hex.DecodeString(test.clientRandom) 55 serverRandom, _ := hex.DecodeString(test.serverRandom) 56 57 masterSecret := masterFromPreMasterSecret(test.version, test.suite, in, clientRandom, serverRandom) 58 if s := hex.EncodeToString(masterSecret); s != test.masterSecret { 59 t.Errorf("#%d: bad master secret %s, want %s", i, s, test.masterSecret) 60 continue 61 } 62 63 clientMAC, serverMAC, clientKey, serverKey, _, _ := keysFromMasterSecret(test.version, test.suite, masterSecret, clientRandom, serverRandom, test.macLen, test.keyLen, 0) 64 clientMACString := hex.EncodeToString(clientMAC) 65 serverMACString := hex.EncodeToString(serverMAC) 66 clientKeyString := hex.EncodeToString(clientKey) 67 serverKeyString := hex.EncodeToString(serverKey) 68 if clientMACString != test.clientMAC || 69 serverMACString != test.serverMAC || 70 clientKeyString != test.clientKey || 71 serverKeyString != test.serverKey { 72 t.Errorf("#%d: got: (%s, %s, %s, %s) want: (%s, %s, %s, %s)", i, clientMACString, serverMACString, clientKeyString, serverKeyString, test.clientMAC, test.serverMAC, test.clientKey, test.serverKey) 73 } 74 75 ekm := ekmFromMasterSecret(test.version, test.suite, masterSecret, clientRandom, serverRandom) 76 contextKeyingMaterial, err := ekm("label", []byte("context"), 32) 77 if err != nil { 78 t.Fatalf("ekmFromMasterSecret failed: %v", err) 79 } 80 81 noContextKeyingMaterial, err := ekm("label", nil, 32) 82 if err != nil { 83 t.Fatalf("ekmFromMasterSecret failed: %v", err) 84 } 85 86 if hex.EncodeToString(contextKeyingMaterial) != test.contextKeyingMaterial || 87 hex.EncodeToString(noContextKeyingMaterial) != test.noContextKeyingMaterial { 88 t.Errorf("#%d: got keying material: (%s, %s) want: (%s, %s)", i, contextKeyingMaterial, noContextKeyingMaterial, test.contextKeyingMaterial, test.noContextKeyingMaterial) 89 } 90 } 91 } 92 93 // These test vectors were generated from GnuTLS using `gnutls-cli --insecure -d 9 ` 94 var testKeysFromTests = []testKeysFromTest{ 95 { 96 VersionTLS10, 97 cipherSuiteByID(TLS_RSA_WITH_RC4_128_SHA), 98 "0302cac83ad4b1db3b9ab49ad05957de2a504a634a386fc600889321e1a971f57479466830ac3e6f468e87f5385fa0c5", 99 "4ae66303755184a3917fcb44880605fcc53baa01912b22ed94473fc69cebd558", 100 "4ae663020ec16e6bb5130be918cfcafd4d765979a3136a5d50c593446e4e44db", 101 "3d851bab6e5556e959a16bc36d66cfae32f672bfa9ecdef6096cbb1b23472df1da63dbbd9827606413221d149ed08ceb", 102 "805aaa19b3d2c0a0759a4b6c9959890e08480119", 103 "2d22f9fe519c075c16448305ceee209fc24ad109", 104 "d50b5771244f850cd8117a9ccafe2cf1", 105 "e076e33206b30507a85c32855acd0919", 106 20, 107 16, 108 "4d1bb6fc278c37d27aa6e2a13c2e079095d143272c2aa939da33d88c1c0cec22", 109 "93fba89599b6321ae538e27c6548ceb8b46821864318f5190d64a375e5d69d41", 110 }, 111 { 112 VersionTLS10, 113 cipherSuiteByID(TLS_RSA_WITH_RC4_128_SHA), 114 "03023f7527316bc12cbcd69e4b9e8275d62c028f27e65c745cfcddc7ce01bd3570a111378b63848127f1c36e5f9e4890", 115 "4ae66364b5ea56b20ce4e25555aed2d7e67f42788dd03f3fee4adae0459ab106", 116 "4ae66363ab815cbf6a248b87d6b556184e945e9b97fbdf247858b0bdafacfa1c", 117 "7d64be7c80c59b740200b4b9c26d0baaa1c5ae56705acbcf2307fe62beb4728c19392c83f20483801cce022c77645460", 118 "97742ed60a0554ca13f04f97ee193177b971e3b0", 119 "37068751700400e03a8477a5c7eec0813ab9e0dc", 120 "207cddbc600d2a200abac6502053ee5c", 121 "df3f94f6e1eacc753b815fe16055cd43", 122 20, 123 16, 124 "2c9f8961a72b97cbe76553b5f954caf8294fc6360ef995ac1256fe9516d0ce7f", 125 "274f19c10291d188857ad8878e2119f5aa437d4da556601cf1337aff23154016", 126 }, 127 { 128 VersionTLS10, 129 cipherSuiteByID(TLS_RSA_WITH_RC4_128_SHA), 130 "832d515f1d61eebb2be56ba0ef79879efb9b527504abb386fb4310ed5d0e3b1f220d3bb6b455033a2773e6d8bdf951d278a187482b400d45deb88a5d5a6bb7d6a7a1decc04eb9ef0642876cd4a82d374d3b6ff35f0351dc5d411104de431375355addc39bfb1f6329fb163b0bc298d658338930d07d313cd980a7e3d9196cac1", 131 "4ae663b2ee389c0de147c509d8f18f5052afc4aaf9699efe8cb05ece883d3a5e", 132 "4ae664d503fd4cff50cfc1fb8fc606580f87b0fcdac9554ba0e01d785bdf278e", 133 "1aff2e7a2c4279d0126f57a65a77a8d9d0087cf2733366699bec27eb53d5740705a8574bb1acc2abbe90e44f0dd28d6c", 134 "3c7647c93c1379a31a609542aa44e7f117a70085", 135 "0d73102994be74a575a3ead8532590ca32a526d4", 136 "ac7581b0b6c10d85bbd905ffbf36c65e", 137 "ff07edde49682b45466bd2e39464b306", 138 20, 139 16, 140 "678b0d43f607de35241dc7e9d1a7388a52c35033a1a0336d4d740060a6638fe2", 141 "f3b4ac743f015ef21d79978297a53da3e579ee047133f38c234d829c0f907dab", 142 }, 143 }