@@ -175,12 +175,11 @@ int wh_Server_CertInit(whServerContext* server)
175175
176176/* Add a trusted certificate to NVM storage */
177177int wh_Server_CertAddTrusted (whServerContext * server , whNvmId id ,
178- const uint8_t * cert , uint32_t cert_len )
178+ const uint8_t * cert , uint32_t cert_len ,
179+ whNvmFlags flags )
179180{
180- int rc ;
181- /* TODO: Properly set access and flags */
181+ int rc ;
182182 whNvmAccess access = WH_NVM_ACCESS_ANY ;
183- whNvmFlags flags = WH_NVM_FLAGS_IMMUTABLE ;
184183 uint8_t label [WH_NVM_LABEL_LEN ] = "trusted_cert" ;
185184 whNvmMetadata metadata ;
186185
@@ -388,7 +387,7 @@ int wh_Server_HandleCertRequest(whServerContext* server, uint16_t magic,
388387
389388 /* Process the add trusted action */
390389 rc = wh_Server_CertAddTrusted (server , req .id , cert_data ,
391- req .cert_len );
390+ req .cert_len , req . flags );
392391 resp .rc = rc ;
393392
394393 /* Convert the response struct */
@@ -416,29 +415,50 @@ int wh_Server_HandleCertRequest(whServerContext* server, uint16_t magic,
416415 }; break ;
417416
418417 case WH_MESSAGE_CERT_ACTION_READTRUSTED : {
418+ const uint32_t max_transport_cert_len =
419+ WOLFHSM_CFG_COMM_DATA_LEN -
420+ sizeof (whMessageCert_ReadTrustedResponse );
419421 whMessageCert_ReadTrustedRequest req = {0 };
420422 whMessageCert_ReadTrustedResponse resp = {0 };
421423 uint8_t * cert_data ;
422424 uint32_t cert_len ;
425+ whNvmMetadata meta ;
423426
424427 /* Convert request struct */
425428 wh_MessageCert_TranslateReadTrustedRequest (
426429 magic , (whMessageCert_ReadTrustedRequest * )req_packet , & req );
427430
428431 /* Get pointer to certificate data buffer */
429432 cert_data = (uint8_t * )resp_packet + sizeof (resp );
430- cert_len = WOLFHSM_CFG_COMM_DATA_LEN - sizeof (resp );
431-
432- /* Process the get trusted action */
433- rc =
434- wh_Server_CertReadTrusted (server , req .id , cert_data , & cert_len );
435- resp .rc = rc ;
436- resp .cert_len = cert_len ;
433+ cert_len = WOLFHSM_CFG_MAX_CERT_SIZE > max_transport_cert_len
434+ ? max_transport_cert_len
435+ : WOLFHSM_CFG_MAX_CERT_SIZE ;
436+
437+ /* Check metadata to check if the certificate is non-exportable.
438+ * This is unfortunately redundant since metadata is checked in
439+ * wh_Server_CertReadTrusted(). */
440+ rc = wh_Nvm_GetMetadata (server -> nvm , req .id , & meta );
441+ if (rc == WH_ERROR_OK ) {
442+ /* Check if the certificate is non-exportable */
443+ if (meta .flags & WH_NVM_FLAGS_NONEXPORTABLE ) {
444+ resp .rc = WH_ERROR_ACCESS ;
445+ }
446+ else {
447+ rc = wh_Server_CertReadTrusted (server , req .id , cert_data ,
448+ & cert_len );
449+ resp .rc = rc ;
450+ resp .cert_len = cert_len ;
451+ }
452+ }
453+ else {
454+ resp .rc = rc ;
455+ resp .cert_len = 0 ;
456+ }
437457
438458 /* Convert the response struct */
439459 wh_MessageCert_TranslateReadTrustedResponse (
440460 magic , & resp , (whMessageCert_ReadTrustedResponse * )resp_packet );
441- * out_resp_size = sizeof (resp ) + cert_len ;
461+ * out_resp_size = sizeof (resp ) + resp . cert_len ;
442462 }; break ;
443463
444464 case WH_MESSAGE_CERT_ACTION_VERIFY : {
@@ -487,7 +507,7 @@ int wh_Server_HandleCertRequest(whServerContext* server, uint16_t magic,
487507 /* Request is malformed */
488508 resp .rc = WH_ERROR_ABORTED ;
489509 }
490- if (resp .rc == 0 ) {
510+ if (resp .rc == WH_ERROR_OK ) {
491511 /* Convert request struct */
492512 wh_MessageCert_TranslateAddTrustedDmaRequest (
493513 magic , (whMessageCert_AddTrustedDmaRequest * )req_packet ,
@@ -498,12 +518,12 @@ int wh_Server_HandleCertRequest(whServerContext* server, uint16_t magic,
498518 server , req .cert_addr , & cert_data , req .cert_len ,
499519 WH_DMA_OPER_CLIENT_READ_PRE , (whServerDmaFlags ){0 });
500520 }
501- if (resp .rc == 0 ) {
521+ if (resp .rc == WH_ERROR_OK ) {
502522 /* Process the add trusted action */
503523 resp .rc = wh_Server_CertAddTrusted (server , req .id , cert_data ,
504- req .cert_len );
524+ req .cert_len , req . flags );
505525 }
506- if (resp .rc == 0 ) {
526+ if (resp .rc == WH_ERROR_OK ) {
507527 /* Post-process client address */
508528 resp .rc = wh_Server_DmaProcessClientAddress (
509529 server , req .cert_addr , & cert_data , req .cert_len ,
@@ -521,12 +541,13 @@ int wh_Server_HandleCertRequest(whServerContext* server, uint16_t magic,
521541 whMessageCert_SimpleResponse resp = {0 };
522542 void * cert_data = NULL ;
523543 uint32_t cert_len ;
544+ whNvmMetadata meta ;
524545
525546 if (req_size != sizeof (req )) {
526547 /* Request is malformed */
527548 resp .rc = WH_ERROR_ABORTED ;
528549 }
529- if (resp .rc == 0 ) {
550+ if (resp .rc == WH_ERROR_OK ) {
530551 /* Convert request struct */
531552 wh_MessageCert_TranslateReadTrustedDmaRequest (
532553 magic , (whMessageCert_ReadTrustedDmaRequest * )req_packet ,
@@ -537,13 +558,22 @@ int wh_Server_HandleCertRequest(whServerContext* server, uint16_t magic,
537558 server , req .cert_addr , & cert_data , req .cert_len ,
538559 WH_DMA_OPER_CLIENT_WRITE_PRE , (whServerDmaFlags ){0 });
539560 }
540- if (resp .rc == 0 ) {
541- /* Process the get trusted action */
542- cert_len = req .cert_len ;
543- resp .rc = wh_Server_CertReadTrusted (server , req .id , cert_data ,
544- & cert_len );
561+ if (resp .rc == WH_ERROR_OK ) {
562+ /* Check metadata to see if the certificate is non-exportable */
563+ resp .rc = wh_Nvm_GetMetadata (server -> nvm , req .id , & meta );
564+ if (resp .rc == WH_ERROR_OK ) {
565+ if ((meta .flags & WH_NVM_FLAGS_NONEXPORTABLE ) != 0 ) {
566+ resp .rc = WH_ERROR_ACCESS ;
567+ }
568+ else {
569+ /* Clamp cert_len to actual stored length */
570+ cert_len = req .cert_len ;
571+ resp .rc = wh_Server_CertReadTrusted (
572+ server , req .id , cert_data , & cert_len );
573+ }
574+ }
545575 }
546- if (resp .rc == 0 ) {
576+ if (resp .rc == WH_ERROR_OK ) {
547577 /* Post-process client address */
548578 resp .rc = wh_Server_DmaProcessClientAddress (
549579 server , req .cert_addr , & cert_data , cert_len ,
@@ -565,7 +595,7 @@ int wh_Server_HandleCertRequest(whServerContext* server, uint16_t magic,
565595 /* Request is malformed */
566596 resp .rc = WH_ERROR_ABORTED ;
567597 }
568- if (resp .rc == 0 ) {
598+ if (resp .rc == WH_ERROR_OK ) {
569599 /* Convert request struct */
570600 wh_MessageCert_TranslateVerifyDmaRequest (
571601 magic , (whMessageCert_VerifyDmaRequest * )req_packet , & req );
@@ -575,7 +605,7 @@ int wh_Server_HandleCertRequest(whServerContext* server, uint16_t magic,
575605 server , req .cert_addr , & cert_data , req .cert_len ,
576606 WH_DMA_OPER_CLIENT_READ_PRE , (whServerDmaFlags ){0 });
577607 }
578- if (resp .rc == 0 ) {
608+ if (resp .rc == WH_ERROR_OK ) {
579609 /* Map client keyId to server keyId space */
580610 whKeyId keyId = WH_MAKE_KEYID (
581611 WH_KEYTYPE_CRYPTO , server -> comm -> client_id , req .keyId );
@@ -588,7 +618,7 @@ int wh_Server_HandleCertRequest(whServerContext* server, uint16_t magic,
588618 /* Propagate the keyId back to the client */
589619 resp .keyId = WH_KEYID_ID (keyId );
590620 }
591- if (resp .rc == 0 ) {
621+ if (resp .rc == WH_ERROR_OK ) {
592622 /* Post-process client address */
593623 resp .rc = wh_Server_DmaProcessClientAddress (
594624 server , req .cert_addr , & cert_data , req .cert_len ,
0 commit comments