Skip to content

Commit f21b07a

Browse files
committed
add overlooked base64 encode to sign request calls
1 parent 1dfc9e6 commit f21b07a

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

lib/fog/aws/requests/kms/sign.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@ class Real
77
# Sign
88
#
99
# ==== Parameters
10+
# * identifier<~String>: id, arn, alias name, or alias arn for key to sign with
11+
# * message<~String>: base64 encoded message to sign
1012
#
1113
# === Returns
14+
# * response<~Excon::Response>:
1215
#
1316
# ==== See Also
1417
# https://docs.aws.amazon.com/kms/latest/APIReference/API_Sign.html
18+
#
1519
def sign(identifier, message, algorithm, options = {})
1620
request({
1721
'Action' => 'Sign',
@@ -32,15 +36,17 @@ def sign(identifier, message, algorithm, options = {})
3236
raise(Excon::Errors.status_error({ expects: 200 }, response))
3337
end
3438

39+
data = Base64.decode64(message)
40+
3541
# FIXME: SM2 support?
3642
sha = "SHA#{algorithm.split('_SHA_').last}"
3743
signopts = {}
3844
signopts[:rsa_padding_mode] = 'pss' if algorithm.start_with?('RSASSA_PSS')
3945

4046
signature = if options['MessageType'] == 'DIGEST'
41-
pkey.sign_raw(sha, message, signopts)
47+
pkey.sign_raw(sha, data, signopts)
4248
else
43-
pkey.sign(sha, message, signopts)
49+
pkey.sign(sha, data, signopts)
4450
end
4551

4652
response.body = {

tests/requests/kms/key_tests.rb

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,12 @@
4545
end
4646

4747
tests('#sign') do
48-
sign_response = Fog::AWS[:kms].sign(key_id, data, 'RSASSA_PKCS1_V1_5_SHA_256', 'MessageType' => 'RAW').body
48+
sign_response = Fog::AWS[:kms].sign(
49+
key_id,
50+
Base64.encode64(data),
51+
'RSASSA_PKCS1_V1_5_SHA_256',
52+
'MessageType' => 'RAW'
53+
).body
4954

5055
tests('format').data_matches_schema(AWS::KMS::Formats::SIGN) { sign_response }
5156

@@ -85,14 +90,24 @@
8590

8691
tests("#sign #{key_spec} #{signing_algorithm} DIGEST").returns(true) do
8792
hash = OpenSSL::Digest.digest(sha, data)
88-
sign_response = Fog::AWS[:kms].sign(key_id, hash, signing_algorithm, 'MessageType' => 'DIGEST').body
93+
sign_response = Fog::AWS[:kms].sign(
94+
key_id,
95+
Base64.encode64(hash),
96+
signing_algorithm,
97+
'MessageType' => 'DIGEST'
98+
).body
8999
signature = Base64.decode64(sign_response['Signature'])
90100

91101
pkey.verify_raw(sha, signature, hash, sign_opts)
92102
end
93103

94104
tests("#sign #{key_spec} #{signing_algorithm} RAW").returns(true) do
95-
sign_response = Fog::AWS[:kms].sign(key_id, data, signing_algorithm, 'MessageType' => 'RAW').body
105+
sign_response = Fog::AWS[:kms].sign(
106+
key_id,
107+
Base64.encode64(data),
108+
signing_algorithm,
109+
'MessageType' => 'RAW'
110+
).body
96111
signature = Base64.decode64(sign_response['Signature'])
97112

98113
pkey.verify(sha, signature, data, sign_opts)

0 commit comments

Comments
 (0)