pem.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. data := []byte(input)
  29. var blocks []byte
  30. var block *pem.Block
  31. var rest []byte
  32. for {
  33. block, rest = pem.Decode(data)
  34. data = rest
  35. if block == nil {
  36. break
  37. }
  38. if !strings.EqualFold(block.Type, pemType) {
  39. continue
  40. }
  41. var buf bytes.Buffer
  42. err := pem.Encode(&buf, block)
  43. if err != nil {
  44. return "", err
  45. }
  46. blocks = append(blocks, buf.Bytes()...)
  47. }
  48. if len(blocks) == 0 && len(rest) != 0 {
  49. return "", errors.New(errJunk)
  50. }
  51. return string(blocks), nil
  52. }
  53. func filterCertChain(certType, input string) (string, error) {
  54. ordered, err := fetchX509CertChains([]byte(input))
  55. if err != nil {
  56. return "", err
  57. }
  58. switch certType {
  59. case certTypeLeaf:
  60. cert := ordered[0]
  61. if cert.AuthorityKeyId != nil && !bytes.Equal(cert.AuthorityKeyId, cert.SubjectKeyId) {
  62. return pemEncode(ordered[0].Raw, pemTypeCertificate)
  63. }
  64. case certTypeIntermediate:
  65. if len(ordered) < 2 {
  66. return "", nil
  67. }
  68. var pemData []byte
  69. for _, cert := range ordered[1:] {
  70. if isRootCertificate(cert) {
  71. break
  72. }
  73. b := &pem.Block{
  74. Type: pemTypeCertificate,
  75. Bytes: cert.Raw,
  76. }
  77. pemData = append(pemData, pem.EncodeToMemory(b)...)
  78. }
  79. return string(pemData), nil
  80. case certTypeRoot:
  81. cert := ordered[len(ordered)-1]
  82. if isRootCertificate(cert) {
  83. return pemEncode(cert.Raw, pemTypeCertificate)
  84. }
  85. }
  86. return "", nil
  87. }
  88. func isRootCertificate(cert *x509.Certificate) bool {
  89. return cert.AuthorityKeyId == nil || bytes.Equal(cert.AuthorityKeyId, cert.SubjectKeyId)
  90. }
  91. func pemEncode(thing []byte, kind string) (string, error) {
  92. buf := bytes.NewBuffer(nil)
  93. err := pem.Encode(buf, &pem.Block{Type: kind, Bytes: thing})
  94. return buf.String(), err
  95. }