certs.go 7.2 KB

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