p12_truststore.go 557 B

12345678910111213141516171819202122232425262728293031
  1. package truststore
  2. import (
  3. "crypto"
  4. "log/slog"
  5. "os"
  6. "golang.org/x/crypto/pkcs12"
  7. )
  8. func newP12TrustStore(filename string) (TrustStore, error) {
  9. data, err := os.ReadFile(filename)
  10. if err != nil {
  11. return nil, err
  12. }
  13. _, cert, err := pkcs12.Decode(data, "test")
  14. if err != nil {
  15. return nil, err
  16. }
  17. result := &InMemoryTrustStore{
  18. Keys: make(map[string]crypto.PublicKey),
  19. }
  20. keyID := string(cert.Subject.CommonName)
  21. result.Keys[keyID] = cert.PublicKey
  22. slog.Debug("certificate loaded into the trustore", "id", keyID)
  23. return result, nil
  24. }