softkms_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. package softkms
  2. import (
  3. "context"
  4. "crypto"
  5. "crypto/ecdsa"
  6. "crypto/ed25519"
  7. "crypto/elliptic"
  8. "crypto/rand"
  9. "crypto/rsa"
  10. "crypto/x509"
  11. "encoding/pem"
  12. "fmt"
  13. "io/ioutil"
  14. "reflect"
  15. "testing"
  16. "github.com/smallstep/certificates/kms/apiv1"
  17. "go.step.sm/crypto/pemutil"
  18. )
  19. func TestNew(t *testing.T) {
  20. type args struct {
  21. ctx context.Context
  22. opts apiv1.Options
  23. }
  24. tests := []struct {
  25. name string
  26. args args
  27. want *SoftKMS
  28. wantErr bool
  29. }{
  30. {"ok", args{context.Background(), apiv1.Options{}}, &SoftKMS{}, false},
  31. }
  32. for _, tt := range tests {
  33. t.Run(tt.name, func(t *testing.T) {
  34. got, err := New(tt.args.ctx, tt.args.opts)
  35. if (err != nil) != tt.wantErr {
  36. t.Errorf("New() error = %v, wantErr %v", err, tt.wantErr)
  37. return
  38. }
  39. if !reflect.DeepEqual(got, tt.want) {
  40. t.Errorf("New() = %v, want %v", got, tt.want)
  41. }
  42. })
  43. }
  44. }
  45. func TestSoftKMS_Close(t *testing.T) {
  46. tests := []struct {
  47. name string
  48. wantErr bool
  49. }{
  50. {"ok", false},
  51. }
  52. for _, tt := range tests {
  53. t.Run(tt.name, func(t *testing.T) {
  54. k := &SoftKMS{}
  55. if err := k.Close(); (err != nil) != tt.wantErr {
  56. t.Errorf("SoftKMS.Close() error = %v, wantErr %v", err, tt.wantErr)
  57. }
  58. })
  59. }
  60. }
  61. func TestSoftKMS_CreateSigner(t *testing.T) {
  62. pk, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
  63. if err != nil {
  64. t.Fatal(err)
  65. }
  66. pemBlock, err := pemutil.Serialize(pk)
  67. if err != nil {
  68. t.Fatal(err)
  69. }
  70. pemBlockPassword, err := pemutil.Serialize(pk, pemutil.WithPassword([]byte("pass")))
  71. if err != nil {
  72. t.Fatal(err)
  73. }
  74. // Read and decode file using standard packages
  75. b, err := ioutil.ReadFile("testdata/priv.pem")
  76. if err != nil {
  77. t.Fatal(err)
  78. }
  79. block, _ := pem.Decode(b)
  80. block.Bytes, err = x509.DecryptPEMBlock(block, []byte("pass")) //nolint
  81. if err != nil {
  82. t.Fatal(err)
  83. }
  84. pk2, err := x509.ParseECPrivateKey(block.Bytes)
  85. if err != nil {
  86. t.Fatal(err)
  87. }
  88. // Create a public PEM
  89. b, err = x509.MarshalPKIXPublicKey(pk.Public())
  90. if err != nil {
  91. t.Fatal(err)
  92. }
  93. pub := pem.EncodeToMemory(&pem.Block{
  94. Type: "PUBLIC KEY",
  95. Bytes: b,
  96. })
  97. type args struct {
  98. req *apiv1.CreateSignerRequest
  99. }
  100. tests := []struct {
  101. name string
  102. args args
  103. want crypto.Signer
  104. wantErr bool
  105. }{
  106. {"signer", args{&apiv1.CreateSignerRequest{Signer: pk}}, pk, false},
  107. {"pem", args{&apiv1.CreateSignerRequest{SigningKeyPEM: pem.EncodeToMemory(pemBlock)}}, pk, false},
  108. {"pem password", args{&apiv1.CreateSignerRequest{SigningKeyPEM: pem.EncodeToMemory(pemBlockPassword), Password: []byte("pass")}}, pk, false},
  109. {"file", args{&apiv1.CreateSignerRequest{SigningKey: "testdata/priv.pem", Password: []byte("pass")}}, pk2, false},
  110. {"fail", args{&apiv1.CreateSignerRequest{}}, nil, true},
  111. {"fail bad pem", args{&apiv1.CreateSignerRequest{SigningKeyPEM: []byte("bad pem")}}, nil, true},
  112. {"fail bad password", args{&apiv1.CreateSignerRequest{SigningKey: "testdata/priv.pem", Password: []byte("bad-pass")}}, nil, true},
  113. {"fail not a signer", args{&apiv1.CreateSignerRequest{SigningKeyPEM: pub}}, nil, true},
  114. {"fail not a signer from file", args{&apiv1.CreateSignerRequest{SigningKey: "testdata/pub.pem"}}, nil, true},
  115. {"fail missing", args{&apiv1.CreateSignerRequest{SigningKey: "testdata/missing"}}, nil, true},
  116. }
  117. for _, tt := range tests {
  118. t.Run(tt.name, func(t *testing.T) {
  119. k := &SoftKMS{}
  120. got, err := k.CreateSigner(tt.args.req)
  121. if (err != nil) != tt.wantErr {
  122. t.Errorf("SoftKMS.CreateSigner() error = %v, wantErr %v", err, tt.wantErr)
  123. return
  124. }
  125. if !reflect.DeepEqual(got, tt.want) {
  126. t.Errorf("SoftKMS.CreateSigner() = %v, want %v", got, tt.want)
  127. }
  128. })
  129. }
  130. }
  131. func restoreGenerateKey() func() {
  132. oldGenerateKey := generateKey
  133. return func() {
  134. generateKey = oldGenerateKey
  135. }
  136. }
  137. func TestSoftKMS_CreateKey(t *testing.T) {
  138. fn := restoreGenerateKey()
  139. defer fn()
  140. p256, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
  141. if err != nil {
  142. t.Fatal(err)
  143. }
  144. rsa2048, err := rsa.GenerateKey(rand.Reader, 2048)
  145. if err != nil {
  146. t.Fatal(err)
  147. }
  148. edpub, edpriv, err := ed25519.GenerateKey(rand.Reader)
  149. if err != nil {
  150. t.Fatal(err)
  151. }
  152. type args struct {
  153. req *apiv1.CreateKeyRequest
  154. }
  155. type params struct {
  156. kty string
  157. crv string
  158. size int
  159. }
  160. tests := []struct {
  161. name string
  162. args args
  163. generateKey func() (interface{}, interface{}, error)
  164. want *apiv1.CreateKeyResponse
  165. wantParams params
  166. wantErr bool
  167. }{
  168. {"p256", args{&apiv1.CreateKeyRequest{Name: "p256", SignatureAlgorithm: apiv1.ECDSAWithSHA256}}, func() (interface{}, interface{}, error) {
  169. return p256.Public(), p256, nil
  170. }, &apiv1.CreateKeyResponse{Name: "p256", PublicKey: p256.Public(), PrivateKey: p256, CreateSignerRequest: apiv1.CreateSignerRequest{Signer: p256}}, params{"EC", "P-256", 0}, false},
  171. {"rsa", args{&apiv1.CreateKeyRequest{Name: "rsa3072", SignatureAlgorithm: apiv1.SHA256WithRSA}}, func() (interface{}, interface{}, error) {
  172. return rsa2048.Public(), rsa2048, nil
  173. }, &apiv1.CreateKeyResponse{Name: "rsa3072", PublicKey: rsa2048.Public(), PrivateKey: rsa2048, CreateSignerRequest: apiv1.CreateSignerRequest{Signer: rsa2048}}, params{"RSA", "", 0}, false},
  174. {"rsa2048", args{&apiv1.CreateKeyRequest{Name: "rsa2048", SignatureAlgorithm: apiv1.SHA256WithRSA, Bits: 2048}}, func() (interface{}, interface{}, error) {
  175. return rsa2048.Public(), rsa2048, nil
  176. }, &apiv1.CreateKeyResponse{Name: "rsa2048", PublicKey: rsa2048.Public(), PrivateKey: rsa2048, CreateSignerRequest: apiv1.CreateSignerRequest{Signer: rsa2048}}, params{"RSA", "", 2048}, false},
  177. {"rsaPSS2048", args{&apiv1.CreateKeyRequest{Name: "rsa2048", SignatureAlgorithm: apiv1.SHA256WithRSAPSS, Bits: 2048}}, func() (interface{}, interface{}, error) {
  178. return rsa2048.Public(), rsa2048, nil
  179. }, &apiv1.CreateKeyResponse{Name: "rsa2048", PublicKey: rsa2048.Public(), PrivateKey: rsa2048, CreateSignerRequest: apiv1.CreateSignerRequest{Signer: rsa2048}}, params{"RSA", "", 2048}, false},
  180. {"ed25519", args{&apiv1.CreateKeyRequest{Name: "ed25519", SignatureAlgorithm: apiv1.PureEd25519}}, func() (interface{}, interface{}, error) {
  181. return edpub, edpriv, nil
  182. }, &apiv1.CreateKeyResponse{Name: "ed25519", PublicKey: edpub, PrivateKey: edpriv, CreateSignerRequest: apiv1.CreateSignerRequest{Signer: edpriv}}, params{"OKP", "Ed25519", 0}, false},
  183. {"default", args{&apiv1.CreateKeyRequest{Name: "default"}}, func() (interface{}, interface{}, error) {
  184. return p256.Public(), p256, nil
  185. }, &apiv1.CreateKeyResponse{Name: "default", PublicKey: p256.Public(), PrivateKey: p256, CreateSignerRequest: apiv1.CreateSignerRequest{Signer: p256}}, params{"EC", "P-256", 0}, false},
  186. {"fail algorithm", args{&apiv1.CreateKeyRequest{Name: "fail", SignatureAlgorithm: apiv1.SignatureAlgorithm(100)}}, func() (interface{}, interface{}, error) {
  187. return p256.Public(), p256, nil
  188. }, nil, params{}, true},
  189. {"fail generate key", args{&apiv1.CreateKeyRequest{Name: "fail", SignatureAlgorithm: apiv1.ECDSAWithSHA256}}, func() (interface{}, interface{}, error) {
  190. return nil, nil, fmt.Errorf("an error")
  191. }, nil, params{"EC", "P-256", 0}, true},
  192. {"fail no signer", args{&apiv1.CreateKeyRequest{Name: "fail", SignatureAlgorithm: apiv1.ECDSAWithSHA256}}, func() (interface{}, interface{}, error) {
  193. return 1, 2, nil
  194. }, nil, params{"EC", "P-256", 0}, true},
  195. }
  196. for _, tt := range tests {
  197. t.Run(tt.name, func(t *testing.T) {
  198. k := &SoftKMS{}
  199. generateKey = func(kty, crv string, size int) (interface{}, interface{}, error) {
  200. if tt.wantParams.kty != kty {
  201. t.Errorf("GenerateKey() kty = %s, want %s", kty, tt.wantParams.kty)
  202. }
  203. if tt.wantParams.crv != crv {
  204. t.Errorf("GenerateKey() crv = %s, want %s", crv, tt.wantParams.crv)
  205. }
  206. if tt.wantParams.size != size {
  207. t.Errorf("GenerateKey() size = %d, want %d", size, tt.wantParams.size)
  208. }
  209. return tt.generateKey()
  210. }
  211. got, err := k.CreateKey(tt.args.req)
  212. if (err != nil) != tt.wantErr {
  213. t.Errorf("SoftKMS.CreateKey() error = %v, wantErr %v", err, tt.wantErr)
  214. return
  215. }
  216. if !reflect.DeepEqual(got, tt.want) {
  217. t.Errorf("SoftKMS.CreateKey() = %v, want %v", got, tt.want)
  218. }
  219. })
  220. }
  221. }
  222. func TestSoftKMS_GetPublicKey(t *testing.T) {
  223. b, err := ioutil.ReadFile("testdata/pub.pem")
  224. if err != nil {
  225. t.Fatal(err)
  226. }
  227. block, _ := pem.Decode(b)
  228. pub, err := x509.ParsePKIXPublicKey(block.Bytes)
  229. if err != nil {
  230. t.Fatal(err)
  231. }
  232. type args struct {
  233. req *apiv1.GetPublicKeyRequest
  234. }
  235. tests := []struct {
  236. name string
  237. args args
  238. want crypto.PublicKey
  239. wantErr bool
  240. }{
  241. {"key", args{&apiv1.GetPublicKeyRequest{Name: "testdata/pub.pem"}}, pub, false},
  242. {"cert", args{&apiv1.GetPublicKeyRequest{Name: "testdata/cert.crt"}}, pub, false},
  243. {"fail not exists", args{&apiv1.GetPublicKeyRequest{Name: "testdata/missing"}}, nil, true},
  244. {"fail type", args{&apiv1.GetPublicKeyRequest{Name: "testdata/cert.key"}}, nil, true},
  245. }
  246. for _, tt := range tests {
  247. t.Run(tt.name, func(t *testing.T) {
  248. k := &SoftKMS{}
  249. got, err := k.GetPublicKey(tt.args.req)
  250. if (err != nil) != tt.wantErr {
  251. t.Errorf("SoftKMS.GetPublicKey() error = %v, wantErr %v", err, tt.wantErr)
  252. return
  253. }
  254. if !reflect.DeepEqual(got, tt.want) {
  255. t.Errorf("SoftKMS.GetPublicKey() = %v, want %v", got, tt.want)
  256. }
  257. })
  258. }
  259. }
  260. func Test_generateKey(t *testing.T) {
  261. type args struct {
  262. kty string
  263. crv string
  264. size int
  265. }
  266. tests := []struct {
  267. name string
  268. args args
  269. wantType interface{}
  270. wantType1 interface{}
  271. wantErr bool
  272. }{
  273. {"rsa2048", args{"RSA", "", 0}, &rsa.PublicKey{}, &rsa.PrivateKey{}, false},
  274. {"rsa2048", args{"RSA", "", 2048}, &rsa.PublicKey{}, &rsa.PrivateKey{}, false},
  275. {"p256", args{"EC", "P-256", 0}, &ecdsa.PublicKey{}, &ecdsa.PrivateKey{}, false},
  276. {"ed25519", args{"OKP", "Ed25519", 0}, ed25519.PublicKey{}, ed25519.PrivateKey{}, false},
  277. {"fail kty", args{"FOO", "", 0}, nil, nil, true},
  278. {"fail crv", args{"EC", "P-123", 0}, nil, nil, true},
  279. {"fail size", args{"RSA", "", 1}, nil, nil, true},
  280. }
  281. for _, tt := range tests {
  282. t.Run(tt.name, func(t *testing.T) {
  283. got, got1, err := generateKey(tt.args.kty, tt.args.crv, tt.args.size)
  284. if (err != nil) != tt.wantErr {
  285. t.Errorf("generateKey() error = %v, wantErr %v", err, tt.wantErr)
  286. return
  287. }
  288. if reflect.TypeOf(got) != reflect.TypeOf(tt.wantType) {
  289. t.Errorf("generateKey() got = %T, want %T", got, tt.wantType)
  290. }
  291. if reflect.TypeOf(got1) != reflect.TypeOf(tt.wantType1) {
  292. t.Errorf("generateKey() got1 = %T, want %T", got1, tt.wantType1)
  293. }
  294. })
  295. }
  296. }