certificate.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package sshutil
  2. import (
  3. "crypto/rand"
  4. "encoding/binary"
  5. "encoding/json"
  6. "github.com/pkg/errors"
  7. "github.com/smallstep/cli/crypto/randutil"
  8. "golang.org/x/crypto/ssh"
  9. )
  10. // Certificate is the json representation of ssh.Certificate.
  11. type Certificate struct {
  12. Nonce []byte `json:"nonce"`
  13. Key ssh.PublicKey `json:"-"`
  14. Serial uint64 `json:"serial"`
  15. Type CertType `json:"type"`
  16. KeyID string `json:"keyId"`
  17. Principals []string `json:"principals"`
  18. ValidAfter uint64 `json:"-"`
  19. ValidBefore uint64 `json:"-"`
  20. CriticalOptions map[string]string `json:"criticalOptions"`
  21. Extensions map[string]string `json:"extensions"`
  22. Reserved []byte `json:"reserved"`
  23. SignatureKey ssh.PublicKey `json:"-"`
  24. Signature *ssh.Signature `json:"-"`
  25. }
  26. // NewCertificate creates a new certificate with the given key after parsing a
  27. // template given in the options.
  28. func NewCertificate(cr CertificateRequest, opts ...Option) (*Certificate, error) {
  29. o, err := new(Options).apply(cr, opts)
  30. if err != nil {
  31. return nil, err
  32. }
  33. if o.CertBuffer == nil {
  34. return nil, errors.New("certificate template cannot be empty")
  35. }
  36. // With templates
  37. var cert Certificate
  38. if err := json.NewDecoder(o.CertBuffer).Decode(&cert); err != nil {
  39. return nil, errors.Wrap(err, "error unmarshaling certificate")
  40. }
  41. // Complete with public key
  42. cert.Key = cr.Key
  43. return &cert, nil
  44. }
  45. func (c *Certificate) GetCertificate() *ssh.Certificate {
  46. return &ssh.Certificate{
  47. Nonce: c.Nonce,
  48. Key: c.Key,
  49. Serial: c.Serial,
  50. CertType: uint32(c.Type),
  51. KeyId: c.KeyID,
  52. ValidPrincipals: c.Principals,
  53. ValidAfter: c.ValidAfter,
  54. ValidBefore: c.ValidBefore,
  55. Permissions: ssh.Permissions{
  56. CriticalOptions: c.CriticalOptions,
  57. Extensions: c.Extensions,
  58. },
  59. Reserved: c.Reserved,
  60. }
  61. }
  62. // CreateCertificate signs the given certificate with the given signer. If the
  63. // certificate does not have a nonce or a serial, it will create random ones.
  64. func CreateCertificate(cert *ssh.Certificate, signer ssh.Signer) (*ssh.Certificate, error) {
  65. if len(cert.Nonce) == 0 {
  66. nonce, err := randutil.ASCII(32)
  67. if err != nil {
  68. return nil, err
  69. }
  70. cert.Nonce = []byte(nonce)
  71. }
  72. if cert.Serial == 0 {
  73. if err := binary.Read(rand.Reader, binary.BigEndian, &cert.Serial); err != nil {
  74. return nil, errors.Wrap(err, "error reading random number")
  75. }
  76. }
  77. // Set signer public key.
  78. cert.SignatureKey = signer.PublicKey()
  79. // Get bytes for signing trailing the signature length.
  80. data := cert.Marshal()
  81. data = data[:len(data)-4]
  82. // Sign the certificate.
  83. sig, err := signer.Sign(rand.Reader, data)
  84. if err != nil {
  85. return nil, errors.Wrap(err, "error signing certificate")
  86. }
  87. cert.Signature = sig
  88. return cert, nil
  89. }