]> gitweb.factorcode.org Git - factor.git/blob - unmaintained/cryptlib/cryptlib-tests.factor
Merge branch 'master' of git://factorcode.org/git/factor
[factor.git] / unmaintained / cryptlib / cryptlib-tests.factor
1 USING: cryptlib.libcl cryptlib prettyprint kernel alien sequences libc math
2 tools.test io io.files continuations alien.c-types splitting generic.math ;
3
4 "=========================================================" print
5 "Envelope/de-envelop test..." print
6 "=========================================================" print
7
8 [
9     ! envelope
10     CRYPT_FORMAT_CRYPTLIB [
11         "Hello world" set-pop-buffer
12         envelope-handle CRYPT_ENVINFO_DATASIZE
13         get-pop-buffer alien>char-string length set-attribute
14         envelope-handle get-pop-buffer dup alien>char-string length push-data
15         get-bytes-copied .
16         envelope-handle flush-data
17         envelope-handle 1024 pop-data
18         get-bytes-copied .
19         pop-buffer-string .
20     ] with-envelope
21
22     ! de-envelope
23     CRYPT_FORMAT_AUTO [
24         envelope-handle get-pop-buffer get-bytes-copied push-data
25         get-bytes-copied .
26         envelope-handle flush-data
27         envelope-handle get-bytes-copied pop-data
28         get-bytes-copied .
29         [ "Hello world" ] [ pop-buffer-string ] unit-test
30     ] with-envelope
31
32 ] with-cryptlib
33
34 "=========================================================" print
35 "Password encryption test..." print
36 "=========================================================" print
37
38 [
39     ! envelope
40     CRYPT_FORMAT_CRYPTLIB [
41         envelope-handle CRYPT_ENVINFO_PASSWORD "password" set-attribute-string
42         "Hello world" set-pop-buffer
43         envelope-handle CRYPT_ENVINFO_DATASIZE
44         get-pop-buffer alien>char-string length set-attribute
45         envelope-handle get-pop-buffer dup alien>char-string length push-data
46         get-bytes-copied .
47         envelope-handle flush-data
48         envelope-handle 1024 pop-data
49         get-bytes-copied .
50         pop-buffer-string .
51     ] with-envelope
52
53         ! de-envelope
54     CRYPT_FORMAT_AUTO [
55         [ envelope-handle get-pop-buffer get-bytes-copied push-data ] [
56             dup CRYPT_ENVELOPE_RESOURCE = [
57                 envelope-handle CRYPT_ENVINFO_PASSWORD
58                 "password" set-attribute-string
59             ] [
60                 rethrow
61             ] if
62         ] recover drop
63         get-bytes-copied .
64         envelope-handle flush-data
65         envelope-handle get-bytes-copied pop-data
66         get-bytes-copied .
67         [ "Hello world" ] [ pop-buffer-string ] unit-test
68     ] with-envelope
69 ] with-cryptlib
70
71 "=========================================================" print
72 "Compression test..." print
73 "=========================================================" print
74
75 [
76     ! envelope
77     CRYPT_FORMAT_CRYPTLIB [
78         envelope-handle CRYPT_ENVINFO_COMPRESSION CRYPT_UNUSED set-attribute
79         "Hello world" set-pop-buffer
80         envelope-handle CRYPT_ENVINFO_DATASIZE
81         get-pop-buffer alien>char-string length set-attribute
82         envelope-handle get-pop-buffer dup alien>char-string length push-data
83         get-bytes-copied .
84         envelope-handle flush-data
85         envelope-handle 1024 pop-data
86         get-bytes-copied .
87         pop-buffer-string .
88     ] with-envelope
89
90     ! de-envelope
91     CRYPT_FORMAT_AUTO [
92         envelope-handle get-pop-buffer get-bytes-copied push-data
93         get-bytes-copied .
94         envelope-handle flush-data
95         envelope-handle get-bytes-copied pop-data
96         get-bytes-copied .
97         [ "Hello world" ] [ pop-buffer-string ] unit-test
98     ] with-envelope
99 ] with-cryptlib
100
101 "=========================================================" print
102 "Conventional encryption test..." print
103 "=========================================================" print
104
105 [
106     ! envelope
107     CRYPT_FORMAT_CRYPTLIB [
108         CRYPT_ALGO_IDEA [
109             context-handle CRYPT_CTXINFO_KEY "0123456789ABCDEF" set-attribute-string
110             envelope-handle CRYPT_ENVINFO_SESSIONKEY context-handle *int set-attribute
111         ] with-context
112
113         "Hello world" set-pop-buffer
114         envelope-handle CRYPT_ENVINFO_DATASIZE
115         get-pop-buffer alien>char-string length set-attribute
116         envelope-handle get-pop-buffer dup alien>char-string length push-data
117         get-bytes-copied .
118         envelope-handle flush-data
119         envelope-handle 1024 pop-data
120         get-bytes-copied .
121         pop-buffer-string .
122     ] with-envelope
123
124     ! de-envelope
125     CRYPT_FORMAT_AUTO [
126         [ envelope-handle get-pop-buffer get-bytes-copied push-data ] [
127             dup CRYPT_ENVELOPE_RESOURCE = [
128                 CRYPT_ALGO_IDEA create-context
129                 context-handle CRYPT_CTXINFO_KEY "0123456789ABCDEF"
130                 set-attribute-string
131                 envelope-handle CRYPT_ENVINFO_SESSIONKEY context-handle *int
132                 set-attribute
133             ] [
134                 rethrow
135             ] if
136         ] recover drop
137
138         get-bytes-copied .
139         destroy-context
140         envelope-handle flush-data
141         envelope-handle get-bytes-copied pop-data
142         get-bytes-copied .
143         [ "Hello world" ] [ pop-buffer-string ] unit-test
144     ] with-envelope
145 ] with-cryptlib
146
147 "=========================================================" print
148 "Large data size envelope/de-envelop test..." print
149 "=========================================================" print
150
151 [
152     ! envelope
153     CRYPT_FORMAT_CRYPTLIB [
154         "extra/cryptlib/test/large_data.txt" resource-path
155         file-contents set-pop-buffer
156         envelope-handle CRYPT_ATTRIBUTE_BUFFERSIZE
157         get-pop-buffer alien>char-string length 10000 + set-attribute
158         envelope-handle CRYPT_ENVINFO_DATASIZE
159         get-pop-buffer alien>char-string length set-attribute
160         envelope-handle get-pop-buffer dup alien>char-string length push-data
161         get-bytes-copied .
162         envelope-handle flush-data
163         envelope-handle get-pop-buffer alien>char-string length 10000 + pop-data
164         get-bytes-copied .
165         ! pop-buffer-string .
166     ] with-envelope
167
168     ! de-envelope
169     CRYPT_FORMAT_AUTO [
170         envelope-handle CRYPT_ATTRIBUTE_BUFFERSIZE
171         get-pop-buffer alien>char-string length 10000 + set-attribute
172         envelope-handle get-pop-buffer get-bytes-copied push-data
173         get-bytes-copied .
174         envelope-handle flush-data
175         envelope-handle get-bytes-copied pop-data
176         get-bytes-copied .
177         ! pop-buffer-string .
178         [ "/opt/local/lib/libcl.dylib(dylib1.o):" ]
179         [ pop-buffer-string "\n" split first ] unit-test
180         [ "00000000 t __mh_dylib_header" ]
181         [ pop-buffer-string "\n" split last/first first ] unit-test
182     ] with-envelope
183 ] with-cryptlib
184
185 "=========================================================" print
186 "Large data size password encryption test..." print
187 "=========================================================" print
188
189 [
190
191     ! envelope
192     CRYPT_FORMAT_CRYPTLIB [
193         envelope-handle CRYPT_ENVINFO_PASSWORD "password" set-attribute-string
194         "extra/cryptlib/test/large_data.txt" resource-path
195         file-contents set-pop-buffer
196         envelope-handle CRYPT_ATTRIBUTE_BUFFERSIZE
197         get-pop-buffer alien>char-string length 10000 + set-attribute
198         envelope-handle CRYPT_ENVINFO_DATASIZE
199         get-pop-buffer alien>char-string length set-attribute
200         envelope-handle get-pop-buffer dup alien>char-string length push-data
201         get-bytes-copied .
202         envelope-handle flush-data
203         envelope-handle get-pop-buffer alien>char-string length 10000 + pop-data
204         get-bytes-copied .
205         pop-buffer-string .
206     ] with-envelope
207
208     ! de-envelope
209     CRYPT_FORMAT_AUTO [
210         envelope-handle CRYPT_ATTRIBUTE_BUFFERSIZE 130000 set-attribute
211         [ envelope-handle get-pop-buffer get-bytes-copied push-data ] [
212             dup CRYPT_ENVELOPE_RESOURCE = [
213                 envelope-handle CRYPT_ENVINFO_PASSWORD
214                 "password" set-attribute-string
215             ] [
216                 rethrow
217             ] if
218         ] recover drop
219
220         get-bytes-copied .
221         envelope-handle flush-data
222         envelope-handle get-bytes-copied pop-data
223         get-bytes-copied .
224         ! pop-buffer-string .
225
226         [ "/opt/local/lib/libcl.dylib(dylib1.o):" ]
227         [ pop-buffer-string "\n" split first ] unit-test
228
229         [ "00000000 t __mh_dylib_header" ]
230         [ pop-buffer-string "\n" split last/first first ] unit-test
231     ] with-envelope
232 ] with-cryptlib
233
234 "=========================================================" print
235 "Generating a key pair test..." print
236 "=========================================================" print
237
238 [
239     CRYPT_ALGO_RSA [
240         context-handle CRYPT_CTXINFO_LABEL "private key" set-attribute-string
241
242         ! a particular key length can be set (e.g. 1536-bit/192-byte key)
243         context-handle CRYPT_CTXINFO_KEYSIZE 1536 8 / set-attribute
244
245         context-handle generate-key
246
247         CRYPT_KEYSET_FILE "extra/cryptlib/test/keys.p15" resource-path
248         CRYPT_KEYOPT_CREATE [
249             "password" add-private-key
250         ] with-keyset
251     ] with-context
252 ] with-cryptlib
253
254 "Passed" print
255
256 "=========================================================" print
257 "Simple certificate creation test..." print
258 "=========================================================" print
259
260 [
261     CRYPT_ALGO_RSA [
262         context-handle CRYPT_CTXINFO_LABEL "private key" set-attribute-string
263         context-handle generate-key
264         CRYPT_KEYSET_FILE "extra/cryptlib/test/keys.p15" resource-path
265         CRYPT_KEYOPT_CREATE [
266             "password" add-private-key
267             CRYPT_CERTTYPE_CERTIFICATE [
268                 certificate-handle CRYPT_CERTINFO_XYZZY 1 set-attribute
269                 certificate-handle CRYPT_CERTINFO_SUBJECTPUBLICKEYINFO
270                 context-handle *int set-attribute
271                 certificate-handle CRYPT_CERTINFO_COMMONNAME "Dave Smith"
272                 set-attribute-string
273                 sign-certificate
274                 check-certificate
275                 add-public-key
276                 f 0 CRYPT_CERTFORMAT_TEXT_CERTIFICATE export-certificate
277                 get-cert-length *int dup malloc swap
278                 CRYPT_CERTFORMAT_TEXT_CERTIFICATE export-certificate
279                 get-cert-buffer alien>char-string print
280             ] with-certificate
281         ] with-keyset
282     ] with-context
283 ] with-cryptlib
284
285 : ssh-session ( -- )
286     "=========================================================" print
287     "SSH session test..." print
288     "=========================================================" print
289
290     ! start client connection with:
291     ! ssh -v localhost -p3000
292     "waiting for: ssh -v localhost -p3000" print flush
293
294     ! Are you sure you want to continue connecting (yes/no)? yes
295     ! ...
296     ! <at> localhost's password: (any password will be accepted)
297
298     ! If you want to run the test again you should clean the [localhost]:3000
299     ! ssh-rsa entry in the known_hosts file, in your home directory under the .ssh
300     ! folder, since the test generates a new RSA certificate on every run.
301
302     [
303         CRYPT_KEYSET_FILE "extra/cryptlib/test/keys.p15" resource-path
304         CRYPT_KEYOPT_READONLY [
305             CRYPT_KEYID_NAME "private key" "password" get-private-key
306
307             CRYPT_SESSION_SSH_SERVER [
308
309                 session-handle CRYPT_SESSINFO_SERVER_NAME "localhost"
310                 set-attribute-string
311
312                 session-handle CRYPT_SESSINFO_SERVER_PORT 3000 set-attribute
313
314                 session-handle CRYPT_SESSINFO_PRIVATEKEY
315
316                 context-handle *int set-attribute
317
318                 [ session-handle CRYPT_SESSINFO_ACTIVE 1 set-attribute ] [
319                     dup CRYPT_ENVELOPE_RESOURCE = [
320                         session-handle CRYPT_SESSINFO_AUTHRESPONSE 1
321                         set-attribute
322
323                         session-handle CRYPT_SESSINFO_ACTIVE 1 set-attribute
324
325                         "Welcome to cryptlib, now go away.\r\n" set-pop-buffer
326
327                         session-handle  get-pop-buffer dup alien>char-string
328                         length push-data
329
330                         session-handle flush-data
331                     ] [
332                         rethrow
333                     ] if
334                 ] recover drop
335             ] with-session
336         ] with-keyset
337     ] with-cryptlib
338
339     "Passed" print
340 ;
341
342 : ssl-session ( -- )
343     "=========================================================" print
344     "SSL session test..." print
345     "=========================================================" print
346
347     ! start client connection with:
348     ! curl -k https://localhost:3000
349     "waiting for: curl -k https://localhost:3000" print flush
350
351     [
352         CRYPT_KEYSET_FILE "extra/cryptlib/test/keys.p15" resource-path
353         CRYPT_KEYOPT_READONLY [
354             CRYPT_KEYID_NAME "private key" "password" get-private-key
355
356             CRYPT_SESSION_SSL_SERVER [
357                 session-handle CRYPT_SESSINFO_SERVER_NAME "localhost"
358                 set-attribute-string
359                 session-handle CRYPT_SESSINFO_SERVER_PORT 3000 set-attribute
360                 session-handle CRYPT_OPTION_NET_WRITETIMEOUT 10 set-attribute
361                 session-handle CRYPT_OPTION_NET_READTIMEOUT 10 set-attribute
362                 session-handle CRYPT_OPTION_NET_CONNECTTIMEOUT 10 set-attribute
363                 session-handle CRYPT_SESSINFO_PRIVATEKEY
364                 context-handle *int set-attribute
365
366                 session-handle CRYPT_SESSINFO_ACTIVE 1 set-attribute
367                 "Welcome to cryptlib, now go away.\r\n" set-pop-buffer
368                 session-handle  get-pop-buffer dup alien>char-string
369                 length push-data
370                 session-handle flush-data
371             ] with-session
372         ] with-keyset
373     ] with-cryptlib
374
375     "Passed" print
376 ;