csr.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. Copyright 2016 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 certificates
  14. import (
  15. "crypto/ecdsa"
  16. "crypto/elliptic"
  17. cryptorand "crypto/rand"
  18. "crypto/rsa"
  19. "crypto/x509"
  20. "crypto/x509/pkix"
  21. "encoding/pem"
  22. "errors"
  23. "fmt"
  24. "net"
  25. "k8s.io/kubernetes/pkg/apis/certificates"
  26. )
  27. // ParseCertificateRequestObject extracts the CSR from the API object and decodes it.
  28. func ParseCertificateRequestObject(obj *certificates.CertificateSigningRequest) (*x509.CertificateRequest, error) {
  29. // extract PEM from request object
  30. pemBytes := obj.Spec.Request
  31. block, _ := pem.Decode(pemBytes)
  32. if block == nil || block.Type != "CERTIFICATE REQUEST" {
  33. return nil, errors.New("PEM block type must be CERTIFICATE REQUEST")
  34. }
  35. csr, err := x509.ParseCertificateRequest(block.Bytes)
  36. if err != nil {
  37. return nil, err
  38. }
  39. return csr, nil
  40. }
  41. // GeneratePrivateKey returns PEM data containing a generated ECDSA private key
  42. func GeneratePrivateKey() ([]byte, error) {
  43. privateKey, err := ecdsa.GenerateKey(elliptic.P256(), cryptorand.Reader)
  44. if err != nil {
  45. return nil, err
  46. }
  47. derBytes, err := x509.MarshalECPrivateKey(privateKey)
  48. if err != nil {
  49. return nil, err
  50. }
  51. privateKeyPemBlock := &pem.Block{
  52. Type: "EC PRIVATE KEY",
  53. Bytes: derBytes,
  54. }
  55. return pem.EncodeToMemory(privateKeyPemBlock), nil
  56. }
  57. // ParsePrivateKey returns a private key parsed from a PEM block in the supplied data.
  58. // Recognizes PEM blocks for "EC PRIVATE KEY" and "RSA PRIVATE KEY"
  59. func ParsePrivateKey(keyData []byte) (interface{}, error) {
  60. for {
  61. var privateKeyPemBlock *pem.Block
  62. privateKeyPemBlock, keyData = pem.Decode(keyData)
  63. if privateKeyPemBlock == nil {
  64. // we read all the PEM blocks and didn't recognize one
  65. return nil, fmt.Errorf("no private key PEM block found")
  66. }
  67. switch privateKeyPemBlock.Type {
  68. case "EC PRIVATE KEY":
  69. return x509.ParseECPrivateKey(privateKeyPemBlock.Bytes)
  70. case "RSA PRIVATE KEY":
  71. return x509.ParsePKCS1PrivateKey(privateKeyPemBlock.Bytes)
  72. }
  73. }
  74. }
  75. // NewCertificateRequest generates a PEM-encoded CSR using the supplied private key, subject, and SANs.
  76. // privateKey must be a *ecdsa.PrivateKey or *rsa.PrivateKey.
  77. func NewCertificateRequest(privateKey interface{}, subject *pkix.Name, dnsSANs []string, ipSANs []net.IP) (csr []byte, err error) {
  78. var sigType x509.SignatureAlgorithm
  79. switch privateKey := privateKey.(type) {
  80. case *ecdsa.PrivateKey:
  81. switch privateKey.Curve {
  82. case elliptic.P224(), elliptic.P256():
  83. sigType = x509.ECDSAWithSHA256
  84. case elliptic.P384():
  85. sigType = x509.ECDSAWithSHA384
  86. case elliptic.P521():
  87. sigType = x509.ECDSAWithSHA512
  88. default:
  89. return nil, fmt.Errorf("unknown elliptic curve: %v", privateKey.Curve)
  90. }
  91. case *rsa.PrivateKey:
  92. keySize := privateKey.N.BitLen()
  93. switch {
  94. case keySize >= 4096:
  95. sigType = x509.SHA512WithRSA
  96. case keySize >= 3072:
  97. sigType = x509.SHA384WithRSA
  98. default:
  99. sigType = x509.SHA256WithRSA
  100. }
  101. default:
  102. return nil, fmt.Errorf("unsupported key type: %T", privateKey)
  103. }
  104. template := &x509.CertificateRequest{
  105. Subject: *subject,
  106. SignatureAlgorithm: sigType,
  107. DNSNames: dnsSANs,
  108. IPAddresses: ipSANs,
  109. }
  110. csr, err = x509.CreateCertificateRequest(cryptorand.Reader, template, privateKey)
  111. if err != nil {
  112. return nil, err
  113. }
  114. csrPemBlock := &pem.Block{
  115. Type: "CERTIFICATE REQUEST",
  116. Bytes: csr,
  117. }
  118. return pem.EncodeToMemory(csrPemBlock), nil
  119. }