sign.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package schema1
  2. import (
  3. "crypto/x509"
  4. "encoding/json"
  5. "github.com/docker/libtrust"
  6. )
  7. // Sign signs the manifest with the provided private key, returning a
  8. // SignedManifest. This typically won't be used within the registry, except
  9. // for testing.
  10. func Sign(m *Manifest, pk libtrust.PrivateKey) (*SignedManifest, error) {
  11. p, err := json.MarshalIndent(m, "", " ")
  12. if err != nil {
  13. return nil, err
  14. }
  15. js, err := libtrust.NewJSONSignature(p)
  16. if err != nil {
  17. return nil, err
  18. }
  19. if err := js.Sign(pk); err != nil {
  20. return nil, err
  21. }
  22. pretty, err := js.PrettySignature("signatures")
  23. if err != nil {
  24. return nil, err
  25. }
  26. return &SignedManifest{
  27. Manifest: *m,
  28. all: pretty,
  29. Canonical: p,
  30. }, nil
  31. }
  32. // SignWithChain signs the manifest with the given private key and x509 chain.
  33. // The public key of the first element in the chain must be the public key
  34. // corresponding with the sign key.
  35. func SignWithChain(m *Manifest, key libtrust.PrivateKey, chain []*x509.Certificate) (*SignedManifest, error) {
  36. p, err := json.MarshalIndent(m, "", " ")
  37. if err != nil {
  38. return nil, err
  39. }
  40. js, err := libtrust.NewJSONSignature(p)
  41. if err != nil {
  42. return nil, err
  43. }
  44. if err := js.SignWithChain(key, chain); err != nil {
  45. return nil, err
  46. }
  47. pretty, err := js.PrettySignature("signatures")
  48. if err != nil {
  49. return nil, err
  50. }
  51. return &SignedManifest{
  52. Manifest: *m,
  53. all: pretty,
  54. Canonical: p,
  55. }, nil
  56. }