pem.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. Copyright © 2025 ESO Maintainer Team
  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. https://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 template
  14. import (
  15. "bytes"
  16. "crypto/x509"
  17. "encoding/pem"
  18. "errors"
  19. "strings"
  20. )
  21. const (
  22. errJunk = "error filtering pem: found junk"
  23. certTypeLeaf = "leaf"
  24. certTypeIntermediate = "intermediate"
  25. certTypeRoot = "root"
  26. )
  27. func filterPEM(pemType, input string) (string, error) {
  28. input = trimJunk(input)
  29. data := []byte(input)
  30. var blocks []byte
  31. var block *pem.Block
  32. var rest []byte
  33. for {
  34. block, rest = pem.Decode(data)
  35. data = rest
  36. if block == nil {
  37. break
  38. }
  39. if !strings.EqualFold(block.Type, pemType) {
  40. continue
  41. }
  42. var buf bytes.Buffer
  43. err := pem.Encode(&buf, block)
  44. if err != nil {
  45. return "", err
  46. }
  47. blocks = append(blocks, buf.Bytes()...)
  48. }
  49. if len(blocks) == 0 && len(rest) != 0 {
  50. return "", errors.New(errJunk)
  51. }
  52. return string(blocks), nil
  53. }
  54. // trimJunk performs the same operation pem.Decode did until:
  55. // https://github.com/golang/go/issues/76124
  56. func trimJunk(input string) string {
  57. index := strings.Index(input, "-----BEGIN")
  58. if index == -1 {
  59. return input
  60. }
  61. return input[index:]
  62. }
  63. func filterCertChain(certType, input string) (string, error) {
  64. ordered, err := fetchX509CertChains([]byte(input))
  65. if err != nil {
  66. return "", err
  67. }
  68. switch certType {
  69. case certTypeLeaf:
  70. cert := ordered[0]
  71. if cert.AuthorityKeyId != nil && !bytes.Equal(cert.AuthorityKeyId, cert.SubjectKeyId) {
  72. return pemEncode(ordered[0].Raw, pemTypeCertificate)
  73. }
  74. case certTypeIntermediate:
  75. if len(ordered) < 2 {
  76. return "", nil
  77. }
  78. var pemData []byte
  79. for _, cert := range ordered[1:] {
  80. if isRootCertificate(cert) {
  81. break
  82. }
  83. b := &pem.Block{
  84. Type: pemTypeCertificate,
  85. Bytes: cert.Raw,
  86. }
  87. pemData = append(pemData, pem.EncodeToMemory(b)...)
  88. }
  89. return string(pemData), nil
  90. case certTypeRoot:
  91. cert := ordered[len(ordered)-1]
  92. if isRootCertificate(cert) {
  93. return pemEncode(cert.Raw, pemTypeCertificate)
  94. }
  95. }
  96. return "", nil
  97. }
  98. func isRootCertificate(cert *x509.Certificate) bool {
  99. return cert.AuthorityKeyId == nil || bytes.Equal(cert.AuthorityKeyId, cert.SubjectKeyId)
  100. }
  101. func pemEncode(thing []byte, kind string) (string, error) {
  102. buf := bytes.NewBuffer(nil)
  103. err := pem.Encode(buf, &pem.Block{Type: kind, Bytes: thing})
  104. return buf.String(), err
  105. }