truststore.go 985 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package truststore
  2. import (
  3. "crypto"
  4. "fmt"
  5. "log/slog"
  6. "path/filepath"
  7. )
  8. // TrustStore is a generic interface to retrieve a public key
  9. type TrustStore interface {
  10. GetPublicKey(keyID string) crypto.PublicKey
  11. }
  12. // InMemoryTrustStore is a in memory storage for public keys
  13. type InMemoryTrustStore struct {
  14. Keys map[string]crypto.PublicKey
  15. }
  16. // GetPublicKey returns the public key with this key ID
  17. func (ts *InMemoryTrustStore) GetPublicKey(keyID string) crypto.PublicKey {
  18. if key, ok := ts.Keys[keyID]; ok {
  19. return key
  20. }
  21. return nil
  22. }
  23. // New creates new Trust Store from URI
  24. func New(filename string) (store TrustStore, err error) {
  25. if filename == "" {
  26. return nil, nil
  27. }
  28. slog.Debug("loading truststore...", "filname", filename)
  29. switch filepath.Ext(filename) {
  30. case ".pem":
  31. store, err = newPEMTrustStore(filename)
  32. case ".p12":
  33. store, err = newP12TrustStore(filename)
  34. default:
  35. err = fmt.Errorf("unsupported truststore file format: %s", filename)
  36. }
  37. return
  38. }