Skip to content

Commit 84d81cb

Browse files
committed
gencrypto-f-044-fix
1 parent 7b05b3d commit 84d81cb

9 files changed

Lines changed: 345 additions & 29 deletions

File tree

include/libwebsockets/lws-genrsa.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@ enum enum_genrsa_mode {
4646
LGRSAM_COUNT
4747
};
4848

49+
/*
50+
* The ctx is user-allocated storage, and may be uninitialized when first
51+
* passed to a create() api. create() marks the ctx once it has brought it
52+
* to a known state, so it can distinguish a ctx that may hold live backend
53+
* key handles (and must release them before re-initializing) from
54+
* uninitialized storage; lws_genrsa_destroy() clears the mark.
55+
*/
56+
57+
#define LWS_GENRSA_CTX_CREATED_MARK 0x4c574752u /* "LWGR" */
58+
4959
struct lws_genrsa_ctx {
5060
#if defined(LWS_WITH_MBEDTLS)
5161
#if !defined(LWS_HAVE_MBEDTLS_V4)
@@ -78,6 +88,8 @@ struct lws_genrsa_ctx {
7888
struct lws_context *context;
7989
enum enum_genrsa_mode mode;
8090
enum lws_genhash_types oaep_hashid;
91+
92+
uint32_t created_mark;
8193
};
8294

8395
/** lws_genrsa_public_decrypt_create() - Create RSA public decrypt context

lib/tls/bearssl/bearssl-crypto.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,11 @@ lws_genhmac_destroy(struct lws_genhmac_ctx *ctx, void *result)
208208
int
209209
lws_genrsa_create(struct lws_genrsa_ctx *ctx, const struct lws_gencrypto_keyelem *el, struct lws_context *context, enum enum_genrsa_mode mode, enum lws_genhash_types hash_type)
210210
{
211+
/* re-init over a live ctx releases the previous incarnation first */
212+
if (ctx->created_mark == LWS_GENRSA_CTX_CREATED_MARK)
213+
lws_genrsa_destroy(ctx);
214+
215+
memset(ctx, 0, sizeof(*ctx));
211216
ctx->context = context;
212217
ctx->mode = mode;
213218

@@ -236,6 +241,8 @@ lws_genrsa_create(struct lws_genrsa_ctx *ctx, const struct lws_gencrypto_keyelem
236241
ctx->priv.p = NULL;
237242
}
238243

244+
ctx->created_mark = LWS_GENRSA_CTX_CREATED_MARK;
245+
239246
return 0;
240247
}
241248

@@ -251,6 +258,10 @@ lws_genrsa_new_keypair(struct lws_context *context, struct lws_genrsa_ctx *ctx,
251258
uint32_t pubexp = 65537;
252259
int ret = -1;
253260

261+
/* re-init over a live ctx releases the previous incarnation first */
262+
if (ctx->created_mark == LWS_GENRSA_CTX_CREATED_MARK)
263+
lws_genrsa_destroy(ctx);
264+
254265
memset(ctx, 0, sizeof(*ctx));
255266
ctx->context = context;
256267
ctx->mode = mode;
@@ -317,6 +328,7 @@ lws_genrsa_new_keypair(struct lws_context *context, struct lws_genrsa_ctx *ctx,
317328
goto bail;
318329

319330
ret = 0;
331+
ctx->created_mark = LWS_GENRSA_CTX_CREATED_MARK;
320332

321333
bail:
322334
if (dbuf)
@@ -437,6 +449,7 @@ void lws_genrsa_destroy(struct lws_genrsa_ctx *ctx)
437449
{
438450
lws_free_set_NULL(ctx->kbuf_priv);
439451
lws_free_set_NULL(ctx->kbuf_pub);
452+
ctx->created_mark = 0;
440453
}
441454

442455
void lws_genec_destroy(struct lws_genec_ctx *ctx)

lib/tls/gnutls/lws-genrsa.c

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ lws_genrsa_create(struct lws_genrsa_ctx *ctx,
3636
{
3737
gnutls_datum_t m, e, d, p, q, u, e1, e2;
3838

39+
/* re-init over a live ctx releases the previous incarnation first */
40+
if (ctx->created_mark == LWS_GENRSA_CTX_CREATED_MARK)
41+
lws_genrsa_destroy(ctx);
42+
3943
memset(ctx, 0, sizeof(*ctx));
4044
ctx->context = context;
4145
ctx->mode = mode;
@@ -68,20 +72,33 @@ lws_genrsa_create(struct lws_genrsa_ctx *ctx,
6872

6973
if (gnutls_privkey_import_rsa_raw(ctx->priv, &m, &e, &d, &p, &q, &u, &e1, &e2) < 0) {
7074
gnutls_privkey_deinit(ctx->priv);
75+
ctx->priv = NULL;
7176
return 1;
7277
}
7378
}
7479

7580
if (m.data && e.data) {
76-
if (gnutls_pubkey_init(&ctx->pub) < 0)
81+
if (gnutls_pubkey_init(&ctx->pub) < 0) {
82+
if (ctx->priv) {
83+
gnutls_privkey_deinit(ctx->priv);
84+
ctx->priv = NULL;
85+
}
7786
return 1;
87+
}
7888

7989
if (gnutls_pubkey_import_rsa_raw(ctx->pub, &m, &e) < 0) {
8090
gnutls_pubkey_deinit(ctx->pub);
91+
ctx->pub = NULL;
92+
if (ctx->priv) {
93+
gnutls_privkey_deinit(ctx->priv);
94+
ctx->priv = NULL;
95+
}
8196
return 1;
8297
}
8398
}
8499

100+
ctx->created_mark = LWS_GENRSA_CTX_CREATED_MARK;
101+
85102
return 0;
86103
}
87104

@@ -104,6 +121,10 @@ lws_genrsa_new_keypair(struct lws_context *context, struct lws_genrsa_ctx *ctx,
104121
gnutls_datum_t m, e, d, p, q, u, e1, e2;
105122
int ret = -1;
106123

124+
/* re-init over a live ctx releases the previous incarnation first */
125+
if (ctx->created_mark == LWS_GENRSA_CTX_CREATED_MARK)
126+
lws_genrsa_destroy(ctx);
127+
107128
memset(ctx, 0, sizeof(*ctx));
108129
ctx->context = context;
109130
ctx->mode = mode;
@@ -153,8 +174,16 @@ lws_genrsa_new_keypair(struct lws_context *context, struct lws_genrsa_ctx *ctx,
153174
ret = 0;
154175

155176
bail:
156-
if (ret)
177+
if (!ret) {
178+
ctx->created_mark = LWS_GENRSA_CTX_CREATED_MARK;
179+
return 0;
180+
}
181+
182+
if (ctx->priv) {
157183
gnutls_privkey_deinit(ctx->priv);
184+
ctx->priv = NULL;
185+
}
186+
158187
return ret;
159188
}
160189

@@ -323,6 +352,7 @@ lws_genrsa_destroy(struct lws_genrsa_ctx *ctx)
323352

324353
ctx->priv = NULL;
325354
ctx->pub = NULL;
355+
ctx->created_mark = 0;
326356
}
327357

328358
void

lib/tls/mbedtls/lws-genrsa.c

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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

206218
cleanup:
@@ -575,11 +587,13 @@ lws_genrsa_render_pkey_asn1(struct lws_genrsa_ctx *ctx, int _private,
575587
void
576588
lws_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

788817
cleanup_der:
789818
lws_genrsa_destroy_elements(el);
819+
psa_destroy_key(ctx->key_id);
820+
ctx->key_id = 0;
790821
return -1;
791822
}
792823

@@ -881,6 +912,8 @@ void
881912
lws_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

lib/tls/openhitls/lws-genrsa.c

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,10 @@ lws_genrsa_create(struct lws_genrsa_ctx *ctx,
249249
{
250250
int ret;
251251

252+
/* re-init over a live ctx releases the previous incarnation first */
253+
if (ctx->created_mark == LWS_GENRSA_CTX_CREATED_MARK)
254+
lws_genrsa_destroy(ctx);
255+
252256
memset(ctx, 0, sizeof(*ctx));
253257
ctx->context = context;
254258
ctx->mode = mode;
@@ -282,8 +286,10 @@ lws_genrsa_create(struct lws_genrsa_ctx *ctx,
282286
}
283287

284288
/* Set private key elements if present */
285-
if (el[LWS_GENCRYPTO_RSA_KEYEL_D].len == 0)
289+
if (el[LWS_GENCRYPTO_RSA_KEYEL_D].len == 0) {
290+
ctx->created_mark = LWS_GENRSA_CTX_CREATED_MARK;
286291
return 0;
292+
}
287293

288294
CRYPT_EAL_PkeyPrv prvKey = {
289295
.id = CRYPT_PKEY_RSA,
@@ -309,6 +315,8 @@ lws_genrsa_create(struct lws_genrsa_ctx *ctx,
309315
goto bail;
310316
}
311317

318+
ctx->created_mark = LWS_GENRSA_CTX_CREATED_MARK;
319+
312320
return 0;
313321

314322
bail:
@@ -327,6 +335,10 @@ lws_genrsa_new_keypair(struct lws_context *context, struct lws_genrsa_ctx *ctx,
327335
struct lws_genrsa_keypair_bufs bufs;
328336
int ret;
329337

338+
/* re-init over a live ctx releases the previous incarnation first */
339+
if (ctx->created_mark == LWS_GENRSA_CTX_CREATED_MARK)
340+
lws_genrsa_destroy(ctx);
341+
330342
memset(ctx, 0, sizeof(*ctx));
331343
ctx->context = context;
332344
ctx->mode = mode;
@@ -414,6 +426,8 @@ lws_genrsa_new_keypair(struct lws_context *context, struct lws_genrsa_ctx *ctx,
414426
/* Note: Padding mode is set separately during encrypt/decrypt operations,
415427
* not during key generation */
416428

429+
ctx->created_mark = LWS_GENRSA_CTX_CREATED_MARK;
430+
417431
return 0;
418432

419433
bail:
@@ -635,9 +649,10 @@ lws_genrsa_hash_sign(struct lws_genrsa_ctx *ctx, const uint8_t *in,
635649
void
636650
lws_genrsa_destroy(struct lws_genrsa_ctx *ctx)
637651
{
638-
if (!ctx->ctx)
639-
return;
652+
if (ctx->ctx) {
653+
CRYPT_EAL_PkeyFreeCtx(ctx->ctx);
654+
ctx->ctx = NULL;
655+
}
640656

641-
CRYPT_EAL_PkeyFreeCtx(ctx->ctx);
642-
ctx->ctx = NULL;
657+
ctx->created_mark = 0;
643658
}

0 commit comments

Comments
 (0)