@@ -175,13 +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+ whNvmAccess access , whNvmFlags flags ,
179+ const uint8_t * label , whNvmSize label_len ,
178180 const uint8_t * cert , uint32_t cert_len )
179181{
180- int rc ;
181- /* TODO: Properly set access and flags */
182- whNvmAccess access = WH_NVM_ACCESS_ANY ;
183- whNvmFlags flags = WH_NVM_FLAGS_IMMUTABLE ;
184- uint8_t label [WH_NVM_LABEL_LEN ] = "trusted_cert" ;
182+ int rc ;
185183 whNvmMetadata metadata ;
186184
187185 if ((server == NULL ) || (cert == NULL ) || (cert_len == 0 )) {
@@ -193,7 +191,16 @@ int wh_Server_CertAddTrusted(whServerContext* server, whNvmId id,
193191 metadata .access = access ;
194192 metadata .flags = flags ;
195193 metadata .len = cert_len ;
196- memcpy (metadata .label , label , sizeof (label ));
194+ memset (metadata .label , 0 , WH_NVM_LABEL_LEN );
195+ if (label != NULL && label_len > 0 ) {
196+ whNvmSize copy_len =
197+ (label_len > WH_NVM_LABEL_LEN ) ? WH_NVM_LABEL_LEN : label_len ;
198+ memcpy (metadata .label , label , copy_len );
199+ }
200+ else {
201+ /* Default label if none provided */
202+ memcpy (metadata .label , "trusted_cert" , sizeof ("trusted_cert" ));
203+ }
197204
198205 rc = wh_Nvm_AddObject (server -> nvm , & metadata , cert_len , cert );
199206
@@ -387,8 +394,9 @@ int wh_Server_HandleCertRequest(whServerContext* server, uint16_t magic,
387394 cert_data = (const uint8_t * )req_packet + sizeof (req );
388395
389396 /* Process the add trusted action */
390- rc = wh_Server_CertAddTrusted (server , req .id , cert_data ,
391- req .cert_len );
397+ rc = wh_Server_CertAddTrusted (server , req .id , req .access , req .flags ,
398+ req .label , WH_NVM_LABEL_LEN ,
399+ cert_data , req .cert_len );
392400 resp .rc = rc ;
393401
394402 /* Convert the response struct */
@@ -416,29 +424,50 @@ int wh_Server_HandleCertRequest(whServerContext* server, uint16_t magic,
416424 }; break ;
417425
418426 case WH_MESSAGE_CERT_ACTION_READTRUSTED : {
427+ const uint32_t max_transport_cert_len =
428+ WOLFHSM_CFG_COMM_DATA_LEN -
429+ sizeof (whMessageCert_ReadTrustedResponse );
419430 whMessageCert_ReadTrustedRequest req = {0 };
420431 whMessageCert_ReadTrustedResponse resp = {0 };
421432 uint8_t * cert_data ;
422433 uint32_t cert_len ;
434+ whNvmMetadata meta ;
423435
424436 /* Convert request struct */
425437 wh_MessageCert_TranslateReadTrustedRequest (
426438 magic , (whMessageCert_ReadTrustedRequest * )req_packet , & req );
427439
428440 /* Get pointer to certificate data buffer */
429441 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 ;
442+ cert_len = WOLFHSM_CFG_MAX_CERT_SIZE > max_transport_cert_len
443+ ? max_transport_cert_len
444+ : WOLFHSM_CFG_MAX_CERT_SIZE ;
445+
446+ /* Check metadata to check if the certificate is non-exportable.
447+ * This is unfortunately redundant since metadata is checked in
448+ * wh_Server_CertReadTrusted(). */
449+ rc = wh_Nvm_GetMetadata (server -> nvm , req .id , & meta );
450+ if (rc == WH_ERROR_OK ) {
451+ /* Check if the certificate is non-exportable */
452+ if (meta .flags & WH_NVM_FLAGS_NONEXPORTABLE ) {
453+ resp .rc = WH_ERROR_ACCESS ;
454+ }
455+ else {
456+ rc = wh_Server_CertReadTrusted (server , req .id , cert_data ,
457+ & cert_len );
458+ resp .rc = rc ;
459+ resp .cert_len = cert_len ;
460+ }
461+ }
462+ else {
463+ resp .rc = rc ;
464+ resp .cert_len = 0 ;
465+ }
437466
438467 /* Convert the response struct */
439468 wh_MessageCert_TranslateReadTrustedResponse (
440469 magic , & resp , (whMessageCert_ReadTrustedResponse * )resp_packet );
441- * out_resp_size = sizeof (resp ) + cert_len ;
470+ * out_resp_size = sizeof (resp ) + resp . cert_len ;
442471 }; break ;
443472
444473 case WH_MESSAGE_CERT_ACTION_VERIFY : {
@@ -487,7 +516,7 @@ int wh_Server_HandleCertRequest(whServerContext* server, uint16_t magic,
487516 /* Request is malformed */
488517 resp .rc = WH_ERROR_ABORTED ;
489518 }
490- if (resp .rc == 0 ) {
519+ if (resp .rc == WH_ERROR_OK ) {
491520 /* Convert request struct */
492521 wh_MessageCert_TranslateAddTrustedDmaRequest (
493522 magic , (whMessageCert_AddTrustedDmaRequest * )req_packet ,
@@ -498,12 +527,13 @@ int wh_Server_HandleCertRequest(whServerContext* server, uint16_t magic,
498527 server , req .cert_addr , & cert_data , req .cert_len ,
499528 WH_DMA_OPER_CLIENT_READ_PRE , (whServerDmaFlags ){0 });
500529 }
501- if (resp .rc == 0 ) {
530+ if (resp .rc == WH_ERROR_OK ) {
502531 /* Process the add trusted action */
503- resp .rc = wh_Server_CertAddTrusted (server , req .id , cert_data ,
504- req .cert_len );
532+ resp .rc = wh_Server_CertAddTrusted (
533+ server , req .id , req .access , req .flags , req .label ,
534+ WH_NVM_LABEL_LEN , cert_data , req .cert_len );
505535 }
506- if (resp .rc == 0 ) {
536+ if (resp .rc == WH_ERROR_OK ) {
507537 /* Post-process client address */
508538 resp .rc = wh_Server_DmaProcessClientAddress (
509539 server , req .cert_addr , & cert_data , req .cert_len ,
@@ -521,12 +551,13 @@ int wh_Server_HandleCertRequest(whServerContext* server, uint16_t magic,
521551 whMessageCert_SimpleResponse resp = {0 };
522552 void * cert_data = NULL ;
523553 uint32_t cert_len ;
554+ whNvmMetadata meta ;
524555
525556 if (req_size != sizeof (req )) {
526557 /* Request is malformed */
527558 resp .rc = WH_ERROR_ABORTED ;
528559 }
529- if (resp .rc == 0 ) {
560+ if (resp .rc == WH_ERROR_OK ) {
530561 /* Convert request struct */
531562 wh_MessageCert_TranslateReadTrustedDmaRequest (
532563 magic , (whMessageCert_ReadTrustedDmaRequest * )req_packet ,
@@ -537,13 +568,22 @@ int wh_Server_HandleCertRequest(whServerContext* server, uint16_t magic,
537568 server , req .cert_addr , & cert_data , req .cert_len ,
538569 WH_DMA_OPER_CLIENT_WRITE_PRE , (whServerDmaFlags ){0 });
539570 }
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 );
571+ if (resp .rc == WH_ERROR_OK ) {
572+ /* Check metadata to see if the certificate is non-exportable */
573+ resp .rc = wh_Nvm_GetMetadata (server -> nvm , req .id , & meta );
574+ if (resp .rc == WH_ERROR_OK ) {
575+ if ((meta .flags & WH_NVM_FLAGS_NONEXPORTABLE ) != 0 ) {
576+ resp .rc = WH_ERROR_ACCESS ;
577+ }
578+ else {
579+ /* Clamp cert_len to actual stored length */
580+ cert_len = req .cert_len ;
581+ resp .rc = wh_Server_CertReadTrusted (
582+ server , req .id , cert_data , & cert_len );
583+ }
584+ }
545585 }
546- if (resp .rc == 0 ) {
586+ if (resp .rc == WH_ERROR_OK ) {
547587 /* Post-process client address */
548588 resp .rc = wh_Server_DmaProcessClientAddress (
549589 server , req .cert_addr , & cert_data , cert_len ,
@@ -565,7 +605,7 @@ int wh_Server_HandleCertRequest(whServerContext* server, uint16_t magic,
565605 /* Request is malformed */
566606 resp .rc = WH_ERROR_ABORTED ;
567607 }
568- if (resp .rc == 0 ) {
608+ if (resp .rc == WH_ERROR_OK ) {
569609 /* Convert request struct */
570610 wh_MessageCert_TranslateVerifyDmaRequest (
571611 magic , (whMessageCert_VerifyDmaRequest * )req_packet , & req );
@@ -575,7 +615,7 @@ int wh_Server_HandleCertRequest(whServerContext* server, uint16_t magic,
575615 server , req .cert_addr , & cert_data , req .cert_len ,
576616 WH_DMA_OPER_CLIENT_READ_PRE , (whServerDmaFlags ){0 });
577617 }
578- if (resp .rc == 0 ) {
618+ if (resp .rc == WH_ERROR_OK ) {
579619 /* Map client keyId to server keyId space */
580620 whKeyId keyId = WH_MAKE_KEYID (
581621 WH_KEYTYPE_CRYPTO , server -> comm -> client_id , req .keyId );
@@ -588,7 +628,7 @@ int wh_Server_HandleCertRequest(whServerContext* server, uint16_t magic,
588628 /* Propagate the keyId back to the client */
589629 resp .keyId = WH_KEYID_ID (keyId );
590630 }
591- if (resp .rc == 0 ) {
631+ if (resp .rc == WH_ERROR_OK ) {
592632 /* Post-process client address */
593633 resp .rc = wh_Server_DmaProcessClientAddress (
594634 server , req .cert_addr , & cert_data , req .cert_len ,
0 commit comments