pem.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2019 Yunion
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /*
  15. Copyright 2014 The Kubernetes Authors.
  16. Licensed under the Apache License, Version 2.0 (the "License");
  17. you may not use this file except in compliance with the License.
  18. You may obtain a copy of the License at
  19. http://www.apache.org/licenses/LICENSE-2.0
  20. Unless required by applicable law or agreed to in writing, software
  21. distributed under the License is distributed on an "AS IS" BASIS,
  22. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23. See the License for the specific language governing permissions and
  24. limitations under the License.
  25. */
  26. package cert
  27. import (
  28. "bytes"
  29. "crypto/x509"
  30. "encoding/pem"
  31. "errors"
  32. )
  33. const (
  34. // CertificateBlockType is a possible value for pem.Block.Type.
  35. CertificateBlockType = "CERTIFICATE"
  36. // CertificateRequestBlockType is a possible value for pem.Block.Type.
  37. CertificateRequestBlockType = "CERTIFICATE REQUEST"
  38. )
  39. // ParseCertsPEM returns the x509.Certificates contained in the given PEM-encoded byte array
  40. // Returns an error if a certificate could not be parsed, or if the data does not contain any certificates
  41. func ParseCertsPEM(pemCerts []byte) ([]*x509.Certificate, error) {
  42. ok := false
  43. certs := []*x509.Certificate{}
  44. for len(pemCerts) > 0 {
  45. var block *pem.Block
  46. block, pemCerts = pem.Decode(pemCerts)
  47. if block == nil {
  48. break
  49. }
  50. // Only use PEM "CERTIFICATE" blocks without extra headers
  51. if block.Type != CertificateBlockType || len(block.Headers) != 0 {
  52. continue
  53. }
  54. cert, err := x509.ParseCertificate(block.Bytes)
  55. if err != nil {
  56. return certs, err
  57. }
  58. certs = append(certs, cert)
  59. ok = true
  60. }
  61. if !ok {
  62. return certs, errors.New("data does not contain any valid RSA or ECDSA certificates")
  63. }
  64. return certs, nil
  65. }
  66. // EncodeCertificates returns the PEM-encoded byte array that represents by the specified certs.
  67. func EncodeCertificates(certs ...*x509.Certificate) ([]byte, error) {
  68. b := bytes.Buffer{}
  69. for _, cert := range certs {
  70. if err := pem.Encode(&b, &pem.Block{Type: CertificateBlockType, Bytes: cert.Raw}); err != nil {
  71. return []byte{}, err
  72. }
  73. }
  74. return b.Bytes(), nil
  75. }