certs.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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) RawKey() interface{} {
  62. return c.Key.RawKey()
  63. }
  64. func (c *OpenSSHCertV01) PrivateKeyAlgo() string {
  65. return c.Key.PrivateKeyAlgo()
  66. }
  67. func (c *OpenSSHCertV01) Verify(data []byte, sig []byte) bool {
  68. return c.Key.Verify(data, sig)
  69. }
  70. func parseOpenSSHCertV01(in []byte, algo string) (out *OpenSSHCertV01, rest []byte, ok bool) {
  71. cert := new(OpenSSHCertV01)
  72. if cert.Nonce, in, ok = parseString(in); !ok {
  73. return
  74. }
  75. cert.Key, in, ok = ParsePublicKey(in)
  76. if !ok {
  77. return
  78. }
  79. if cert.Key.PrivateKeyAlgo() != algo {
  80. ok = false
  81. return
  82. }
  83. if cert.Serial, in, ok = parseUint64(in); !ok {
  84. return
  85. }
  86. if cert.Type, in, ok = parseUint32(in); !ok || cert.Type != UserCert && cert.Type != HostCert {
  87. return
  88. }
  89. keyId, in, ok := parseString(in)
  90. if !ok {
  91. return
  92. }
  93. cert.KeyId = string(keyId)
  94. if cert.ValidPrincipals, in, ok = parseLengthPrefixedNameList(in); !ok {
  95. return
  96. }
  97. va, in, ok := parseUint64(in)
  98. if !ok {
  99. return
  100. }
  101. cert.ValidAfter = time.Unix(int64(va), 0)
  102. vb, in, ok := parseUint64(in)
  103. if !ok {
  104. return
  105. }
  106. cert.ValidBefore = time.Unix(int64(vb), 0)
  107. if cert.CriticalOptions, in, ok = parseTupleList(in); !ok {
  108. return
  109. }
  110. if cert.Extensions, in, ok = parseTupleList(in); !ok {
  111. return
  112. }
  113. if cert.Reserved, in, ok = parseString(in); !ok {
  114. return
  115. }
  116. sigKey, in, ok := parseString(in)
  117. if !ok {
  118. return
  119. }
  120. if cert.SignatureKey, _, ok = parsePubKey(sigKey); !ok {
  121. return
  122. }
  123. if cert.Signature, in, ok = parseSignature(in); !ok {
  124. return
  125. }
  126. ok = true
  127. return cert, in, ok
  128. }
  129. func (cert *OpenSSHCertV01) Marshal() []byte {
  130. pubKey := MarshalPublicKey(cert.Key)
  131. sigKey := MarshalPublicKey(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 parseSignatureBody(in []byte) (out *signature, rest []byte, ok bool) {
  250. var format []byte
  251. if format, in, ok = parseString(in); !ok {
  252. return
  253. }
  254. out = &signature{
  255. Format: string(format),
  256. }
  257. if out.Blob, in, ok = parseString(in); !ok {
  258. return
  259. }
  260. return out, in, ok
  261. }
  262. func parseSignature(in []byte) (out *signature, rest []byte, ok bool) {
  263. var sigBytes []byte
  264. if sigBytes, rest, ok = parseString(in); !ok {
  265. return
  266. }
  267. // TODO(hanwen): this is a bug; 'rest' gets swallowed.
  268. return parseSignatureBody(sigBytes)
  269. }