37
37
#include "debug_p11.h"
38
38
#include "../common/util.h"
39
39
#include "../common/openssl-compat.h"
40
+ #include "../common/insecure_memzero.h"
40
41
41
42
#define ASN1_OID 0x06
42
43
static const uint8_t oid_secp224r1 [] = {ASN1_OID , 0x05 , 0x2b , 0x81 ,
@@ -1452,33 +1453,22 @@ static CK_RV get_attribute_public_key(CK_ATTRIBUTE_TYPE type,
1452
1453
return CKR_OK ;
1453
1454
}
1454
1455
1455
- CK_RV get_attribute (CK_ATTRIBUTE_TYPE type , yh_object_descriptor * object ,
1456
- CK_VOID_PTR value , CK_ULONG_PTR length ,
1457
- yh_session * session ) {
1458
-
1459
- CK_BYTE tmp [2048 ];
1460
- CK_VOID_PTR ptr ;
1461
- if (value == NULL ) {
1462
- // NOTE(adma): we just need the length, use a scratchpad for the data
1463
- ptr = tmp ;
1464
- * length = sizeof (tmp );
1465
- } else {
1466
- // NOTE(adma): otherwise actually save the data
1467
- ptr = value ;
1468
- }
1456
+ static CK_RV get_attribute (CK_ATTRIBUTE_TYPE type , yh_object_descriptor * object ,
1457
+ CK_BYTE_PTR value , CK_ULONG_PTR length ,
1458
+ yh_session * session ) {
1469
1459
1470
1460
switch (object -> type ) {
1471
1461
case YH_OPAQUE :
1472
- return get_attribute_opaque (type , object , ptr , length , session );
1462
+ return get_attribute_opaque (type , object , value , length , session );
1473
1463
1474
1464
case YH_WRAP_KEY :
1475
1465
case YH_HMAC_KEY :
1476
- return get_attribute_secret_key (type , object , ptr , length );
1466
+ return get_attribute_secret_key (type , object , value , length );
1477
1467
1478
1468
case YH_ASYMMETRIC_KEY :
1479
- return get_attribute_private_key (type , object , ptr , length , session );
1469
+ return get_attribute_private_key (type , object , value , length , session );
1480
1470
case 0x80 | YH_ASYMMETRIC_KEY :
1481
- return get_attribute_public_key (type , object , ptr , length , session );
1471
+ return get_attribute_public_key (type , object , value , length , session );
1482
1472
1483
1473
case YH_TEMPLATE :
1484
1474
case YH_AUTHENTICATION_KEY :
@@ -1490,50 +1480,43 @@ CK_RV get_attribute(CK_ATTRIBUTE_TYPE type, yh_object_descriptor *object,
1490
1480
return CKR_OK ;
1491
1481
}
1492
1482
1493
- CK_RV get_attribute_ecsession_key (CK_ATTRIBUTE_TYPE type , ecdh_session_key * key ,
1494
- CK_VOID_PTR value , CK_ULONG_PTR length ) {
1495
-
1496
- CK_BYTE tmp [2048 ];
1497
- CK_VOID_PTR ptr ;
1498
- if (value == NULL ) {
1499
- ptr = tmp ;
1500
- * length = sizeof (tmp );
1501
- } else {
1502
- ptr = value ;
1503
- }
1483
+ static CK_RV get_attribute_ecsession_key (CK_ATTRIBUTE_TYPE type ,
1484
+ ecdh_session_key * key ,
1485
+ CK_BYTE_PTR value ,
1486
+ CK_ULONG_PTR length ) {
1504
1487
1505
1488
switch (type ) {
1506
1489
case CKA_CLASS :
1507
- * ((CK_OBJECT_CLASS * ) ptr ) = CKO_SECRET_KEY ;
1490
+ * ((CK_OBJECT_CLASS * ) value ) = CKO_SECRET_KEY ;
1508
1491
* length = sizeof (CK_OBJECT_CLASS );
1509
1492
break ;
1510
1493
1511
1494
case CKA_KEY_TYPE :
1512
- * ((CK_KEY_TYPE * ) ptr ) = CKK_GENERIC_SECRET ;
1495
+ * ((CK_KEY_TYPE * ) value ) = CKK_GENERIC_SECRET ;
1513
1496
* length = sizeof (CK_KEY_TYPE );
1514
1497
break ;
1515
1498
1516
1499
case CKA_ID : {
1517
- CK_OBJECT_HANDLE * id = ptr ;
1500
+ CK_OBJECT_HANDLE * id = ( CK_OBJECT_HANDLE * ) value ;
1518
1501
* id = key -> id ;
1519
1502
* length = sizeof (CK_OBJECT_HANDLE );
1520
1503
break ;
1521
1504
}
1522
1505
1523
1506
case CKA_LABEL :
1524
1507
* length = strlen (key -> label );
1525
- memcpy (ptr , key -> label , * length );
1508
+ memcpy (value , key -> label , * length );
1526
1509
break ;
1527
1510
1528
1511
case CKA_LOCAL :
1529
1512
case CKA_TOKEN :
1530
- * ((CK_BBOOL * ) ptr ) = CK_FALSE ;
1513
+ * ((CK_BBOOL * ) value ) = CK_FALSE ;
1531
1514
* length = sizeof (CK_BBOOL );
1532
1515
break ;
1533
1516
1534
1517
case CKA_DESTROYABLE :
1535
1518
case CKA_EXTRACTABLE :
1536
- * ((CK_BBOOL * ) ptr ) = CK_TRUE ;
1519
+ * ((CK_BBOOL * ) value ) = CK_TRUE ;
1537
1520
* length = sizeof (CK_BBOOL );
1538
1521
break ;
1539
1522
@@ -1551,12 +1534,12 @@ CK_RV get_attribute_ecsession_key(CK_ATTRIBUTE_TYPE type, ecdh_session_key *key,
1551
1534
case CKA_WRAP_WITH_TRUSTED :
1552
1535
case CKA_VERIFY :
1553
1536
case CKA_ENCRYPT :
1554
- * ((CK_BBOOL * ) ptr ) = CK_FALSE ;
1537
+ * ((CK_BBOOL * ) value ) = CK_FALSE ;
1555
1538
* length = sizeof (CK_BBOOL );
1556
1539
break ;
1557
1540
1558
1541
case CKA_VALUE :
1559
- memcpy (ptr , key -> ecdh_key , key -> len );
1542
+ memcpy (value , key -> ecdh_key , key -> len );
1560
1543
* length = key -> len ;
1561
1544
break ;
1562
1545
@@ -3873,31 +3856,37 @@ CK_RV populate_template(int type, void *object, CK_ATTRIBUTE_PTR pTemplate,
3873
3856
CK_ULONG ulCount , yh_session * session ) {
3874
3857
3875
3858
CK_RV rv = CKR_OK ;
3859
+ CK_BYTE tmp [8192 ];
3876
3860
3877
3861
for (CK_ULONG i = 0 ; i < ulCount ; i ++ ) {
3878
3862
DBG_INFO ("Getting attribute 0x%lx" , pTemplate [i ].type );
3879
-
3880
- CK_VOID_PTR object_ptr ;
3881
- if (pTemplate [i ].pValue == NULL ) {
3882
- // NOTE(adma): just asking for the length
3883
- object_ptr = NULL ;
3884
- DBG_INFO ("Retrieving length" );
3885
- } else {
3886
- // NOTE(adma): actually get the attribute
3887
- object_ptr = pTemplate [i ].pValue ;
3888
- DBG_INFO ("Retrieving attribute" );
3889
- }
3890
-
3863
+ CK_ULONG len = sizeof (tmp );
3891
3864
CK_RV attribute_rc ;
3865
+
3892
3866
if (type == ECDH_KEY_TYPE ) {
3893
3867
ecdh_session_key * key = object ;
3894
3868
attribute_rc =
3895
- get_attribute_ecsession_key (pTemplate [i ].type , key , object_ptr ,
3896
- & pTemplate [i ].ulValueLen );
3869
+ get_attribute_ecsession_key (pTemplate [i ].type , key , tmp , & len );
3897
3870
} else {
3898
3871
yubihsm_pkcs11_object_desc * desc = object ;
3899
- attribute_rc = get_attribute (pTemplate [i ].type , & desc -> object , object_ptr ,
3900
- & pTemplate [i ].ulValueLen , session );
3872
+ attribute_rc =
3873
+ get_attribute (pTemplate [i ].type , & desc -> object , tmp , & len , session );
3874
+ }
3875
+
3876
+ if (attribute_rc == CKR_OK ) {
3877
+ if (pTemplate [i ].pValue == NULL ) {
3878
+ DBG_INFO ("Retrieving only length which is %lu" , len );
3879
+ pTemplate [i ].ulValueLen = len ;
3880
+ } else if (len > pTemplate [i ].ulValueLen ) {
3881
+ DBG_WARN ("Skipping attribute, buffer to small %lu > %lu" , len ,
3882
+ pTemplate [i ].ulValueLen );
3883
+ attribute_rc = CKR_BUFFER_TOO_SMALL ;
3884
+ pTemplate [i ].ulValueLen = CK_UNAVAILABLE_INFORMATION ;
3885
+ } else {
3886
+ DBG_INFO ("Retrieving attribute value, length is %lu" , len );
3887
+ memcpy (pTemplate [i ].pValue , tmp , len );
3888
+ pTemplate [i ].ulValueLen = len ;
3889
+ }
3901
3890
}
3902
3891
3903
3892
if (attribute_rc != CKR_OK ) {
@@ -3907,7 +3896,7 @@ CK_RV populate_template(int type, void *object, CK_ATTRIBUTE_PTR pTemplate,
3907
3896
} else if (attribute_rc == CKR_BUFFER_TOO_SMALL ) {
3908
3897
DBG_ERR ("Skipping attribute because buffer is too small" );
3909
3898
} else {
3910
- DBG_ERR ("Get attribute failed. %s" , yh_strerror ( attribute_rc ) );
3899
+ DBG_ERR ("Get attribute failed." );
3911
3900
}
3912
3901
} else {
3913
3902
DBG_INFO ("Attribute/length successfully returned with length %lu" ,
@@ -3934,6 +3923,8 @@ CK_RV populate_template(int type, void *object, CK_ATTRIBUTE_PTR pTemplate,
3934
3923
* type having the CKF_ARRAY_ATTRIBUTE bit set.*/
3935
3924
}
3936
3925
3926
+ insecure_memzero (tmp , sizeof (tmp ));
3927
+
3937
3928
return rv ;
3938
3929
}
3939
3930
0 commit comments