selfsign.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Package selfsign implements certificate selfsigning. This is very
  2. // dangerous and should never be used in production.
  3. package selfsign
  4. import (
  5. "crypto"
  6. "crypto/rand"
  7. "crypto/sha1"
  8. "crypto/x509"
  9. "crypto/x509/pkix"
  10. "encoding/asn1"
  11. "encoding/pem"
  12. "errors"
  13. "math"
  14. "math/big"
  15. "time"
  16. "github.com/cloudflare/cfssl/config"
  17. cferr "github.com/cloudflare/cfssl/errors"
  18. "github.com/cloudflare/cfssl/signer"
  19. )
  20. const threeMonths = 2190 * time.Hour
  21. // parseCertificateRequest takes an incoming certificate request and
  22. // builds a certificate template from it.
  23. func parseCertificateRequest(priv crypto.Signer, csrBytes []byte) (template *x509.Certificate, err error) {
  24. csr, err := x509.ParseCertificateRequest(csrBytes)
  25. if err != nil {
  26. err = cferr.Wrap(cferr.CSRError, cferr.ParseFailed, err)
  27. return
  28. }
  29. if err = csr.CheckSignature(); err != nil {
  30. err = cferr.Wrap(cferr.CSRError, cferr.KeyMismatch, err)
  31. return
  32. }
  33. template = &x509.Certificate{
  34. Subject: csr.Subject,
  35. PublicKeyAlgorithm: csr.PublicKeyAlgorithm,
  36. PublicKey: csr.PublicKey,
  37. SignatureAlgorithm: signer.DefaultSigAlgo(priv),
  38. DNSNames: csr.DNSNames,
  39. EmailAddresses: csr.EmailAddresses,
  40. IPAddresses: csr.IPAddresses,
  41. URIs: csr.URIs,
  42. }
  43. return
  44. }
  45. type subjectPublicKeyInfo struct {
  46. Algorithm pkix.AlgorithmIdentifier
  47. SubjectPublicKey asn1.BitString
  48. }
  49. // Sign creates a new self-signed certificate.
  50. func Sign(priv crypto.Signer, csrPEM []byte, profile *config.SigningProfile) ([]byte, error) {
  51. if profile == nil {
  52. return nil, cferr.Wrap(cferr.PolicyError, cferr.InvalidPolicy, errors.New("no profile for self-signing"))
  53. }
  54. p, _ := pem.Decode(csrPEM)
  55. if p == nil || p.Type != "CERTIFICATE REQUEST" {
  56. return nil, cferr.New(cferr.CSRError, cferr.BadRequest)
  57. }
  58. template, err := parseCertificateRequest(priv, p.Bytes)
  59. if err != nil {
  60. return nil, err
  61. }
  62. pub := template.PublicKey
  63. encodedpub, err := x509.MarshalPKIXPublicKey(pub)
  64. if err != nil {
  65. return nil, err
  66. }
  67. var subPKI subjectPublicKeyInfo
  68. _, err = asn1.Unmarshal(encodedpub, &subPKI)
  69. if err != nil {
  70. return nil, err
  71. }
  72. pubhash := sha1.New()
  73. pubhash.Write(subPKI.SubjectPublicKey.Bytes)
  74. var (
  75. eku []x509.ExtKeyUsage
  76. ku x509.KeyUsage
  77. expiry time.Duration
  78. crlURL, ocspURL string
  79. )
  80. // The third value returned from Usages is a list of unknown key usages.
  81. // This should be used when validating the profile at load, and isn't used
  82. // here.
  83. ku, eku, _ = profile.Usages()
  84. expiry = profile.Expiry
  85. if ku == 0 && len(eku) == 0 {
  86. err = cferr.New(cferr.PolicyError, cferr.NoKeyUsages)
  87. return nil, err
  88. }
  89. if expiry == 0 {
  90. expiry = threeMonths
  91. }
  92. now := time.Now()
  93. serialNumber, err := rand.Int(rand.Reader, new(big.Int).SetInt64(math.MaxInt64))
  94. if err != nil {
  95. err = cferr.Wrap(cferr.CSRError, cferr.Unknown, err)
  96. return nil, err
  97. }
  98. template.SerialNumber = serialNumber
  99. template.NotBefore = now.Add(-5 * time.Minute).UTC()
  100. template.NotAfter = now.Add(expiry).UTC()
  101. template.KeyUsage = ku
  102. template.ExtKeyUsage = eku
  103. template.BasicConstraintsValid = true
  104. template.IsCA = profile.CAConstraint.IsCA
  105. template.SubjectKeyId = pubhash.Sum(nil)
  106. if ocspURL != "" {
  107. template.OCSPServer = []string{ocspURL}
  108. }
  109. if crlURL != "" {
  110. template.CRLDistributionPoints = []string{crlURL}
  111. }
  112. if len(profile.IssuerURL) != 0 {
  113. template.IssuingCertificateURL = profile.IssuerURL
  114. }
  115. cert, err := x509.CreateCertificate(rand.Reader, template, template, pub, priv)
  116. if err != nil {
  117. return nil, err
  118. }
  119. cert = pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: cert})
  120. return cert, nil
  121. }