-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathrsa.rb
148 lines (127 loc) · 3.54 KB
/
rsa.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# frozen_string_literal: true
require "cose/key/base"
require "openssl"
module COSE
module Key
class RSA < Base
LABEL_N = -1
LABEL_E = -2
LABEL_D = -3
LABEL_P = -4
LABEL_Q = -5
LABEL_DP = -6
LABEL_DQ = -7
LABEL_QINV = -8
KTY_RSA = 3
def self.enforce_type(map)
if map[LABEL_KTY] != KTY_RSA
raise "Not an RSA key"
end
end
def self.from_pkey(pkey)
params = pkey.params
attributes = {
n: params["n"].to_s(2),
e: params["e"].to_s(2)
}
if pkey.private?
attributes.merge!(
d: params["d"].to_s(2),
p: params["p"].to_s(2),
q: params["q"].to_s(2),
dp: params["dmp1"].to_s(2),
dq: params["dmq1"].to_s(2),
qinv: params["iqmp"].to_s(2)
)
end
new(**attributes)
end
attr_reader :n, :e, :d, :p, :q, :dp, :dq, :qinv
def initialize(n:, e:, d: nil, p: nil, q: nil, dp: nil, dq: nil, qinv: nil, **keyword_arguments) # rubocop:disable Naming/MethodParameterName
super(**keyword_arguments)
if !n
raise ArgumentError, "Required public field n is missing"
elsif !e
raise ArgumentError, "Required public field e is missing"
else
private_fields = { d: d, p: p, q: q, dp: dp, dq: dq, qinv: qinv }
if private_fields.values.all?(&:nil?) || private_fields.values.none?(&:nil?)
@n = n
@e = e
@d = d
@p = p
@q = q
@dp = dp
@dq = dq
@qinv = qinv
else
missing = private_fields.detect { |_k, v| v.nil? }[0]
raise ArgumentError, "Incomplete private fields, #{missing} is missing"
end
end
end
def map
super.merge(
Base::LABEL_KTY => KTY_RSA,
LABEL_N => n,
LABEL_E => e,
LABEL_D => d,
LABEL_P => p,
LABEL_Q => q,
LABEL_DP => dp,
LABEL_DQ => dq,
LABEL_QINV => qinv
).compact
end
def to_pkey
# PKCS#1 RSAPublicKey
asn1 = OpenSSL::ASN1::Sequence(
[
OpenSSL::ASN1::Integer.new(bn(n)),
OpenSSL::ASN1::Integer.new(bn(e)),
]
)
pkey = OpenSSL::PKey::RSA.new(asn1.to_der)
if private?
# PKCS#1 RSAPrivateKey
asn1 = OpenSSL::ASN1::Sequence(
[
OpenSSL::ASN1::Integer.new(0),
OpenSSL::ASN1::Integer.new(bn(n)),
OpenSSL::ASN1::Integer.new(bn(e)),
OpenSSL::ASN1::Integer.new(bn(d)),
OpenSSL::ASN1::Integer.new(bn(p)),
OpenSSL::ASN1::Integer.new(bn(q)),
OpenSSL::ASN1::Integer.new(bn(dp)),
OpenSSL::ASN1::Integer.new(bn(dq)),
OpenSSL::ASN1::Integer.new(bn(qinv)),
]
)
pkey = OpenSSL::PKey::RSA.new(asn1.to_der)
end
pkey
end
def self.keyword_arguments_for_initialize(map)
{
n: map[LABEL_N],
e: map[LABEL_E],
d: map[LABEL_D],
p: map[LABEL_P],
q: map[LABEL_Q],
dp: map[LABEL_DP],
dq: map[LABEL_DQ],
qinv: map[LABEL_QINV]
}
end
private
def private?
[d, p, q, dp, dq, qinv].none?(&:nil?)
end
def bn(data)
if data
OpenSSL::BN.new(data, 2)
end
end
end
end
end