@@ -50,7 +50,7 @@ int wh_Client_KeyWrapRequest(whClientContext* ctx,
5050
5151int wh_Client_KeyWrapResponse (whClientContext * ctx ,
5252 enum wc_CipherType cipherType ,
53- void * wrappedKeyOut , uint16_t wrappedKeySz )
53+ void * wrappedKeyOut , uint16_t * wrappedKeySz )
5454{
5555 int ret ;
5656 uint16_t group ;
@@ -59,7 +59,7 @@ int wh_Client_KeyWrapResponse(whClientContext* ctx,
5959 whMessageKeystore_KeyWrapResponse * resp = NULL ;
6060 uint8_t * respData ;
6161
62- if (ctx == NULL || wrappedKeyOut == NULL ) {
62+ if (ctx == NULL || wrappedKeyOut == NULL || wrappedKeySz == NULL ) {
6363 return WH_ERROR_BADARGS ;
6464 }
6565
@@ -77,31 +77,35 @@ int wh_Client_KeyWrapResponse(whClientContext* ctx,
7777 }
7878
7979 if (group != WH_MESSAGE_GROUP_KEY || action != WH_KEY_KEYWRAP ||
80- size < sizeof (* resp ) || size > sizeof (* resp ) + wrappedKeySz ||
80+ size < sizeof (* resp ) || size > sizeof (* resp ) + resp -> wrappedKeySz ||
8181 resp -> cipherType != cipherType ) {
8282 return WH_ERROR_ABORTED ;
8383 }
8484
8585 if (resp -> rc != 0 ) {
8686 return resp -> rc ;
8787 }
88+ else if (resp -> wrappedKeySz > * wrappedKeySz ) {
89+ return WH_ERROR_BUFFER_SIZE ;
90+ }
8891
8992 /* Copy the wrapped key from the response data into wrappedKeyOut */
9093 respData = (uint8_t * )(resp + 1 );
91- memcpy (wrappedKeyOut , respData , wrappedKeySz );
94+ memcpy (wrappedKeyOut , respData , resp -> wrappedKeySz );
95+ * wrappedKeySz = resp -> wrappedKeySz ;
9296
9397 return WH_ERROR_OK ;
9498}
9599
96100int wh_Client_KeyWrap (whClientContext * ctx , enum wc_CipherType cipherType ,
97101 uint16_t serverKeyId , void * keyIn , uint16_t keySz ,
98102 whNvmMetadata * metadataIn , void * wrappedKeyOut ,
99- uint16_t wrappedKeySz )
103+ uint16_t * wrappedKeySz )
100104{
101105 int ret = WH_ERROR_OK ;
102106
103107 if (ctx == NULL || keyIn == NULL || metadataIn == NULL ||
104- wrappedKeyOut == NULL ) {
108+ wrappedKeyOut == NULL || wrappedKeySz == NULL ) {
105109 return WH_ERROR_BADARGS ;
106110 }
107111
@@ -159,7 +163,7 @@ int wh_Client_KeyUnwrapAndExportRequest(whClientContext* ctx,
159163int wh_Client_KeyUnwrapAndExportResponse (whClientContext * ctx ,
160164 enum wc_CipherType cipherType ,
161165 whNvmMetadata * metadataOut ,
162- void * keyOut , uint16_t keySz )
166+ void * keyOut , uint16_t * keySz )
163167{
164168 int ret ;
165169 uint16_t group ;
@@ -168,7 +172,7 @@ int wh_Client_KeyUnwrapAndExportResponse(whClientContext* ctx,
168172 whMessageKeystore_KeyUnwrapAndExportResponse * resp = NULL ;
169173 uint8_t * respData ;
170174
171- if (ctx == NULL || metadataOut == NULL || keyOut == NULL ) {
175+ if (ctx == NULL || metadataOut == NULL || keyOut == NULL || keySz == NULL ) {
172176 return WH_ERROR_BADARGS ;
173177 }
174178
@@ -188,23 +192,24 @@ int wh_Client_KeyUnwrapAndExportResponse(whClientContext* ctx,
188192
189193 if (group != WH_MESSAGE_GROUP_KEY || action != WH_KEY_KEYUNWRAPEXPORT ||
190194 size < sizeof (* resp ) ||
191- size > sizeof (* resp ) + sizeof (* metadataOut ) + keySz ||
195+ size > sizeof (* resp ) + sizeof (* metadataOut ) + resp -> keySz ||
192196 resp -> cipherType != cipherType ) {
193197 return WH_ERROR_ABORTED ;
194198 }
195199
196200 if (resp -> rc != WH_ERROR_OK ) {
197201 return resp -> rc ;
198202 }
199- else if (resp -> keySz != keySz ) {
203+ else if (resp -> keySz > * keySz ) {
200204 return WH_ERROR_BUFFER_SIZE ;
201205 }
202206
203207 /* Copy the metadata and key from the response data into metadataOut and
204208 * keyOut */
205209 respData = (uint8_t * )(resp + 1 );
206210 memcpy (metadataOut , respData , sizeof (* metadataOut ));
207- memcpy (keyOut , respData + sizeof (* metadataOut ), keySz );
211+ memcpy (keyOut , respData + sizeof (* metadataOut ), resp -> keySz );
212+ * keySz = resp -> keySz ;
208213
209214 return WH_ERROR_OK ;
210215}
@@ -214,12 +219,12 @@ int wh_Client_KeyUnwrapAndExport(whClientContext* ctx,
214219 uint16_t serverKeyId , void * wrappedKeyIn ,
215220 uint16_t wrappedKeySz ,
216221 whNvmMetadata * metadataOut , void * keyOut ,
217- uint16_t keySz )
222+ uint16_t * keySz )
218223{
219224 int ret = WH_ERROR_OK ;
220225
221226 if (ctx == NULL || wrappedKeyIn == NULL || metadataOut == NULL ||
222- keyOut == NULL )
227+ keyOut == NULL || keySz == NULL )
223228 return WH_ERROR_BADARGS ;
224229
225230 ret = wh_Client_KeyUnwrapAndExportRequest (ctx , cipherType , serverKeyId ,
@@ -372,7 +377,7 @@ int wh_Client_DataWrapRequest(whClientContext* ctx,
372377
373378int wh_Client_DataWrapResponse (whClientContext * ctx ,
374379 enum wc_CipherType cipherType ,
375- void * wrappedDataOut , uint32_t wrappedDataSz )
380+ void * wrappedDataOut , uint32_t * wrappedDataSz )
376381{
377382 int ret ;
378383 uint16_t group ;
@@ -381,7 +386,7 @@ int wh_Client_DataWrapResponse(whClientContext* ctx,
381386 whMessageKeystore_DataWrapResponse * resp = NULL ;
382387 uint8_t * respData ;
383388
384- if (ctx == NULL || wrappedDataOut == NULL ) {
389+ if (ctx == NULL || wrappedDataOut == NULL || wrappedDataSz == NULL ) {
385390 return WH_ERROR_BADARGS ;
386391 }
387392
@@ -399,29 +404,33 @@ int wh_Client_DataWrapResponse(whClientContext* ctx,
399404 }
400405
401406 if (group != WH_MESSAGE_GROUP_KEY || action != WH_KEY_DATAWRAP ||
402- size < sizeof (* resp ) || size > sizeof (* resp ) + wrappedDataSz ||
403- resp -> wrappedDataSz != wrappedDataSz ||
407+ size < sizeof (* resp ) || size > sizeof (* resp ) + resp -> wrappedDataSz ||
404408 resp -> cipherType != cipherType ) {
405409 return WH_ERROR_ABORTED ;
406410 }
407411
408412 if (resp -> rc != 0 ) {
409413 return resp -> rc ;
410414 }
415+ else if (resp -> wrappedDataSz > * wrappedDataSz ) {
416+ return WH_ERROR_BUFFER_SIZE ;
417+ }
411418
412419 /* Copy the wrapped key from the response data into wrappedKeyOut */
413420 respData = (uint8_t * )(resp + 1 );
414- memcpy (wrappedDataOut , respData , wrappedDataSz );
421+ memcpy (wrappedDataOut , respData , resp -> wrappedDataSz );
422+ * wrappedDataSz = resp -> wrappedDataSz ;
415423
416424 return WH_ERROR_OK ;
417425}
418426
419427int wh_Client_DataWrap (whClientContext * ctx , enum wc_CipherType cipherType ,
420428 uint16_t serverKeyId , void * dataIn , uint32_t dataInSz ,
421- void * wrappedDataOut , uint32_t wrappedDataOutSz )
429+ void * wrappedDataOut , uint32_t * wrappedDataOutSz )
422430{
423431 int ret ;
424- if (ctx == NULL || wrappedDataOut == NULL || dataIn == NULL ) {
432+ if (ctx == NULL || wrappedDataOut == NULL || dataIn == NULL ||
433+ wrappedDataOutSz == NULL ) {
425434 return WH_ERROR_BADARGS ;
426435 }
427436
@@ -476,7 +485,7 @@ int wh_Client_DataUnwrapRequest(whClientContext* ctx,
476485
477486int wh_Client_DataUnwrapResponse (whClientContext * ctx ,
478487 enum wc_CipherType cipherType , void * dataOut ,
479- uint32_t dataSz )
488+ uint32_t * dataSz )
480489{
481490 int ret ;
482491 uint16_t group ;
@@ -503,28 +512,33 @@ int wh_Client_DataUnwrapResponse(whClientContext* ctx,
503512 }
504513
505514 if (group != WH_MESSAGE_GROUP_KEY || action != WH_KEY_DATAUNWRAP ||
506- size < sizeof (* resp ) || size > sizeof (* resp ) + dataSz ||
507- resp -> dataSz != dataSz || resp -> cipherType != cipherType ) {
515+ size < sizeof (* resp ) || size > sizeof (* resp ) + resp -> dataSz ||
516+ resp -> cipherType != cipherType ) {
508517 return WH_ERROR_ABORTED ;
509518 }
510519
511520 if (resp -> rc != 0 ) {
512521 return resp -> rc ;
513522 }
523+ else if (resp -> dataSz > * dataSz ) {
524+ return WH_ERROR_BUFFER_SIZE ;
525+ }
514526
515527 /* Copy the wrapped key from the response data into wrappedKeyOut */
516528 respData = (uint8_t * )(resp + 1 );
517- memcpy (dataOut , respData , dataSz );
529+ memcpy (dataOut , respData , resp -> dataSz );
530+ * dataSz = resp -> dataSz ;
518531
519532 return WH_ERROR_OK ;
520533}
521534int wh_Client_DataUnwrap (whClientContext * ctx , enum wc_CipherType cipherType ,
522535 uint16_t serverKeyId , void * wrappedDataIn ,
523536 uint32_t wrappedDataInSz , void * dataOut ,
524- uint32_t dataOutSz )
537+ uint32_t * dataOutSz )
525538{
526539 int ret ;
527- if (ctx == NULL || wrappedDataIn == NULL || dataOut == NULL ) {
540+ if (ctx == NULL || wrappedDataIn == NULL || dataOut == NULL ||
541+ dataOutSz == NULL ) {
528542 return WH_ERROR_BADARGS ;
529543 }
530544
0 commit comments