12
12
13
13
#include <jwt.h>
14
14
15
+ #ifdef USE_KCAPI_MD
16
+ #include <kcapi.h>
17
+ #define KCAPI_MAX_MD_SIZE 64
18
+ #endif
19
+
15
20
/* https://github.com/zhicheng/base64 */
16
21
#include "base64.h"
17
22
@@ -333,6 +338,67 @@ static int __check_key_bits(jwt_t *jwt)
333
338
return 1 ; // LCOV_EXCL_LINE
334
339
}
335
340
341
+ static int sign_sha_hmac (jwt_t * jwt , char * * out , unsigned int * len ,
342
+ const char * str , unsigned int str_len )
343
+ {
344
+ #ifdef USE_KCAPI_MD
345
+ static int skip_kcapi = 0 ;
346
+ size_t key_len ;
347
+ void * key ;
348
+ int ret ;
349
+
350
+ /* If kcapi fails once, we don't try again. */
351
+ if (skip_kcapi )
352
+ goto fallback_ops ; // LCOV_EXCL_LINE
353
+
354
+ key = jwt -> key -> oct .key ;
355
+ key_len = jwt -> key -> oct .len ;
356
+
357
+ * out = jwt_malloc (KCAPI_MAX_MD_SIZE );
358
+ if (* out == NULL )
359
+ return 1 ; // LCOV_EXCL_LINE
360
+
361
+ switch (jwt -> alg ) {
362
+ /* HMAC */
363
+ case JWT_ALG_HS256 :
364
+ ret = kcapi_md_hmac_sha256 (key , key_len , (uint8_t * )str ,
365
+ str_len , (uint8_t * )* out ,
366
+ KCAPI_MAX_MD_SIZE );
367
+ break ;
368
+ case JWT_ALG_HS384 :
369
+ ret = kcapi_md_hmac_sha384 (key , key_len , (uint8_t * )str ,
370
+ str_len , (uint8_t * )* out ,
371
+ KCAPI_MAX_MD_SIZE );
372
+ break ;
373
+ case JWT_ALG_HS512 :
374
+ ret = kcapi_md_hmac_sha512 (key , key_len , (uint8_t * )str ,
375
+ str_len , (uint8_t * )* out ,
376
+ KCAPI_MAX_MD_SIZE );
377
+ break ;
378
+ // LCOV_EXCL_START
379
+ default :
380
+ /* This isn't a failure in kcapi, so just error out */
381
+ jwt_freemem (out );
382
+ return 1 ;
383
+ // LCOV_EXCL_STOP
384
+ }
385
+
386
+ if (ret > 0 ) {
387
+ * len = ret ;
388
+ return 0 ;
389
+ }
390
+
391
+ /* Fallthrough to normal ops */
392
+ // LCOV_EXCL_START
393
+ jwt_freemem (* out );
394
+ skip_kcapi = 1 ;
395
+ // LCOV_EXCL_STOP
396
+
397
+ fallback_ops :
398
+ #endif
399
+ return jwt_ops -> sign_sha_hmac (jwt , out , len , str , str_len );
400
+ }
401
+
336
402
int jwt_sign (jwt_t * jwt , char * * out , unsigned int * len , const char * str ,
337
403
unsigned int str_len )
338
404
{
@@ -343,7 +409,7 @@ int jwt_sign(jwt_t *jwt, char **out, unsigned int *len, const char *str,
343
409
case JWT_ALG_HS512 :
344
410
if (__check_hmac (jwt ))
345
411
return 1 ;
346
- if (jwt_ops -> sign_sha_hmac (jwt , out , len , str , str_len )) {
412
+ if (sign_sha_hmac (jwt , out , len , str , str_len )) {
347
413
/* There's not really a way to induce failure here,
348
414
* and there's not really much of a chance this can fail
349
415
* other than an internal fatal error in the crypto
@@ -415,7 +481,7 @@ jwt_t *jwt_verify_sig(jwt_t *jwt, const char *head, unsigned int head_len,
415
481
const char * sig_b64 )
416
482
{
417
483
int sig_len ;
418
- unsigned char * sig = NULL ;
484
+ char_auth * sig = NULL ;
419
485
420
486
switch (jwt -> alg ) {
421
487
/* HMAC */
@@ -455,8 +521,6 @@ jwt_t *jwt_verify_sig(jwt_t *jwt, const char *head, unsigned int head_len,
455
521
456
522
if (jwt_ops -> verify_sha_pem (jwt , head , head_len , sig , sig_len ))
457
523
jwt_write_error (jwt , "Token failed verification" );
458
-
459
- jwt_freemem (sig );
460
524
break ;
461
525
462
526
/* You wut, mate? */
0 commit comments