Skip to content

Commit 2507de0

Browse files
authored
fix sigv4a signing issue with derive_asymmetric_key (#3129)
1 parent 7fd998d commit 2507de0

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

gems/aws-sigv4/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
Unreleased Changes
22
------------------
33

4+
* Issue - Fix sigv4a signing issue with derive_asymmetric_key for certain credentials.
5+
46
1.10.0 (2024-09-17)
57
------------------
68

gems/aws-sigv4/lib/aws-sigv4/asymmetric_credentials.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,24 @@ def self.be_bytes_to_num(bytes)
6060
x
6161
end
6262

63+
# @return [Array] value of the BigNumber as a big-endian unsigned byte array.
64+
def self.bn_to_be_bytes(bn)
65+
bytes = []
66+
while bn > 0
67+
bytes << (bn & 0xff)
68+
bn = bn >> 8
69+
end
70+
bytes.reverse
71+
end
72+
6373
# Prior to openssl3 we could directly set public and private key on EC
6474
# However, openssl3 deprecated those methods and we must now construct
6575
# a der with the keys and load the EC from it.
6676
def self.generate_ec(public_key, d)
6777
# format reversed from: OpenSSL::ASN1.decode_all(OpenSSL::PKey::EC.new.to_der)
6878
asn1 = OpenSSL::ASN1::Sequence([
6979
OpenSSL::ASN1::Integer(OpenSSL::BN.new(1)),
70-
OpenSSL::ASN1::OctetString([d.to_s(16)].pack('H*')),
80+
OpenSSL::ASN1::OctetString(bn_to_be_bytes(d).pack('C*')),
7181
OpenSSL::ASN1::ASN1Data.new([OpenSSL::ASN1::ObjectId("prime256v1")], 0, :CONTEXT_SPECIFIC),
7282
OpenSSL::ASN1::ASN1Data.new(
7383
[OpenSSL::ASN1::BitString(public_key.to_octet_string(:uncompressed))],

gems/aws-sigv4/spec/asymmetric_credentials_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,19 @@ module Sigv4
3838
expect(extra[:pk_y]).to be_a(Integer)
3939
expect(extra[:pk_y]).to eq 60777455846638291266199385583357715250110920888403467466325436560561456866584
4040
end
41+
42+
# ensure that encoding of private keys with MSB set result in valid EC objects
43+
context 'private key with most significant bit set' do
44+
45+
let(:access_key_id) { 'ASIAZRFOHJT45NGNWXS3' }
46+
let(:secret_access_key) { 'WOuDKprKr+rt3Dl7+RCiNpZGzi3Jw/DdVifyifuC' }
47+
let(:test_value) { 'test_value' }
48+
49+
it 'derives a valid EC PKey' do
50+
signature = ec.dsa_sign_asn1(test_value)
51+
expect(ec.dsa_verify_asn1(test_value, signature)).to be_truthy
52+
end
53+
end
4154
end
4255
end
4356
end

0 commit comments

Comments
 (0)