ssh.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package authority
  2. import (
  3. "crypto/rand"
  4. "crypto/sha256"
  5. "encoding/binary"
  6. "encoding/hex"
  7. "net/http"
  8. "strings"
  9. "github.com/pkg/errors"
  10. "github.com/smallstep/certificates/authority/provisioner"
  11. "github.com/smallstep/cli/crypto/randutil"
  12. "golang.org/x/crypto/ssh"
  13. )
  14. func generateSSHPublicKeyID(key ssh.PublicKey) string {
  15. sum := sha256.Sum256(key.Marshal())
  16. return strings.ToLower(hex.EncodeToString(sum[:]))
  17. }
  18. // SignSSH creates a signed SSH certificate with the given public key and options.
  19. func (a *Authority) SignSSH(key ssh.PublicKey, opts provisioner.SSHOptions, signOpts ...provisioner.SignOption) (*ssh.Certificate, error) {
  20. var mods []provisioner.SSHCertificateModifier
  21. var validators []provisioner.SSHCertificateValidator
  22. for _, op := range signOpts {
  23. switch o := op.(type) {
  24. // modify the ssh.Certificate
  25. case provisioner.SSHCertificateModifier:
  26. mods = append(mods, o)
  27. // modify the ssh.Certificate given the SSHOptions
  28. case provisioner.SSHCertificateOptionModifier:
  29. mods = append(mods, o.Option(opts))
  30. // validate the ssh.Certificate
  31. case provisioner.SSHCertificateValidator:
  32. validators = append(validators, o)
  33. // validate the given SSHOptions
  34. case provisioner.SSHCertificateOptionsValidator:
  35. if err := o.Valid(opts); err != nil {
  36. return nil, &apiError{err: err, code: http.StatusUnauthorized}
  37. }
  38. default:
  39. return nil, &apiError{
  40. err: errors.Errorf("signSSH: invalid extra option type %T", o),
  41. code: http.StatusInternalServerError,
  42. }
  43. }
  44. }
  45. nonce, err := randutil.ASCII(32)
  46. if err != nil {
  47. return nil, err
  48. }
  49. var serial uint64
  50. if err := binary.Read(rand.Reader, binary.BigEndian, &serial); err != nil {
  51. return nil, errors.Wrap(err, "error reading random number")
  52. }
  53. // Build base certificate with the key and some random values
  54. cert := &ssh.Certificate{
  55. Nonce: []byte(nonce),
  56. Key: key,
  57. Serial: serial,
  58. }
  59. // Use opts to modify the certificate
  60. if err := opts.Modify(cert); err != nil {
  61. return nil, err
  62. }
  63. // Use provisioner modifiers
  64. for _, m := range mods {
  65. if err := m.Modify(cert); err != nil {
  66. return nil, &apiError{err: err, code: http.StatusInternalServerError}
  67. }
  68. }
  69. // Get signer from authority keys
  70. var signer ssh.Signer
  71. switch cert.CertType {
  72. case ssh.UserCert:
  73. signer, err = ssh.NewSignerFromSigner(a.sshCAUserCertSignKey)
  74. case ssh.HostCert:
  75. signer, err = ssh.NewSignerFromSigner(a.sshCAHostCertSignKey)
  76. default:
  77. return nil, &apiError{
  78. err: errors.Errorf("unexpected ssh certificate type: %d", cert.CertType),
  79. code: http.StatusInternalServerError,
  80. }
  81. }
  82. cert.SignatureKey = signer.PublicKey()
  83. // Get bytes for signing trailing the signature length.
  84. data := cert.Marshal()
  85. data = data[:len(data)-4]
  86. // Sign the certificate
  87. sig, err := signer.Sign(rand.Reader, data)
  88. if err != nil {
  89. return nil, err
  90. }
  91. cert.Signature = sig
  92. // User provisioners validators
  93. for _, v := range validators {
  94. if err := v.Valid(cert); err != nil {
  95. return nil, &apiError{err: err, code: http.StatusUnauthorized}
  96. }
  97. }
  98. return cert, nil
  99. }