@@ -123,7 +123,7 @@ int UTIL_CTX_set_ui_method(UTIL_CTX *ctx, UI_METHOD *ui_method, void *ui_data)
123123 return 1 ;
124124}
125125
126- static int UTIL_CTX_enumerate_slots_unlocked (UTIL_CTX * ctx )
126+ static int util_ctx_enumerate_slots_unlocked (UTIL_CTX * ctx )
127127{
128128 /* PKCS11_update_slots() uses C_GetSlotList() via libp11 */
129129 if (PKCS11_update_slots (ctx -> pkcs11_ctx , & ctx -> slot_list , & ctx -> slot_count ) < 0 ) {
@@ -145,7 +145,7 @@ int UTIL_CTX_enumerate_slots(UTIL_CTX *ctx)
145145
146146 pthread_mutex_lock (& ctx -> lock );
147147 if (ctx -> pkcs11_ctx )
148- rv = UTIL_CTX_enumerate_slots_unlocked (ctx );
148+ rv = util_ctx_enumerate_slots_unlocked (ctx );
149149 else
150150 rv = UTIL_CTX_init_libp11 (ctx ) == 0 ;
151151 pthread_mutex_unlock (& ctx -> lock );
@@ -172,7 +172,7 @@ int UTIL_CTX_init_libp11(UTIL_CTX *ctx)
172172 UTIL_CTX_free_libp11 (ctx );
173173 return -1 ;
174174 }
175- if (UTIL_CTX_enumerate_slots_unlocked (ctx ) != 1 ) {
175+ if (util_ctx_enumerate_slots_unlocked (ctx ) != 1 ) {
176176 UTIL_CTX_free_libp11 (ctx );
177177 return -1 ;
178178 }
@@ -503,15 +503,6 @@ static int util_ctx_login(UTIL_CTX *ctx, PKCS11_SLOT *slot, PKCS11_TOKEN *tok,
503503 return 1 ;
504504}
505505
506- int UTIL_CTX_login (UTIL_CTX * ctx , PKCS11_SLOT * slot , UI_METHOD * ui_method ,
507- void * ui_data )
508- {
509- if (!slot -> token )
510- return 0 ;
511-
512- return util_ctx_login (ctx , slot , slot -> token , ui_method , ui_data );
513- }
514-
515506/******************************************************************************/
516507/* URI parsing */
517508/******************************************************************************/
@@ -1200,28 +1191,6 @@ static void *util_ctx_load_object(UTIL_CTX *ctx,
12001191 return obj ;
12011192}
12021193
1203- PKCS11_SLOT * UTIL_CTX_find_token (UTIL_CTX * ctx , const char * tok_lbl )
1204- {
1205- PKCS11_SLOT * slot = NULL ;
1206-
1207- if (!ctx -> pkcs11_ctx )
1208- return NULL ;
1209-
1210- do {
1211- slot = PKCS11_find_next_token (ctx -> pkcs11_ctx , ctx -> slot_list ,
1212- ctx -> slot_count , slot );
1213- if (slot && slot -> token && slot -> token -> initialized
1214- && slot -> token -> label
1215- && !strncmp (slot -> token -> label , tok_lbl , 32 )) {
1216- return slot ;
1217- }
1218- } while (!slot );
1219-
1220- UTIL_CTX_log (ctx , LOG_ERR ,
1221- "Initialized token with matching label not found...\n" );
1222- return NULL ;
1223- }
1224-
12251194/******************************************************************************/
12261195/* Certificate handling */
12271196/******************************************************************************/
@@ -1531,4 +1500,63 @@ EVP_PKEY *UTIL_CTX_get_privkey_from_uri(UTIL_CTX *ctx, const char *uri,
15311500 return key ? PKCS11_get_private_key (key ) : NULL ;
15321501}
15331502
1503+ /******************************************************************************/
1504+ /* Key pair generation */
1505+ /******************************************************************************/
1506+
1507+ static PKCS11_SLOT * util_ctx_find_token (UTIL_CTX * ctx , const char * tok_lbl )
1508+ {
1509+ PKCS11_SLOT * slot = NULL ;
1510+
1511+ if (!ctx -> pkcs11_ctx )
1512+ return NULL ;
1513+
1514+ do {
1515+ slot = PKCS11_find_next_token (ctx -> pkcs11_ctx , ctx -> slot_list ,
1516+ ctx -> slot_count , slot );
1517+ if (slot && slot -> token && slot -> token -> initialized
1518+ && slot -> token -> label
1519+ && !strncmp (slot -> token -> label , tok_lbl , 32 ))
1520+ return slot ;
1521+ } while (!slot );
1522+
1523+ UTIL_CTX_log (ctx , LOG_ERR ,
1524+ "Initialized token with matching label not found...\n" );
1525+ return NULL ;
1526+ }
1527+
1528+ int UTIL_CTX_keygen (UTIL_CTX * ctx , PKCS11_KGEN_ATTRS * kg_attrs )
1529+ {
1530+ int rv ;
1531+ PKCS11_SLOT * slot = NULL ;
1532+
1533+ if (kg_attrs == NULL )
1534+ return 0 ;
1535+
1536+ /* Delayed libp11 initialization */
1537+ if (UTIL_CTX_init_libp11 (ctx ))
1538+ return 0 ;
1539+
1540+ slot = util_ctx_find_token (ctx , kg_attrs -> token_label );
1541+ if (!slot || !slot -> token )
1542+ return 0 ;
1543+
1544+ /* Try logging in */
1545+ ERR_clear_error ();
1546+ if (slot -> token -> loginRequired )
1547+ if (!util_ctx_login (ctx , slot , slot -> token ,
1548+ ctx -> ui_method , ctx -> ui_data ))
1549+ return 0 ;
1550+
1551+ rv = PKCS11_keygen (slot -> token , kg_attrs );
1552+ if (rv < 0 ) {
1553+ UTIL_CTX_log (ctx , LOG_ERR ,
1554+ "Failed to generate a key pair on the token. Error code: %d\n" ,
1555+ rv );
1556+ return 0 ;
1557+ }
1558+
1559+ return 1 ;
1560+ }
1561+
15341562/* vim: set noexpandtab: */
0 commit comments