signer_test.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package awskms
  2. import (
  3. "crypto"
  4. "crypto/ecdsa"
  5. "crypto/rand"
  6. "crypto/rsa"
  7. "fmt"
  8. "io"
  9. "reflect"
  10. "testing"
  11. "github.com/aws/aws-sdk-go/aws"
  12. "github.com/aws/aws-sdk-go/aws/request"
  13. "github.com/aws/aws-sdk-go/service/kms"
  14. "go.step.sm/crypto/pemutil"
  15. )
  16. func TestNewSigner(t *testing.T) {
  17. okClient := getOKClient()
  18. key, err := pemutil.ParseKey([]byte(publicKey))
  19. if err != nil {
  20. t.Fatal(err)
  21. }
  22. type args struct {
  23. svc KeyManagementClient
  24. signingKey string
  25. }
  26. tests := []struct {
  27. name string
  28. args args
  29. want *Signer
  30. wantErr bool
  31. }{
  32. {"ok", args{okClient, "awskms:key-id=be468355-ca7a-40d9-a28b-8ae1c4c7f936"}, &Signer{
  33. service: okClient,
  34. keyID: "be468355-ca7a-40d9-a28b-8ae1c4c7f936",
  35. publicKey: key,
  36. }, false},
  37. {"fail parse", args{okClient, "awskms:key-id="}, nil, true},
  38. {"fail preload", args{&MockClient{
  39. getPublicKeyWithContext: func(ctx aws.Context, input *kms.GetPublicKeyInput, opts ...request.Option) (*kms.GetPublicKeyOutput, error) {
  40. return nil, fmt.Errorf("an error")
  41. },
  42. }, "awskms:key-id=be468355-ca7a-40d9-a28b-8ae1c4c7f936"}, nil, true},
  43. {"fail preload not der", args{&MockClient{
  44. getPublicKeyWithContext: func(ctx aws.Context, input *kms.GetPublicKeyInput, opts ...request.Option) (*kms.GetPublicKeyOutput, error) {
  45. return &kms.GetPublicKeyOutput{
  46. KeyId: input.KeyId,
  47. PublicKey: []byte(publicKey),
  48. }, nil
  49. },
  50. }, "awskms:key-id=be468355-ca7a-40d9-a28b-8ae1c4c7f936"}, nil, true},
  51. }
  52. for _, tt := range tests {
  53. t.Run(tt.name, func(t *testing.T) {
  54. got, err := NewSigner(tt.args.svc, tt.args.signingKey)
  55. if (err != nil) != tt.wantErr {
  56. t.Errorf("NewSigner() error = %v, wantErr %v", err, tt.wantErr)
  57. return
  58. }
  59. if !reflect.DeepEqual(got, tt.want) {
  60. t.Errorf("NewSigner() = %v, want %v", got, tt.want)
  61. }
  62. })
  63. }
  64. }
  65. func TestSigner_Public(t *testing.T) {
  66. okClient := getOKClient()
  67. key, err := pemutil.ParseKey([]byte(publicKey))
  68. if err != nil {
  69. t.Fatal(err)
  70. }
  71. type fields struct {
  72. service KeyManagementClient
  73. keyID string
  74. publicKey crypto.PublicKey
  75. }
  76. tests := []struct {
  77. name string
  78. fields fields
  79. want crypto.PublicKey
  80. }{
  81. {"ok", fields{okClient, "be468355-ca7a-40d9-a28b-8ae1c4c7f936", key}, key},
  82. }
  83. for _, tt := range tests {
  84. t.Run(tt.name, func(t *testing.T) {
  85. s := &Signer{
  86. service: tt.fields.service,
  87. keyID: tt.fields.keyID,
  88. publicKey: tt.fields.publicKey,
  89. }
  90. if got := s.Public(); !reflect.DeepEqual(got, tt.want) {
  91. t.Errorf("Signer.Public() = %v, want %v", got, tt.want)
  92. }
  93. })
  94. }
  95. }
  96. func TestSigner_Sign(t *testing.T) {
  97. okClient := getOKClient()
  98. key, err := pemutil.ParseKey([]byte(publicKey))
  99. if err != nil {
  100. t.Fatal(err)
  101. }
  102. type fields struct {
  103. service KeyManagementClient
  104. keyID string
  105. publicKey crypto.PublicKey
  106. }
  107. type args struct {
  108. rand io.Reader
  109. digest []byte
  110. opts crypto.SignerOpts
  111. }
  112. tests := []struct {
  113. name string
  114. fields fields
  115. args args
  116. want []byte
  117. wantErr bool
  118. }{
  119. {"ok", fields{okClient, "be468355-ca7a-40d9-a28b-8ae1c4c7f936", key}, args{rand.Reader, []byte("digest"), crypto.SHA256}, signature, false},
  120. {"fail alg", fields{okClient, "be468355-ca7a-40d9-a28b-8ae1c4c7f936", key}, args{rand.Reader, []byte("digest"), crypto.MD5}, nil, true},
  121. {"fail key", fields{okClient, "be468355-ca7a-40d9-a28b-8ae1c4c7f936", []byte("key")}, args{rand.Reader, []byte("digest"), crypto.SHA256}, nil, true},
  122. {"fail sign", fields{&MockClient{
  123. signWithContext: func(ctx aws.Context, input *kms.SignInput, opts ...request.Option) (*kms.SignOutput, error) {
  124. return nil, fmt.Errorf("an error")
  125. },
  126. }, "be468355-ca7a-40d9-a28b-8ae1c4c7f936", key}, args{rand.Reader, []byte("digest"), crypto.SHA256}, nil, true},
  127. }
  128. for _, tt := range tests {
  129. t.Run(tt.name, func(t *testing.T) {
  130. s := &Signer{
  131. service: tt.fields.service,
  132. keyID: tt.fields.keyID,
  133. publicKey: tt.fields.publicKey,
  134. }
  135. got, err := s.Sign(tt.args.rand, tt.args.digest, tt.args.opts)
  136. if (err != nil) != tt.wantErr {
  137. t.Errorf("Signer.Sign() error = %v, wantErr %v", err, tt.wantErr)
  138. return
  139. }
  140. if !reflect.DeepEqual(got, tt.want) {
  141. t.Errorf("Signer.Sign() = %v, want %v", got, tt.want)
  142. }
  143. })
  144. }
  145. }
  146. func Test_getSigningAlgorithm(t *testing.T) {
  147. type args struct {
  148. key crypto.PublicKey
  149. opts crypto.SignerOpts
  150. }
  151. tests := []struct {
  152. name string
  153. args args
  154. want string
  155. wantErr bool
  156. }{
  157. {"rsa+sha256", args{&rsa.PublicKey{}, crypto.SHA256}, "RSASSA_PKCS1_V1_5_SHA_256", false},
  158. {"rsa+sha384", args{&rsa.PublicKey{}, crypto.SHA384}, "RSASSA_PKCS1_V1_5_SHA_384", false},
  159. {"rsa+sha512", args{&rsa.PublicKey{}, crypto.SHA512}, "RSASSA_PKCS1_V1_5_SHA_512", false},
  160. {"pssrsa+sha256", args{&rsa.PublicKey{}, &rsa.PSSOptions{Hash: crypto.SHA256.HashFunc()}}, "RSASSA_PSS_SHA_256", false},
  161. {"pssrsa+sha384", args{&rsa.PublicKey{}, &rsa.PSSOptions{Hash: crypto.SHA384.HashFunc()}}, "RSASSA_PSS_SHA_384", false},
  162. {"pssrsa+sha512", args{&rsa.PublicKey{}, &rsa.PSSOptions{Hash: crypto.SHA512.HashFunc()}}, "RSASSA_PSS_SHA_512", false},
  163. {"P256", args{&ecdsa.PublicKey{}, crypto.SHA256}, "ECDSA_SHA_256", false},
  164. {"P384", args{&ecdsa.PublicKey{}, crypto.SHA384}, "ECDSA_SHA_384", false},
  165. {"P521", args{&ecdsa.PublicKey{}, crypto.SHA512}, "ECDSA_SHA_512", false},
  166. {"fail type", args{[]byte("key"), crypto.SHA256}, "", true},
  167. {"fail rsa alg", args{&rsa.PublicKey{}, crypto.MD5}, "", true},
  168. {"fail ecdsa alg", args{&ecdsa.PublicKey{}, crypto.MD5}, "", true},
  169. }
  170. for _, tt := range tests {
  171. t.Run(tt.name, func(t *testing.T) {
  172. got, err := getSigningAlgorithm(tt.args.key, tt.args.opts)
  173. if (err != nil) != tt.wantErr {
  174. t.Errorf("getSigningAlgorithm() error = %v, wantErr %v", err, tt.wantErr)
  175. return
  176. }
  177. if got != tt.want {
  178. t.Errorf("getSigningAlgorithm() = %v, want %v", got, tt.want)
  179. }
  180. })
  181. }
  182. }