roots.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. package distsign
  4. import (
  5. "crypto/ed25519"
  6. "embed"
  7. "errors"
  8. "fmt"
  9. "path"
  10. "path/filepath"
  11. "sync"
  12. )
  13. //go:embed roots
  14. var rootsFS embed.FS
  15. var roots = sync.OnceValue(func() []ed25519.PublicKey {
  16. roots, err := parseRoots()
  17. if err != nil {
  18. panic(err)
  19. }
  20. return roots
  21. })
  22. func parseRoots() ([]ed25519.PublicKey, error) {
  23. files, err := rootsFS.ReadDir("roots")
  24. if err != nil {
  25. return nil, err
  26. }
  27. var keys []ed25519.PublicKey
  28. for _, f := range files {
  29. if !f.Type().IsRegular() {
  30. continue
  31. }
  32. if filepath.Ext(f.Name()) != ".pem" {
  33. continue
  34. }
  35. raw, err := rootsFS.ReadFile(path.Join("roots", f.Name()))
  36. if err != nil {
  37. return nil, err
  38. }
  39. key, err := parseSinglePublicKey(raw, pemTypeRootPublic)
  40. if err != nil {
  41. return nil, fmt.Errorf("parsing root key %q: %w", f.Name(), err)
  42. }
  43. keys = append(keys, key)
  44. }
  45. if len(keys) == 0 {
  46. return nil, errors.New("no embedded root keys, please check clientupdate/distsign/roots/")
  47. }
  48. return keys, nil
  49. }