Source file conex_openssl.ml
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
module B64 = struct
let default_alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
let padding = '='
let of_char ?(alphabet=default_alphabet) may x =
if may && x = padding then 0 else String.index alphabet x
let decode ?alphabet input =
let length = String.length input in
let input =
if length mod 4 = 0 then input
else input ^ (String.make (4 - length mod 4) padding)
in
let words = length / 4 in
let padding =
match length with
| 0 -> 0
| _ when input.[length - 2] = padding -> 2
| _ when input.[length - 1] = padding -> 1
| _ -> 0
in
let output = Bytes.make (words * 3 - padding) '\000' in
let may_pad i idx = i = words - 1 && idx >= padding in
for i = 0 to words - 1 do
let a = of_char ?alphabet (may_pad i 0) (String.get input (4 * i + 0))
and b = of_char ?alphabet (may_pad i 1) (String.get input (4 * i + 1))
and c = of_char ?alphabet (may_pad i 2) (String.get input (4 * i + 2))
and d = of_char ?alphabet (may_pad i 3) (String.get input (4 * i + 3)) in
let n = (a lsl 18) lor (b lsl 12) lor (c lsl 6) lor d in
let x = (n lsr 16) land 255
and y = (n lsr 8) land 255
and z = n land 255 in
Bytes.set output (3 * i + 0) (char_of_int x);
if i <> words - 1 || padding < 2 then
Bytes.set output (3 * i + 1) (char_of_int y);
if i <> words - 1 || padding < 1 then
Bytes.set output (3 * i + 2) (char_of_int z);
done;
Bytes.unsafe_to_string output
end
open Conex_utils
let ( let* ) = Result.bind
module V = struct
let check_version () =
let cmd = "openssl version" in
let input = Unix.open_process_in cmd in
let output = input_line input in
let _ = Unix.close_process_in input in
if
String.is_prefix ~prefix:"OpenSSL 0." output ||
String.is_prefix ~prefix:"OpenSSL 1." output ||
String.is_prefix ~prefix:"OpenSSL 2." output ||
String.is_prefix ~prefix:"OpenSSL 3.0.0" output
then
Error ("need at least OpenSSL 3.0.1, found: " ^ output)
else
Ok ()
let verify_rsa_pss ~key ~data ~signature id =
let* signature =
try Ok (B64.decode signature) with _ -> Error (`InvalidBase64Encoding id)
in
Result.map_error (function
| "broken" -> `InvalidSignature id
| _ -> `InvalidPublicKey id)
(let filename = Filename.temp_file "conex" "sig" in
let* () = Conex_unix_persistency.write_replace (filename ^ ".key") key in
let* () = Conex_unix_persistency.write_replace (filename ^ ".txt") data in
let* () = Conex_unix_persistency.write_replace (filename ^ ".sig") signature in
let cmd = Printf.sprintf "openssl dgst -sha256 -verify %s.key -sigopt rsa_padding_mode:pss -signature %s.sig %s.txt > /dev/null" filename filename filename in
let res = if 0 = Sys.command cmd then Ok () else Error "broken" in
let _ = Conex_unix_persistency.remove (filename ^ ".txt")
and _ = Conex_unix_persistency.remove (filename ^ ".key")
and _ = Conex_unix_persistency.remove (filename ^ ".sig")
and _ = Conex_unix_persistency.remove filename
in
res)
let pem_of_ed25519 k =
{|-----BEGIN PUBLIC KEY-----
MCowBQYDK2VwAyEA|} ^ k ^ {|
-----END PUBLIC KEY-----|}
let verify_ed25519 ~key ~data ~signature id =
let* signature =
try Ok (B64.decode signature) with _ -> Error (`InvalidBase64Encoding id)
in
Result.map_error (function
| "broken" -> `InvalidSignature id
| _ -> `InvalidPublicKey id)
(let filename = Filename.temp_file "conex" "sig" in
let key = pem_of_ed25519 key in
let* () = Conex_unix_persistency.write_replace (filename ^ ".key") key in
let* () = Conex_unix_persistency.write_replace (filename ^ ".txt") data in
let* () = Conex_unix_persistency.write_replace (filename ^ ".sig") signature in
let cmd = Printf.sprintf "openssl pkeyutl -verify -pubin -inkey %s.key -rawin -in %s.txt -sigfile %s.sig > /dev/null" filename filename filename in
let res = if 0 = Sys.command cmd then Ok () else Error "broken" in
let _ = Conex_unix_persistency.remove (filename ^ ".txt")
and _ = Conex_unix_persistency.remove (filename ^ ".key")
and _ = Conex_unix_persistency.remove (filename ^ ".sig")
and _ = Conex_unix_persistency.remove filename
in
res)
let sha256 data = Sha256.to_hex (Sha256.string data)
end
module O_V = Conex_verify.Make (V)