certificate_test.go 10 KB

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