ocsp_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. package ocsp
  2. import (
  3. "io/ioutil"
  4. "testing"
  5. "time"
  6. "golang.org/x/crypto/ocsp"
  7. "github.com/cloudflare/cfssl/helpers"
  8. )
  9. const (
  10. serverCertFile = "testdata/ca.pem"
  11. serverKeyFile = "testdata/ca-key.pem"
  12. otherCertFile = "testdata/cert.pem"
  13. brokenServerCert = "testdata/server_broken.crt"
  14. brokenServerKey = "testdata/server_broken.key"
  15. wrongServerCertFile = "testdata/server.crt"
  16. wrongServerKeyFile = "testdata/server.key"
  17. )
  18. func TestNewSignerFromFile(t *testing.T) {
  19. // arbitrary duration
  20. dur, _ := time.ParseDuration("1ms")
  21. // nonexistent files
  22. _, err := NewSignerFromFile("", "", "", dur)
  23. if err == nil {
  24. t.Fatal("Failed to issue error on improper file")
  25. }
  26. _, err = NewSignerFromFile(serverCertFile, "", "", dur)
  27. if err == nil {
  28. t.Fatal("Failed to issue error on improper file")
  29. }
  30. _, err = NewSignerFromFile(serverCertFile, otherCertFile, "", dur)
  31. if err == nil {
  32. t.Fatal("Failed to issue error on improper file")
  33. }
  34. // malformed certs
  35. _, err = NewSignerFromFile(brokenServerCert, otherCertFile, serverKeyFile, dur)
  36. if err == nil {
  37. t.Fatal("Didn't fail on malformed file")
  38. }
  39. _, err = NewSignerFromFile(serverCertFile, brokenServerCert, serverKeyFile, dur)
  40. if err == nil {
  41. t.Fatal("Didn't fail on malformed file")
  42. }
  43. _, err = NewSignerFromFile(serverCertFile, otherCertFile, brokenServerKey, dur)
  44. if err == nil {
  45. t.Fatal("Didn't fail on malformed file")
  46. }
  47. // expected case
  48. _, err = NewSignerFromFile(serverCertFile, otherCertFile, serverKeyFile, dur)
  49. if err != nil {
  50. t.Fatalf("Signer creation failed %v", err)
  51. }
  52. }
  53. func setup(t *testing.T) (SignRequest, time.Duration) {
  54. dur, _ := time.ParseDuration("1ms")
  55. certPEM, err := ioutil.ReadFile(otherCertFile)
  56. if err != nil {
  57. t.Fatal(err)
  58. }
  59. leafCert, err := helpers.ParseCertificatePEM(certPEM)
  60. if err != nil {
  61. t.Fatal(err)
  62. }
  63. req := SignRequest{
  64. Certificate: leafCert,
  65. Status: "good",
  66. }
  67. return req, dur
  68. }
  69. func TestSignNoResponder(t *testing.T) {
  70. req, dur := setup(t)
  71. s, err := NewSignerFromFile(serverCertFile, serverCertFile, serverKeyFile, dur)
  72. if err != nil {
  73. t.Fatalf("Signer creation failed: %v", err)
  74. }
  75. respBytes, err := s.Sign(req)
  76. if err != nil {
  77. t.Fatal("Failed to sign with no responder cert")
  78. }
  79. resp, err := ocsp.ParseResponse(respBytes, nil)
  80. if err != nil {
  81. t.Fatal("Failed to fail on improper status code")
  82. }
  83. if resp.Certificate != nil {
  84. t.Fatal("Response contain responder cert even though it was identical to issuer")
  85. }
  86. }
  87. func TestSign(t *testing.T) {
  88. req, dur := setup(t)
  89. // expected case
  90. s, err := NewSignerFromFile(serverCertFile, otherCertFile, serverKeyFile, dur)
  91. if err != nil {
  92. t.Fatalf("Signer creation failed: %v", err)
  93. }
  94. _, err = s.Sign(SignRequest{})
  95. if err == nil {
  96. t.Fatal("Signed request with nil certificate")
  97. }
  98. _, err = s.Sign(req)
  99. if err != nil {
  100. t.Fatal("Sign failed")
  101. }
  102. sMismatch, err := NewSignerFromFile(wrongServerCertFile, otherCertFile, wrongServerKeyFile, dur)
  103. if err != nil {
  104. t.Fatal("NewSigner failed:", err)
  105. }
  106. _, err = sMismatch.Sign(req)
  107. if err == nil {
  108. t.Fatal("Signed a certificate from the wrong issuer")
  109. }
  110. // incorrect status code
  111. req.Status = "aalkjsfdlkafdslkjahds"
  112. _, err = s.Sign(req)
  113. if err == nil {
  114. t.Fatal("Failed to fail on improper status code")
  115. }
  116. // revoked
  117. req.Status = "revoked"
  118. _, err = s.Sign(req)
  119. if err != nil {
  120. t.Fatal("Error on revoked certificate")
  121. }
  122. }
  123. func TestSignCustomInterval(t *testing.T) {
  124. req, _ := setup(t)
  125. dur := time.Hour
  126. s, err := NewSignerFromFile(serverCertFile, serverCertFile, serverKeyFile, dur)
  127. if err != nil {
  128. t.Fatalf("Signer creation failed: %v", err)
  129. }
  130. // default case
  131. n := time.Now().UTC().Truncate(time.Hour)
  132. respBytes, err := s.Sign(req)
  133. if err != nil {
  134. t.Fatalf("Error signing default request: %s", err)
  135. }
  136. resp, err := ocsp.ParseResponse(respBytes, nil)
  137. if err != nil {
  138. t.Fatalf("Error parsing response: %s", err)
  139. }
  140. if !resp.ThisUpdate.Equal(n) {
  141. t.Fatalf("Unexpected ThisUpdate: wanted %s, got %s", n, resp.ThisUpdate)
  142. }
  143. if !resp.NextUpdate.Equal(n.Add(dur)) {
  144. t.Fatalf("Unexpected NextUpdate: wanted %s, got %s", n.Add(dur), resp.NextUpdate)
  145. }
  146. // custom case, ThisUpdate only
  147. this := time.Now().UTC().Add(-time.Hour * 5).Truncate(time.Hour)
  148. req.ThisUpdate = &this
  149. respBytes, err = s.Sign(req)
  150. if err != nil {
  151. t.Fatalf("Error signing default request: %s", err)
  152. }
  153. resp, err = ocsp.ParseResponse(respBytes, nil)
  154. if err != nil {
  155. t.Fatalf("Error parsing response: %s", err)
  156. }
  157. if !resp.ThisUpdate.Equal(this) {
  158. t.Fatalf("Unexpected ThisUpdate: wanted %s, got %s", this, resp.ThisUpdate)
  159. }
  160. if !resp.NextUpdate.Equal(this.Add(dur)) {
  161. t.Fatalf("Unexpected NextUpdate: wanted %s, got %s", this.Add(dur), resp.NextUpdate)
  162. }
  163. // custom case, ThisUpdate and NextUpdate
  164. next := this.Add(time.Hour * 2)
  165. req.NextUpdate = &next
  166. respBytes, err = s.Sign(req)
  167. if err != nil {
  168. t.Fatalf("Error signing default request: %s", err)
  169. }
  170. resp, err = ocsp.ParseResponse(respBytes, nil)
  171. if err != nil {
  172. t.Fatalf("Error parsing response: %s", err)
  173. }
  174. if !resp.ThisUpdate.Equal(this) {
  175. t.Fatalf("Unexpected ThisUpdate: wanted %s, got %s", this, resp.ThisUpdate)
  176. }
  177. if !resp.NextUpdate.Equal(next) {
  178. t.Fatalf("Unexpected NextUpdate: wanted %s, got %s", next, resp.NextUpdate)
  179. }
  180. }