selfsign.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. csr.CheckSignature()
  30. if err != nil {
  31. err = cferr.Wrap(cferr.CSRError, cferr.KeyMismatch, err)
  32. return
  33. }
  34. template = &x509.Certificate{
  35. Subject: csr.Subject,
  36. PublicKeyAlgorithm: csr.PublicKeyAlgorithm,
  37. PublicKey: csr.PublicKey,
  38. SignatureAlgorithm: signer.DefaultSigAlgo(priv),
  39. }
  40. return
  41. }
  42. type subjectPublicKeyInfo struct {
  43. Algorithm pkix.AlgorithmIdentifier
  44. SubjectPublicKey asn1.BitString
  45. }
  46. // Sign creates a new self-signed certificate.
  47. func Sign(priv crypto.Signer, csrPEM []byte, profile *config.SigningProfile) ([]byte, error) {
  48. if profile == nil {
  49. return nil, cferr.Wrap(cferr.PolicyError, cferr.InvalidPolicy, errors.New("no profile for self-signing"))
  50. }
  51. p, _ := pem.Decode(csrPEM)
  52. if p == nil || p.Type != "CERTIFICATE REQUEST" {
  53. return nil, cferr.New(cferr.CSRError, cferr.BadRequest)
  54. }
  55. template, err := parseCertificateRequest(priv, p.Bytes)
  56. if err != nil {
  57. return nil, err
  58. }
  59. pub := template.PublicKey
  60. encodedpub, err := x509.MarshalPKIXPublicKey(pub)
  61. if err != nil {
  62. return nil, err
  63. }
  64. var subPKI subjectPublicKeyInfo
  65. _, err = asn1.Unmarshal(encodedpub, &subPKI)
  66. if err != nil {
  67. return nil, err
  68. }
  69. pubhash := sha1.New()
  70. pubhash.Write(subPKI.SubjectPublicKey.Bytes)
  71. var (
  72. eku []x509.ExtKeyUsage
  73. ku x509.KeyUsage
  74. expiry time.Duration
  75. crlURL, ocspURL string
  76. )
  77. // The third value returned from Usages is a list of unknown key usages.
  78. // This should be used when validating the profile at load, and isn't used
  79. // here.
  80. ku, eku, _ = profile.Usages()
  81. expiry = profile.Expiry
  82. if ku == 0 && len(eku) == 0 {
  83. err = cferr.New(cferr.PolicyError, cferr.NoKeyUsages)
  84. return nil, err
  85. }
  86. if expiry == 0 {
  87. expiry = threeMonths
  88. }
  89. now := time.Now()
  90. serialNumber, err := rand.Int(rand.Reader, new(big.Int).SetInt64(math.MaxInt64))
  91. if err != nil {
  92. err = cferr.Wrap(cferr.CSRError, cferr.Unknown, err)
  93. return nil, err
  94. }
  95. template.SerialNumber = serialNumber
  96. template.NotBefore = now.Add(-5 * time.Minute).UTC()
  97. template.NotAfter = now.Add(expiry).UTC()
  98. template.KeyUsage = ku
  99. template.ExtKeyUsage = eku
  100. template.BasicConstraintsValid = true
  101. template.IsCA = profile.CAConstraint.IsCA
  102. template.SubjectKeyId = pubhash.Sum(nil)
  103. if ocspURL != "" {
  104. template.OCSPServer = []string{ocspURL}
  105. }
  106. if crlURL != "" {
  107. template.CRLDistributionPoints = []string{crlURL}
  108. }
  109. if len(profile.IssuerURL) != 0 {
  110. template.IssuingCertificateURL = profile.IssuerURL
  111. }
  112. cert, err := x509.CreateCertificate(rand.Reader, template, template, pub, priv)
  113. if err != nil {
  114. return nil, err
  115. }
  116. cert = pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: cert})
  117. return cert, nil
  118. }