pkcs12.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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/base64"
  17. "encoding/pem"
  18. "errors"
  19. "fmt"
  20. gopkcs12 "software.sslmate.com/src/go-pkcs12"
  21. )
  22. func pkcs12keyPass(pass, input string) (string, error) {
  23. privateKey, _, _, err := gopkcs12.DecodeChain([]byte(input), pass)
  24. if err != nil {
  25. return "", fmt.Errorf(errDecodePKCS12WithPass, err)
  26. }
  27. marshalPrivateKey, err := x509.MarshalPKCS8PrivateKey(privateKey)
  28. if err != nil {
  29. return "", err
  30. }
  31. var buf bytes.Buffer
  32. if err := pem.Encode(&buf, &pem.Block{
  33. Type: pemTypeKey,
  34. Bytes: marshalPrivateKey,
  35. }); err != nil {
  36. return "", err
  37. }
  38. return buf.String(), nil
  39. }
  40. func parsePrivateKey(block []byte) (any, error) {
  41. if k, err := x509.ParsePKCS1PrivateKey(block); err == nil {
  42. return k, nil
  43. }
  44. if k, err := x509.ParsePKCS8PrivateKey(block); err == nil {
  45. return k, nil
  46. }
  47. if k, err := x509.ParseECPrivateKey(block); err == nil {
  48. return k, nil
  49. }
  50. return nil, errors.New(errParsePrivKey)
  51. }
  52. func pkcs12key(input string) (string, error) {
  53. return pkcs12keyPass("", input)
  54. }
  55. func pkcs12certPass(pass, input string) (string, error) {
  56. _, certificate, caCerts, err := gopkcs12.DecodeChain([]byte(input), pass)
  57. if err != nil {
  58. return "", fmt.Errorf(errDecodeCertWithPass, err)
  59. }
  60. var pemData []byte
  61. var buf bytes.Buffer
  62. if err := pem.Encode(&buf, &pem.Block{
  63. Type: pemTypeCertificate,
  64. Bytes: certificate.Raw,
  65. }); err != nil {
  66. return "", err
  67. }
  68. pemData = append(pemData, buf.Bytes()...)
  69. for _, ca := range caCerts {
  70. var buf bytes.Buffer
  71. if err := pem.Encode(&buf, &pem.Block{
  72. Type: pemTypeCertificate,
  73. Bytes: ca.Raw,
  74. }); err != nil {
  75. return "", err
  76. }
  77. pemData = append(pemData, buf.Bytes()...)
  78. }
  79. // try to order certificate chain. If it fails we return
  80. // the unordered raw pem data.
  81. // This fails if multiple leaf or disjunct certs are provided.
  82. ordered, err := fetchCertChains(pemData)
  83. if err != nil {
  84. return string(pemData), nil
  85. }
  86. return string(ordered), nil
  87. }
  88. func pkcs12cert(input string) (string, error) {
  89. return pkcs12certPass("", input)
  90. }
  91. func pemToPkcs12(cert, key string) (string, error) {
  92. return pemToPkcs12Pass(cert, key, "")
  93. }
  94. func pemToPkcs12Pass(cert, key, pass string) (string, error) {
  95. certPem, _ := pem.Decode([]byte(cert))
  96. parsedCert, err := x509.ParseCertificate(certPem.Bytes)
  97. if err != nil {
  98. return "", err
  99. }
  100. return certsToPkcs12(parsedCert, key, nil, pass)
  101. }
  102. func fullPemToPkcs12(cert, key string) (string, error) {
  103. return fullPemToPkcs12Pass(cert, key, "")
  104. }
  105. func fullPemToPkcs12Pass(cert, key, pass string) (string, error) {
  106. certPem, rest := pem.Decode([]byte(cert))
  107. parsedCert, err := x509.ParseCertificate(certPem.Bytes)
  108. if err != nil {
  109. return "", err
  110. }
  111. caCerts := make([]*x509.Certificate, 0)
  112. for len(rest) > 0 {
  113. caPem, restBytes := pem.Decode(rest)
  114. rest = restBytes
  115. caCert, err := x509.ParseCertificate(caPem.Bytes)
  116. if err != nil {
  117. return "", err
  118. }
  119. caCerts = append(caCerts, caCert)
  120. }
  121. return certsToPkcs12(parsedCert, key, caCerts, pass)
  122. }
  123. func certsToPkcs12(cert *x509.Certificate, key string, caCerts []*x509.Certificate, password string) (string, error) {
  124. keyPem, _ := pem.Decode([]byte(key))
  125. parsedKey, err := parsePrivateKey(keyPem.Bytes)
  126. if err != nil {
  127. return "", err
  128. }
  129. pfx, err := gopkcs12.Modern.Encode(parsedKey, cert, caCerts, password)
  130. if err != nil {
  131. return "", err
  132. }
  133. return base64.StdEncoding.EncodeToString(pfx), nil
  134. }