1 ! Copyright (C) 2007 Elie CHAFTARI
2 ! See http://factorcode.org/license.txt for BSD license.
4 ! libs/cryptib/cryptlib.factor
6 ! Adapted from cryptlib.h
7 ! Tested with cryptlib 3.3.1.0
8 USING: cryptlib.libcl kernel hashtables alien math
9 namespaces sequences assocs libc alien.c-types alien.accessors continuations ;
23 ! =========================================================
24 ! Error-handling routines
25 ! =========================================================
27 : check-result ( result -- )
31 dup CRYPT_ENVELOPE_RESOURCE = [
34 dup error-messages >hashtable at throw
38 ! =========================================================
39 ! Secure pointer-freeing routines
40 ! =========================================================
42 : secure-free ( ptr n -- )
43 [ dupd 0 -rot set-alien-unsigned-1 ] each free ;
45 : secure-free-array ( ptr n type -- )
46 heap-size * [ dupd 0 -rot set-alien-unsigned-1 ] each free ;
48 : secure-free-object ( ptr type -- )
49 1 swap secure-free-array ;
51 ! =========================================================
52 ! Initialise and shut down cryptlib
53 ! =========================================================
56 cryptInit check-result ;
59 cryptEnd check-result ;
61 : with-cryptlib ( quot -- )
62 [ init [ end ] [ ] cleanup ] with-scope ; inline
64 ! =========================================================
65 ! Create and destroy an encryption context
66 ! =========================================================
68 : create-context ( algo -- )
69 >r "int" <c-object> dup swap CRYPT_UNUSED r> cryptCreateContext
70 check-result context set ;
72 : destroy-context ( -- )
73 context get [ *int cryptDestroyContext check-result ] when*
76 : with-context ( algo quot -- )
77 swap create-context [ destroy-context ] [ ] cleanup ; inline
79 ! =========================================================
81 ! =========================================================
83 : open-keyset ( type name options -- )
84 >r >r >r "int" <c-object> dup swap CRYPT_UNUSED r> r> string>char-alien
85 r> cryptKeysetOpen check-result keyset set ;
88 keyset get *int cryptKeysetClose check-result
91 : with-keyset ( type name options quot -- )
92 >r open-keyset r> [ close-keyset ] [ ] cleanup ; inline
94 : get-public-key ( idtype id -- )
95 >r >r keyset get *int "int*" <c-object> tuck r> r> string>char-alien
96 cryptGetPublicKey check-result context set ;
98 : get-private-key ( idtype id password -- )
99 >r >r >r keyset get *int "int*" <c-object> tuck r>
100 r> string>char-alien r> string>char-alien cryptGetPrivateKey
101 check-result context set ;
103 : get-key ( idtype id password -- )
104 >r >r >r keyset get *int "int*" <c-object> tuck r>
105 r> string>char-alien r> string>char-alien cryptGetKey
106 check-result context set ;
108 : add-public-key ( -- )
109 keyset get *int certificate get *int cryptAddPublicKey check-result ;
111 : add-private-key ( password -- )
112 >r keyset get *int context get *int r> string>char-alien
113 cryptAddPrivateKey check-result ;
115 : delete-key ( type id -- )
116 >r >r keyset get *int r> r> string>char-alien cryptDeleteKey
119 ! =========================================================
120 ! Certificate routines
121 ! =========================================================
123 : create-certificate ( type -- )
124 >r "int" <c-object> dup swap CRYPT_UNUSED r>
125 cryptCreateCert check-result certificate set ;
127 : destroy-certificate ( -- )
128 certificate get *int cryptDestroyCert check-result ;
130 : with-certificate ( type quot -- )
131 swap create-certificate [ destroy-certificate ] [ ] cleanup ; inline
133 : sign-certificate ( -- )
134 certificate get *int context get *int cryptSignCert check-result ;
136 : check-certificate ( -- )
137 certificate get *int context get *int cryptCheckCert check-result ;
139 : import-certificate ( certbuffer length -- )
140 >r r> CRYPT_UNUSED "int*" malloc-object dup >r
141 cryptImportCert check-result r> certificate set ;
143 : export-certificate ( certbuffer maxlength format -- )
144 >r >r dup swap r> "int*" malloc-object dup r> swap >r
145 certificate get *int cryptExportCert check-result
146 cert-buffer set r> cert-length set ;
148 ! =========================================================
149 ! Generate a key into a context
150 ! =========================================================
152 : generate-key ( handle -- )
153 *int cryptGenerateKey check-result ;
155 ! =========================================================
156 ! Get/set/delete attribute functions
157 ! =========================================================
159 : set-attribute ( handle attribute value -- )
160 >r >r *int r> r> cryptSetAttribute check-result ;
162 : set-attribute-string ( handle attribute value -- )
163 >r >r *int r> r> dup length swap string>char-alien swap
164 cryptSetAttributeString check-result ;
166 ! =========================================================
167 ! Envelope and Session routines
168 ! =========================================================
170 : create-envelope ( format -- )
171 >r "int" <c-object> dup swap CRYPT_UNUSED r> cryptCreateEnvelope
172 check-result envelope set ;
174 : destroy-envelope ( -- )
175 envelope get *int cryptDestroyEnvelope check-result ;
177 : with-envelope ( format quot -- )
178 swap create-envelope [ destroy-envelope ] [ ] cleanup ;
180 : create-session ( format -- )
181 >r "int" <c-object> dup swap CRYPT_UNUSED r> cryptCreateSession
182 check-result session set ;
184 : destroy-session ( -- )
185 session get *int cryptDestroySession check-result ;
187 : with-session ( format quot -- )
188 swap create-session [ destroy-session ] [ ] cleanup ;
190 : push-data ( handle buffer length -- )
191 >r >r *int r> r> "int" <c-object> [ cryptPushData ]
192 keep swap check-result bytes-copied set ;
194 : flush-data ( handle -- )
195 *int cryptFlushData check-result ;
197 : pop-data ( handle length -- )
198 dup >r >r *int r> "uchar*" malloc-array
199 dup r> swap >r "int" <c-object> [ cryptPopData ] keep
200 swap check-result bytes-copied set r> pop-buffer set ;
202 ! =========================================================
204 ! =========================================================
206 : envelope-handle ( -- envelope )
209 : context-handle ( -- context )
212 : certificate-handle ( -- certificate )
215 : session-handle ( -- session )
218 : set-pop-buffer ( data -- )
219 string>char-alien pop-buffer set ;
221 : get-pop-buffer ( -- buffer )
224 : pop-buffer-string ( -- s )
225 pop-buffer get alien>char-string ;
227 : get-bytes-copied ( -- value )
228 bytes-copied get *int ;
230 : get-cert-buffer ( -- certreq )
233 : get-cert-length ( -- certlength )