policies.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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/x509"
  16. "fmt"
  17. "sort"
  18. "time"
  19. capi "k8s.io/api/certificates/v1beta1"
  20. )
  21. // SigningPolicy validates a CertificateRequest before it's signed by the
  22. // CertificateAuthority. It may default or otherwise mutate a certificate
  23. // template.
  24. type SigningPolicy interface {
  25. // not-exporting apply forces signing policy implementations to be internal
  26. // to this package.
  27. apply(template *x509.Certificate) error
  28. }
  29. // PermissiveSigningPolicy is the signing policy historically used by the local
  30. // signer.
  31. //
  32. // * It forwards all SANs from the original signing request.
  33. // * It sets allowed usages as configured in the policy.
  34. // * It sets NotAfter based on the TTL configured in the policy.
  35. // * It zeros all extensions.
  36. // * It sets BasicConstraints to true.
  37. // * It sets IsCA to false.
  38. type PermissiveSigningPolicy struct {
  39. // TTL is the certificate TTL. It's used to calculate the NotAfter value of
  40. // the certificate.
  41. TTL time.Duration
  42. // Usages are the allowed usages of a certificate.
  43. Usages []capi.KeyUsage
  44. }
  45. func (p PermissiveSigningPolicy) apply(tmpl *x509.Certificate) error {
  46. usage, extUsages, err := keyUsagesFromStrings(p.Usages)
  47. if err != nil {
  48. return err
  49. }
  50. tmpl.KeyUsage = usage
  51. tmpl.ExtKeyUsage = extUsages
  52. tmpl.NotAfter = tmpl.NotBefore.Add(p.TTL)
  53. tmpl.ExtraExtensions = nil
  54. tmpl.Extensions = nil
  55. tmpl.BasicConstraintsValid = true
  56. tmpl.IsCA = false
  57. return nil
  58. }
  59. var keyUsageDict = map[capi.KeyUsage]x509.KeyUsage{
  60. capi.UsageSigning: x509.KeyUsageDigitalSignature,
  61. capi.UsageDigitalSignature: x509.KeyUsageDigitalSignature,
  62. capi.UsageContentCommitment: x509.KeyUsageContentCommitment,
  63. capi.UsageKeyEncipherment: x509.KeyUsageKeyEncipherment,
  64. capi.UsageKeyAgreement: x509.KeyUsageKeyAgreement,
  65. capi.UsageDataEncipherment: x509.KeyUsageDataEncipherment,
  66. capi.UsageCertSign: x509.KeyUsageCertSign,
  67. capi.UsageCRLSign: x509.KeyUsageCRLSign,
  68. capi.UsageEncipherOnly: x509.KeyUsageEncipherOnly,
  69. capi.UsageDecipherOnly: x509.KeyUsageDecipherOnly,
  70. }
  71. var extKeyUsageDict = map[capi.KeyUsage]x509.ExtKeyUsage{
  72. capi.UsageAny: x509.ExtKeyUsageAny,
  73. capi.UsageServerAuth: x509.ExtKeyUsageServerAuth,
  74. capi.UsageClientAuth: x509.ExtKeyUsageClientAuth,
  75. capi.UsageCodeSigning: x509.ExtKeyUsageCodeSigning,
  76. capi.UsageEmailProtection: x509.ExtKeyUsageEmailProtection,
  77. capi.UsageSMIME: x509.ExtKeyUsageEmailProtection,
  78. capi.UsageIPsecEndSystem: x509.ExtKeyUsageIPSECEndSystem,
  79. capi.UsageIPsecTunnel: x509.ExtKeyUsageIPSECTunnel,
  80. capi.UsageIPsecUser: x509.ExtKeyUsageIPSECUser,
  81. capi.UsageTimestamping: x509.ExtKeyUsageTimeStamping,
  82. capi.UsageOCSPSigning: x509.ExtKeyUsageOCSPSigning,
  83. capi.UsageMicrosoftSGC: x509.ExtKeyUsageMicrosoftServerGatedCrypto,
  84. capi.UsageNetscapeSGC: x509.ExtKeyUsageNetscapeServerGatedCrypto,
  85. }
  86. // keyUsagesFromStrings will translate a slice of usage strings from the
  87. // certificates API ("pkg/apis/certificates".KeyUsage) to x509.KeyUsage and
  88. // x509.ExtKeyUsage types.
  89. func keyUsagesFromStrings(usages []capi.KeyUsage) (x509.KeyUsage, []x509.ExtKeyUsage, error) {
  90. var keyUsage x509.KeyUsage
  91. var unrecognized []capi.KeyUsage
  92. extKeyUsages := make(map[x509.ExtKeyUsage]struct{})
  93. for _, usage := range usages {
  94. if val, ok := keyUsageDict[usage]; ok {
  95. keyUsage |= val
  96. } else if val, ok := extKeyUsageDict[usage]; ok {
  97. extKeyUsages[val] = struct{}{}
  98. } else {
  99. unrecognized = append(unrecognized, usage)
  100. }
  101. }
  102. var sorted sortedExtKeyUsage
  103. for eku := range extKeyUsages {
  104. sorted = append(sorted, eku)
  105. }
  106. sort.Sort(sorted)
  107. if len(unrecognized) > 0 {
  108. return 0, nil, fmt.Errorf("unrecognized usage values: %q", unrecognized)
  109. }
  110. return keyUsage, []x509.ExtKeyUsage(sorted), nil
  111. }
  112. type sortedExtKeyUsage []x509.ExtKeyUsage
  113. func (s sortedExtKeyUsage) Len() int {
  114. return len(s)
  115. }
  116. func (s sortedExtKeyUsage) Swap(i, j int) {
  117. s[i], s[j] = s[j], s[i]
  118. }
  119. func (s sortedExtKeyUsage) Less(i, j int) bool {
  120. return s[i] < s[j]
  121. }