Skip to content

Commit e22657f

Browse files
committed
FEAT: added certificate's fingerprint calculation
1 parent d19b885 commit e22657f

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

src/mezz/codec-crt.reb

+38-7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,21 @@ register-codec [
1010
type: 'cryptography
1111
title: "Internet X.509 Public Key Infrastructure Certificate and Certificate Revocation List (CRL) Profile"
1212
suffixes: [%.crt]
13+
14+
get-fingerprint: function[data [binary!] method [word!]][
15+
bin: binary data
16+
loop 2 [
17+
binary/read bin [
18+
flags: UI8
19+
length: LENGTH
20+
]
21+
if any [
22+
flags <> 48 ; 0x30 = class universal, constructed, SEQUENCE
23+
length > length? bin/buffer
24+
][ return none ]
25+
]
26+
checksum/part at data 5 'sha256 probe (length + 4)
27+
]
1328
decode: wrap [
1429
*oid:
1530
*val:
@@ -99,7 +114,7 @@ register-codec [
99114

100115
func [
101116
data [binary! block!]
102-
/local pkix version serialNumber issuer subject validity
117+
/local pkix version serialNumber issuer subject validity der
103118
][
104119
try [all [
105120
; as there seems to be no standard, the *.crt file
@@ -108,12 +123,15 @@ register-codec [
108123
pkix/label = "CERTIFICATE"
109124
data: pkix/binary
110125
]]
111-
if binary? data [ data: der-codec/decode data ]
126+
127+
der: either binary? data [
128+
der-codec/decode data
129+
][ data ]
112130
if all [
113-
2 = length? data
114-
'SEQUENCE = data/1
115-
block? data/2
116-
] [data: data/2]
131+
2 = length? der
132+
'SEQUENCE = der/1
133+
block? der/2
134+
] [der: der/2]
117135

118136
result: object [
119137
version:
@@ -127,10 +145,11 @@ register-codec [
127145
issuer-id:
128146
subject-id:
129147
extensions:
148+
fingerprint:
130149
none
131150
]
132151

133-
parse data [
152+
parse der [
134153
'SEQUENCE into [
135154
;-- version:
136155
'CS0 into [
@@ -199,6 +218,18 @@ register-codec [
199218
print dump-obj result
200219
]
201220
]
221+
if all [
222+
binary? data
223+
hash: select [
224+
sha256WithRSAEncryption sha256
225+
sha384WithRSAEncryption sha384
226+
sha512WithRSAEncryption sha512
227+
md5withRSAEncryption md5
228+
md4withRSAEncryption md4
229+
] result/signature/1
230+
][
231+
try [result/fingerprint: get-fingerprint :data :hash]
232+
]
202233
result
203234
]
204235
]

src/mezz/codec-der.reb

+2
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,8 @@ register-codec [
247247
| #"^(04)" (name: 'md5withRSAEncryption)
248248
| #"^(05)" (name: 'sha1WithRSAEncrption)
249249
| #"^(0B)" (name: 'sha256WithRSAEncryption)
250+
| #"^(0C)" (name: 'sha384WithRSAEncryption)
251+
| #"^(0D)" (name: 'sha512WithRSAEncryption)
250252

251253
] end
252254
|

0 commit comments

Comments
 (0)