helpers_test.go 18 KB

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