📄 template.c
字号:
new_attr = (CK_ATTRIBUTE *)malloc(len); if (!new_attr){ st_err_log(0, __FILE__, __LINE__); return CKR_HOST_MEMORY; } memcpy( new_attr, attr, len ); new_attr->pValue = (CK_BYTE *)new_attr + sizeof(CK_ATTRIBUTE); dest->attribute_list = dlist_add_as_first( dest->attribute_list, new_attr ); node = node->next; } return CKR_OK;}// template_flatten() - this still gets used when saving token objects to disk//CK_RVtemplate_flatten( TEMPLATE * tmpl, CK_BYTE * dest ){ DL_NODE * node = NULL; CK_BYTE * ptr = NULL; CK_ULONG_32 long_len; CK_ATTRIBUTE_32 *attr_32; CK_ULONG Val; CK_ULONG_32 Val_32; CK_ULONG * pVal; CK_ULONG_32 * pVal_32; long_len = sizeof(CK_ULONG); if (!tmpl || !dest){ st_err_log(4, __FILE__, __LINE__, __FUNCTION__); return CKR_FUNCTION_FAILED; } ptr = dest; node = tmpl->attribute_list; while (node) { CK_ATTRIBUTE *attr = (CK_ATTRIBUTE *)node->data; if (long_len == 4) { memcpy( ptr, attr, sizeof(CK_ATTRIBUTE) + attr->ulValueLen ); ptr += sizeof(CK_ATTRIBUTE) + attr->ulValueLen; } else { attr_32 = malloc(sizeof(CK_ATTRIBUTE_32)); if (!attr_32) { st_err_log(0, __FILE__, __LINE__); return CKR_HOST_MEMORY; } attr_32->type = attr->type; attr_32->pValue = 0x00; if ( ( attr->type == CKA_CLASS || attr->type == CKA_KEY_TYPE || attr->type == CKA_MODULUS_BITS || attr->type == CKA_VALUE_BITS || attr->type == CKA_CERTIFICATE_TYPE || attr->type == CKA_VALUE_LEN ) && attr->ulValueLen != 0 ) { attr_32->ulValueLen = sizeof(CK_ULONG_32); memcpy( ptr, attr_32, sizeof(CK_ATTRIBUTE_32)); ptr += sizeof(CK_ATTRIBUTE_32); pVal = (CK_ULONG *)attr->pValue; Val = *pVal; Val_32 = (CK_ULONG_32)Val; memcpy( ptr, &Val_32, sizeof(CK_ULONG_32)); ptr += sizeof(CK_ULONG_32); } else { attr_32->ulValueLen = attr->ulValueLen; memcpy( ptr, attr_32, sizeof(CK_ATTRIBUTE_32)); ptr += sizeof(CK_ATTRIBUTE_32); if (attr->ulValueLen != 0) { memcpy( ptr, attr->pValue, attr->ulValueLen); ptr += attr->ulValueLen; } } } node = node->next; } if (long_len != 4) free(attr_32); return CKR_OK;}////CK_RVtemplate_unflatten( TEMPLATE ** new_tmpl, CK_BYTE * buf, CK_ULONG count ){ TEMPLATE * tmpl = NULL; CK_ATTRIBUTE * a2 = NULL; CK_BYTE * ptr = NULL; CK_ULONG i, len; CK_RV rc; CK_ULONG_32 long_len = sizeof(CK_ULONG); CK_ULONG_32 attr_ulong_32; CK_ULONG attr_ulong; CK_ATTRIBUTE * a1_64 = NULL; CK_ATTRIBUTE_32 * a1 = NULL; if (!new_tmpl || !buf){ st_err_log(4, __FILE__, __LINE__, __FUNCTION__); return CKR_FUNCTION_FAILED; } tmpl = (TEMPLATE *)malloc(sizeof(TEMPLATE)); if (!tmpl){ st_err_log(0, __FILE__, __LINE__); return CKR_HOST_MEMORY; } memset( tmpl, 0x0, sizeof(TEMPLATE) ); ptr = buf; for (i=0; i < count; i++) { if(long_len == 4) { a1_64 = (CK_ATTRIBUTE *)ptr; len = sizeof(CK_ATTRIBUTE) + a1_64->ulValueLen; a2 = (CK_ATTRIBUTE *)malloc(len); if (!a2) { template_free( tmpl ); st_err_log(0, __FILE__, __LINE__); return CKR_HOST_MEMORY; } memcpy( a2, a1_64, len ); }else{ a1 = (CK_ATTRIBUTE_32 *)ptr; if ( ( a1->type == CKA_CLASS || a1->type == CKA_KEY_TYPE || a1->type == CKA_MODULUS_BITS || a1->type == CKA_VALUE_BITS || a1->type == CKA_CERTIFICATE_TYPE || a1->type == CKA_VALUE_LEN ) && a1->ulValueLen != 0 ) { len = sizeof(CK_ATTRIBUTE) + sizeof(CK_ULONG); } else { len = sizeof(CK_ATTRIBUTE) + a1->ulValueLen; } a2 = (CK_ATTRIBUTE *)malloc(len); if (!a2){ st_err_log(0, __FILE__, __LINE__); return CKR_HOST_MEMORY; } a2->type = a1->type; if ( ( a1->type == CKA_CLASS || a1->type == CKA_KEY_TYPE || a1->type == CKA_MODULUS_BITS || a1->type == CKA_VALUE_BITS || a1->type == CKA_CERTIFICATE_TYPE || a1->type == CKA_VALUE_LEN ) && a1->ulValueLen != 0 ) { a2->ulValueLen = sizeof(CK_ULONG); { CK_ULONG_32 *p32; CK_BYTE *pb2; pb2 = (CK_BYTE *)a1; pb2 += sizeof (CK_ATTRIBUTE_32); p32 = (CK_ULONG_32 *)pb2; attr_ulong_32 = *p32; } attr_ulong = attr_ulong_32; { CK_BYTE *pb2; pb2 = (CK_BYTE *)a2; pb2 += sizeof(CK_ATTRIBUTE); memcpy( pb2 , (CK_BYTE *)&attr_ulong, sizeof(CK_ULONG)); } } else { CK_BYTE *pb2,*pb; a2->ulValueLen = a1->ulValueLen; pb2 = (CK_BYTE *)a2; pb2 += sizeof(CK_ATTRIBUTE); pb = (CK_BYTE *)a1; pb += sizeof(CK_ATTRIBUTE_32); memcpy( pb2, pb, a1->ulValueLen ); } } if (a2->ulValueLen != 0) a2->pValue = (CK_BYTE *)a2 + sizeof(CK_ATTRIBUTE); else a2->pValue = NULL; rc = template_update_attribute( tmpl, a2 ); if (rc != CKR_OK) { free( a2 ); template_free( tmpl ); return rc; } if(long_len == 4) ptr += len; else ptr += sizeof(CK_ATTRIBUTE_32) + a1->ulValueLen; } *new_tmpl = tmpl; return CKR_OK;}// template_free()//CK_RVtemplate_free( TEMPLATE *tmpl ){ if (!tmpl) return CKR_OK; while (tmpl->attribute_list) { CK_ATTRIBUTE *attr = (CK_ATTRIBUTE *)tmpl->attribute_list->data; if (attr) free( attr ); tmpl->attribute_list = dlist_remove_node( tmpl->attribute_list, tmpl->attribute_list ); } free( tmpl ); return CKR_OK;}// template_get_class//CK_BBOOLtemplate_get_class( TEMPLATE * tmpl, CK_ULONG * class, CK_ULONG * subclass ){ DL_NODE * node; CK_BBOOL found; if (!tmpl || !class || !subclass) return FALSE; node = tmpl->attribute_list; // have to iterate through all attributes. no early exits // while (node) { CK_ATTRIBUTE *attr = (CK_ATTRIBUTE *)node->data; if (attr->type == CKA_CLASS) { *class = *(CK_OBJECT_CLASS *)attr->pValue; found = TRUE; } // underneath, these guys are both CK_ULONG so we could combine this // if (attr->type == CKA_CERTIFICATE_TYPE) *subclass = *(CK_CERTIFICATE_TYPE *)attr->pValue; if (attr->type == CKA_KEY_TYPE) *subclass = *(CK_KEY_TYPE *)attr->pValue; node = node->next; } return found;}////CK_ULONGtemplate_get_count( TEMPLATE *tmpl ){ if (tmpl == NULL) return 0; return dlist_length( tmpl->attribute_list );}////CK_ULONGtemplate_get_size( TEMPLATE *tmpl ){ DL_NODE * node; CK_ULONG size = 0; if (tmpl == NULL) return 0; node = tmpl->attribute_list; while (node) { CK_ATTRIBUTE *attr = (CK_ATTRIBUTE *)node->data; size += sizeof(CK_ATTRIBUTE) + attr->ulValueLen; node = node->next; } return size;}////CK_ULONGtemplate_get_compressed_size( TEMPLATE *tmpl ){ DL_NODE * node; CK_ULONG size = 0; if (tmpl == NULL) return 0; node = tmpl->attribute_list; while (node) { CK_ATTRIBUTE *attr = (CK_ATTRIBUTE *)node->data; size += sizeof(CK_ATTRIBUTE_32); if ( ( attr->type == CKA_CLASS || attr->type == CKA_KEY_TYPE || attr->type == CKA_MODULUS_BITS || attr->type == CKA_VALUE_BITS || attr->type == CKA_CERTIFICATE_TYPE || attr->type == CKA_VALUE_LEN ) && attr->ulValueLen != 0 ) { size += sizeof(CK_ULONG_32); } else { size += attr->ulValueLen; } node = node->next; } return size;}// template_is_okay_to_reveal_attribute()//// determines whether the specified CK_ATTRIBUTE_TYPE is allowed to// be leave the card in the clear. note: the specified template doesn't need// to actually posess an attribute of type 'type'. The template is// provided mainly to determine the object class and subclass//// this routine is called by C_GetAttributeValue which exports the attributes// in the clear. this routine is NOT called when wrapping a key.//CK_BBOOLtemplate_check_exportability( TEMPLATE *tmpl, CK_ATTRIBUTE_TYPE type ){ CK_ATTRIBUTE * attr = NULL; CK_ULONG class; CK_ULONG subclass; CK_BBOOL val; if (!tmpl) return FALSE; // since 'tmpl' belongs to a validated object, it's safe to assume that // the following routine works // template_get_class( tmpl, &class, &subclass ); // Early exits: // 1) CKA_SENSITIVE and CKA_EXTRACTABLE only apply to private key // and secret key objects. If object type is any other, then // by default the attribute is exportable. // // 2) If CKA_SENSITIVE = FALSE then all attributes are exportable // if (class != CKO_PRIVATE_KEY && class != CKO_SECRET_KEY) return TRUE; val = template_attribute_find( tmpl, CKA_SENSITIVE, &attr ); if (val) { val = *(CK_BBOOL *)attr->pValue; if (val == FALSE) return TRUE; } else { // technically, we should throw an error here... // return FALSE; } // at this point, we know the object must have CKA_SENSITIVE = TRUE. // need to determine whether the particular attribute in question is // a "sensitive" attribute. // if (class == CKO_PRIVATE_KEY) { switch (subclass) { case CKK_RSA:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -