@@ -72,6 +72,15 @@ static int async_rsa_finish(RSA *rsa);
7272
7373static RSA_METHOD * async_rsa_method = NULL ;
7474
75+ EVP_PKEY * async_load_privkey (ENGINE * e , const char * s_key_id , UI_METHOD * ui_method , void * callback_data )
76+ {
77+ printf ("Loading key %s\n" , s_key_id );
78+ FILE * f = fopen (s_key_id , "r" );
79+ EVP_PKEY * key = PEM_read_PrivateKey (f , NULL , NULL , NULL );
80+ fclose (f );
81+ return key ;
82+ }
83+
7584static int bind_async (ENGINE * e )
7685{
7786 /* Setup RSA_METHOD */
@@ -96,7 +105,8 @@ static int bind_async(ENGINE *e)
96105 || !ENGINE_set_RSA (e , async_rsa_method )
97106 || !ENGINE_set_destroy_function (e , async_destroy )
98107 || !ENGINE_set_init_function (e , engine_async_init )
99- || !ENGINE_set_finish_function (e , async_finish )) {
108+ || !ENGINE_set_finish_function (e , async_finish )
109+ || !ENGINE_set_load_privkey_function (e , async_load_privkey )) {
100110 fprintf (stderr , "Failed to initialize\n" );
101111 return 0 ;
102112 }
@@ -176,14 +186,17 @@ static void async_pause_job(void) {
176186 OSSL_ASYNC_FD * writefd ;
177187 char buf = DUMMY_CHAR ;
178188
179- if ((job = ASYNC_get_current_job ()) == NULL )
189+ if ((job = ASYNC_get_current_job ()) == NULL ) {
190+ printf ("No job\n" );
180191 return ;
192+ }
181193
182194 waitctx = ASYNC_get_wait_ctx (job );
183195
184196 if (ASYNC_WAIT_CTX_get_fd (waitctx , engine_id , & pipefds [0 ],
185197 (void * * )& writefd )) {
186- pipefds [1 ] = * writefd ;
198+ printf ("Existing wait ctx\n" );
199+ return ;
187200 } else {
188201 writefd = (OSSL_ASYNC_FD * )OPENSSL_malloc (sizeof (* writefd ));
189202 if (writefd == NULL )
@@ -194,6 +207,8 @@ static void async_pause_job(void) {
194207 }
195208 * writefd = pipefds [1 ];
196209
210+ printf ("New wait ctx %d %d\n" , pipefds [0 ], pipefds [1 ]);
211+
197212 if (!ASYNC_WAIT_CTX_set_wait_fd (waitctx , engine_id , pipefds [0 ],
198213 writefd , wait_cleanup )) {
199214 wait_cleanup (waitctx , engine_id , pipefds [0 ], writefd );
@@ -213,35 +228,39 @@ void *
213228delay_method (void * arg )
214229{
215230 int signal_fd = (intptr_t )arg ;
216- sleep (5 );
217- uint64_t buf = 1 ;
231+ sleep (2 );
232+ char buf = DUMMY_CHAR ;
218233 write (signal_fd , & buf , sizeof (buf ));
234+ printf ("Send signal to %d\n" , signal_fd );
235+ return NULL ;
219236}
220237
221238
222239void
223240spawn_delay_thread ()
224241{
225242 pthread_t thread_id ;
226- OSSL_ASYNC_FD signal_fd ;
227- OSSL_ASYNC_FD pipefds [2 ] = {0 , 0 };
228243 ASYNC_JOB * job ;
229- if ((job = ASYNC_get_current_job ()) == NULL )
244+ if ((job = ASYNC_get_current_job ()) == NULL ) {
245+ printf ("Spawn no job\n" );
230246 return ;
247+ }
231248
232249 ASYNC_WAIT_CTX * waitctx = ASYNC_get_wait_ctx (job );
233250
234251 size_t numfds ;
235- if (ASYNC_WAIT_CTX_get_all_fds (waitctx , & signal_fd , & numfds ) && numfds > 0 ) {
252+ if (ASYNC_WAIT_CTX_get_all_fds (waitctx , NULL , & numfds ) && numfds > 0 ) {
253+ printf ("Spawn, wait_ctx exists. Go away, something else is using this job\n" );
236254 } else {
255+ OSSL_ASYNC_FD signal_fd ;
237256 OSSL_ASYNC_FD pipefds [2 ] = {0 ,0 };
238257 OSSL_ASYNC_FD * writefd = OPENSSL_malloc (sizeof (* writefd ));
239258 pipe (pipefds );
240259 signal_fd = * writefd = pipefds [1 ];
241260 ASYNC_WAIT_CTX_set_wait_fd (waitctx , engine_id , pipefds [0 ], writefd , wait_cleanup );
261+ printf ("Spawn, create wait_ctx %d %d\n" , pipefds [0 ], pipefds [1 ]);
262+ pthread_create (& thread_id , NULL , delay_method , (void * )((intptr_t )signal_fd ));
242263 }
243-
244- pthread_create (& thread_id , NULL , delay_method , (void * )((intptr_t )signal_fd ));
245264}
246265
247266
@@ -264,7 +283,7 @@ static int async_pub_dec(int flen, const unsigned char *from,
264283static int async_rsa_priv_enc (int flen , const unsigned char * from ,
265284 unsigned char * to , RSA * rsa , int padding )
266285{
267- // printf("async_priv_enc\n");
286+ printf ("async_priv_enc\n" );
268287 spawn_delay_thread ();
269288 async_pause_job ();
270289 return RSA_meth_get_priv_enc (RSA_PKCS1_OpenSSL ())
@@ -274,7 +293,7 @@ static int async_rsa_priv_enc(int flen, const unsigned char *from,
274293static int async_rsa_priv_dec (int flen , const unsigned char * from ,
275294 unsigned char * to , RSA * rsa , int padding )
276295{
277- // printf("async_priv_dec\n");
296+ printf ("async_priv_dec\n" );
278297 spawn_delay_thread ();
279298 async_pause_job ();
280299 return RSA_meth_get_priv_dec (RSA_PKCS1_OpenSSL ())
0 commit comments