ssh.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package api
  2. import (
  3. "encoding/base64"
  4. "encoding/json"
  5. "net/http"
  6. "github.com/pkg/errors"
  7. "github.com/smallstep/certificates/authority/provisioner"
  8. "golang.org/x/crypto/ssh"
  9. )
  10. // SSHAuthority is the interface implemented by a SSH CA authority.
  11. type SSHAuthority interface {
  12. SignSSH(key ssh.PublicKey, opts provisioner.SSHOptions, signOpts ...provisioner.SignOption) (*ssh.Certificate, error)
  13. }
  14. // SignSSHRequest is the request body of an SSH certificate request.
  15. type SignSSHRequest struct {
  16. PublicKey []byte `json:"publicKey"` //base64 encoded
  17. OTT string `json:"ott"`
  18. CertType string `json:"certType"`
  19. Principals []string `json:"principals"`
  20. ValidAfter TimeDuration `json:"validAfter"`
  21. ValidBefore TimeDuration `json:"validBefore"`
  22. }
  23. // SignSSHResponse is the response object that returns the SSH certificate.
  24. type SignSSHResponse struct {
  25. Certificate SSHCertificate `json:"crt"`
  26. }
  27. // SSHCertificate represents the response SSH certificate.
  28. type SSHCertificate struct {
  29. *ssh.Certificate
  30. }
  31. // MarshalJSON implements the json.Marshaler interface. The certificate is
  32. // quoted string using the PEM encoding.
  33. func (c SSHCertificate) MarshalJSON() ([]byte, error) {
  34. if c.Certificate == nil {
  35. return []byte("null"), nil
  36. }
  37. s := base64.StdEncoding.EncodeToString(c.Certificate.Marshal())
  38. return []byte(`"` + s + `"`), nil
  39. }
  40. // UnmarshalJSON implements the json.Unmarshaler interface. The certificate is
  41. // expected to be a quoted string using the PEM encoding.
  42. func (c *SSHCertificate) UnmarshalJSON(data []byte) error {
  43. var s string
  44. if err := json.Unmarshal(data, &s); err != nil {
  45. return errors.Wrap(err, "error decoding certificate")
  46. }
  47. certData, err := base64.StdEncoding.DecodeString(s)
  48. if err != nil {
  49. return errors.Wrap(err, "error decoding certificate")
  50. }
  51. pub, err := ssh.ParsePublicKey(certData)
  52. if err != nil {
  53. return errors.Wrap(err, "error decoding certificate")
  54. }
  55. cert, ok := pub.(*ssh.Certificate)
  56. if !ok {
  57. return errors.Errorf("error decoding certificate: %T is not an *ssh.Certificate", pub)
  58. }
  59. c.Certificate = cert
  60. return nil
  61. }
  62. // Validate validates the SignSSHRequest.
  63. func (s *SignSSHRequest) Validate() error {
  64. switch {
  65. case s.CertType != "" && s.CertType != provisioner.SSHUserCert && s.CertType != provisioner.SSHHostCert:
  66. return errors.Errorf("unknown certType %s", s.CertType)
  67. case len(s.PublicKey) == 0:
  68. return errors.New("missing or empty publicKey")
  69. case len(s.OTT) == 0:
  70. return errors.New("missing or empty ott")
  71. default:
  72. return nil
  73. }
  74. }
  75. // SignSSH is an HTTP handler that reads an SignSSHRequest with a one-time-token
  76. // (ott) from the body and creates a new SSH certificate with the information in
  77. // the request.
  78. func (h *caHandler) SignSSH(w http.ResponseWriter, r *http.Request) {
  79. var body SignSSHRequest
  80. if err := ReadJSON(r.Body, &body); err != nil {
  81. WriteError(w, BadRequest(errors.Wrap(err, "error reading request body")))
  82. return
  83. }
  84. logOtt(w, body.OTT)
  85. if err := body.Validate(); err != nil {
  86. WriteError(w, err)
  87. return
  88. }
  89. publicKey, err := ssh.ParsePublicKey(body.PublicKey)
  90. if err != nil {
  91. WriteError(w, BadRequest(errors.Wrap(err, "error parsing publicKey")))
  92. return
  93. }
  94. opts := provisioner.SSHOptions{
  95. CertType: body.CertType,
  96. Principals: body.Principals,
  97. ValidBefore: body.ValidBefore,
  98. ValidAfter: body.ValidAfter,
  99. }
  100. signOpts, err := h.Authority.AuthorizeSign(body.OTT)
  101. if err != nil {
  102. WriteError(w, Unauthorized(err))
  103. return
  104. }
  105. cert, err := h.Authority.SignSSH(publicKey, opts, signOpts...)
  106. if err != nil {
  107. WriteError(w, Forbidden(err))
  108. return
  109. }
  110. w.WriteHeader(http.StatusCreated)
  111. // logCertificate(w, cert)
  112. JSON(w, &SignSSHResponse{
  113. Certificate: SSHCertificate{cert},
  114. })
  115. }