@@ -315,78 +315,57 @@ void wolfssl_priv_der_unblind(DerBuffer* key, DerBuffer* mask)
315315 const unsigned char* secret, int secretSz, void* ctx);
316316#endif
317317
318-
319- /* Label string for client random. */
320- #define SSC_CR "CLIENT_RANDOM"
321-
322318 /*
323319 * This function builds up string for key-logging then call user's
324- * key-log-callback to pass the string for TLS1.2 and older .
320+ * key-log-callback to pass the string.
325321 * The user's key-logging callback has been set via
326322 * wolfSSL_CTX_set_keylog_callback function. The logging string format is:
327- * "CLIENT_RANDOM <hex-encoded client random> <hex-encoded master-secret>"
323+ * "<Label> <hex-encoded client random> <hex-encoded secret>"
324+ *
328325 * parameter
329326 * - ssl: WOLFSSL object
330- * - secret: pointer to the buffer holding master- secret
327+ * - secret: pointer to the buffer holding secret
331328 * - secretSz: size of secret
332- * - ctx: not used
329+ * - label: for logging string
330+ * - labelSz: label size
333331 * returns 0 on success, negative value on failure.
334332 */
335- static int SessionSecret_callback(WOLFSSL* ssl, void* secret,
336- int* secretSz, void* ctx)
333+ static int SessionSecret_callback_common(const WOLFSSL* ssl,
334+ const unsigned char* secret, int secretSz,
335+ const char* label, int labelSz)
337336 {
338337 wolfSSL_CTX_keylog_cb_func logCb = NULL;
339- int msSz;
340- int invalidCount;
341- int i;
342- const char* label = SSC_CR;
343- int labelSz = sizeof(SSC_CR);
344338 int buffSz;
345339 byte* log = NULL;
346340 word32 outSz;
347341 int idx;
348342 int ret;
349- (void)ctx;
350343
351- if (ssl == NULL || secret == NULL || secretSz == NULL || *secretSz == 0)
344+ if (ssl == NULL || secret == NULL || secretSz == 0 ||
345+ label == NULL || labelSz == 0)
352346 return BAD_FUNC_ARG;
353- if (ssl->arrays == NULL)
347+ if (ssl->arrays == NULL || ssl->ctx == NULL )
354348 return BAD_FUNC_ARG;
355349
356350 /* get the user-callback func from CTX */
357351 logCb = ssl->ctx->keyLogCb;
358- if (logCb == NULL) {
359- return 0; /* no logging callback */
360- }
361-
362- /* make sure the given master-secret has a meaningful value */
363- msSz = *secretSz;
364- invalidCount = 0;
365- for (i = 0; i < msSz; i++) {
366- if (((byte*)secret)[i] == 0) {
367- invalidCount++;
368- }
369- }
370- if (invalidCount == *secretSz) {
371- WOLFSSL_MSG("master-secret is not valid");
372- return 0; /* ignore error */
373- }
352+ if (logCb == NULL)
353+ return 0;
374354
375- /* build up a hex-decoded keylog string
376- * "CLIENT_RANDOM <hex-encoded client rand> <hex-encoded master-secret>"
377- * note that each keylog string does not have CR/LF.
378- */
379- buffSz = labelSz + (RAN_LEN * 2) + 1 + ((*secretSz) * 2) + 1;
380- log = XMALLOC(buffSz, ssl->heap, DYNAMIC_TYPE_SECRET);
355+ /* prepare a log string for passing user callback
356+ * "<Label> <hex-encoded client random> <hex-encoded secret>" */
357+ buffSz = labelSz + (RAN_LEN * 2) + 1 + secretSz * 2 + 1;
358+ log = XMALLOC(buffSz, ssl->heap, DYNAMIC_TYPE_SECRET);
381359 if (log == NULL)
382360 return MEMORY_E;
383361#ifdef WOLFSSL_CHECK_MEM_ZERO
384362 wc_MemZero_Add("SessionSecret log", log, buffSz);
385363#endif
386364
387365 XMEMSET(log, 0, buffSz);
388- XMEMCPY(log, label, labelSz -1); /* put label w/o terminator */
366+ XMEMCPY(log, label, labelSz - 1); /* put label w/o terminator */
389367 log[labelSz - 1] = ' '; /* '\0' -> ' ' */
368+
390369 idx = labelSz;
391370 outSz = buffSz - idx;
392371 if ((ret = Base16_Encode(ssl->arrays->clientRandom, RAN_LEN,
@@ -395,26 +374,66 @@ void wolfssl_priv_der_unblind(DerBuffer* key, DerBuffer* mask)
395374 outSz = buffSz - idx;
396375
397376 if (outSz > 1) {
398- log[idx++] = ' '; /* add space*/
377+ log[idx++] = ' '; /* add space*/
399378 outSz = buffSz - idx;
400379
401- if ((ret = Base16_Encode((byte*)secret, *secretSz,
402- log + idx, &outSz)) == 0) {
403- /* pass the log to the client callback*/
380+ if ((ret = Base16_Encode((byte*)secret, secretSz,
381+ log + idx, &outSz)) == 0) {
404382 logCb(ssl, (char*)log);
405383 ret = 0;
406384 }
407385 }
408- else {
409- ret = BUFFER_E;
410- }
386+ else
387+ ret = MEMORY_E;
411388 }
412389 /* Zero out Base16 encoded secret and other data. */
413390 ForceZero(log, buffSz);
414391 XFREE(log, ssl->heap, DYNAMIC_TYPE_SECRET);
415392 return ret;
416393 }
417394
395+ /* Label string for client random. */
396+ #define SSC_CR "CLIENT_RANDOM"
397+
398+ /*
399+ * This function builds up string for key-logging then call user's
400+ * key-log-callback to pass the string for TLS1.2 and older.
401+ * The user's key-logging callback has been set via
402+ * wolfSSL_CTX_set_keylog_callback function. The logging string format is:
403+ * "CLIENT_RANDOM <hex-encoded client random> <hex-encoded master-secret>"
404+ * parameter
405+ * - ssl: WOLFSSL object
406+ * - secret: pointer to the buffer holding master-secret
407+ * - secretSz: size of secret
408+ * - ctx: not used
409+ * returns 0 on success, negative value on failure.
410+ */
411+ static int SessionSecret_callback(WOLFSSL* ssl, void* secret,
412+ int* secretSz, void* ctx)
413+ {
414+ int invalidCount;
415+ int i;
416+ (void)ctx;
417+
418+ if (secret == NULL || secretSz == NULL || *secretSz == 0)
419+ return BAD_FUNC_ARG;
420+
421+ /* make sure the given master-secret has a meaningful value */
422+ invalidCount = 0;
423+ for (i = 0; i < *secretSz; i++) {
424+ if (((byte*)secret)[i] == 0) {
425+ invalidCount++;
426+ }
427+ }
428+ if (invalidCount == *secretSz) {
429+ WOLFSSL_MSG("master-secret is not valid");
430+ return 0; /* ignore error */
431+ }
432+
433+ return SessionSecret_callback_common(ssl, secret, *secretSz,
434+ SSC_CR, sizeof(SSC_CR));
435+ }
436+
418437#if defined(WOLFSSL_TLS13)
419438
420439 /* Label string for client early traffic secret. */
@@ -450,27 +469,10 @@ void wolfssl_priv_der_unblind(DerBuffer* key, DerBuffer* mask)
450469 static int SessionSecret_callback_Tls13(WOLFSSL* ssl, int id,
451470 const unsigned char* secret, int secretSz, void* ctx)
452471 {
453- wolfSSL_CTX_keylog_cb_func logCb = NULL;
454472 const char* label;
455473 int labelSz = 0;
456- int buffSz = 0;
457- byte* log = NULL;
458- word32 outSz;
459- int idx;
460- int ret;
461-
462474 (void)ctx;
463475
464- if (ssl == NULL || secret == NULL || secretSz == 0)
465- return BAD_FUNC_ARG;
466- if (ssl->arrays == NULL)
467- return BAD_FUNC_ARG;
468-
469- /* get the user-callback func from CTX*/
470- logCb = ssl->ctx->keyLogCb;
471- if (logCb == NULL)
472- return 0;
473-
474476 switch (id) {
475477 case CLIENT_EARLY_TRAFFIC_SECRET:
476478 labelSz = sizeof(SSC_TLS13_CETS);
@@ -510,44 +512,8 @@ void wolfssl_priv_der_unblind(DerBuffer* key, DerBuffer* mask)
510512 default:
511513 return BAD_FUNC_ARG;
512514 }
513- /* prepare a log string for passing user callback
514- * "<Label> <hex-encoded client random> <hex-encoded secret>" */
515- buffSz = labelSz + (RAN_LEN * 2) + 1 + secretSz * 2 + 1;
516- log = XMALLOC(buffSz, ssl->heap, DYNAMIC_TYPE_SECRET);
517- if (log == NULL)
518- return MEMORY_E;
519- #ifdef WOLFSSL_CHECK_MEM_ZERO
520- wc_MemZero_Add("SessionSecret log", log, buffSz);
521- #endif
522-
523- XMEMSET(log, 0, buffSz);
524- XMEMCPY(log, label, labelSz - 1); /* put label w/o terminator */
525- log[labelSz - 1] = ' '; /* '\0' -> ' ' */
526-
527- idx = labelSz;
528- outSz = buffSz - idx;
529- if ((ret = Base16_Encode(ssl->arrays->clientRandom, RAN_LEN,
530- log + idx, &outSz)) == 0) {
531- idx += (outSz - 1); /* reduce terminator byte */
532- outSz = buffSz - idx;
533-
534- if (outSz >1) {
535- log[idx++] = ' '; /* add space*/
536- outSz = buffSz - idx;
537-
538- if ((ret = Base16_Encode((byte*)secret, secretSz,
539- log + idx, &outSz)) == 0) {
540- logCb(ssl, (char*)log);
541- ret = 0;
542- }
543- }
544- else
545- ret = MEMORY_E;
546- }
547- /* Zero out Base16 encoded secret and other data. */
548- ForceZero(log, buffSz);
549- XFREE(log, ssl->heap, DYNAMIC_TYPE_SECRET);
550- return ret;
515+ return SessionSecret_callback_common(ssl, secret, secretSz,
516+ label, labelSz);
551517 }
552518#endif /* WOLFSSL_TLS13*/
553519#endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK*/
0 commit comments