selfsign.go 3.6 KB

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