selfsign_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package selfsign
  2. import (
  3. "crypto/x509"
  4. "encoding/pem"
  5. "encoding/asn1"
  6. "net"
  7. "net/url"
  8. "os"
  9. "reflect"
  10. "testing"
  11. "time"
  12. "github.com/cloudflare/cfssl/config"
  13. "github.com/cloudflare/cfssl/helpers"
  14. )
  15. const (
  16. keyFile = "testdata/localhost.key"
  17. csrFile = "testdata/localhost.csr"
  18. csr2File = "testdata/sans.csr"
  19. extCsrFile = "testdata/extension.csr"
  20. )
  21. func TestDefaultSign(t *testing.T) {
  22. csrBytes, err := os.ReadFile(csrFile)
  23. if err != nil {
  24. t.Fatal(err)
  25. }
  26. keyBytes, err := os.ReadFile(keyFile)
  27. if err != nil {
  28. t.Fatal(err)
  29. }
  30. priv, err := helpers.ParsePrivateKeyPEM(keyBytes)
  31. if err != nil {
  32. t.Fatal(err)
  33. }
  34. profile := config.DefaultConfig()
  35. profile.Expiry = 10 * time.Hour
  36. _, err = Sign(priv, csrBytes, profile)
  37. if err != nil {
  38. t.Fatal(err)
  39. }
  40. }
  41. func TestSANs(t *testing.T) {
  42. t.Skip("broken relating to https://github.com/cloudflare/cfssl/issues/1230")
  43. csrBytes, err := os.ReadFile(csr2File)
  44. if err != nil {
  45. t.Fatal(err)
  46. }
  47. keyBytes, err := os.ReadFile(keyFile)
  48. if err != nil {
  49. t.Fatal(err)
  50. }
  51. priv, err := helpers.ParsePrivateKeyPEM(keyBytes)
  52. if err != nil {
  53. t.Fatal(err)
  54. }
  55. profile := config.DefaultConfig()
  56. profile.Expiry = 10 * time.Hour
  57. certPEM, err := Sign(priv, csrBytes, profile)
  58. if err != nil {
  59. t.Fatal(err)
  60. }
  61. p, _ := pem.Decode(certPEM)
  62. if p == nil || p.Type != "CERTIFICATE" {
  63. // this seems unlikely
  64. t.Fatalf("failed creating certificate")
  65. }
  66. cert, err := x509.ParseCertificate(p.Bytes)
  67. if err != nil {
  68. t.Fatal(err)
  69. }
  70. expectedEmailAddresses := []string{"jdoe@example.com"}
  71. if !reflect.DeepEqual(cert.EmailAddresses, expectedEmailAddresses) {
  72. t.Errorf("cert should have contained EmailAddresses %#v but had %#v", expectedEmailAddresses, cert.EmailAddresses)
  73. }
  74. expectedDNSNames := []string{"cloudflare.com", "www.cloudflare.com"}
  75. if !reflect.DeepEqual(cert.DNSNames, expectedDNSNames) {
  76. t.Errorf("cert should have contained DNSNames %#v but had %#v", expectedDNSNames, cert.DNSNames)
  77. }
  78. expectedIPAddresses := []net.IP{{0xc0, 0xa8, 0x0, 0x1}}
  79. if !reflect.DeepEqual(cert.IPAddresses, expectedIPAddresses) {
  80. t.Errorf("cert should have contained IPAddresses %#v but had %#v", expectedIPAddresses, cert.IPAddresses)
  81. }
  82. expectedURIs := []*url.URL{{Scheme: "https", Host: "www.cloudflare.com"}}
  83. if !reflect.DeepEqual(cert.URIs, expectedURIs) {
  84. t.Errorf("cert should have contained URIs %#v but had %#v", expectedURIs, cert.URIs)
  85. }
  86. }
  87. func TestExtensions(t *testing.T){
  88. csrBytes, err := os.ReadFile(extCsrFile)
  89. if err != nil {
  90. t.Fatal(err)
  91. }
  92. keyBytes, err := os.ReadFile(keyFile)
  93. if err != nil {
  94. t.Fatal(err)
  95. }
  96. priv, err := helpers.ParsePrivateKeyPEM(keyBytes)
  97. if err != nil {
  98. t.Fatal(err)
  99. }
  100. profile := config.DefaultConfig()
  101. profile.Expiry = 10 * time.Hour
  102. certData, err := Sign(priv, csrBytes, profile)
  103. if err != nil {
  104. t.Fatal(err)
  105. }
  106. cert, err := helpers.ParseCertificatePEM(certData)
  107. if err != nil {
  108. t.Fatal(err)
  109. }
  110. // Testing for 1.3.6.1.4.1.311.84.1.1=ASN1:UTF8String:example1
  111. extFound := false
  112. sampleCustomOid := asn1.ObjectIdentifier{1,3,6,1,4,1,311,84,1,1}
  113. sampleValue := "example1"
  114. for _, e := range cert.Extensions {
  115. if(e.Id.Equal(sampleCustomOid) ){
  116. var extValue string
  117. _, err = asn1.Unmarshal(e.Value, &extValue)
  118. if err != nil {
  119. t.Fatal(err)
  120. }
  121. if(extValue == sampleValue){
  122. extFound = true
  123. }
  124. }
  125. }
  126. if !extFound {
  127. t.Errorf("Custom x509 extension not found in certificate.")
  128. }
  129. }