authority.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. Copyright 2019 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package authority
  14. import (
  15. "crypto"
  16. "crypto/rand"
  17. "crypto/x509"
  18. "fmt"
  19. "math/big"
  20. "time"
  21. )
  22. var serialNumberLimit = new(big.Int).Lsh(big.NewInt(1), 128)
  23. // CertificateAuthority implements a certificate authority that supports policy
  24. // based signing. It's used by the signing controller.
  25. type CertificateAuthority struct {
  26. // RawCert is an optional field to determine if signing cert/key pairs have changed
  27. RawCert []byte
  28. // RawKey is an optional field to determine if signing cert/key pairs have changed
  29. RawKey []byte
  30. Certificate *x509.Certificate
  31. PrivateKey crypto.Signer
  32. Backdate time.Duration
  33. Now func() time.Time
  34. }
  35. // Sign signs a certificate request, applying a SigningPolicy and returns a DER
  36. // encoded x509 certificate.
  37. func (ca *CertificateAuthority) Sign(crDER []byte, policy SigningPolicy) ([]byte, error) {
  38. now := time.Now()
  39. if ca.Now != nil {
  40. now = ca.Now()
  41. }
  42. nbf := now.Add(-ca.Backdate)
  43. if !nbf.Before(ca.Certificate.NotAfter) {
  44. return nil, fmt.Errorf("the signer has expired: NotAfter=%v", ca.Certificate.NotAfter)
  45. }
  46. cr, err := x509.ParseCertificateRequest(crDER)
  47. if err != nil {
  48. return nil, fmt.Errorf("unable to parse certificate request: %v", err)
  49. }
  50. if err := cr.CheckSignature(); err != nil {
  51. return nil, fmt.Errorf("unable to verify certificate request signature: %v", err)
  52. }
  53. serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
  54. if err != nil {
  55. return nil, fmt.Errorf("unable to generate a serial number for %s: %v", cr.Subject.CommonName, err)
  56. }
  57. tmpl := &x509.Certificate{
  58. SerialNumber: serialNumber,
  59. Subject: cr.Subject,
  60. DNSNames: cr.DNSNames,
  61. IPAddresses: cr.IPAddresses,
  62. EmailAddresses: cr.EmailAddresses,
  63. URIs: cr.URIs,
  64. PublicKeyAlgorithm: cr.PublicKeyAlgorithm,
  65. PublicKey: cr.PublicKey,
  66. Extensions: cr.Extensions,
  67. ExtraExtensions: cr.ExtraExtensions,
  68. NotBefore: nbf,
  69. }
  70. if err := policy.apply(tmpl); err != nil {
  71. return nil, err
  72. }
  73. if !tmpl.NotAfter.Before(ca.Certificate.NotAfter) {
  74. tmpl.NotAfter = ca.Certificate.NotAfter
  75. }
  76. if !now.Before(ca.Certificate.NotAfter) {
  77. return nil, fmt.Errorf("refusing to sign a certificate that expired in the past")
  78. }
  79. der, err := x509.CreateCertificate(rand.Reader, tmpl, ca.Certificate, cr.PublicKey, ca.PrivateKey)
  80. if err != nil {
  81. return nil, fmt.Errorf("failed to sign certificate: %v", err)
  82. }
  83. return der, nil
  84. }