helpers_test.go 18 KB

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