helpers_test.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. package helpers
  2. import (
  3. "bytes"
  4. "crypto/ecdsa"
  5. "crypto/elliptic"
  6. "crypto/rand"
  7. "crypto/rsa"
  8. "crypto/x509"
  9. "crypto/x509/pkix"
  10. "encoding/asn1"
  11. "encoding/pem"
  12. "io/ioutil"
  13. "math"
  14. "testing"
  15. "time"
  16. "golang.org/x/crypto/ocsp"
  17. "github.com/google/certificate-transparency-go"
  18. )
  19. const (
  20. testCertFile = "testdata/cert.pem"
  21. testCertDERFile = "testdata/cert.der"
  22. testBundleFile = "testdata/bundle.pem"
  23. testExtraWSCertFile = "testdata/cert_with_whitespace.pem"
  24. testExtraWSBundleFile = "testdata/bundle_with_whitespace.pem"
  25. testMessedUpBundleFile = "testdata/messed_up_bundle.pem"
  26. testMessedUpCertFile = "testdata/messedupcert.pem"
  27. testEmptyCertFile = "testdata/emptycert.pem"
  28. testPrivateRSAKey = "testdata/priv_rsa_key.pem"
  29. testPrivateECDSAKey = "testdata/private_ecdsa_key.pem"
  30. testUnsupportedECDSAKey = "testdata/secp256k1-key.pem"
  31. testMessedUpPrivateKey = "testdata/messed_up_priv_key.pem"
  32. testEncryptedPrivateKey = "testdata/enc_priv_key.pem"
  33. testEmptyPem = "testdata/empty.pem"
  34. testNoHeaderCert = "testdata/noheadercert.pem"
  35. testSinglePKCS7 = "testdata/cert_pkcs7.pem" // openssl crl2pkcs7 -nocrl -out cert_pkcs7.pem -in cert.pem
  36. testEmptyPKCS7DER = "testdata/empty_pkcs7.der" // openssl crl2pkcs7 -nocrl -out empty_pkcs7.der -outform der
  37. testEmptyPKCS7PEM = "testdata/empty_pkcs7.pem" // openssl crl2pkcs7 -nocrl -out empty_pkcs7.pem -outform pem
  38. testMultiplePKCS7 = "testdata/bundle_pkcs7.pem"
  39. testPKCS12EmptyPswd = "testdata/emptypasswordpkcs12.p12"
  40. testPKCS12Passwordispassword = "testdata/passwordpkcs12.p12"
  41. testPKCS12MultipleCerts = "testdata/multiplecerts.p12"
  42. testCSRPEM = "testdata/test.csr.pem"
  43. testCSRPEMBad = "testdata/test.bad.csr.pem"
  44. )
  45. func TestParseCertificatesDER(t *testing.T) {
  46. var password = []string{"password", "", ""}
  47. for i, testFile := range []string{testPKCS12Passwordispassword, testPKCS12EmptyPswd, testCertDERFile} {
  48. testDER, err := ioutil.ReadFile(testFile)
  49. if err != nil {
  50. t.Fatal(err)
  51. }
  52. if _, _, err := ParseCertificatesDER(testDER, password[i]); err != nil {
  53. t.Fatal(err)
  54. }
  55. // Incorrect Password for PKCS12 formatted files
  56. if _, _, err := ParseCertificatesDER(testDER, "incorrectpassword"); err == nil && i != 2 {
  57. t.Fatal(err)
  58. }
  59. }
  60. testDER, err := ioutil.ReadFile(testEmptyPKCS7DER)
  61. if err != nil {
  62. t.Fatal(err)
  63. }
  64. // PKCS7 with no certificates
  65. if _, _, err := ParseCertificatesDER(testDER, ""); err == nil {
  66. t.Fatal(err)
  67. }
  68. }
  69. func TestKeyLength(t *testing.T) {
  70. expNil := 0
  71. recNil := KeyLength(nil)
  72. if expNil != recNil {
  73. t.Fatal("KeyLength on nil did not return 0")
  74. }
  75. expNonsense := 0
  76. inNonsense := "string?"
  77. outNonsense := KeyLength(inNonsense)
  78. if expNonsense != outNonsense {
  79. t.Fatal("KeyLength malfunctioning on nonsense input")
  80. }
  81. //test the ecdsa branch
  82. ecdsaPriv, _ := ecdsa.GenerateKey(elliptic.P224(), rand.Reader)
  83. ecdsaIn, _ := ecdsaPriv.Public().(*ecdsa.PublicKey)
  84. expEcdsa := ecdsaIn.Curve.Params().BitSize
  85. outEcdsa := KeyLength(ecdsaIn)
  86. if expEcdsa != outEcdsa {
  87. t.Fatal("KeyLength malfunctioning on ecdsa input")
  88. }
  89. //test the rsa branch
  90. rsaPriv, _ := rsa.GenerateKey(rand.Reader, 256)
  91. rsaIn, _ := rsaPriv.Public().(*rsa.PublicKey)
  92. expRsa := rsaIn.N.BitLen()
  93. outRsa := KeyLength(rsaIn)
  94. if expRsa != outRsa {
  95. t.Fatal("KeyLength malfunctioning on rsa input")
  96. }
  97. }
  98. func TestExpiryTime(t *testing.T) {
  99. // nil case
  100. var expNil time.Time
  101. inNil := []*x509.Certificate{}
  102. outNil := ExpiryTime(inNil)
  103. if expNil != outNil {
  104. t.Fatal("Expiry time is malfunctioning on empty input")
  105. }
  106. //read a pem file and use that expiry date
  107. bytes, _ := ioutil.ReadFile(testBundleFile)
  108. certs, err := ParseCertificatesPEM(bytes)
  109. if err != nil {
  110. t.Fatalf("%v", err)
  111. }
  112. expected := time.Date(2014, time.April, 15, 0, 0, 0, 0, time.UTC)
  113. out := ExpiryTime(certs)
  114. if out != expected {
  115. t.Fatalf("Expected %v, got %v", expected, out)
  116. }
  117. }
  118. func TestMonthsValid(t *testing.T) {
  119. var cert = &x509.Certificate{
  120. NotBefore: time.Date(2015, time.April, 01, 0, 0, 0, 0, time.UTC),
  121. NotAfter: time.Date(2015, time.April, 01, 0, 0, 0, 0, time.UTC),
  122. }
  123. if MonthsValid(cert) != 0 {
  124. t.Fail()
  125. }
  126. cert.NotAfter = time.Date(2016, time.April, 01, 0, 0, 0, 0, time.UTC)
  127. if MonthsValid(cert) != 12 {
  128. t.Fail()
  129. }
  130. // extra days should be rounded up to 1 month
  131. cert.NotAfter = time.Date(2016, time.April, 02, 0, 0, 0, 0, time.UTC)
  132. if MonthsValid(cert) != 13 {
  133. t.Fail()
  134. }
  135. }
  136. func TestHasValidExpiry(t *testing.T) {
  137. // Issue period > April 1, 2015
  138. var cert = &x509.Certificate{
  139. NotBefore: time.Date(2015, time.April, 01, 0, 0, 0, 0, time.UTC),
  140. NotAfter: time.Date(2016, time.April, 01, 0, 0, 0, 0, time.UTC),
  141. }
  142. if !ValidExpiry(cert) {
  143. t.Fail()
  144. }
  145. cert.NotAfter = time.Date(2019, time.April, 01, 01, 0, 0, 0, time.UTC)
  146. if ValidExpiry(cert) {
  147. t.Fail()
  148. }
  149. // Issue period < July 1, 2012
  150. cert.NotBefore = time.Date(2009, time.March, 01, 0, 0, 0, 0, time.UTC)
  151. if ValidExpiry(cert) {
  152. t.Fail()
  153. }
  154. // Issue period July 1, 2012 - April 1, 2015
  155. cert.NotBefore = time.Date(2012, time.July, 01, 0, 0, 0, 0, time.UTC)
  156. cert.NotAfter = time.Date(2017, time.July, 01, 0, 0, 0, 0, time.UTC)
  157. if !ValidExpiry(cert) {
  158. t.Fail()
  159. }
  160. }
  161. func TestHashAlgoString(t *testing.T) {
  162. if HashAlgoString(x509.MD2WithRSA) != "MD2" {
  163. t.Fatal("standin")
  164. }
  165. if HashAlgoString(x509.MD5WithRSA) != "MD5" {
  166. t.Fatal("standin")
  167. }
  168. if HashAlgoString(x509.SHA1WithRSA) != "SHA1" {
  169. t.Fatal("standin")
  170. }
  171. if HashAlgoString(x509.SHA256WithRSA) != "SHA256" {
  172. t.Fatal("standin")
  173. }
  174. if HashAlgoString(x509.SHA384WithRSA) != "SHA384" {
  175. t.Fatal("standin")
  176. }
  177. if HashAlgoString(x509.SHA512WithRSA) != "SHA512" {
  178. t.Fatal("standin")
  179. }
  180. if HashAlgoString(x509.DSAWithSHA1) != "SHA1" {
  181. t.Fatal("standin")
  182. }
  183. if HashAlgoString(x509.DSAWithSHA256) != "SHA256" {
  184. t.Fatal("standin")
  185. }
  186. if HashAlgoString(x509.ECDSAWithSHA1) != "SHA1" {
  187. t.Fatal("standin")
  188. }
  189. if HashAlgoString(x509.ECDSAWithSHA256) != "SHA256" {
  190. t.Fatal("standin")
  191. }
  192. if HashAlgoString(x509.ECDSAWithSHA384) != "SHA384" {
  193. t.Fatal("standin")
  194. }
  195. if HashAlgoString(x509.ECDSAWithSHA512) != "SHA512" {
  196. t.Fatal("standin")
  197. }
  198. if HashAlgoString(math.MaxInt32) != "Unknown Hash Algorithm" {
  199. t.Fatal("standin")
  200. }
  201. }
  202. func TestSignatureString(t *testing.T) {
  203. if SignatureString(x509.MD2WithRSA) != "MD2WithRSA" {
  204. t.Fatal("Signature String functioning improperly")
  205. }
  206. if SignatureString(x509.MD5WithRSA) != "MD5WithRSA" {
  207. t.Fatal("Signature String functioning improperly")
  208. }
  209. if SignatureString(x509.SHA1WithRSA) != "SHA1WithRSA" {
  210. t.Fatal("Signature String functioning improperly")
  211. }
  212. if SignatureString(x509.SHA256WithRSA) != "SHA256WithRSA" {
  213. t.Fatal("Signature String functioning improperly")
  214. }
  215. if SignatureString(x509.SHA384WithRSA) != "SHA384WithRSA" {
  216. t.Fatal("Signature String functioning improperly")
  217. }
  218. if SignatureString(x509.SHA512WithRSA) != "SHA512WithRSA" {
  219. t.Fatal("Signature String functioning improperly")
  220. }
  221. if SignatureString(x509.DSAWithSHA1) != "DSAWithSHA1" {
  222. t.Fatal("Signature String functioning improperly")
  223. }
  224. if SignatureString(x509.DSAWithSHA256) != "DSAWithSHA256" {
  225. t.Fatal("Signature String functioning improperly")
  226. }
  227. if SignatureString(x509.ECDSAWithSHA1) != "ECDSAWithSHA1" {
  228. t.Fatal("Signature String functioning improperly")
  229. }
  230. if SignatureString(x509.ECDSAWithSHA256) != "ECDSAWithSHA256" {
  231. t.Fatal("Signature String functioning improperly")
  232. }
  233. if SignatureString(x509.ECDSAWithSHA384) != "ECDSAWithSHA384" {
  234. t.Fatal("Signature String functioning improperly")
  235. }
  236. if SignatureString(x509.ECDSAWithSHA512) != "ECDSAWithSHA512" {
  237. t.Fatal("Signature String functioning improperly")
  238. }
  239. if SignatureString(math.MaxInt32) != "Unknown Signature" {
  240. t.Fatal("Signature String functioning improperly")
  241. }
  242. }
  243. func TestParseCertificatePEM(t *testing.T) {
  244. for _, testFile := range []string{testCertFile, testExtraWSCertFile, testSinglePKCS7} {
  245. certPEM, err := ioutil.ReadFile(testFile)
  246. if err != nil {
  247. t.Fatal(err)
  248. }
  249. if _, err := ParseCertificatePEM(certPEM); err != nil {
  250. t.Log(testFile)
  251. t.Fatal(err)
  252. }
  253. }
  254. for _, testFile := range []string{testBundleFile, testMessedUpCertFile, testEmptyPKCS7PEM, testEmptyCertFile, testMultiplePKCS7} {
  255. certPEM, err := ioutil.ReadFile(testFile)
  256. if err != nil {
  257. t.Fatal(err)
  258. }
  259. if _, err := ParseCertificatePEM(certPEM); err == nil {
  260. t.Fatal("Incorrect cert failed to raise error")
  261. }
  262. }
  263. }
  264. func TestParseCertificatesPEM(t *testing.T) {
  265. // expected cases
  266. for _, testFile := range []string{testBundleFile, testExtraWSBundleFile, testSinglePKCS7, testMultiplePKCS7} {
  267. bundlePEM, err := ioutil.ReadFile(testFile)
  268. if err != nil {
  269. t.Fatal(err)
  270. }
  271. if _, err := ParseCertificatesPEM(bundlePEM); err != nil {
  272. t.Log(testFile)
  273. t.Fatal(err)
  274. }
  275. }
  276. // test failure cases
  277. // few lines deleted, then headers removed
  278. for _, testFile := range []string{testMessedUpBundleFile, testEmptyPKCS7PEM, testNoHeaderCert} {
  279. bundlePEM, err := ioutil.ReadFile(testFile)
  280. if err != nil {
  281. t.Fatal(err)
  282. }
  283. if _, err := ParseCertificatesPEM(bundlePEM); err == nil {
  284. t.Fatal("Incorrectly-formatted file failed to produce an error")
  285. }
  286. }
  287. }
  288. func TestSelfSignedCertificatePEM(t *testing.T) {
  289. testPEM, _ := ioutil.ReadFile(testCertFile)
  290. _, err := ParseSelfSignedCertificatePEM(testPEM)
  291. if err != nil {
  292. t.Fatalf("%v", err)
  293. }
  294. // a few lines deleted from the pem file
  295. wrongPEM, _ := ioutil.ReadFile(testMessedUpCertFile)
  296. _, err2 := ParseSelfSignedCertificatePEM(wrongPEM)
  297. if err2 == nil {
  298. t.Fatal("Improper pem file failed to raise an error")
  299. }
  300. // alter the signature of a valid certificate
  301. blk, _ := pem.Decode(testPEM)
  302. blk.Bytes[len(blk.Bytes)-10]++ // some hacking to get to the sig
  303. alteredBytes := pem.EncodeToMemory(blk)
  304. _, err = ParseSelfSignedCertificatePEM(alteredBytes)
  305. if err == nil {
  306. t.Fatal("Incorrect cert failed to produce an error")
  307. }
  308. }
  309. func TestParsePrivateKeyPEM(t *testing.T) {
  310. // expected cases
  311. testRSAPEM, _ := ioutil.ReadFile(testPrivateRSAKey)
  312. _, err := ParsePrivateKeyPEM(testRSAPEM)
  313. if err != nil {
  314. t.Fatal(err)
  315. }
  316. testECDSAPEM, _ := ioutil.ReadFile(testPrivateECDSAKey)
  317. _, err = ParsePrivateKeyPEM(testECDSAPEM)
  318. if err != nil {
  319. t.Fatal(err)
  320. }
  321. // error cases
  322. errCases := []string{
  323. testMessedUpPrivateKey, // a few lines deleted
  324. testEmptyPem, // empty file
  325. testEncryptedPrivateKey, // encrypted key
  326. testUnsupportedECDSAKey, // ECDSA curve not currently supported by Go standard library
  327. }
  328. for _, fname := range errCases {
  329. testPEM, _ := ioutil.ReadFile(fname)
  330. _, err = ParsePrivateKeyPEM(testPEM)
  331. if err == nil {
  332. t.Fatal("Incorrect private key failed to produce an error")
  333. }
  334. }
  335. }
  336. // Imported from signers/local/testdata/
  337. const ecdsaTestCSR = "testdata/ecdsa256.csr"
  338. func TestParseCSRPEM(t *testing.T) {
  339. in, err := ioutil.ReadFile(ecdsaTestCSR)
  340. if err != nil {
  341. t.Fatalf("%v", err)
  342. }
  343. _, _, err = ParseCSR(in)
  344. if err != nil {
  345. t.Fatalf("%v", err)
  346. }
  347. in[12]++
  348. _, _, err = ParseCSR(in)
  349. if err == nil {
  350. t.Fatalf("Expected an invalid CSR.")
  351. }
  352. in[12]--
  353. }
  354. func TestParseCSRPEMMore(t *testing.T) {
  355. csrPEM, err := ioutil.ReadFile(testCSRPEM)
  356. if err != nil {
  357. t.Fatal(err)
  358. }
  359. if _, err := ParseCSRPEM(csrPEM); err != nil {
  360. t.Fatal(err)
  361. }
  362. csrPEM, err = ioutil.ReadFile(testCSRPEMBad)
  363. if err != nil {
  364. t.Fatal(err)
  365. }
  366. if _, err := ParseCSRPEM(csrPEM); err == nil {
  367. t.Fatal(err)
  368. }
  369. if _, err := ParseCSRPEM([]byte("not even pem")); err == nil {
  370. t.Fatal("Expected an invalid CSR.")
  371. }
  372. }
  373. // Imported from signers/local/testdata/
  374. const rsaOldTestCSR = "testdata/rsa-old.csr"
  375. func TestParseOldCSR(t *testing.T) {
  376. in, err := ioutil.ReadFile(rsaOldTestCSR)
  377. if err != nil {
  378. t.Fatalf("%v", err)
  379. }
  380. _, _, err = ParseCSR(in)
  381. if err != nil {
  382. t.Fatalf("%v", err)
  383. }
  384. }
  385. // Imported from signers/local/testdata/
  386. const clientCertFile = "testdata/ca.pem"
  387. const clientKeyFile = "testdata/ca_key.pem"
  388. func TestClientCertParams(t *testing.T) {
  389. _, err := LoadClientCertificate(testCertFile, testPrivateRSAKey)
  390. if err == nil {
  391. t.Fatal("Unmatched cert/key should generate error")
  392. }
  393. cert, err := LoadClientCertificate("", "")
  394. if err != nil || cert != nil {
  395. t.Fatal("Certificate atempted to loaded with missing key and cert")
  396. }
  397. cert, err = LoadClientCertificate(clientCertFile, "")
  398. if err != nil || cert != nil {
  399. t.Fatal("Certificate atempted to loaded with missing key")
  400. }
  401. cert, err = LoadClientCertificate("", clientKeyFile)
  402. if err != nil || cert != nil {
  403. t.Fatal("Certificate atempted to loaded with missing cert")
  404. }
  405. cert, err = LoadClientCertificate(clientCertFile, clientKeyFile)
  406. if err != nil {
  407. t.Fatal(err)
  408. }
  409. if cert == nil {
  410. t.Fatal("cert not created")
  411. }
  412. }
  413. func TestLoadPEMCertPool(t *testing.T) {
  414. certPool, err := PEMToCertPool([]byte{})
  415. if certPool != nil || err != nil {
  416. t.Fatal("Empty file name should not generate error or a cert pool")
  417. }
  418. in, err := ioutil.ReadFile(testEmptyPem)
  419. if err != nil {
  420. t.Fatalf("%v", err)
  421. }
  422. certPool, err = PEMToCertPool(in)
  423. if certPool != nil {
  424. t.Fatal("Empty file should not generate a cert pool")
  425. } else if err == nil {
  426. t.Fatal("Expected error for empty file")
  427. }
  428. in, err = ioutil.ReadFile(testEmptyCertFile)
  429. if err != nil {
  430. t.Fatalf("%v", err)
  431. }
  432. certPool, err = PEMToCertPool(in)
  433. if certPool != nil {
  434. t.Fatal("Empty cert should not generate a cert pool")
  435. } else if err == nil {
  436. t.Fatal("Expected error for empty cert")
  437. }
  438. in, err = ioutil.ReadFile(clientCertFile)
  439. if err != nil {
  440. t.Fatalf("%v", err)
  441. }
  442. certPool, err = PEMToCertPool(in)
  443. if err != nil {
  444. t.Fatalf("%v", err)
  445. } else if certPool == nil {
  446. t.Fatal("cert pool not created")
  447. }
  448. }
  449. // sctEquals returns true if all fields of both SCTs are equivalent.
  450. func sctEquals(sctA, sctB ct.SignedCertificateTimestamp) bool {
  451. if sctA.SCTVersion == sctB.SCTVersion &&
  452. sctA.LogID == sctB.LogID &&
  453. sctA.Timestamp == sctB.Timestamp &&
  454. bytes.Equal(sctA.Extensions, sctB.Extensions) &&
  455. sctA.Signature.Algorithm == sctB.Signature.Algorithm &&
  456. bytes.Equal(sctA.Signature.Signature, sctA.Signature.Signature) {
  457. return true
  458. }
  459. return false
  460. }
  461. // NOTE: TestDeserializeSCTList tests both DeserializeSCTList and
  462. // SerializeSCTList.
  463. func TestDeserializeSCTList(t *testing.T) {
  464. // Here we make sure that empty SCT lists return an error
  465. emptyLists := [][]byte{nil, {}}
  466. for _, emptyList := range emptyLists {
  467. _, err := DeserializeSCTList(emptyList)
  468. if err == nil {
  469. t.Fatalf("DeserializeSCTList(%v) should raise an error\n", emptyList)
  470. }
  471. }
  472. // Here we make sure that an SCT list with a zero SCT is deserialized
  473. // correctly
  474. var zeroSCT ct.SignedCertificateTimestamp
  475. serializedSCT, err := SerializeSCTList([]ct.SignedCertificateTimestamp{zeroSCT})
  476. if err != nil {
  477. t.Fatal(err)
  478. }
  479. deserializedSCTList, err := DeserializeSCTList(serializedSCT)
  480. if err != nil {
  481. t.Fatal(err)
  482. }
  483. if !sctEquals(zeroSCT, (*deserializedSCTList)[0]) {
  484. t.Fatal("SCTs don't match")
  485. }
  486. // Here we verify that an error is raised when the SCT list length
  487. // field is greater than its actual length
  488. serializedSCT, err = SerializeSCTList([]ct.SignedCertificateTimestamp{zeroSCT})
  489. if err != nil {
  490. t.Fatal(err)
  491. }
  492. serializedSCT[0] = 15
  493. _, err = DeserializeSCTList(serializedSCT)
  494. if err == nil {
  495. t.Fatalf("DeserializeSCTList should raise an error when " +
  496. "the SCT list length field and the list length don't match\n")
  497. }
  498. // Here we verify that an error is raised when the SCT list length
  499. // field is less than its actual length
  500. serializedSCT[0] = 0
  501. serializedSCT[1] = 0
  502. _, err = DeserializeSCTList(serializedSCT)
  503. if err == nil {
  504. t.Fatalf("DeserializeSCTList should raise an error when " +
  505. "the SCT list length field and the list length don't match\n")
  506. }
  507. // Here we verify that an error is raised when the SCT length field is
  508. // greater than its actual length
  509. serializedSCT[0] = 0
  510. serializedSCT[1] = 49
  511. serializedSCT[2] = 1
  512. _, err = DeserializeSCTList(serializedSCT)
  513. if err == nil {
  514. t.Fatalf("DeserializeSCTList should raise an error when " +
  515. "the SCT length field and the SCT length don't match\n")
  516. }
  517. // Here we verify that an error is raised when the SCT length field is
  518. // less than its actual length
  519. serializedSCT[2] = 0
  520. serializedSCT[3] = 0
  521. _, err = DeserializeSCTList(serializedSCT)
  522. if err == nil {
  523. t.Fatalf("DeserializeSCTList should raise an error when " +
  524. "the SCT length field and the SCT length don't match\n")
  525. }
  526. }
  527. func TestSCTListFromOCSPResponse(t *testing.T) {
  528. var response ocsp.Response
  529. lst, err := SCTListFromOCSPResponse(&response)
  530. if err != nil {
  531. t.Fatal(err)
  532. }
  533. if len(lst) != 0 {
  534. t.Fatal("SCTListFromOCSPResponse should return an empty SCT list for an empty extension")
  535. }
  536. var zeroSCT ct.SignedCertificateTimestamp
  537. serializedSCTList, err := SerializeSCTList([]ct.SignedCertificateTimestamp{zeroSCT})
  538. if err != nil {
  539. t.Fatal("failed to serialize SCT list")
  540. }
  541. serializedSCTList, err = asn1.Marshal(serializedSCTList)
  542. if err != nil {
  543. t.Fatal("failed to serialize SCT list")
  544. }
  545. // The value of Id below is the object identifier of the OCSP Stapling
  546. // SCT extension (see section 3.3. of RFC 6962).
  547. response.Extensions = []pkix.Extension{{
  548. Id: asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 11129, 2, 4, 5},
  549. Critical: false,
  550. Value: serializedSCTList,
  551. }}
  552. lst, err = SCTListFromOCSPResponse(&response)
  553. if err != nil {
  554. t.Fatal(err)
  555. }
  556. if !sctEquals(zeroSCT, lst[0]) {
  557. t.Fatal("SCTs don't match")
  558. }
  559. }