certs.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. // Copyright 2012 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package ssh
  5. // References
  6. // [PROTOCOL.certkeys]: http://www.openbsd.org/cgi-bin/cvsweb/src/usr.bin/ssh/PROTOCOL.certkeys
  7. import (
  8. "crypto/dsa"
  9. "crypto/rsa"
  10. "time"
  11. )
  12. // String constants in [PROTOCOL.certkeys] for certificate algorithm names.
  13. const (
  14. hostAlgoRSACertV01 = "ssh-rsa-cert-v01@openssh.com"
  15. hostAlgoDSACertV01 = "ssh-dss-cert-v01@openssh.com"
  16. hostAlgoECDSA256CertV01 = "ecdsa-sha2-nistp256-cert-v01@openssh.com"
  17. hostAlgoECDSA384CertV01 = "ecdsa-sha2-nistp384-cert-v01@openssh.com"
  18. hostAlgoECDSA521CertV01 = "ecdsa-sha2-nistp521-cert-v01@openssh.com"
  19. )
  20. // Certificate types are used to specify whether a certificate is for identification
  21. // of a user or a host. Current identities are defined in [PROTOCOL.certkeys].
  22. const (
  23. UserCert = 1
  24. HostCert = 2
  25. )
  26. type signature struct {
  27. Format string
  28. Blob []byte
  29. }
  30. type tuple struct {
  31. Name string
  32. Data string
  33. }
  34. // An OpenSSHCertV01 represents an OpenSSH certificate as defined in
  35. // [PROTOCOL.certkeys] rev 1.8. Supported formats include
  36. // ssh-rsa-cert-v01@openssh.com and ssh-dss-cert-v01@openssh.com.
  37. type OpenSSHCertV01 struct {
  38. Nonce []byte
  39. Key interface{} // rsa or dsa *PublicKey
  40. Serial uint64
  41. Type uint32
  42. KeyId string
  43. ValidPrincipals []string
  44. ValidAfter, ValidBefore time.Time
  45. CriticalOptions []tuple
  46. Extensions []tuple
  47. Reserved []byte
  48. SignatureKey interface{} // rsa, dsa, or ecdsa *PublicKey
  49. Signature *signature
  50. }
  51. func parseOpenSSHCertV01(in []byte, algo string) (out *OpenSSHCertV01, rest []byte, ok bool) {
  52. cert := new(OpenSSHCertV01)
  53. if cert.Nonce, in, ok = parseString(in); !ok {
  54. return
  55. }
  56. switch algo {
  57. case hostAlgoRSACertV01:
  58. var rsaPubKey *rsa.PublicKey
  59. if rsaPubKey, in, ok = parseRSA(in); !ok {
  60. return
  61. }
  62. cert.Key = rsaPubKey
  63. case hostAlgoDSACertV01:
  64. var dsaPubKey *dsa.PublicKey
  65. if dsaPubKey, in, ok = parseDSA(in); !ok {
  66. return
  67. }
  68. cert.Key = dsaPubKey
  69. default:
  70. ok = false
  71. return
  72. }
  73. if cert.Serial, in, ok = parseUint64(in); !ok {
  74. return
  75. }
  76. if cert.Type, in, ok = parseUint32(in); !ok || cert.Type != UserCert && cert.Type != HostCert {
  77. return
  78. }
  79. keyId, in, ok := parseString(in)
  80. if !ok {
  81. return
  82. }
  83. cert.KeyId = string(keyId)
  84. if cert.ValidPrincipals, in, ok = parseLengthPrefixedNameList(in); !ok {
  85. return
  86. }
  87. va, in, ok := parseUint64(in)
  88. if !ok {
  89. return
  90. }
  91. cert.ValidAfter = time.Unix(int64(va), 0)
  92. vb, in, ok := parseUint64(in)
  93. if !ok {
  94. return
  95. }
  96. cert.ValidBefore = time.Unix(int64(vb), 0)
  97. if cert.CriticalOptions, in, ok = parseTupleList(in); !ok {
  98. return
  99. }
  100. if cert.Extensions, in, ok = parseTupleList(in); !ok {
  101. return
  102. }
  103. if cert.Reserved, in, ok = parseString(in); !ok {
  104. return
  105. }
  106. sigKey, in, ok := parseString(in)
  107. if !ok {
  108. return
  109. }
  110. if cert.SignatureKey, _, ok = parsePubKey(sigKey); !ok {
  111. return
  112. }
  113. if cert.Signature, in, ok = parseSignature(in); !ok {
  114. return
  115. }
  116. ok = true
  117. return cert, in, ok
  118. }
  119. func marshalOpenSSHCertV01(cert *OpenSSHCertV01) []byte {
  120. var pubKey []byte
  121. switch cert.Key.(type) {
  122. case *rsa.PublicKey:
  123. k := cert.Key.(*rsa.PublicKey)
  124. pubKey = marshalPubRSA(k)
  125. case *dsa.PublicKey:
  126. k := cert.Key.(*dsa.PublicKey)
  127. pubKey = marshalPubDSA(k)
  128. default:
  129. panic("ssh: unknown public key type in cert")
  130. }
  131. sigKey := serializePublickey(cert.SignatureKey)
  132. length := stringLength(len(cert.Nonce))
  133. length += len(pubKey)
  134. length += 8 // Length of Serial
  135. length += 4 // Length of Type
  136. length += stringLength(len(cert.KeyId))
  137. length += lengthPrefixedNameListLength(cert.ValidPrincipals)
  138. length += 8 // Length of ValidAfter
  139. length += 8 // Length of ValidBefore
  140. length += tupleListLength(cert.CriticalOptions)
  141. length += tupleListLength(cert.Extensions)
  142. length += stringLength(len(cert.Reserved))
  143. length += stringLength(len(sigKey))
  144. length += signatureLength(cert.Signature)
  145. ret := make([]byte, length)
  146. r := marshalString(ret, cert.Nonce)
  147. copy(r, pubKey)
  148. r = r[len(pubKey):]
  149. r = marshalUint64(r, cert.Serial)
  150. r = marshalUint32(r, cert.Type)
  151. r = marshalString(r, []byte(cert.KeyId))
  152. r = marshalLengthPrefixedNameList(r, cert.ValidPrincipals)
  153. r = marshalUint64(r, uint64(cert.ValidAfter.Unix()))
  154. r = marshalUint64(r, uint64(cert.ValidBefore.Unix()))
  155. r = marshalTupleList(r, cert.CriticalOptions)
  156. r = marshalTupleList(r, cert.Extensions)
  157. r = marshalString(r, cert.Reserved)
  158. r = marshalString(r, sigKey)
  159. r = marshalSignature(r, cert.Signature)
  160. if len(r) > 0 {
  161. panic("internal error")
  162. }
  163. return ret
  164. }
  165. func lengthPrefixedNameListLength(namelist []string) int {
  166. length := 4 // length prefix for list
  167. for _, name := range namelist {
  168. length += 4 // length prefix for name
  169. length += len(name)
  170. }
  171. return length
  172. }
  173. func marshalLengthPrefixedNameList(to []byte, namelist []string) []byte {
  174. length := uint32(lengthPrefixedNameListLength(namelist) - 4)
  175. to = marshalUint32(to, length)
  176. for _, name := range namelist {
  177. to = marshalString(to, []byte(name))
  178. }
  179. return to
  180. }
  181. func parseLengthPrefixedNameList(in []byte) (out []string, rest []byte, ok bool) {
  182. list, rest, ok := parseString(in)
  183. if !ok {
  184. return
  185. }
  186. for len(list) > 0 {
  187. var next []byte
  188. if next, list, ok = parseString(list); !ok {
  189. return nil, nil, false
  190. }
  191. out = append(out, string(next))
  192. }
  193. ok = true
  194. return
  195. }
  196. func tupleListLength(tupleList []tuple) int {
  197. length := 4 // length prefix for list
  198. for _, t := range tupleList {
  199. length += 4 // length prefix for t.Name
  200. length += len(t.Name)
  201. length += 4 // length prefix for t.Data
  202. length += len(t.Data)
  203. }
  204. return length
  205. }
  206. func marshalTupleList(to []byte, tuplelist []tuple) []byte {
  207. length := uint32(tupleListLength(tuplelist) - 4)
  208. to = marshalUint32(to, length)
  209. for _, t := range tuplelist {
  210. to = marshalString(to, []byte(t.Name))
  211. to = marshalString(to, []byte(t.Data))
  212. }
  213. return to
  214. }
  215. func parseTupleList(in []byte) (out []tuple, rest []byte, ok bool) {
  216. list, rest, ok := parseString(in)
  217. if !ok {
  218. return
  219. }
  220. for len(list) > 0 {
  221. var name, data []byte
  222. var ok bool
  223. name, list, ok = parseString(list)
  224. if !ok {
  225. return nil, nil, false
  226. }
  227. data, list, ok = parseString(list)
  228. if !ok {
  229. return nil, nil, false
  230. }
  231. out = append(out, tuple{string(name), string(data)})
  232. }
  233. ok = true
  234. return
  235. }
  236. func signatureLength(sig *signature) int {
  237. length := 4 // length prefix for signature
  238. length += stringLength(len(sig.Format))
  239. length += stringLength(len(sig.Blob))
  240. return length
  241. }
  242. func marshalSignature(to []byte, sig *signature) []byte {
  243. length := uint32(signatureLength(sig) - 4)
  244. to = marshalUint32(to, length)
  245. to = marshalString(to, []byte(sig.Format))
  246. to = marshalString(to, sig.Blob)
  247. return to
  248. }
  249. func parseSignature(in []byte) (out *signature, rest []byte, ok bool) {
  250. var sigBytes, format []byte
  251. sig := new(signature)
  252. if sigBytes, rest, ok = parseString(in); !ok {
  253. return
  254. }
  255. if format, sigBytes, ok = parseString(sigBytes); !ok {
  256. return
  257. }
  258. sig.Format = string(format)
  259. if sig.Blob, sigBytes, ok = parseString(sigBytes); !ok {
  260. return
  261. }
  262. return sig, rest, ok
  263. }