Skip to content

Commit 891afb9

Browse files
committed
SQ-and-clang-fixes
1 parent 84d81cb commit 891afb9

32 files changed

Lines changed: 218 additions & 95 deletions

File tree

include/libwebsockets/lws-genec.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ struct lws_ec_curves {
114114
* .name = NULL, of curves you want to allow
115115
*
116116
* Initializes a genecdh
117+
*
118+
* \p ctx must be zeroed (eg, using memset) before it is passed to create
119+
* apis the first time; if the ctx is still live from an earlier create, the
120+
* call fails and returns nonzero. After lws_genec_destroy() the same ctx
121+
* can be passed to the create apis again.
117122
*/
118123
LWS_VISIBLE LWS_EXTERN int
119124
lws_genecdh_create(struct lws_genec_ctx *ctx, struct lws_context *context,
@@ -159,7 +164,9 @@ lws_genecdh_compute_shared_secret(struct lws_genec_ctx *ctx, uint8_t *ss,
159164
* struct lws_ec_curves array, terminated by an entry with
160165
* .name = NULL, of curves you want to allow
161166
*
162-
* Initializes a genecdh
167+
* Initializes a genecdsa. \p ctx follows the same lifetime rules as for
168+
* lws_genecdh_create(): zeroed before first use, and create over a ctx that
169+
* is still live fails until lws_genec_destroy() is called on it.
163170
*/
164171
LWS_VISIBLE LWS_EXTERN int
165172
lws_genecdsa_create(struct lws_genec_ctx *ctx, struct lws_context *context,
@@ -248,7 +255,9 @@ lws_genecdsa_hash_sign_jws(struct lws_genec_ctx *ctx, const uint8_t *in,
248255
* struct lws_ec_curves array, terminated by an entry with
249256
* .name = NULL, of curves you want to allow
250257
*
251-
* Initializes a geneddsa
258+
* Initializes a geneddsa. \p ctx follows the same lifetime rules as for
259+
* lws_genecdh_create(): zeroed before first use, and create over a ctx that
260+
* is still live fails until lws_genec_destroy() is called on it.
252261
*/
253262
LWS_VISIBLE LWS_EXTERN int
254263
lws_geneddsa_create(struct lws_genec_ctx *ctx, struct lws_context *context,

include/libwebsockets/lws-genrsa.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ struct lws_genrsa_ctx {
104104
* Creates an RSA context with a public key associated with it, formed from
105105
* the key elements in \p el.
106106
*
107+
* \p ctx must be zeroed (eg, using memset) before it is passed to create
108+
* apis the first time; if the ctx is still live from an earlier create, the
109+
* call fails and returns nonzero. After lws_genrsa_destroy() the same ctx
110+
* can be passed to the create apis again.
111+
*
107112
* Mode LGRSAM_PKCS1_1_5 is in widespread use but has weaknesses. It's
108113
* recommended to use LGRSAM_PKCS1_OAEP_PSS for new implementations.
109114
*
@@ -142,6 +147,10 @@ lws_genrsa_destroy_elements(struct lws_gencrypto_keyelem *el);
142147
* Creates a new RSA context and generates a new keypair into it, with \p bits
143148
* bits.
144149
*
150+
* \p ctx follows the same lifetime rules as for lws_genrsa_create(): it must
151+
* be zeroed before first use and must not still be live from an earlier
152+
* create or new_keypair, the call fails otherwise.
153+
*
145154
* Returns 0 for OK or nonzero for error.
146155
*
147156
* Mode LGRSAM_PKCS1_1_5 is in widespread use but has weaknesses. It's

lib/cose/cose_key.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,8 @@ lws_cose_key_generate(struct lws_context *context, cose_param_t cose_kty,
929929
{
930930
struct lws_genec_ctx ctx;
931931

932+
memset(&ctx, 0, sizeof(ctx));
933+
932934
if (cose_kty == LWSCOSE_WKKTV_OKP)
933935
ck->gencrypto_kty = LWS_GENCRYPTO_KTY_OKP;
934936
else

lib/jose/jwe/jwe-ecdh-es-aeskw.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@ lws_jwe_encrypt_ecdh(struct lws_jwe *jwe, char *temp, int *temp_len,
206206
struct lws_genec_ctx ecctx;
207207
struct lws_jwk *ephem = &jwe->jose.recipient[jwe->recip].jwk_ephemeral;
208208

209+
memset(&ecctx, 0, sizeof(ecctx));
210+
209211
if (jwe->jws.jwk->kty != LWS_GENCRYPTO_KTY_EC) {
210212
lwsl_err("%s: unexpected kty %d\n", __func__, jwe->jws.jwk->kty);
211213

lib/jose/jwe/jwe-rsa-aescbc.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ lws_jwe_encrypt_rsa_aes_cbc_hs(struct lws_jwe *jwe,
5151
char ekey[LWS_GENHASH_LARGEST];
5252
struct lws_genrsa_ctx rsactx;
5353

54+
memset(&rsactx, 0, sizeof(rsactx));
55+
5456
if (jwe->jws.jwk->kty != LWS_GENCRYPTO_KTY_RSA) {
5557
lwsl_err("%s: unexpected kty %d\n", __func__, jwe->jws.jwk->kty);
5658

@@ -144,6 +146,8 @@ lws_jwe_auth_and_decrypt_rsa_aes_cbc_hs(struct lws_jwe *jwe)
144146
{
145147
int n;
146148
struct lws_genrsa_ctx rsactx;
149+
150+
memset(&rsactx, 0, sizeof(rsactx));
147151
uint8_t enc_cek[512];
148152

149153
if (jwe->jws.jwk->kty != LWS_GENCRYPTO_KTY_RSA) {

lib/jose/jwe/jwe-rsa-aesgcm.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ lws_jwe_encrypt_rsa_aes_gcm(struct lws_jwe *jwe, char *temp, int *temp_len)
3333
{
3434
int ekbytes = jwe->jose.enc_alg->keybits_fixed / 8;
3535
struct lws_genrsa_ctx rsactx;
36+
37+
memset(&rsactx, 0, sizeof(rsactx));
3638
int n, ret = -1, ot = *temp_len;
3739

3840
if (jwe->jws.jwk->kty != LWS_GENCRYPTO_KTY_RSA) {
@@ -138,6 +140,8 @@ lws_jwe_auth_and_decrypt_rsa_aes_gcm(struct lws_jwe *jwe)
138140
{
139141
int n;
140142
struct lws_genrsa_ctx rsactx;
143+
144+
memset(&rsactx, 0, sizeof(rsactx));
141145
uint8_t enc_cek[LWS_JWE_LIMIT_KEY_ELEMENT_BYTES];
142146

143147
if (jwe->jws.jwk->kty != LWS_GENCRYPTO_KTY_RSA) {

lib/jose/jwk/jwk.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,8 @@ lws_jwk_generate(struct lws_context *context, struct lws_jwk *jwk,
201201
{
202202
struct lws_genrsa_ctx ctx;
203203

204+
memset(&ctx, 0, sizeof(ctx));
205+
204206
lwsl_notice("%s: generating %d bit RSA key\n", __func__, bits);
205207
n = lws_genrsa_new_keypair(context, &ctx, LGRSAM_PKCS1_1_5,
206208
jwk->e, bits);
@@ -227,6 +229,8 @@ lws_jwk_generate(struct lws_context *context, struct lws_jwk *jwk,
227229
{
228230
struct lws_genec_ctx ctx;
229231

232+
memset(&ctx, 0, sizeof(ctx));
233+
230234
if (!curve) {
231235
lwsl_err("%s: must have a named curve\n", __func__);
232236

@@ -252,6 +256,8 @@ lws_jwk_generate(struct lws_context *context, struct lws_jwk *jwk,
252256
{
253257
struct lws_genec_ctx ctx;
254258

259+
memset(&ctx, 0, sizeof(ctx));
260+
255261
if (!curve) {
256262
lwsl_err("%s: must have a named curve\n", __func__);
257263

lib/jose/jws/jws.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,9 @@ lws_jws_sig_confirm(struct lws_jws_map *map_b64, struct lws_jws_map *map,
468468
struct lws_genec_ctx ecdsactx;
469469
struct lws_genrsa_ctx rsactx;
470470
struct lws_genhmac_ctx ctx;
471+
472+
memset(&ecdsactx, 0, sizeof(ecdsactx));
473+
memset(&rsactx, 0, sizeof(rsactx));
471474
struct lws_jose jose;
472475

473476
lws_jose_init(&jose);
@@ -817,6 +820,9 @@ lws_jws_sign_from_b64(struct lws_jose *jose, struct lws_jws *jws,
817820
struct lws_genhmac_ctx hmac_ctx;
818821
struct lws_genec_ctx ecdsactx;
819822
struct lws_genrsa_ctx rsactx;
823+
824+
memset(&ecdsactx, 0, sizeof(ecdsactx));
825+
memset(&rsactx, 0, sizeof(rsactx));
820826
uint8_t *buf;
821827
int n, m;
822828

lib/system/async-dns/dnssec.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,8 @@ lws_dnssec_dnskey_cb(struct lws *wsi, const char *name, const struct addrinfo *d
281281

282282
if (alg == LWS_ADNS_DSA_ECDSAP256SHA256 || alg == LWS_ADNS_DSA_ECDSAP384SHA384) {
283283
struct lws_genec_ctx ctx;
284+
285+
memset(&ctx, 0, sizeof(ctx));
284286
size_t curvelen = (alg == LWS_ADNS_DSA_ECDSAP256SHA256) ? 32 : 48;
285287
enum lws_genhash_types hashtype = (alg == LWS_ADNS_DSA_ECDSAP256SHA256) ? LWS_GENHASH_TYPE_SHA256 : LWS_GENHASH_TYPE_SHA384;
286288

@@ -314,6 +316,8 @@ lws_dnssec_dnskey_cb(struct lws *wsi, const char *name, const struct addrinfo *d
314316
}
315317
} else if (alg == LWS_ADNS_DSA_RSA_SHA256 || alg == LWS_ADNS_DSA_RSA_SHA512) {
316318
struct lws_genrsa_ctx ctx;
319+
320+
memset(&ctx, 0, sizeof(ctx));
317321
enum lws_genhash_types hashtype = (alg == LWS_ADNS_DSA_RSA_SHA256) ? LWS_GENHASH_TYPE_SHA256 : LWS_GENHASH_TYPE_SHA512;
318322

319323
if (key_data_len < 1)

lib/system/auth-dns/sign.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1612,6 +1612,7 @@ lws_auth_dns_verify_zone(struct lws_auth_dns_sign_info *info)
16121612
int raw_l = lws_b64_decode_string(b64, (char *)raw, sizeof(raw));
16131613
if (raw_l > 0) {
16141614
struct lws_genec_ctx *target_genec = (flags == 257) ? &genec_ksk : &genec_zsk;
1615+
lws_genec_destroy(target_genec); /* may already hold an earlier DNSKEY */
16151616
if (lws_genecdsa_create(target_genec, info->cx, NULL) == 0) {
16161617
/* We need to re-construct an ephemeral struct lws_jwk's EC elements
16171618
* from the RAW ANS.1/DNSKEY export format: [flags][proto][alg][key...]

0 commit comments

Comments
 (0)