certificate_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. package sshutil
  2. import (
  3. "bytes"
  4. "crypto/ed25519"
  5. "crypto/rand"
  6. "encoding/base64"
  7. "reflect"
  8. "testing"
  9. "golang.org/x/crypto/ssh"
  10. )
  11. func mustGenerateKey(t *testing.T) (ssh.PublicKey, ssh.Signer) {
  12. t.Helper()
  13. pub, priv, err := ed25519.GenerateKey(rand.Reader)
  14. if err != nil {
  15. t.Fatal(err)
  16. }
  17. key, err := ssh.NewPublicKey(pub)
  18. if err != nil {
  19. t.Fatal(err)
  20. }
  21. signer, err := ssh.NewSignerFromKey(priv)
  22. if err != nil {
  23. t.Fatal(err)
  24. }
  25. return key, signer
  26. }
  27. func mustGeneratePublicKey(t *testing.T) ssh.PublicKey {
  28. t.Helper()
  29. key, _ := mustGenerateKey(t)
  30. return key
  31. }
  32. func TestNewCertificate(t *testing.T) {
  33. key := mustGeneratePublicKey(t)
  34. cr := CertificateRequest{
  35. Key: key,
  36. }
  37. type args struct {
  38. cr CertificateRequest
  39. opts []Option
  40. }
  41. tests := []struct {
  42. name string
  43. args args
  44. want *Certificate
  45. wantErr bool
  46. }{
  47. {"user", args{cr, []Option{WithTemplate(DefaultCertificate, CreateTemplateData(UserCert, "jane@doe.com", []string{"jane"}))}}, &Certificate{
  48. Nonce: nil,
  49. Key: key,
  50. Serial: 0,
  51. Type: UserCert,
  52. KeyID: "jane@doe.com",
  53. Principals: []string{"jane"},
  54. ValidAfter: 0,
  55. ValidBefore: 0,
  56. CriticalOptions: nil,
  57. Extensions: map[string]string{
  58. "permit-X11-forwarding": "",
  59. "permit-agent-forwarding": "",
  60. "permit-port-forwarding": "",
  61. "permit-pty": "",
  62. "permit-user-rc": "",
  63. },
  64. Reserved: nil,
  65. SignatureKey: nil,
  66. Signature: nil,
  67. }, false},
  68. {"host", args{cr, []Option{WithTemplate(DefaultCertificate, CreateTemplateData(HostCert, "foobar", []string{"foo.internal", "bar.internal"}))}}, &Certificate{
  69. Nonce: nil,
  70. Key: key,
  71. Serial: 0,
  72. Type: HostCert,
  73. KeyID: "foobar",
  74. Principals: []string{"foo.internal", "bar.internal"},
  75. ValidAfter: 0,
  76. ValidBefore: 0,
  77. CriticalOptions: nil,
  78. Extensions: nil,
  79. Reserved: nil,
  80. SignatureKey: nil,
  81. Signature: nil,
  82. }, false},
  83. {"file", args{cr, []Option{WithTemplateFile("./testdata/github.tpl", TemplateData{
  84. TypeKey: UserCert,
  85. KeyIDKey: "john@doe.com",
  86. PrincipalsKey: []string{"john", "john@doe.com"},
  87. ExtensionsKey: DefaultExtensions(UserCert),
  88. InsecureKey: TemplateData{
  89. "User": map[string]interface{}{"username": "john"},
  90. },
  91. })}}, &Certificate{
  92. Nonce: nil,
  93. Key: key,
  94. Serial: 0,
  95. Type: UserCert,
  96. KeyID: "john@doe.com",
  97. Principals: []string{"john", "john@doe.com"},
  98. ValidAfter: 0,
  99. ValidBefore: 0,
  100. CriticalOptions: nil,
  101. Extensions: map[string]string{
  102. "login@github.com": "john",
  103. "permit-X11-forwarding": "",
  104. "permit-agent-forwarding": "",
  105. "permit-port-forwarding": "",
  106. "permit-pty": "",
  107. "permit-user-rc": "",
  108. },
  109. Reserved: nil,
  110. SignatureKey: nil,
  111. Signature: nil,
  112. }, false},
  113. {"base64", args{cr, []Option{WithTemplateBase64(base64.StdEncoding.EncodeToString([]byte(DefaultCertificate)), CreateTemplateData(HostCert, "foo.internal", nil))}}, &Certificate{
  114. Nonce: nil,
  115. Key: key,
  116. Serial: 0,
  117. Type: HostCert,
  118. KeyID: "foo.internal",
  119. Principals: nil,
  120. ValidAfter: 0,
  121. ValidBefore: 0,
  122. CriticalOptions: nil,
  123. Extensions: nil,
  124. Reserved: nil,
  125. SignatureKey: nil,
  126. Signature: nil,
  127. }, false},
  128. {"failNilOptions", args{cr, nil}, nil, true},
  129. {"failEmptyOptions", args{cr, nil}, nil, true},
  130. {"badBase64Template", args{cr, []Option{WithTemplateBase64("foobar", TemplateData{})}}, nil, true},
  131. {"badFileTemplate", args{cr, []Option{WithTemplateFile("./testdata/missing.tpl", TemplateData{})}}, nil, true},
  132. {"badJsonTemplate", args{cr, []Option{WithTemplate(`{"type":{{ .Type }}}`, TemplateData{})}}, nil, true},
  133. {"failTemplate", args{cr, []Option{WithTemplate(`{{ fail "an error" }}`, TemplateData{})}}, nil, true},
  134. }
  135. for _, tt := range tests {
  136. t.Run(tt.name, func(t *testing.T) {
  137. got, err := NewCertificate(tt.args.cr, tt.args.opts...)
  138. if (err != nil) != tt.wantErr {
  139. t.Errorf("NewCertificate() error = %v, wantErr %v", err, tt.wantErr)
  140. return
  141. }
  142. if !reflect.DeepEqual(got, tt.want) {
  143. t.Errorf("NewCertificate() = \n%+v, want \n%+v", got, tt.want)
  144. }
  145. })
  146. }
  147. }
  148. func TestCertificate_GetCertificate(t *testing.T) {
  149. key := mustGeneratePublicKey(t)
  150. type fields struct {
  151. Nonce []byte
  152. Key ssh.PublicKey
  153. Serial uint64
  154. Type CertType
  155. KeyID string
  156. Principals []string
  157. ValidAfter uint64
  158. ValidBefore uint64
  159. CriticalOptions map[string]string
  160. Extensions map[string]string
  161. Reserved []byte
  162. SignatureKey ssh.PublicKey
  163. Signature *ssh.Signature
  164. }
  165. tests := []struct {
  166. name string
  167. fields fields
  168. want *ssh.Certificate
  169. }{
  170. {"user", fields{
  171. Nonce: []byte("0123456789"),
  172. Key: key,
  173. Serial: 123,
  174. Type: UserCert,
  175. KeyID: "key-id",
  176. Principals: []string{"john"},
  177. ValidAfter: 1111,
  178. ValidBefore: 2222,
  179. CriticalOptions: map[string]string{"foo": "bar"},
  180. Extensions: map[string]string{"login@github.com": "john"},
  181. Reserved: []byte("reserved"),
  182. SignatureKey: key,
  183. Signature: &ssh.Signature{Format: "foo", Blob: []byte("bar")},
  184. }, &ssh.Certificate{
  185. Nonce: []byte("0123456789"),
  186. Key: key,
  187. Serial: 123,
  188. CertType: ssh.UserCert,
  189. KeyId: "key-id",
  190. ValidPrincipals: []string{"john"},
  191. ValidAfter: 1111,
  192. ValidBefore: 2222,
  193. Permissions: ssh.Permissions{
  194. CriticalOptions: map[string]string{"foo": "bar"},
  195. Extensions: map[string]string{"login@github.com": "john"},
  196. },
  197. Reserved: []byte("reserved"),
  198. }},
  199. {"host", fields{
  200. Nonce: []byte("0123456789"),
  201. Key: key,
  202. Serial: 123,
  203. Type: HostCert,
  204. KeyID: "key-id",
  205. Principals: []string{"foo.internal", "bar.internal"},
  206. ValidAfter: 1111,
  207. ValidBefore: 2222,
  208. CriticalOptions: map[string]string{"foo": "bar"},
  209. Extensions: nil,
  210. Reserved: []byte("reserved"),
  211. SignatureKey: key,
  212. Signature: &ssh.Signature{Format: "foo", Blob: []byte("bar")},
  213. }, &ssh.Certificate{
  214. Nonce: []byte("0123456789"),
  215. Key: key,
  216. Serial: 123,
  217. CertType: ssh.HostCert,
  218. KeyId: "key-id",
  219. ValidPrincipals: []string{"foo.internal", "bar.internal"},
  220. ValidAfter: 1111,
  221. ValidBefore: 2222,
  222. Permissions: ssh.Permissions{
  223. CriticalOptions: map[string]string{"foo": "bar"},
  224. Extensions: nil,
  225. },
  226. Reserved: []byte("reserved"),
  227. }},
  228. }
  229. for _, tt := range tests {
  230. t.Run(tt.name, func(t *testing.T) {
  231. c := &Certificate{
  232. Nonce: tt.fields.Nonce,
  233. Key: tt.fields.Key,
  234. Serial: tt.fields.Serial,
  235. Type: tt.fields.Type,
  236. KeyID: tt.fields.KeyID,
  237. Principals: tt.fields.Principals,
  238. ValidAfter: tt.fields.ValidAfter,
  239. ValidBefore: tt.fields.ValidBefore,
  240. CriticalOptions: tt.fields.CriticalOptions,
  241. Extensions: tt.fields.Extensions,
  242. Reserved: tt.fields.Reserved,
  243. SignatureKey: tt.fields.SignatureKey,
  244. Signature: tt.fields.Signature,
  245. }
  246. if got := c.GetCertificate(); !reflect.DeepEqual(got, tt.want) {
  247. t.Errorf("Certificate.GetCertificate() = %v, want %v", got, tt.want)
  248. }
  249. })
  250. }
  251. }
  252. func TestCreateCertificate(t *testing.T) {
  253. key, signer := mustGenerateKey(t)
  254. type args struct {
  255. cert *ssh.Certificate
  256. signer ssh.Signer
  257. }
  258. tests := []struct {
  259. name string
  260. args args
  261. wantErr bool
  262. }{
  263. {"ok", args{&ssh.Certificate{
  264. Nonce: []byte("0123456789"),
  265. Key: key,
  266. Serial: 123,
  267. CertType: ssh.HostCert,
  268. KeyId: "foo",
  269. ValidPrincipals: []string{"foo.internal"},
  270. ValidAfter: 1111,
  271. ValidBefore: 2222,
  272. Permissions: ssh.Permissions{},
  273. Reserved: []byte("reserved"),
  274. }, signer}, false},
  275. {"emptyNonce", args{&ssh.Certificate{
  276. Key: key,
  277. Serial: 123,
  278. CertType: ssh.UserCert,
  279. KeyId: "jane@doe.com",
  280. ValidPrincipals: []string{"jane"},
  281. ValidAfter: 1111,
  282. ValidBefore: 2222,
  283. Permissions: ssh.Permissions{},
  284. Reserved: []byte("reserved"),
  285. }, signer}, false},
  286. {"emptySerial", args{&ssh.Certificate{
  287. Nonce: []byte("0123456789"),
  288. Key: key,
  289. CertType: ssh.UserCert,
  290. KeyId: "jane@doe.com",
  291. ValidPrincipals: []string{"jane"},
  292. ValidAfter: 1111,
  293. ValidBefore: 2222,
  294. Permissions: ssh.Permissions{},
  295. Reserved: []byte("reserved"),
  296. }, signer}, false},
  297. }
  298. for _, tt := range tests {
  299. t.Run(tt.name, func(t *testing.T) {
  300. got, err := CreateCertificate(tt.args.cert, tt.args.signer)
  301. if (err != nil) != tt.wantErr {
  302. t.Errorf("CreateCertificate() error = %v, wantErr %v", err, tt.wantErr)
  303. return
  304. }
  305. if got != nil {
  306. switch {
  307. case len(got.Nonce) == 0:
  308. t.Errorf("CreateCertificate() nonce should not be empty")
  309. case got.Serial == 0:
  310. t.Errorf("CreateCertificate() serial should not be 0")
  311. case got.Signature == nil:
  312. t.Errorf("CreateCertificate() signature should not be nil")
  313. case !bytes.Equal(got.SignatureKey.Marshal(), tt.args.signer.PublicKey().Marshal()):
  314. t.Errorf("CreateCertificate() signature key is not the expected one")
  315. }
  316. signature := got.Signature
  317. got.Signature = nil
  318. data := got.Marshal()
  319. data = data[:len(data)-4]
  320. sig, err := signer.Sign(rand.Reader, data)
  321. if err != nil {
  322. t.Errorf("signer.Sign() error = %v", err)
  323. }
  324. // Verify signature
  325. got.Signature = signature
  326. if err := signer.PublicKey().Verify(data, got.Signature); err != nil {
  327. t.Errorf("CreateCertificate() signature verify error = %v", err)
  328. }
  329. // Verify data with public key in cert
  330. if err := got.Verify(data, sig); err != nil {
  331. t.Errorf("CreateCertificate() certificate verify error = %v", err)
  332. }
  333. }
  334. })
  335. }
  336. }