@@ -82,98 +82,6 @@ static whKeyCacheContext* _GetCacheContext(whServerContext* server,
8282 return & server -> localCache ;
8383}
8484
85- #ifdef WOLFHSM_CFG_KEYWRAP
86- /*
87- * @brief Retrieve the wrapped-key registry associated with keyId
88- */
89- static whWrappedKeyRegistry * _GetWrappedRegistry (whServerContext * server ,
90- whKeyId keyId )
91- {
92- whKeyCacheContext * ctx = _GetCacheContext (server , keyId );
93- return (ctx != NULL ) ? & ctx -> wrappedKeys : NULL ;
94- }
95-
96- /**
97- * @brief Check if keyId is present in a wrapped-key registry
98- *
99- * Note: Registry stores client-relative IDs (0-255), so keyId parameter
100- * should be the extracted client-relative ID, not a full server-encoded keyId.
101- */
102- static int _WrappedRegistryContains (const whWrappedKeyRegistry * registry ,
103- whKeyId keyId )
104- {
105- uint16_t i ;
106-
107- if (registry == NULL ) {
108- return 0 ;
109- }
110-
111- for (i = 0 ; i < registry -> count ; i ++ ) {
112- if (registry -> ids [i ] == keyId ) {
113- return 1 ;
114- }
115- }
116- return 0 ;
117- }
118-
119- /**
120- * @brief Add keyId to a wrapped-key registry if not already present
121- *
122- * Extracts and stores only the client-relative ID portion (0-255) from keyId.
123- * This allows registration before knowing which client will connect.
124- */
125- static int _WrappedRegistryAdd (whWrappedKeyRegistry * registry , whKeyId keyId )
126- {
127- uint8_t clientRelativeId ;
128-
129- if (registry == NULL ) {
130- return WH_ERROR_BADARGS ;
131- }
132-
133- /* Extract client-relative ID portion (0-255) from keyId */
134- clientRelativeId = WH_KEYID_ID (keyId );
135-
136- if (clientRelativeId == WH_KEYID_ERASED ) {
137- return WH_ERROR_BADARGS ;
138- }
139-
140- /* Check if already registered (using client-relative ID) */
141- if (_WrappedRegistryContains (registry , (whKeyId )clientRelativeId )) {
142- return WH_ERROR_OK ;
143- }
144-
145- if (registry -> count >= WOLFHSM_CFG_SERVER_WRAPPED_KEY_COUNT ) {
146- return WH_ERROR_NOSPACE ;
147- }
148-
149- /* Store only the client-relative ID portion */
150- registry -> ids [registry -> count ++ ] = (whKeyId )clientRelativeId ;
151- return WH_ERROR_OK ;
152- }
153-
154- /**
155- * @brief Determine if a keyId is registered as a wrapped key
156- *
157- * Extracts the client-relative ID portion from keyId before checking registry.
158- * This allows the registry to match regardless of the client ID encoded in
159- * keyId.
160- */
161- static int _IsKnownWrappedKey (whServerContext * server , whKeyId keyId )
162- {
163- whWrappedKeyRegistry * registry ;
164- uint8_t clientRelativeId ;
165-
166- /* Get the appropriate registry (local or global) based on keyId encoding */
167- registry = _GetWrappedRegistry (server , keyId );
168-
169- /* Extract client-relative ID (0-255) for comparison */
170- clientRelativeId = WH_KEYID_ID (keyId );
171-
172- /* Registry stores client-relative IDs, so pass extracted ID */
173- return _WrappedRegistryContains (registry , (whKeyId )clientRelativeId );
174- }
175- #endif /* WOLFHSM_CFG_KEYWRAP */
176-
17785/**
17886 * @brief Find a key in the specified cache context
17987 */
@@ -354,19 +262,17 @@ int wh_Server_KeystoreGetUniqueId(whServerContext* server, whNvmId* inout_id)
354262
355263 whKeyCacheContext * ctx = _GetCacheContext (server , key_id );
356264
265+ /* Wrapped keys must be provisioned with explicit identifiers */
266+ if (type == WH_KEYTYPE_WRAPPED ) {
267+ return WH_ERROR_BADARGS ;
268+ }
269+
357270 /* try every index until we find a unique one, don't worry about capacity */
358271 for (id = WH_KEYID_IDMAX ; id > WH_KEYID_ERASED ; id -- ) {
359272 /* id loop var is not an input client ID so we don't need to handle the
360273 * global case */
361274 buildId = WH_MAKE_KEYID (type , user , id );
362275
363- #ifdef WOLFHSM_CFG_KEYWRAP
364- /* Skip over known wrapped keys */
365- if (_IsKnownWrappedKey (server , buildId )) {
366- continue ;
367- }
368- #endif
369-
370276 /* Check against cache keys using unified cache functions */
371277 ret = _FindInKeyCache (ctx , buildId , NULL , NULL , NULL , NULL );
372278 if (ret == WH_ERROR_OK ) {
@@ -579,6 +485,26 @@ static int _ExistsInCache(whServerContext* server, whKeyId keyId)
579485}
580486#endif /* WOLFHSM_CFG_KEYWRAP */
581487
488+ #ifdef WOLFHSM_CFG_KEYWRAP
489+ int wh_Server_KeystoreIsWrappedKey (whServerContext * server , whKeyId keyId ,
490+ int * outIsWrapped )
491+ {
492+ int isWrapped ;
493+
494+ if (server == NULL || WH_KEYID_ISERASED (keyId )) {
495+ return WH_ERROR_BADARGS ;
496+ }
497+
498+ (void )server ;
499+ isWrapped = (WH_KEYID_TYPE (keyId ) == WH_KEYTYPE_WRAPPED );
500+ if (outIsWrapped != NULL ) {
501+ * outIsWrapped = isWrapped ;
502+ }
503+
504+ return WH_ERROR_OK ;
505+ }
506+ #endif /* WOLFHSM_CFG_KEYWRAP */
507+
582508/* try to put the specified key into cache if it isn't already, return pointers
583509 * to meta and the cached data*/
584510int wh_Server_KeystoreFreshenKey (whServerContext * server , whKeyId keyId ,
@@ -593,12 +519,10 @@ int wh_Server_KeystoreFreshenKey(whServerContext* server, whKeyId keyId,
593519 return WH_ERROR_BADARGS ;
594520 }
595521
596- #ifdef WOLFHSM_CFG_KEYWRAP
597- /* Reject attempts to freshen a known wrapped key */
598- if (_IsKnownWrappedKey (server , keyId )) {
522+ /* Reject attempts to freshen wrapped keys from NVM */
523+ if (WH_KEYID_TYPE (keyId ) == WH_KEYTYPE_WRAPPED ) {
599524 return WH_ERROR_ABORTED ;
600525 }
601- #endif
602526
603527 ret = _FindInCache (server , keyId , & foundIndex , & foundBigIndex , outBuf ,
604528 outMeta );
@@ -658,12 +582,10 @@ int wh_Server_KeystoreReadKey(whServerContext* server, whKeyId keyId,
658582 return 0 ;
659583 }
660584
661- #ifdef WOLFHSM_CFG_KEYWRAP
662585 /* Prevent exposing wrapped blobs through the unwrapped read path */
663- if (_IsKnownWrappedKey ( server , keyId )) {
586+ if (WH_KEYID_TYPE ( keyId ) == WH_KEYTYPE_WRAPPED ) {
664587 return WH_ERROR_NOTFOUND ;
665588 }
666- #endif
667589
668590 /* Not in cache, try to read the metadata from NVM */
669591 ret = wh_Nvm_GetMetadata (server -> nvm , keyId , meta );
@@ -737,11 +659,9 @@ int wh_Server_KeystoreCommitKey(whServerContext* server, whNvmId keyId)
737659 return WH_ERROR_BADARGS ;
738660 }
739661
740- #ifdef WOLFHSM_CFG_KEYWRAP
741- if (_IsKnownWrappedKey (server , keyId )) {
662+ if (WH_KEYID_TYPE (keyId ) == WH_KEYTYPE_WRAPPED ) {
742663 return WH_ERROR_ABORTED ;
743664 }
744- #endif
745665
746666 /* Get the appropriate cache context for this key */
747667 ctx = _GetCacheContext (server , keyId );
@@ -765,11 +685,9 @@ int wh_Server_KeystoreEraseKey(whServerContext* server, whNvmId keyId)
765685 return WH_ERROR_BADARGS ;
766686 }
767687
768- #ifdef WOLFHSM_CFG_KEYWRAP
769- if (_IsKnownWrappedKey (server , keyId )) {
688+ if (WH_KEYID_TYPE (keyId ) == WH_KEYTYPE_WRAPPED ) {
770689 return WH_ERROR_ABORTED ;
771690 }
772- #endif
773691
774692 /* remove the key from the cache if present */
775693 (void )wh_Server_KeystoreEvictKey (server , keyId );
@@ -779,52 +697,6 @@ int wh_Server_KeystoreEraseKey(whServerContext* server, whNvmId keyId)
779697}
780698
781699#ifdef WOLFHSM_CFG_KEYWRAP
782- int wh_Server_KeystoreRegisterWrappedKeys (whServerContext * server ,
783- const whKeyId * keyIds , size_t count )
784- {
785- uint16_t i ;
786- int ret ;
787-
788- if (server == NULL || keyIds == NULL || count == 0 ) {
789- return WH_ERROR_BADARGS ;
790- }
791-
792- if (count > WOLFHSM_CFG_SERVER_WRAPPED_KEY_COUNT ) {
793- return WH_ERROR_BADARGS ;
794- }
795-
796- for (i = 0 ; i < count ; i ++ ) {
797- whWrappedKeyRegistry * registry = _GetWrappedRegistry (server , keyIds [i ]);
798- ret = _WrappedRegistryAdd (registry , keyIds [i ]);
799- if (ret != WH_ERROR_OK ) {
800- return ret ;
801- }
802- }
803-
804- return WH_ERROR_OK ;
805- }
806-
807- int wh_Server_KeystoreRegisterWrappedKey (whServerContext * server , whKeyId keyId )
808- {
809- return wh_Server_KeystoreRegisterWrappedKeys (server , & keyId , 1 );
810- }
811-
812- int wh_Server_KeystoreIsWrappedKey (whServerContext * server , whKeyId keyId ,
813- int * outIsWrapped )
814- {
815- int isWrapped ;
816-
817- if (server == NULL || WH_KEYID_ISERASED (keyId )) {
818- return WH_ERROR_BADARGS ;
819- }
820-
821- isWrapped = _IsKnownWrappedKey (server , keyId );
822- if (outIsWrapped != NULL ) {
823- * outIsWrapped = isWrapped ;
824- }
825-
826- return WH_ERROR_OK ;
827- }
828700
829701#ifndef NO_AES
830702#ifdef HAVE_AESGCM
@@ -1001,6 +873,10 @@ static int _HandleWrapKeyRequest(whServerContext* server,
1001873 memcpy (& metadata , reqData , sizeof (metadata ));
1002874 memcpy (key , reqData + sizeof (metadata ), req -> keySz );
1003875
876+ if (!WH_KEYID_ISWRAPPED (metadata .id )) {
877+ return WH_ERROR_CONFIG ;
878+ }
879+
1004880 /* Store the wrapped key in the response data */
1005881 wrappedKey = respData ;
1006882
@@ -1089,6 +965,10 @@ static int _HandleUnwrapAndExportKeyRequest(
1089965 return ret ;
1090966 }
1091967
968+ if (!WH_KEYID_ISWRAPPED (metadata -> id )) {
969+ return WH_ERROR_CONFIG ;
970+ }
971+
1092972 /* Check if the key is exportable */
1093973 if (metadata -> flags & WH_NVM_FLAGS_NONEXPORTABLE ) {
1094974 return WH_ERROR_ACCESS ;
@@ -1177,9 +1057,8 @@ _HandleUnwrapAndCacheKeyRequest(whServerContext* server,
11771057 metadata .id = WH_TRANSLATE_CLIENT_KEYID (
11781058 WH_KEYTYPE_CRYPTO , server -> comm -> client_id , metadata .id );
11791059
1180- /* If not a known wrapped key then there is a configuration error, we can't
1181- * handle unknown wrapped keys in the system */
1182- if (!_IsKnownWrappedKey (server , metadata .id )) {
1060+ /* Require explicit wrapped-key encoding */
1061+ if (WH_KEYID_TYPE (metadata .id ) != WH_KEYTYPE_WRAPPED ) {
11831062 return WH_ERROR_CONFIG ;
11841063 }
11851064
@@ -1191,12 +1070,11 @@ _HandleUnwrapAndCacheKeyRequest(whServerContext* server,
11911070 /* Store the assigned key ID in the response (ID portion only). We should
11921071 * NOT return the upper bits back to the client */
11931072 /* TODO: should we not return the global bit? */
1194- resp -> keyId = WH_KEYID_ID (metadata .id );
1073+ resp -> keyId = WH_KEYID_TO_CLIENT (metadata .id );
11951074
11961075 /* Cache the key */
11971076 return wh_Server_KeystoreCacheKey (server , & metadata , key );
11981077}
1199-
12001078#endif /* WOLFHSM_CFG_KEYWRAP */
12011079
12021080int wh_Server_HandleKeyRequest (whServerContext * server , uint16_t magic ,
0 commit comments