@@ -23,9 +23,37 @@ wrap [
23
23
return rsa-init n e
24
24
]
25
25
]
26
- print ["Not RSA key! (" v ")" ]
26
+ sys /log/error 'REBOL ["Not RSA key! (" v ")" ]
27
27
none
28
28
]
29
+ init-rsa-public-key : function [ data [block! ]] [
30
+ parse data [
31
+ 'SEQUENCE into [
32
+ 'INTEGER set n: binary! ;modulus
33
+ 'INTEGER set e: binary! ;publicExponent
34
+ ]
35
+ ]
36
+ rsa-init n e
37
+ ]
38
+ init-rsa-private-key : function [ data [block! ]] [
39
+ parse data [
40
+ 'SEQUENCE into [
41
+ 'INTEGER set v: binary! ;version
42
+ 'INTEGER set n: binary! ;modulus
43
+ 'INTEGER set e: binary! ;publicExponent
44
+ 'INTEGER set d: binary! ;privateExponent
45
+ 'INTEGER set p: binary! ;prime1
46
+ 'INTEGER set q: binary! ;prime2
47
+ ;- dp, dq and qp are computed when initialized, so not used here
48
+ ;'INTEGER set dp: binary! ;exponent1 d mod (p-1)
49
+ ;'INTEGER set dq: binary! ;exponent2 d mod (q-1)
50
+ ;'INTEGER set qp: binary! ;coefficient (inverse of q) mod p
51
+ to end
52
+ ]
53
+ to end
54
+ ]
55
+ rsa-init/private n e d p q
56
+ ]
29
57
30
58
register-codec [
31
59
name: 'ssh-key
@@ -52,22 +80,21 @@ wrap [
52
80
][ init-from-ssh2-key key ]
53
81
]
54
82
if "4,ENCRYPTED" = select pkix/header "Proc-Type" [
55
- print "ENCRYPTED key!"
83
+ sysl /log/info 'REBOL "ENCRYPTED key!"
56
84
try /except [
57
85
dek-info: select pkix/header "DEK-Info"
58
- ;probe dek-info
86
+ sysl /log/info 'REBOL [ "Using:" dek-info]
59
87
parse dek-info [
60
88
"AES-128-CBC" #"," copy iv to end
61
89
]
62
90
iv: debase iv 16
63
91
unless p [p: ask/hide "Pasword: " ]
64
- p: checksum
65
- join to binary! p copy/part iv 8
66
- 'md5
92
+ p: checksum (join to binary! p copy/part iv 8 ) 'md5
67
93
d: aes/key/decrypt p iv
68
94
pkix/binary: aes/stream d pkix/binary
69
95
][ return none ]
70
96
]
97
+ sys/log/info 'REBOL ["Trying to resolve:" pkix/label ]
71
98
72
99
switch pkix/label [
73
100
"SSH2 PUBLIC KEY" [
@@ -78,66 +105,43 @@ wrap [
78
105
try /except [
79
106
data: codecs/der/decode pkix/binary
80
107
][
81
- print "Failed to decode DER day for RSA key!"
82
- probe system/state/last-error
108
+ sys /log/error 'REBOL "Failed to decode DER day for RSA key!"
109
+ sys /log/error 'REBOL system/state/last-error
83
110
return none
84
111
]
85
112
86
113
switch pkix/label [
87
- "PUBLIC KEY" [
88
- ; resolve RSA public data from the DER structure (PKCS#1)
89
- all [
114
+ "PUBLIC KEY"
115
+ "PRIVATE KEY" [
116
+ ; resolve key data from the DER structure (PKCS#1)
117
+ return attempt [
90
118
parse data [
91
119
'SEQUENCE into [
92
- 'SEQUENCE set v: block! ; AlgorithmIdentifier
93
- 'BIT_STRING set data: binary! ; PublicKey
120
+ opt ['INTEGER binary! ]
121
+ 'SEQUENCE into [
122
+ 'OBJECT_IDENTIFIER set oid: binary! ; AlgorithmIdentifier
123
+ to end ;'NULL binary!
124
+ ]
125
+ ['BIT_STRING | 'OCTET_STRING] set data: binary! ; PublicKey
94
126
(
95
127
data: codecs/der/decode data
96
128
)
97
129
]
98
130
]
99
- v /OBJECT_IDENTIFIER = #{ 2A864886F70D010101 } ;= rsaEncryption
100
- parse data [
101
- 'SEQUENCE into [
102
- 'INTEGER set n: binary! ;modulus
103
- 'INTEGER set e: binary! ;publicExponent
131
+ switch /default oid [
132
+ #{ 2A864886F70D010101 } [ ;= rsaEncryption
133
+ return either pkix /label = "PUBLIC KEY" [
134
+ init-rsa-public-key data
135
+ ][ init-rsa-private-key data ]
104
136
]
137
+ ][
138
+ sys/log/error 'REBOL ["Unknown key type:" codecs/der/decode-OID oid]
139
+ none
105
140
]
106
141
]
107
- ; resolve RSA handle from parsed data
108
- return rsa-init n e
109
- ]
110
- "RSA PUBLIC KEY" [
111
- ; resolve RSA public data from the DER structure (PKCS#1)
112
- parse data [
113
- 'SEQUENCE into [
114
- 'INTEGER set n: binary! ;modulus
115
- 'INTEGER set e: binary! ;publicExponent
116
- ]
117
- ]
118
- ; resolve RSA handle from parsed data
119
- return rsa-init n e
120
- ]
121
- "RSA PRIVATE KEY" [
122
- ; resolve RSA private data from the DER structure (PKCS#1)
123
- parse data [
124
- 'SEQUENCE into [
125
- 'INTEGER set v: binary! ;version
126
- 'INTEGER set n: binary! ;modulus
127
- 'INTEGER set e: binary! ;publicExponent
128
- 'INTEGER set d: binary! ;privateExponent
129
- 'INTEGER set p: binary! ;prime1
130
- 'INTEGER set q: binary! ;prime2
131
- 'INTEGER set dp: binary! ;exponent1 d mod (p-1)
132
- 'INTEGER set dq: binary! ;exponent2 d mod (q-1)
133
- 'INTEGER set inv: binary! ;coefficient (inverse of q) mod p
134
- to end
135
- ]
136
- to end
137
- ]
138
- ; resolve RSA handle from parsed data
139
- return rsa-init/private n e d p q dp dq inv
140
142
]
143
+ "RSA PUBLIC KEY" [ return init-rsa-public-key data ]
144
+ "RSA PRIVATE KEY" [ return init-rsa-private-key data ]
141
145
]
142
146
none ; no success!
143
147
]
0 commit comments