pem.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. */
  12. package template
  13. import (
  14. "bytes"
  15. "crypto/x509"
  16. "encoding/pem"
  17. "errors"
  18. "strings"
  19. )
  20. const (
  21. errJunk = "error filtering pem: found junk"
  22. certTypeLeaf = "leaf"
  23. certTypeIntermediate = "intermediate"
  24. certTypeRoot = "root"
  25. )
  26. func filterPEM(pemType, input string) (string, error) {
  27. data := []byte(input)
  28. var blocks []byte
  29. var block *pem.Block
  30. var rest []byte
  31. for {
  32. block, rest = pem.Decode(data)
  33. data = rest
  34. if block == nil {
  35. break
  36. }
  37. if !strings.EqualFold(block.Type, pemType) {
  38. continue
  39. }
  40. var buf bytes.Buffer
  41. err := pem.Encode(&buf, block)
  42. if err != nil {
  43. return "", err
  44. }
  45. blocks = append(blocks, buf.Bytes()...)
  46. }
  47. if len(blocks) == 0 && len(rest) != 0 {
  48. return "", errors.New(errJunk)
  49. }
  50. return string(blocks), nil
  51. }
  52. func filterCertChain(certType, input string) (string, error) {
  53. ordered, err := fetchX509CertChains([]byte(input))
  54. if err != nil {
  55. return "", err
  56. }
  57. switch certType {
  58. case certTypeLeaf:
  59. cert := ordered[0]
  60. if cert.AuthorityKeyId != nil && !bytes.Equal(cert.AuthorityKeyId, cert.SubjectKeyId) {
  61. return pemEncode(ordered[0].Raw, pemTypeCertificate)
  62. }
  63. case certTypeIntermediate:
  64. if len(ordered) < 2 {
  65. return "", nil
  66. }
  67. var pemData []byte
  68. for _, cert := range ordered[1:] {
  69. if isRootCertificate(cert) {
  70. break
  71. }
  72. b := &pem.Block{
  73. Type: pemTypeCertificate,
  74. Bytes: cert.Raw,
  75. }
  76. pemData = append(pemData, pem.EncodeToMemory(b)...)
  77. }
  78. return string(pemData), nil
  79. case certTypeRoot:
  80. cert := ordered[len(ordered)-1]
  81. if isRootCertificate(cert) {
  82. return pemEncode(cert.Raw, pemTypeCertificate)
  83. }
  84. }
  85. return "", nil
  86. }
  87. func isRootCertificate(cert *x509.Certificate) bool {
  88. return cert.AuthorityKeyId == nil || bytes.Equal(cert.AuthorityKeyId, cert.SubjectKeyId)
  89. }
  90. func pemEncode(thing []byte, kind string) (string, error) {
  91. buf := bytes.NewBuffer(nil)
  92. err := pem.Encode(buf, &pem.Block{Type: kind, Bytes: thing})
  93. return buf.String(), err
  94. }