@@ -49,6 +49,13 @@ lws_genrsa_create(struct lws_genrsa_ctx *ctx,
4949{
5050 int hash_id ;
5151
52+ /* re-init over a live ctx releases the previous incarnation first */
53+ if (ctx -> created_mark == LWS_GENRSA_CTX_CREATED_MARK )
54+ lws_genrsa_destroy (ctx );
55+
56+ if (mode >= LGRSAM_COUNT )
57+ return -1 ;
58+
5259 memset (ctx , 0 , sizeof (* ctx ));
5360 ctx -> ctx = lws_zalloc (sizeof (* ctx -> ctx ), "genrsa" );
5461 if (!ctx -> ctx )
@@ -57,9 +64,6 @@ lws_genrsa_create(struct lws_genrsa_ctx *ctx,
5764 ctx -> context = context ;
5865 ctx -> mode = mode ;
5966
60- if (mode >= LGRSAM_COUNT )
61- return -1 ;
62-
6367 /*
6468 * OAEP needs a real md for the MGF1 hash... if the caller has no
6569 * preference, use the RFC8017 default of SHA-1. The mapped md type
@@ -133,6 +137,8 @@ lws_genrsa_create(struct lws_genrsa_ctx *ctx,
133137
134138 ctx -> ctx -> MBEDTLS_PRIVATE (len ) = el [LWS_GENCRYPTO_RSA_KEYEL_N ].len ;
135139
140+ ctx -> created_mark = LWS_GENRSA_CTX_CREATED_MARK ;
141+
136142 return 0 ;
137143}
138144
@@ -152,6 +158,13 @@ lws_genrsa_new_keypair(struct lws_context *context, struct lws_genrsa_ctx *ctx,
152158{
153159 int n ;
154160
161+ /* re-init over a live ctx releases the previous incarnation first */
162+ if (ctx -> created_mark == LWS_GENRSA_CTX_CREATED_MARK )
163+ lws_genrsa_destroy (ctx );
164+
165+ if (mode >= LGRSAM_COUNT )
166+ return -1 ;
167+
155168 memset (ctx , 0 , sizeof (* ctx ));
156169 ctx -> ctx = lws_zalloc (sizeof (* ctx -> ctx ), "genrsa" );
157170 if (!ctx -> ctx )
@@ -160,9 +173,6 @@ lws_genrsa_new_keypair(struct lws_context *context, struct lws_genrsa_ctx *ctx,
160173 ctx -> context = context ;
161174 ctx -> mode = mode ;
162175
163- if (mode >= LGRSAM_COUNT )
164- return -1 ;
165-
166176#if !defined(MBEDTLS_VERSION_NUMBER ) || MBEDTLS_VERSION_NUMBER < 0x03000000
167177 mbedtls_rsa_init (ctx -> ctx , mode_map [mode ], 0 );
168178#else
@@ -201,6 +211,8 @@ lws_genrsa_new_keypair(struct lws_context *context, struct lws_genrsa_ctx *ctx,
201211 }
202212 }
203213
214+ ctx -> created_mark = LWS_GENRSA_CTX_CREATED_MARK ;
215+
204216 return 0 ;
205217
206218cleanup :
@@ -575,11 +587,13 @@ lws_genrsa_render_pkey_asn1(struct lws_genrsa_ctx *ctx, int _private,
575587void
576588lws_genrsa_destroy (struct lws_genrsa_ctx * ctx )
577589{
578- if (!ctx -> ctx )
579- return ;
580- mbedtls_rsa_free (ctx -> ctx );
581- lws_free (ctx -> ctx );
582- ctx -> ctx = NULL ;
590+ if (ctx -> ctx ) {
591+ mbedtls_rsa_free (ctx -> ctx );
592+ lws_free (ctx -> ctx );
593+ ctx -> ctx = NULL ;
594+ }
595+
596+ ctx -> created_mark = 0 ;
583597}
584598#else /* LWS_HAVE_MBEDTLS_V4 */
585599
@@ -664,6 +678,10 @@ lws_genrsa_create(struct lws_genrsa_ctx *ctx,
664678 struct lws_gencrypto_keyelem version = { (uint8_t * )"\0" , 1 };
665679 psa_key_attributes_t attr = PSA_KEY_ATTRIBUTES_INIT ;
666680
681+ /* re-init over a live ctx releases the previous incarnation first */
682+ if (ctx -> created_mark == LWS_GENRSA_CTX_CREATED_MARK )
683+ lws_genrsa_destroy (ctx );
684+
667685 memset (ctx , 0 , sizeof (* ctx ));
668686 ctx -> context = context ;
669687 ctx -> mode = mode ;
@@ -700,6 +718,8 @@ lws_genrsa_create(struct lws_genrsa_ctx *ctx,
700718 if (psa_import_key (& attr , der , (size_t )(p - der ), & ctx -> key_id ) != PSA_SUCCESS )
701719 return -1 ;
702720
721+ ctx -> created_mark = LWS_GENRSA_CTX_CREATED_MARK ;
722+
703723 return 0 ;
704724}
705725
@@ -712,6 +732,10 @@ lws_genrsa_new_keypair(struct lws_context *context, struct lws_genrsa_ctx *ctx,
712732 uint8_t der [4096 ];
713733 size_t der_len ;
714734
735+ /* re-init over a live ctx releases the previous incarnation first */
736+ if (ctx -> created_mark == LWS_GENRSA_CTX_CREATED_MARK )
737+ lws_genrsa_destroy (ctx );
738+
715739 memset (ctx , 0 , sizeof (* ctx ));
716740 ctx -> context = context ;
717741 ctx -> mode = mode ;
@@ -730,8 +754,11 @@ lws_genrsa_new_keypair(struct lws_context *context, struct lws_genrsa_ctx *ctx,
730754 if (psa_generate_key (& attr , & ctx -> key_id ) != PSA_SUCCESS )
731755 return -1 ;
732756
733- if (psa_export_key (ctx -> key_id , der , sizeof (der ), & der_len ) != PSA_SUCCESS )
757+ if (psa_export_key (ctx -> key_id , der , sizeof (der ), & der_len ) != PSA_SUCCESS ) {
758+ psa_destroy_key (ctx -> key_id );
759+ ctx -> key_id = 0 ;
734760 return -1 ;
761+ }
735762
736763 {
737764 uint8_t * p = der ;
@@ -783,10 +810,14 @@ lws_genrsa_new_keypair(struct lws_context *context, struct lws_genrsa_ctx *ctx,
783810 }
784811 }
785812
813+ ctx -> created_mark = LWS_GENRSA_CTX_CREATED_MARK ;
814+
786815 return 0 ;
787816
788817cleanup_der :
789818 lws_genrsa_destroy_elements (el );
819+ psa_destroy_key (ctx -> key_id );
820+ ctx -> key_id = 0 ;
790821 return -1 ;
791822}
792823
881912lws_genrsa_destroy (struct lws_genrsa_ctx * ctx )
882913{
883914 psa_destroy_key (ctx -> key_id );
915+ ctx -> key_id = 0 ;
916+ ctx -> created_mark = 0 ;
884917}
885918
886919#endif
0 commit comments