Blame usr/lib/common/template.c

Packit 8681c6
/*
Packit 8681c6
 * COPYRIGHT (c) International Business Machines Corp. 2001-2017
Packit 8681c6
 *
Packit 8681c6
 * This program is provided under the terms of the Common Public License,
Packit 8681c6
 * version 1.0 (CPL-1.0). Any use, reproduction or distribution for this
Packit 8681c6
 * software constitutes recipient's acceptance of CPL-1.0 terms which can be
Packit 8681c6
 * found in the file LICENSE file or at
Packit 8681c6
 * https://opensource.org/licenses/cpl1.0.php
Packit 8681c6
 */
Packit 8681c6
Packit 8681c6
/* File:  template.c
Packit 8681c6
 *
Packit 8681c6
 * Attribute template management routines
Packit 8681c6
 *
Packit 8681c6
 * Functions contained in this file:
Packit 8681c6
 *
Packit 8681c6
 *    template_add_attributes
Packit 8681c6
 *    template_add_default_attributes
Packit 8681c6
 *    template_attribute_find
Packit 8681c6
 *    template_check_required_attributes
Packit 8681c6
 *    template_check_required_base_attributes
Packit 8681c6
 *    template_free
Packit 8681c6
 *    template_set_default_common_attributes
Packit 8681c6
 *    template_update_attribute
Packit 8681c6
 *    template_validate_attribute
Packit 8681c6
 *    template_validate_attributes
Packit 8681c6
 *    template_validate_base_attribute
Packit 8681c6
 */
Packit 8681c6
Packit 8681c6
#include <pthread.h>
Packit 8681c6
#include <stdlib.h>
Packit 8681c6
#include <stdio.h>
Packit 8681c6
#include <string.h>
Packit Service 8aa27d
#include <openssl/rand.h>
Packit 8681c6
Packit 8681c6
#include "pkcs11types.h"
Packit 8681c6
#include "defs.h"
Packit 8681c6
#include "host_defs.h"
Packit 8681c6
#include "h_extern.h"
Packit 8681c6
#include "tok_spec_struct.h"
Packit 8681c6
#include "pkcs32.h"
Packit 8681c6
#include "p11util.h"
Packit 8681c6
#include "trace.h"
Packit 8681c6
Packit Service 8aa27d
/* Random 32 byte string is unique with overwhelming probability. */
Packit Service 8aa27d
#define UNIQUE_ID_LEN 32
Packit Service 8aa27d
Packit Service 8aa27d
static CK_RV get_unique_id_str(char unique_id_str[2 * UNIQUE_ID_LEN + 1])
Packit Service 8aa27d
{
Packit Service 8aa27d
    unsigned char buf[UNIQUE_ID_LEN];
Packit Service 8aa27d
    size_t i;
Packit Service 8aa27d
Packit Service 8aa27d
    if (RAND_bytes(buf, sizeof(buf)) != 1)
Packit Service 8aa27d
        return CKR_FUNCTION_FAILED;
Packit Service 8aa27d
Packit Service 8aa27d
    for (i = 0; i < sizeof(buf); i++)
Packit Service 8aa27d
        sprintf(&unique_id_str[i * 2], "%02x", buf[i]);
Packit Service 8aa27d
Packit Service 8aa27d
    return CKR_OK;
Packit Service 8aa27d
}
Packit Service 8aa27d
Packit 8681c6
/* template_add_attributes()
Packit 8681c6
 *
Packit 8681c6
 * blindly add the given attributes to the template. do no sanity checking
Packit 8681c6
 * at this point. sanity checking will occur later.
Packit 8681c6
 */
Packit 8681c6
CK_RV template_add_attributes(TEMPLATE *tmpl, CK_ATTRIBUTE *pTemplate,
Packit 8681c6
                              CK_ULONG ulCount)
Packit 8681c6
{
Packit 8681c6
    CK_ATTRIBUTE *attr = NULL;
Packit 8681c6
    CK_RV rc;
Packit 8681c6
    unsigned int i;
Packit 8681c6
Packit 8681c6
    for (i = 0; i < ulCount; i++) {
Packit 8681c6
        if (!is_attribute_defined(pTemplate[i].type)) {
Packit Service 8aa27d
            TRACE_ERROR("%s: %lx\n", ock_err(ERR_ATTRIBUTE_TYPE_INVALID),
Packit Service 8aa27d
                        pTemplate[i].type);
Packit 8681c6
            return CKR_ATTRIBUTE_TYPE_INVALID;
Packit 8681c6
        }
Packit 8681c6
        attr = (CK_ATTRIBUTE *) malloc(sizeof(CK_ATTRIBUTE) +
Packit 8681c6
                                       pTemplate[i].ulValueLen);
Packit 8681c6
        if (!attr) {
Packit 8681c6
            TRACE_ERROR("%s\n", ock_err(ERR_HOST_MEMORY));
Packit 8681c6
            return CKR_HOST_MEMORY;
Packit 8681c6
        }
Packit 8681c6
        attr->type = pTemplate[i].type;
Packit 8681c6
        attr->ulValueLen = pTemplate[i].ulValueLen;
Packit 8681c6
Packit 8681c6
        if (attr->ulValueLen != 0) {
Packit 8681c6
            attr->pValue = (CK_BYTE *) attr + sizeof(CK_ATTRIBUTE);
Packit 8681c6
            memcpy(attr->pValue, pTemplate[i].pValue, attr->ulValueLen);
Packit 8681c6
        } else {
Packit 8681c6
            attr->pValue = NULL;
Packit 8681c6
        }
Packit 8681c6
Packit 8681c6
        rc = template_update_attribute(tmpl, attr);
Packit 8681c6
        if (rc != CKR_OK) {
Packit 8681c6
            free(attr);
Packit 8681c6
            TRACE_DEVEL("template_update_attribute failed.\n");
Packit 8681c6
            return rc;
Packit 8681c6
        }
Packit 8681c6
    }
Packit 8681c6
Packit 8681c6
    return CKR_OK;
Packit 8681c6
}
Packit 8681c6
Packit 8681c6
Packit 8681c6
/* template_add_default_attributes()
Packit 8681c6
 * Add default attributes to '*tmpl'.
Packit 8681c6
 * '*basetmpl' may be used to derive values to the default attributes
Packit 8681c6
 */
Packit 8681c6
CK_RV template_add_default_attributes(TEMPLATE *tmpl, TEMPLATE *basetmpl,
Packit 8681c6
                                      CK_ULONG class, CK_ULONG subclass,
Packit 8681c6
                                      CK_ULONG mode)
Packit 8681c6
{
Packit 8681c6
    CK_RV rc;
Packit 8681c6
Packit 8681c6
    /* first add the default common attributes */
Packit 8681c6
    rc = template_set_default_common_attributes(tmpl);
Packit 8681c6
    if (rc != CKR_OK) {
Packit 8681c6
        TRACE_DEVEL("template_set_default_common_attributes failed.\n");
Packit 8681c6
        return rc;
Packit 8681c6
    }
Packit 8681c6
Packit 8681c6
    /* set the template class-specific default attributes */
Packit 8681c6
    switch (class) {
Packit 8681c6
    case CKO_DATA:
Packit 8681c6
        return data_object_set_default_attributes(tmpl, mode);
Packit 8681c6
    case CKO_CERTIFICATE:
Packit 8681c6
        if (subclass == CKC_X_509)
Packit 8681c6
            return cert_x509_set_default_attributes(tmpl, mode);
Packit 8681c6
        else
Packit 8681c6
            return CKR_OK;
Packit 8681c6
    case CKO_PUBLIC_KEY:
Packit 8681c6
        switch (subclass) {
Packit 8681c6
        case CKK_RSA:
Packit 8681c6
            return rsa_publ_set_default_attributes(tmpl, basetmpl, mode);
Packit 8681c6
        case CKK_DSA:
Packit 8681c6
            return dsa_publ_set_default_attributes(tmpl, mode);
Packit 8681c6
        case CKK_ECDSA:
Packit 8681c6
            return ecdsa_publ_set_default_attributes(tmpl, mode);
Packit 8681c6
        case CKK_DH:
Packit 8681c6
            return dh_publ_set_default_attributes(tmpl, mode);
Packit 8681c6
        case CKK_KEA:
Packit 8681c6
            return kea_publ_set_default_attributes(tmpl, mode);
Packit 8681c6
        case CKK_IBM_PQC_DILITHIUM:
Packit 8681c6
            return ibm_dilithium_publ_set_default_attributes(tmpl, mode);
Packit 8681c6
        default:
Packit Service 8aa27d
            TRACE_ERROR("%s: %lx\n", ock_err(ERR_ATTRIBUTE_VALUE_INVALID),
Packit Service 8aa27d
                        subclass);
Packit 8681c6
            return CKR_ATTRIBUTE_VALUE_INVALID; // unknown key type
Packit 8681c6
        }
Packit 8681c6
    case CKO_PRIVATE_KEY:
Packit 8681c6
        switch (subclass) {
Packit 8681c6
        case CKK_RSA:
Packit 8681c6
            return rsa_priv_set_default_attributes(tmpl, mode);
Packit 8681c6
        case CKK_DSA:
Packit 8681c6
            return dsa_priv_set_default_attributes(tmpl, mode);
Packit 8681c6
        case CKK_ECDSA:
Packit 8681c6
            return ecdsa_priv_set_default_attributes(tmpl, mode);
Packit 8681c6
        case CKK_DH:
Packit 8681c6
            return dh_priv_set_default_attributes(tmpl, mode);
Packit 8681c6
        case CKK_KEA:
Packit 8681c6
            return kea_priv_set_default_attributes(tmpl, mode);
Packit 8681c6
        case CKK_IBM_PQC_DILITHIUM:
Packit 8681c6
            return ibm_dilithium_priv_set_default_attributes(tmpl, mode);
Packit 8681c6
        default:
Packit Service 8aa27d
            TRACE_ERROR("%s: %lx\n", ock_err(ERR_ATTRIBUTE_VALUE_INVALID),
Packit Service 8aa27d
                        subclass);
Packit 8681c6
            return CKR_ATTRIBUTE_VALUE_INVALID; // unknown key type
Packit 8681c6
        }
Packit 8681c6
    case CKO_SECRET_KEY:
Packit 8681c6
        switch (subclass) {
Packit 8681c6
        case CKK_GENERIC_SECRET:
Packit 8681c6
            return generic_secret_set_default_attributes(tmpl, mode);
Packit 8681c6
        case CKK_RC2:
Packit 8681c6
            return rc2_set_default_attributes(tmpl, mode);
Packit 8681c6
        case CKK_RC4:
Packit 8681c6
            return rc4_set_default_attributes(tmpl, mode);
Packit 8681c6
        case CKK_RC5:
Packit 8681c6
            return rc5_set_default_attributes(tmpl, mode);
Packit 8681c6
        case CKK_DES:
Packit 8681c6
            return des_set_default_attributes(tmpl, mode);
Packit 8681c6
        case CKK_DES2:
Packit 8681c6
            return des2_set_default_attributes(tmpl, mode);
Packit 8681c6
        case CKK_DES3:
Packit 8681c6
            return des3_set_default_attributes(tmpl, mode);
Packit 8681c6
        case CKK_CAST:
Packit 8681c6
            return cast_set_default_attributes(tmpl, mode);
Packit 8681c6
        case CKK_CAST3:
Packit 8681c6
            return cast3_set_default_attributes(tmpl, mode);
Packit 8681c6
        case CKK_CAST5:
Packit 8681c6
            return cast5_set_default_attributes(tmpl, mode);
Packit 8681c6
        case CKK_IDEA:
Packit 8681c6
            return idea_set_default_attributes(tmpl, mode);
Packit 8681c6
#if !(NOCDMF)
Packit 8681c6
        case CKK_CDMF:
Packit 8681c6
            return cdmf_set_default_attributes(tmpl, mode);
Packit 8681c6
#endif
Packit 8681c6
        case CKK_SKIPJACK:
Packit 8681c6
            return skipjack_set_default_attributes(tmpl, mode);
Packit 8681c6
        case CKK_BATON:
Packit 8681c6
            return baton_set_default_attributes(tmpl, mode);
Packit 8681c6
        case CKK_JUNIPER:
Packit 8681c6
            return juniper_set_default_attributes(tmpl, mode);
Packit 8681c6
        case CKK_AES:
Packit 8681c6
            return aes_set_default_attributes(tmpl, mode);
Packit 8681c6
        default:
Packit Service 8aa27d
            TRACE_ERROR("%s: %lx\n", ock_err(ERR_ATTRIBUTE_VALUE_INVALID),
Packit Service 8aa27d
                        subclass);
Packit 8681c6
            return CKR_ATTRIBUTE_VALUE_INVALID; // unknown key type
Packit 8681c6
        }
Packit 8681c6
    case CKO_HW_FEATURE:
Packit 8681c6
        if (subclass >= CKH_VENDOR_DEFINED)
Packit 8681c6
            return CKR_OK;
Packit 8681c6
        switch (subclass) {
Packit 8681c6
        case CKH_CLOCK:
Packit 8681c6
            return clock_set_default_attributes(tmpl, mode);
Packit 8681c6
        case CKH_MONOTONIC_COUNTER:
Packit 8681c6
            return counter_set_default_attributes(tmpl, mode);
Packit 8681c6
        default:
Packit Service 8aa27d
            TRACE_ERROR("%s: %lx\n", ock_err(ERR_ATTRIBUTE_VALUE_INVALID),
Packit Service 8aa27d
                        subclass);
Packit 8681c6
            return CKR_ATTRIBUTE_VALUE_INVALID;
Packit 8681c6
        }
Packit 8681c6
    case CKO_DOMAIN_PARAMETERS:
Packit 8681c6
        switch (subclass) {
Packit 8681c6
        case CKK_DSA:
Packit 8681c6
            return dp_dsa_set_default_attributes(tmpl, mode);
Packit 8681c6
        case CKK_DH:
Packit 8681c6
            return dp_dh_set_default_attributes(tmpl, mode);
Packit 8681c6
        case CKK_X9_42_DH:
Packit 8681c6
            return dp_x9dh_set_default_attributes(tmpl, mode);
Packit 8681c6
        default:
Packit Service 8aa27d
            TRACE_ERROR("%s: %lx\n", ock_err(ERR_ATTRIBUTE_VALUE_INVALID),
Packit Service 8aa27d
                        subclass);
Packit 8681c6
            return CKR_ATTRIBUTE_VALUE_INVALID;
Packit 8681c6
        }
Packit Service 8aa27d
    case CKO_PROFILE:
Packit Service 8aa27d
        return profile_object_set_default_attributes(tmpl, mode);
Packit 8681c6
    default:
Packit Service 8aa27d
        TRACE_ERROR("%s: %lx\n", ock_err(ERR_ATTRIBUTE_VALUE_INVALID), class);
Packit 8681c6
        return CKR_ATTRIBUTE_VALUE_INVALID;
Packit 8681c6
    }
Packit 8681c6
}
Packit 8681c6
Packit 8681c6
Packit 8681c6
/* template_attribute_find()
Packit 8681c6
 *
Packit 8681c6
 * find the attribute in the list and return its value
Packit 8681c6
 */
Packit 8681c6
CK_BBOOL template_attribute_find(TEMPLATE *tmpl, CK_ATTRIBUTE_TYPE type,
Packit 8681c6
                                 CK_ATTRIBUTE **attr)
Packit 8681c6
{
Packit 8681c6
    DL_NODE *node = NULL;
Packit 8681c6
    CK_ATTRIBUTE *a = NULL;
Packit 8681c6
Packit 8681c6
    if (!tmpl || !attr)
Packit 8681c6
        return FALSE;
Packit 8681c6
Packit 8681c6
    node = tmpl->attribute_list;
Packit 8681c6
Packit 8681c6
    while (node != NULL) {
Packit 8681c6
        a = (CK_ATTRIBUTE *) node->data;
Packit 8681c6
Packit 8681c6
        if (type == a->type) {
Packit 8681c6
            *attr = a;
Packit 8681c6
            return TRUE;
Packit 8681c6
        }
Packit 8681c6
Packit 8681c6
        node = node->next;
Packit 8681c6
    }
Packit 8681c6
Packit 8681c6
    *attr = NULL;
Packit 8681c6
Packit 8681c6
    return FALSE;
Packit 8681c6
}
Packit 8681c6
Packit 8681c6
/* template_attribute_find_multiple()
Packit 8681c6
 *
Packit 8681c6
 * find the attributes in the list and return their values
Packit 8681c6
 */
Packit 8681c6
void template_attribute_find_multiple(TEMPLATE *tmpl,
Packit 8681c6
                                      ATTRIBUTE_PARSE_LIST *parselist,
Packit 8681c6
                                      CK_ULONG plcount)
Packit 8681c6
{
Packit 8681c6
    CK_ATTRIBUTE *attr = NULL;
Packit 8681c6
    CK_ULONG i;
Packit 8681c6
Packit 8681c6
    for (i = 0; i < plcount; i++) {
Packit 8681c6
        parselist[i].found = template_attribute_find(tmpl,
Packit 8681c6
                                                     parselist[i].type, &attr);
Packit 8681c6
Packit 8681c6
        if (parselist[i].found && parselist[i].ptr != NULL)
Packit 8681c6
            memcpy(parselist[i].ptr, attr->pValue, parselist[i].len);
Packit 8681c6
    }
Packit 8681c6
}
Packit 8681c6
Packit 8681c6
Packit 8681c6
/* template_check_required_attributes() */
Packit 8681c6
CK_RV template_check_required_attributes(TEMPLATE *tmpl, CK_ULONG class,
Packit 8681c6
                                         CK_ULONG subclass, CK_ULONG mode)
Packit 8681c6
{
Packit 8681c6
    if (class == CKO_DATA) {
Packit 8681c6
        return data_object_check_required_attributes(tmpl, mode);
Packit 8681c6
    } else if (class == CKO_CERTIFICATE) {
Packit 8681c6
        if (subclass == CKC_X_509)
Packit 8681c6
            return cert_x509_check_required_attributes(tmpl, mode);
Packit 8681c6
        else
Packit 8681c6
            return cert_vendor_check_required_attributes(tmpl, mode);
Packit 8681c6
    } else if (class == CKO_PUBLIC_KEY) {
Packit 8681c6
        switch (subclass) {
Packit 8681c6
        case CKK_RSA:
Packit 8681c6
            return rsa_publ_check_required_attributes(tmpl, mode);
Packit 8681c6
        case CKK_DSA:
Packit 8681c6
            return dsa_publ_check_required_attributes(tmpl, mode);
Packit 8681c6
        case CKK_ECDSA:
Packit 8681c6
            return ecdsa_publ_check_required_attributes(tmpl, mode);
Packit 8681c6
        case CKK_DH:
Packit 8681c6
            return dh_publ_check_required_attributes(tmpl, mode);
Packit 8681c6
        case CKK_KEA:
Packit 8681c6
            return kea_publ_check_required_attributes(tmpl, mode);
Packit 8681c6
        case CKK_IBM_PQC_DILITHIUM:
Packit 8681c6
            return ibm_dilithium_publ_check_required_attributes(tmpl, mode);
Packit 8681c6
        default:
Packit Service 8aa27d
            TRACE_ERROR("%s: %lx\n", ock_err(ERR_ATTRIBUTE_VALUE_INVALID),
Packit Service 8aa27d
                        subclass);
Packit 8681c6
            return CKR_ATTRIBUTE_VALUE_INVALID; // unknown keytype
Packit 8681c6
        }
Packit 8681c6
    } else if (class == CKO_PRIVATE_KEY) {
Packit 8681c6
        switch (subclass) {
Packit 8681c6
        case CKK_RSA:
Packit 8681c6
            return rsa_priv_check_required_attributes(tmpl, mode);
Packit 8681c6
        case CKK_DSA:
Packit 8681c6
            return dsa_priv_check_required_attributes(tmpl, mode);
Packit 8681c6
        case CKK_ECDSA:
Packit 8681c6
            return ecdsa_priv_check_required_attributes(tmpl, mode);
Packit 8681c6
        case CKK_DH:
Packit 8681c6
            return dh_priv_check_required_attributes(tmpl, mode);
Packit 8681c6
        case CKK_KEA:
Packit 8681c6
            return kea_priv_check_required_attributes(tmpl, mode);
Packit 8681c6
        case CKK_IBM_PQC_DILITHIUM:
Packit 8681c6
            return ibm_dilithium_priv_check_required_attributes(tmpl, mode);
Packit 8681c6
        default:
Packit Service 8aa27d
            TRACE_ERROR("%s: %lx\n", ock_err(ERR_ATTRIBUTE_VALUE_INVALID),
Packit Service 8aa27d
                        subclass);
Packit 8681c6
            return CKR_ATTRIBUTE_VALUE_INVALID; // unknown key type
Packit 8681c6
        }
Packit 8681c6
    } else if (class == CKO_SECRET_KEY) {
Packit 8681c6
        switch (subclass) {
Packit 8681c6
        case CKK_GENERIC_SECRET:
Packit 8681c6
            return generic_secret_check_required_attributes(tmpl, mode);
Packit 8681c6
        case CKK_RC2:
Packit 8681c6
            return rc2_check_required_attributes(tmpl, mode);
Packit 8681c6
        case CKK_RC4:
Packit 8681c6
            return rc4_check_required_attributes(tmpl, mode);
Packit 8681c6
        case CKK_RC5:
Packit 8681c6
            return rc5_check_required_attributes(tmpl, mode);
Packit 8681c6
        case CKK_DES:
Packit 8681c6
            return des_check_required_attributes(tmpl, mode);
Packit 8681c6
        case CKK_DES2:
Packit 8681c6
            return des2_check_required_attributes(tmpl, mode);
Packit 8681c6
        case CKK_DES3:
Packit 8681c6
            return des3_check_required_attributes(tmpl, mode);
Packit 8681c6
        case CKK_CAST:
Packit 8681c6
            return cast_check_required_attributes(tmpl, mode);
Packit 8681c6
        case CKK_CAST3:
Packit 8681c6
            return cast3_check_required_attributes(tmpl, mode);
Packit 8681c6
        case CKK_CAST5:
Packit 8681c6
            return cast5_check_required_attributes(tmpl, mode);
Packit 8681c6
        case CKK_IDEA:
Packit 8681c6
            return idea_check_required_attributes(tmpl, mode);
Packit 8681c6
#if !(NOCDMF)
Packit 8681c6
        case CKK_CDMF:
Packit 8681c6
            return cdmf_check_required_attributes(tmpl, mode);
Packit 8681c6
#endif
Packit 8681c6
        case CKK_SKIPJACK:
Packit 8681c6
            return skipjack_check_required_attributes(tmpl, mode);
Packit 8681c6
        case CKK_BATON:
Packit 8681c6
            return baton_check_required_attributes(tmpl, mode);
Packit 8681c6
        case CKK_JUNIPER:
Packit 8681c6
            return juniper_check_required_attributes(tmpl, mode);
Packit 8681c6
        case CKK_AES:
Packit 8681c6
            return aes_check_required_attributes(tmpl, mode);
Packit 8681c6
        default:
Packit Service 8aa27d
            TRACE_ERROR("%s: %lx\n", ock_err(ERR_ATTRIBUTE_VALUE_INVALID),
Packit Service 8aa27d
                        subclass);
Packit 8681c6
            return CKR_ATTRIBUTE_VALUE_INVALID; // unknown key type
Packit 8681c6
        }
Packit 8681c6
    } else if (class == CKO_HW_FEATURE) {
Packit 8681c6
        if (subclass >= CKH_VENDOR_DEFINED)
Packit 8681c6
            return CKR_OK;
Packit 8681c6
        switch (subclass) {
Packit 8681c6
        case CKH_CLOCK:
Packit 8681c6
            return clock_check_required_attributes(tmpl, mode);
Packit 8681c6
        case CKH_MONOTONIC_COUNTER:
Packit 8681c6
            return counter_check_required_attributes(tmpl, mode);
Packit 8681c6
        default:
Packit Service 8aa27d
            TRACE_ERROR("%s: %lx\n", ock_err(ERR_ATTRIBUTE_VALUE_INVALID),
Packit Service 8aa27d
                        subclass);
Packit 8681c6
            return CKR_ATTRIBUTE_VALUE_INVALID;
Packit 8681c6
        }
Packit 8681c6
    } else if (class == CKO_DOMAIN_PARAMETERS) {
Packit 8681c6
        switch (subclass) {
Packit 8681c6
        case CKK_DSA:
Packit 8681c6
            return dp_dsa_check_required_attributes(tmpl, mode);
Packit 8681c6
        case CKK_DH:
Packit 8681c6
            return dp_dh_check_required_attributes(tmpl, mode);
Packit 8681c6
        case CKK_X9_42_DH:
Packit 8681c6
            return dp_x9dh_check_required_attributes(tmpl, mode);
Packit 8681c6
        default:
Packit Service 8aa27d
            TRACE_ERROR("%s: %lx\n", ock_err(ERR_ATTRIBUTE_VALUE_INVALID),
Packit Service 8aa27d
                        subclass);
Packit 8681c6
            return CKR_ATTRIBUTE_VALUE_INVALID;
Packit 8681c6
        }
Packit Service 8aa27d
    } else if (class == CKO_PROFILE) {
Packit Service 8aa27d
        return profile_object_check_required_attributes(tmpl, mode);
Packit 8681c6
    }
Packit 8681c6
Packit Service 8aa27d
    TRACE_ERROR("%s: %lx\n", ock_err(ERR_ATTRIBUTE_VALUE_INVALID),
Packit Service 8aa27d
                class);
Packit 8681c6
Packit 8681c6
    return CKR_ATTRIBUTE_VALUE_INVALID; // default fallthru
Packit 8681c6
}
Packit 8681c6
Packit 8681c6
Packit 8681c6
/* template_check_required_base_attributes()
Packit 8681c6
 *
Packit 8681c6
 * check to make sure that attributes required by Cryptoki are
Packit 8681c6
 * present.  does not check to see if the attribute makes sense
Packit 8681c6
 * for the particular object (that is done in the 'validate' routines)
Packit 8681c6
 */
Packit 8681c6
CK_RV template_check_required_base_attributes(TEMPLATE *tmpl, CK_ULONG mode)
Packit 8681c6
{
Packit 8681c6
    CK_ATTRIBUTE *attr;
Packit 8681c6
    CK_BBOOL found;
Packit 8681c6
Packit 8681c6
    found = template_attribute_find(tmpl, CKA_CLASS, &attr);
Packit 8681c6
    if (mode == MODE_CREATE && found == FALSE)
Packit 8681c6
        return CKR_TEMPLATE_INCOMPLETE;
Packit 8681c6
Packit 8681c6
    return CKR_OK;
Packit 8681c6
}
Packit 8681c6
Packit 8681c6
/* template_compare() */
Packit 8681c6
CK_BBOOL template_compare(CK_ATTRIBUTE *t1, CK_ULONG ulCount, TEMPLATE *t2)
Packit 8681c6
{
Packit 8681c6
    CK_ATTRIBUTE *attr1 = NULL;
Packit 8681c6
    CK_ATTRIBUTE *attr2 = NULL;
Packit 8681c6
    CK_ULONG i;
Packit 8681c6
    CK_RV rc;
Packit 8681c6
Packit 8681c6
    if (!t1 || !t2)
Packit 8681c6
        return FALSE;
Packit 8681c6
Packit 8681c6
    attr1 = t1;
Packit 8681c6
Packit 8681c6
    for (i = 0; i < ulCount; i++) {
Packit 8681c6
        rc = template_attribute_find(t2, attr1->type, &attr2);
Packit 8681c6
        if (rc == FALSE)
Packit 8681c6
            return FALSE;
Packit 8681c6
Packit 8681c6
        if (attr1->ulValueLen != attr2->ulValueLen)
Packit 8681c6
            return FALSE;
Packit 8681c6
Packit 8681c6
        if (memcmp(attr1->pValue, attr2->pValue, attr1->ulValueLen) != 0)
Packit 8681c6
            return FALSE;
Packit 8681c6
Packit 8681c6
        attr1++;
Packit 8681c6
    }
Packit 8681c6
Packit 8681c6
    return TRUE;
Packit 8681c6
}
Packit 8681c6
Packit 8681c6
Packit 8681c6
/* template_copy()
Packit 8681c6
 *
Packit 8681c6
 * This doesn't copy the template items verbatim.  The new template is in
Packit 8681c6
 * the reverse order of the old one.  This should not have any effect.
Packit 8681c6
 *
Packit 8681c6
 * This is very similar to template_merge().  template_merge() can also
Packit 8681c6
 * be used to copy a list (of unique attributes) but is slower because for
Packit 8681c6
 * each attribute, it must search through the list.
Packit 8681c6
 */
Packit 8681c6
CK_RV template_copy(TEMPLATE *dest, TEMPLATE *src)
Packit 8681c6
{
Packit Service 8aa27d
    char unique_id_str[2 * UNIQUE_ID_LEN + 1];
Packit 8681c6
    DL_NODE *node;
Packit 8681c6
Packit 8681c6
    if (!dest || !src) {
Packit 8681c6
        TRACE_ERROR("Invalid function arguments.\n");
Packit 8681c6
        return CKR_FUNCTION_FAILED;
Packit 8681c6
    }
Packit 8681c6
    node = src->attribute_list;
Packit 8681c6
Packit 8681c6
    while (node) {
Packit 8681c6
        CK_ATTRIBUTE *attr = (CK_ATTRIBUTE *) node->data;
Packit 8681c6
        CK_ATTRIBUTE *new_attr = NULL;
Packit 8681c6
        CK_ULONG len;
Packit 8681c6
Packit 8681c6
        len = sizeof(CK_ATTRIBUTE) + attr->ulValueLen;
Packit 8681c6
Packit 8681c6
        new_attr = (CK_ATTRIBUTE *) malloc(len);
Packit 8681c6
        if (!new_attr) {
Packit 8681c6
            TRACE_ERROR("%s\n", ock_err(ERR_HOST_MEMORY));
Packit 8681c6
            return CKR_HOST_MEMORY;
Packit 8681c6
        }
Packit 8681c6
Packit Service 8aa27d
        memcpy(new_attr, attr, len);
Packit 8681c6
        new_attr->pValue = (CK_BYTE *) new_attr + sizeof(CK_ATTRIBUTE);
Packit 8681c6
Packit Service 8aa27d
        if (attr->type == CKA_UNIQUE_ID) {
Packit Service 8aa27d
            if (attr->ulValueLen < 2 * UNIQUE_ID_LEN) {
Packit Service 8aa27d
                free(new_attr);
Packit Service 8aa27d
                TRACE_ERROR("%s\n", ock_err(ERR_ATTRIBUTE_VALUE_INVALID));
Packit Service 8aa27d
                return CKR_ATTRIBUTE_VALUE_INVALID;
Packit Service 8aa27d
            }
Packit Service 8aa27d
            if (get_unique_id_str(unique_id_str) != CKR_OK) {
Packit Service 8aa27d
                free(new_attr);
Packit Service 8aa27d
                TRACE_ERROR("%s\n", ock_err(ERR_FUNCTION_FAILED));
Packit Service 8aa27d
                return CKR_FUNCTION_FAILED;
Packit Service 8aa27d
            }
Packit Service 8aa27d
            memcpy(new_attr->pValue, unique_id_str, 2 * UNIQUE_ID_LEN);
Packit Service 8aa27d
            new_attr->ulValueLen = 2 * UNIQUE_ID_LEN;
Packit Service 8aa27d
        }
Packit Service 8aa27d
Packit 8681c6
        dest->attribute_list = dlist_add_as_first(dest->attribute_list,
Packit 8681c6
                                                  new_attr);
Packit 8681c6
        node = node->next;
Packit 8681c6
    }
Packit 8681c6
Packit 8681c6
    return CKR_OK;
Packit 8681c6
}
Packit 8681c6
Packit 8681c6
Packit 8681c6
/* template_flatten()
Packit 8681c6
 * this still gets used when saving token objects to disk
Packit 8681c6
 */
Packit 8681c6
CK_RV template_flatten(TEMPLATE *tmpl, CK_BYTE *dest)
Packit 8681c6
{
Packit 8681c6
    DL_NODE *node = NULL;
Packit 8681c6
    CK_BYTE *ptr = NULL;
Packit 8681c6
    CK_ULONG_32 long_len;
Packit 8681c6
    CK_ATTRIBUTE_32 *attr_32 = NULL;
Packit 8681c6
    CK_ULONG Val;
Packit 8681c6
    CK_ULONG_32 Val_32;
Packit 8681c6
    CK_ULONG *pVal;
Packit 8681c6
Packit 8681c6
    long_len = sizeof(CK_ULONG);
Packit 8681c6
Packit 8681c6
    if (!tmpl || !dest) {
Packit 8681c6
        TRACE_ERROR("Invalid function arguments.\n");
Packit 8681c6
        return CKR_FUNCTION_FAILED;
Packit 8681c6
    }
Packit 8681c6
    ptr = dest;
Packit 8681c6
    node = tmpl->attribute_list;
Packit 8681c6
    while (node) {
Packit 8681c6
        CK_ATTRIBUTE *attr = (CK_ATTRIBUTE *) node->data;
Packit 8681c6
Packit 8681c6
        if (long_len == 4) {
Packit 8681c6
            memcpy(ptr, attr, sizeof(CK_ATTRIBUTE) + attr->ulValueLen);
Packit 8681c6
            ptr += sizeof(CK_ATTRIBUTE) + attr->ulValueLen;
Packit 8681c6
        } else {
Packit 8681c6
            attr_32 = malloc(sizeof(CK_ATTRIBUTE_32));
Packit 8681c6
            if (!attr_32) {
Packit 8681c6
                TRACE_ERROR("%s\n", ock_err(ERR_HOST_MEMORY));
Packit 8681c6
                return CKR_HOST_MEMORY;
Packit 8681c6
            }
Packit 8681c6
            attr_32->type = attr->type;
Packit 8681c6
            attr_32->pValue = 0x00;
Packit 8681c6
            if ((attr->type == CKA_CLASS ||
Packit 8681c6
                 attr->type == CKA_KEY_TYPE ||
Packit 8681c6
                 attr->type == CKA_MODULUS_BITS ||
Packit 8681c6
                 attr->type == CKA_VALUE_BITS ||
Packit 8681c6
                 attr->type == CKA_CERTIFICATE_TYPE ||
Packit 8681c6
                 attr->type == CKA_VALUE_LEN) && attr->ulValueLen != 0) {
Packit 8681c6
Packit 8681c6
                attr_32->ulValueLen = sizeof(CK_ULONG_32);
Packit 8681c6
Packit 8681c6
                memcpy(ptr, attr_32, sizeof(CK_ATTRIBUTE_32));
Packit 8681c6
                ptr += sizeof(CK_ATTRIBUTE_32);
Packit 8681c6
Packit 8681c6
                pVal = (CK_ULONG *) attr->pValue;
Packit 8681c6
                Val = *pVal;
Packit 8681c6
                Val_32 = (CK_ULONG_32) Val;
Packit 8681c6
                memcpy(ptr, &Val_32, sizeof(CK_ULONG_32));
Packit 8681c6
                ptr += sizeof(CK_ULONG_32);
Packit 8681c6
            } else {
Packit 8681c6
                attr_32->ulValueLen = attr->ulValueLen;
Packit 8681c6
                memcpy(ptr, attr_32, sizeof(CK_ATTRIBUTE_32));
Packit 8681c6
                ptr += sizeof(CK_ATTRIBUTE_32);
Packit 8681c6
                if (attr->ulValueLen != 0) {
Packit 8681c6
                    memcpy(ptr, attr->pValue, attr->ulValueLen);
Packit 8681c6
                    ptr += attr->ulValueLen;
Packit 8681c6
                }
Packit 8681c6
            }
Packit 8681c6
            free(attr_32);
Packit 8681c6
        }
Packit 8681c6
Packit 8681c6
        node = node->next;
Packit 8681c6
    }
Packit 8681c6
Packit 8681c6
    return CKR_OK;
Packit 8681c6
}
Packit 8681c6
Packit 8681c6
CK_RV template_unflatten(TEMPLATE **new_tmpl, CK_BYTE *buf, CK_ULONG count)
Packit 8681c6
{
Packit 8681c6
    return template_unflatten_withSize(new_tmpl, buf, count, -1);
Packit 8681c6
}
Packit 8681c6
Packit 8681c6
/* Modified version of template_unflatten that checks
Packit 8681c6
 * that buf isn't overread.  buf_size=-1 turns off checking
Packit 8681c6
 * (for backwards compatability)
Packit 8681c6
 */
Packit 8681c6
CK_RV template_unflatten_withSize(TEMPLATE **new_tmpl, CK_BYTE *buf,
Packit 8681c6
                                  CK_ULONG count, int buf_size)
Packit 8681c6
{
Packit 8681c6
    TEMPLATE *tmpl = NULL;
Packit 8681c6
    CK_ATTRIBUTE *a2 = NULL;
Packit 8681c6
    CK_BYTE *ptr = NULL;
Packit 8681c6
    CK_ULONG i, len;
Packit 8681c6
    CK_RV rc;
Packit 8681c6
    CK_ULONG_32 long_len = sizeof(CK_ULONG);
Packit 8681c6
    CK_ULONG_32 attr_ulong_32;
Packit 8681c6
    CK_ULONG attr_ulong;
Packit 8681c6
    CK_ATTRIBUTE *a1_64 = NULL;
Packit Service 8aa27d
    CK_ATTRIBUTE_32 a1;
Packit 8681c6
Packit Service 8aa27d
    if (!new_tmpl) {
Packit 8681c6
        TRACE_ERROR("Invalid function arguments.\n");
Packit 8681c6
        return CKR_FUNCTION_FAILED;
Packit 8681c6
    }
Packit 8681c6
    tmpl = (TEMPLATE *) malloc(sizeof(TEMPLATE));
Packit 8681c6
    if (!tmpl) {
Packit 8681c6
        TRACE_ERROR("%s\n", ock_err(ERR_HOST_MEMORY));
Packit 8681c6
        return CKR_HOST_MEMORY;
Packit 8681c6
    }
Packit 8681c6
    memset(tmpl, 0x0, sizeof(TEMPLATE));
Packit 8681c6
Packit 8681c6
    ptr = buf;
Packit 8681c6
    for (i = 0; i < count; i++) {
Packit 8681c6
        if (buf_size >= 0 &&
Packit 8681c6
            ((ptr + sizeof(CK_ATTRIBUTE)) > (buf + buf_size))) {
Packit 8681c6
            template_free(tmpl);
Packit 8681c6
            return CKR_FUNCTION_FAILED;
Packit 8681c6
        }
Packit 8681c6
Packit 8681c6
        if (long_len == 4) {
Packit 8681c6
            a1_64 = (CK_ATTRIBUTE *) ptr;
Packit 8681c6
Packit 8681c6
            len = sizeof(CK_ATTRIBUTE) + a1_64->ulValueLen;
Packit 8681c6
            a2 = (CK_ATTRIBUTE *) malloc(len);
Packit 8681c6
            if (!a2) {
Packit 8681c6
                template_free(tmpl);
Packit 8681c6
                TRACE_ERROR("%s\n", ock_err(ERR_HOST_MEMORY));
Packit 8681c6
                return CKR_HOST_MEMORY;
Packit 8681c6
            }
Packit 8681c6
Packit 8681c6
            /* if a buffer size is given, make sure it
Packit 8681c6
             * doesn't get overrun
Packit 8681c6
             */
Packit 8681c6
            if (buf_size >= 0 &&
Packit 8681c6
                (((unsigned char *) a1_64 + len)
Packit 8681c6
                 > ((unsigned char *) buf + buf_size))) {
Packit 8681c6
                free(a2);
Packit 8681c6
                template_free(tmpl);
Packit 8681c6
                return CKR_FUNCTION_FAILED;
Packit 8681c6
            }
Packit 8681c6
            memcpy(a2, a1_64, len);
Packit 8681c6
        } else {
Packit Service 8aa27d
            memcpy(&a1, ptr, sizeof(a1));
Packit Service 8aa27d
            if ((a1.type == CKA_CLASS || a1.type == CKA_KEY_TYPE
Packit Service 8aa27d
                 || a1.type == CKA_MODULUS_BITS
Packit Service 8aa27d
                 || a1.type == CKA_VALUE_BITS
Packit Service 8aa27d
                 || a1.type == CKA_CERTIFICATE_TYPE
Packit Service 8aa27d
                 || a1.type == CKA_VALUE_LEN)
Packit Service 8aa27d
                && a1.ulValueLen != 0) {
Packit 8681c6
                len = sizeof(CK_ATTRIBUTE) + sizeof(CK_ULONG);
Packit 8681c6
            } else {
Packit Service 8aa27d
                len = sizeof(CK_ATTRIBUTE) + a1.ulValueLen;
Packit 8681c6
            }
Packit 8681c6
Packit 8681c6
            a2 = (CK_ATTRIBUTE *) malloc(len);
Packit 8681c6
            if (!a2) {
Packit 8681c6
                template_free(tmpl);
Packit 8681c6
                TRACE_ERROR("%s\n", ock_err(ERR_HOST_MEMORY));
Packit 8681c6
                return CKR_HOST_MEMORY;
Packit 8681c6
            }
Packit Service 8aa27d
            a2->type = a1.type;
Packit 8681c6
Packit Service 8aa27d
            if ((a1.type == CKA_CLASS || a1.type == CKA_KEY_TYPE
Packit Service 8aa27d
                 || a1.type == CKA_MODULUS_BITS
Packit Service 8aa27d
                 || a1.type == CKA_VALUE_BITS
Packit Service 8aa27d
                 || a1.type == CKA_CERTIFICATE_TYPE
Packit Service 8aa27d
                 || a1.type == CKA_VALUE_LEN)
Packit Service 8aa27d
                && a1.ulValueLen != 0) {
Packit 8681c6
Packit 8681c6
                a2->ulValueLen = sizeof(CK_ULONG);
Packit 8681c6
Packit 8681c6
                {
Packit Service 8aa27d
                    CK_ULONG_32 u32;
Packit 8681c6
                    CK_BYTE *pb2;
Packit 8681c6
Packit Service 8aa27d
                    pb2 = (CK_BYTE *) ptr;
Packit 8681c6
                    pb2 += sizeof(CK_ATTRIBUTE_32);
Packit Service 8aa27d
                    memcpy(&u32, pb2, sizeof(u32));
Packit Service 8aa27d
                    attr_ulong_32 = u32;
Packit 8681c6
                }
Packit 8681c6
Packit 8681c6
                attr_ulong = attr_ulong_32;
Packit 8681c6
Packit 8681c6
                {
Packit 8681c6
                    CK_BYTE *pb2;
Packit 8681c6
                    pb2 = (CK_BYTE *) a2;
Packit 8681c6
                    pb2 += sizeof(CK_ATTRIBUTE);
Packit 8681c6
                    memcpy(pb2, (CK_BYTE *) & attr_ulong, sizeof(CK_ULONG));
Packit 8681c6
                }
Packit 8681c6
            } else {
Packit 8681c6
                CK_BYTE *pb2, *pb;
Packit 8681c6
Packit Service 8aa27d
                a2->ulValueLen = a1.ulValueLen;
Packit 8681c6
                pb2 = (CK_BYTE *) a2;
Packit 8681c6
                pb2 += sizeof(CK_ATTRIBUTE);
Packit Service 8aa27d
                pb = (CK_BYTE *) ptr;
Packit 8681c6
                pb += sizeof(CK_ATTRIBUTE_32);
Packit 8681c6
                /* if a buffer size is given, make sure it
Packit 8681c6
                 * doesn't get overrun
Packit 8681c6
                 */
Packit Service 8aa27d
                if (buf_size >= 0 && (pb + a1.ulValueLen) > (buf + buf_size)) {
Packit 8681c6
                    free(a2);
Packit 8681c6
                    template_free(tmpl);
Packit 8681c6
                    return CKR_FUNCTION_FAILED;
Packit 8681c6
                }
Packit Service 8aa27d
                memcpy(pb2, pb, a1.ulValueLen);
Packit 8681c6
            }
Packit 8681c6
        }
Packit 8681c6
Packit 8681c6
        if (a2->ulValueLen != 0)
Packit 8681c6
            a2->pValue = (CK_BYTE *) a2 + sizeof(CK_ATTRIBUTE);
Packit 8681c6
        else
Packit 8681c6
            a2->pValue = NULL;
Packit 8681c6
Packit 8681c6
        rc = template_update_attribute(tmpl, a2);
Packit 8681c6
        if (rc != CKR_OK) {
Packit 8681c6
            free(a2);
Packit 8681c6
            template_free(tmpl);
Packit 8681c6
            return rc;
Packit 8681c6
        }
Packit 8681c6
        if (long_len == 4)
Packit 8681c6
            ptr += len;
Packit 8681c6
        else
Packit Service 8aa27d
            ptr += sizeof(CK_ATTRIBUTE_32) + a1.ulValueLen;
Packit 8681c6
    }
Packit 8681c6
Packit 8681c6
    *new_tmpl = tmpl;
Packit 8681c6
Packit 8681c6
    return CKR_OK;
Packit 8681c6
}
Packit 8681c6
Packit 8681c6
Packit 8681c6
/* template_free() */
Packit 8681c6
CK_RV template_free(TEMPLATE *tmpl)
Packit 8681c6
{
Packit 8681c6
    if (!tmpl)
Packit 8681c6
        return CKR_OK;
Packit 8681c6
Packit 8681c6
    while (tmpl->attribute_list) {
Packit 8681c6
        CK_ATTRIBUTE *attr = (CK_ATTRIBUTE *) tmpl->attribute_list->data;
Packit 8681c6
Packit 8681c6
        if (attr)
Packit 8681c6
            free(attr);
Packit 8681c6
Packit 8681c6
        tmpl->attribute_list = dlist_remove_node(tmpl->attribute_list,
Packit 8681c6
                                                 tmpl->attribute_list);
Packit 8681c6
    }
Packit 8681c6
Packit 8681c6
    free(tmpl);
Packit 8681c6
Packit 8681c6
    return CKR_OK;
Packit 8681c6
}
Packit 8681c6
Packit 8681c6
/* template_get_class */
Packit 8681c6
CK_BBOOL template_get_class(TEMPLATE *tmpl, CK_ULONG *class,
Packit 8681c6
                            CK_ULONG *subclass)
Packit 8681c6
{
Packit 8681c6
    DL_NODE *node;
Packit 8681c6
    CK_BBOOL found = FALSE;
Packit 8681c6
Packit 8681c6
    if (!tmpl || !class || !subclass)
Packit 8681c6
        return FALSE;
Packit 8681c6
Packit 8681c6
    node = tmpl->attribute_list;
Packit 8681c6
Packit 8681c6
    /* have to iterate through all attributes. no early exits */
Packit 8681c6
    while (node) {
Packit 8681c6
        CK_ATTRIBUTE *attr = (CK_ATTRIBUTE *) node->data;
Packit 8681c6
Packit 8681c6
        if (attr->type == CKA_CLASS) {
Packit 8681c6
            *class = *(CK_OBJECT_CLASS *) attr->pValue;
Packit 8681c6
            found = TRUE;
Packit 8681c6
        }
Packit 8681c6
Packit 8681c6
        /* underneath, these guys are both CK_ULONG so we
Packit 8681c6
         * could combine this
Packit 8681c6
         */
Packit 8681c6
        if (attr->type == CKA_CERTIFICATE_TYPE)
Packit 8681c6
            *subclass = *(CK_CERTIFICATE_TYPE *) attr->pValue;
Packit 8681c6
Packit 8681c6
        if (attr->type == CKA_KEY_TYPE)
Packit 8681c6
            *subclass = *(CK_KEY_TYPE *) attr->pValue;
Packit 8681c6
Packit 8681c6
        node = node->next;
Packit 8681c6
    }
Packit 8681c6
Packit 8681c6
    return found;
Packit 8681c6
}
Packit 8681c6
Packit 8681c6
CK_ULONG template_get_count(TEMPLATE *tmpl)
Packit 8681c6
{
Packit 8681c6
    if (tmpl == NULL)
Packit 8681c6
        return 0;
Packit 8681c6
Packit 8681c6
    return dlist_length(tmpl->attribute_list);
Packit 8681c6
}
Packit 8681c6
Packit 8681c6
CK_ULONG template_get_size(TEMPLATE *tmpl)
Packit 8681c6
{
Packit 8681c6
    DL_NODE *node;
Packit 8681c6
    CK_ULONG size = 0;
Packit 8681c6
Packit 8681c6
    if (tmpl == NULL)
Packit 8681c6
        return 0;
Packit 8681c6
Packit 8681c6
    node = tmpl->attribute_list;
Packit 8681c6
    while (node) {
Packit 8681c6
        CK_ATTRIBUTE *attr = (CK_ATTRIBUTE *) node->data;
Packit 8681c6
Packit 8681c6
        size += sizeof(CK_ATTRIBUTE) + attr->ulValueLen;
Packit 8681c6
Packit 8681c6
        node = node->next;
Packit 8681c6
    }
Packit 8681c6
Packit 8681c6
    return size;
Packit 8681c6
}
Packit 8681c6
Packit 8681c6
CK_ULONG template_get_compressed_size(TEMPLATE *tmpl)
Packit 8681c6
{
Packit 8681c6
    DL_NODE *node;
Packit 8681c6
    CK_ULONG size = 0;
Packit 8681c6
Packit 8681c6
    if (tmpl == NULL)
Packit 8681c6
        return 0;
Packit 8681c6
    node = tmpl->attribute_list;
Packit 8681c6
    while (node) {
Packit 8681c6
        CK_ATTRIBUTE *attr = (CK_ATTRIBUTE *) node->data;
Packit 8681c6
Packit 8681c6
        size += sizeof(CK_ATTRIBUTE_32);
Packit 8681c6
        if ((attr->type == CKA_CLASS || attr->type == CKA_KEY_TYPE
Packit 8681c6
             || attr->type == CKA_MODULUS_BITS
Packit 8681c6
             || attr->type == CKA_VALUE_BITS
Packit 8681c6
             || attr->type == CKA_CERTIFICATE_TYPE
Packit 8681c6
             || attr->type == CKA_VALUE_LEN)
Packit 8681c6
            && attr->ulValueLen != 0) {
Packit 8681c6
            size += sizeof(CK_ULONG_32);
Packit 8681c6
        } else {
Packit 8681c6
            size += attr->ulValueLen;
Packit 8681c6
        }
Packit 8681c6
Packit 8681c6
        node = node->next;
Packit 8681c6
    }
Packit 8681c6
Packit 8681c6
    return size;
Packit 8681c6
}
Packit 8681c6
Packit 8681c6
/* template_is_okay_to_reveal_attribute()
Packit 8681c6
 *
Packit 8681c6
 * determines whether the specified CK_ATTRIBUTE_TYPE is allowed to
Packit 8681c6
 * be leave the card in the clear.  note: the specified template doesn't need
Packit 8681c6
 * to actually posess an attribute of type 'type'.  The template is
Packit 8681c6
 * provided mainly to determine the object class and subclass
Packit 8681c6
 *
Packit 8681c6
 * this routine is called by C_GetAttributeValue which exports the attributes
Packit 8681c6
 * in the clear.  this routine is NOT called when wrapping a key.
Packit 8681c6
 */
Packit 8681c6
CK_BBOOL template_check_exportability(TEMPLATE *tmpl, CK_ATTRIBUTE_TYPE type)
Packit 8681c6
{
Packit 8681c6
    CK_ATTRIBUTE *sensitive = NULL;
Packit 8681c6
    CK_ATTRIBUTE *extractable = NULL;
Packit 8681c6
    CK_ULONG class;
Packit 8681c6
    CK_ULONG subclass;
Packit 8681c6
    CK_BBOOL sensitive_val;
Packit 8681c6
    CK_BBOOL extractable_val;
Packit 8681c6
Packit 8681c6
    if (!tmpl)
Packit 8681c6
        return FALSE;
Packit 8681c6
Packit 8681c6
    /* since 'tmpl' belongs to a validated object, it's safe
Packit 8681c6
     * to assume that the following routine works
Packit 8681c6
     */
Packit 8681c6
    template_get_class(tmpl, &class, &subclass);
Packit 8681c6
Packit 8681c6
    /* Early exits:
Packit 8681c6
     * 1) CKA_SENSITIVE and CKA_EXTRACTABLE only apply to private key
Packit 8681c6
     * and secret key objects.  If object type is any other, then
Packit 8681c6
     * by default the attribute is exportable.
Packit 8681c6
     *
Packit 8681c6
     * 2) If CKA_SENSITIVE = FALSE  and CKA_EXTRACTABLE = TRUE then
Packit 8681c6
     * all attributes are exportable
Packit 8681c6
     */
Packit 8681c6
    if (class != CKO_PRIVATE_KEY && class != CKO_SECRET_KEY)
Packit 8681c6
        return TRUE;
Packit 8681c6
Packit 8681c6
    sensitive_val = template_attribute_find(tmpl, CKA_SENSITIVE, &sensitive);
Packit 8681c6
    extractable_val = template_attribute_find(tmpl, CKA_EXTRACTABLE,
Packit 8681c6
                                              &extractable);
Packit 8681c6
    if (sensitive_val && extractable_val) {
Packit 8681c6
        sensitive_val = *(CK_BBOOL *) sensitive->pValue;
Packit 8681c6
        extractable_val = *(CK_BBOOL *) extractable->pValue;
Packit 8681c6
        if (sensitive_val == FALSE && extractable_val == TRUE)
Packit 8681c6
            return TRUE;
Packit 8681c6
    } else {
Packit 8681c6
        /* technically, we should throw an error here... */
Packit 8681c6
        return FALSE;
Packit 8681c6
    }
Packit 8681c6
Packit 8681c6
    /* at this point, we know the object must have CKA_SENSITIVE = TRUE
Packit 8681c6
     * or CKA_EXTRACTABLE = FALSE (or both).
Packit 8681c6
     * need to determine whether the particular attribute in question is
Packit 8681c6
     * a "sensitive" attribute.
Packit 8681c6
     */
Packit 8681c6
Packit 8681c6
    if (class == CKO_PRIVATE_KEY) {
Packit 8681c6
        switch (subclass) {
Packit 8681c6
        case CKK_RSA:
Packit 8681c6
            return rsa_priv_check_exportability(type);
Packit 8681c6
        case CKK_DSA:
Packit 8681c6
            return dsa_priv_check_exportability(type);
Packit 8681c6
        case CKK_ECDSA:
Packit 8681c6
            return ecdsa_priv_check_exportability(type);
Packit 8681c6
        case CKK_X9_42_DH:
Packit 8681c6
        case CKK_DH:
Packit 8681c6
            return dh_priv_check_exportability(type);
Packit 8681c6
        case CKK_KEA:
Packit 8681c6
            return kea_priv_check_exportability(type);
Packit 8681c6
        default:
Packit Service 8aa27d
            TRACE_ERROR("%s: %lx\n", ock_err(ERR_ATTRIBUTE_VALUE_INVALID),
Packit Service 8aa27d
                        subclass);
Packit 8681c6
            return TRUE;
Packit 8681c6
        }
Packit 8681c6
    } else if (class == CKO_SECRET_KEY) {
Packit 8681c6
        return secret_key_check_exportability(type);
Packit 8681c6
    }
Packit 8681c6
Packit Service 8aa27d
    TRACE_ERROR("%s: %lx\n", ock_err(ERR_ATTRIBUTE_VALUE_INVALID),
Packit Service 8aa27d
                class);
Packit 8681c6
Packit 8681c6
    return TRUE;
Packit 8681c6
}
Packit 8681c6
Packit 8681c6
/*  template_merge()
Packit 8681c6
 *
Packit 8681c6
 * Merge two templates together:  dest = dest U src
Packit 8681c6
 *
Packit 8681c6
 * src is destroyed in the process
Packit 8681c6
 */
Packit 8681c6
CK_RV template_merge(TEMPLATE *dest, TEMPLATE **src)
Packit 8681c6
{
Packit 8681c6
    DL_NODE *node;
Packit 8681c6
    CK_RV rc;
Packit 8681c6
Packit 8681c6
    if (!dest || !src) {
Packit 8681c6
        TRACE_ERROR("Invalid function arguments.\n");
Packit 8681c6
        return CKR_FUNCTION_FAILED;
Packit 8681c6
    }
Packit 8681c6
    node = (*src)->attribute_list;
Packit 8681c6
Packit 8681c6
    while (node) {
Packit 8681c6
        CK_ATTRIBUTE *attr = (CK_ATTRIBUTE *) node->data;
Packit 8681c6
Packit 8681c6
        rc = template_update_attribute(dest, attr);
Packit 8681c6
        if (rc != CKR_OK) {
Packit 8681c6
            TRACE_DEVEL("template_update_attribute failed.\n");
Packit 8681c6
            return rc;
Packit 8681c6
        }
Packit 8681c6
        /* we've assigned the node's data to a node in 'dest' */
Packit 8681c6
        node->data = NULL;
Packit 8681c6
        node = node->next;
Packit 8681c6
    }
Packit 8681c6
Packit 8681c6
    template_free(*src);
Packit 8681c6
    *src = NULL;
Packit 8681c6
Packit 8681c6
    return CKR_OK;
Packit 8681c6
}
Packit 8681c6
Packit 8681c6
/* template_set_default_common_attributes()
Packit 8681c6
 *
Packit 8681c6
 * Set the default attributes common to all objects:
Packit 8681c6
 *
Packit 8681c6
 * CKA_TOKEN: FALSE
Packit 8681c6
 * CKA_PRIVATE: TRUE -- Cryptoki leaves this up to the token to decide
Packit 8681c6
 * CKA_MODIFIABLE: TRUE
Packit 8681c6
 * CKA_LABEL: empty string
Packit 8681c6
 */
Packit 8681c6
CK_RV template_set_default_common_attributes(TEMPLATE *tmpl)
Packit 8681c6
{
Packit Service 8aa27d
    char unique_id_str[2 * UNIQUE_ID_LEN + 1];
Packit 8681c6
    CK_ATTRIBUTE *token_attr;
Packit 8681c6
    CK_ATTRIBUTE *priv_attr;
Packit 8681c6
    CK_ATTRIBUTE *mod_attr;
Packit 8681c6
    CK_ATTRIBUTE *label_attr;
Packit Service 8aa27d
    CK_ATTRIBUTE *unique_id_attr;
Packit Service 8aa27d
Packit Service 8aa27d
    if (get_unique_id_str(unique_id_str) != CKR_OK)
Packit Service 8aa27d
        return CKR_FUNCTION_FAILED;
Packit 8681c6
Packit 8681c6
    /* add the default common attributes */
Packit 8681c6
    token_attr = (CK_ATTRIBUTE *) malloc(sizeof(CK_ATTRIBUTE)
Packit 8681c6
                                         + sizeof(CK_BBOOL));
Packit 8681c6
    priv_attr = (CK_ATTRIBUTE *) malloc(sizeof(CK_ATTRIBUTE)
Packit 8681c6
                                        + sizeof(CK_BBOOL));
Packit 8681c6
    mod_attr = (CK_ATTRIBUTE *) malloc(sizeof(CK_ATTRIBUTE)
Packit 8681c6
                                       + sizeof(CK_BBOOL));
Packit 8681c6
    label_attr = (CK_ATTRIBUTE *) malloc(sizeof(CK_ATTRIBUTE) + 0);
Packit Service 8aa27d
    unique_id_attr = (CK_ATTRIBUTE *) malloc(sizeof(CK_ATTRIBUTE) + UNIQUE_ID_LEN * 2);
Packit 8681c6
Packit Service 8aa27d
    if (!token_attr || !priv_attr || !mod_attr || !label_attr || !unique_id_attr) {
Packit 8681c6
        if (token_attr)
Packit 8681c6
            free(token_attr);
Packit 8681c6
        if (priv_attr)
Packit 8681c6
            free(priv_attr);
Packit 8681c6
        if (mod_attr)
Packit 8681c6
            free(mod_attr);
Packit 8681c6
        if (label_attr)
Packit 8681c6
            free(label_attr);
Packit Service 8aa27d
        if (unique_id_attr)
Packit Service 8aa27d
            free(unique_id_attr);
Packit 8681c6
Packit 8681c6
        TRACE_ERROR("%s\n", ock_err(ERR_HOST_MEMORY));
Packit 8681c6
        return CKR_HOST_MEMORY;
Packit 8681c6
    }
Packit 8681c6
Packit 8681c6
    token_attr->type = CKA_TOKEN;
Packit 8681c6
    token_attr->ulValueLen = sizeof(CK_BBOOL);
Packit 8681c6
    token_attr->pValue = (CK_BYTE *) token_attr + sizeof(CK_ATTRIBUTE);
Packit 8681c6
    *(CK_BBOOL *) token_attr->pValue = FALSE;
Packit 8681c6
Packit 8681c6
    priv_attr->type = CKA_PRIVATE;
Packit 8681c6
    priv_attr->ulValueLen = sizeof(CK_BBOOL);
Packit 8681c6
    priv_attr->pValue = (CK_BYTE *) priv_attr + sizeof(CK_ATTRIBUTE);
Packit 8681c6
    *(CK_BBOOL *) priv_attr->pValue = FALSE;
Packit 8681c6
Packit 8681c6
    mod_attr->type = CKA_MODIFIABLE;
Packit 8681c6
    mod_attr->ulValueLen = sizeof(CK_BBOOL);
Packit 8681c6
    mod_attr->pValue = (CK_BYTE *) mod_attr + sizeof(CK_ATTRIBUTE);
Packit 8681c6
    *(CK_BBOOL *) mod_attr->pValue = TRUE;
Packit 8681c6
Packit 8681c6
    label_attr->type = CKA_LABEL;
Packit 8681c6
    label_attr->ulValueLen = 0; // empty string
Packit 8681c6
    label_attr->pValue = NULL;
Packit 8681c6
Packit Service 8aa27d
    unique_id_attr->type = CKA_UNIQUE_ID;
Packit Service 8aa27d
    unique_id_attr->ulValueLen = UNIQUE_ID_LEN * 2;
Packit Service 8aa27d
    unique_id_attr->pValue = (CK_BYTE *) unique_id_attr + sizeof(CK_ATTRIBUTE);
Packit Service 8aa27d
    memcpy(unique_id_attr->pValue, unique_id_str, UNIQUE_ID_LEN * 2);
Packit Service 8aa27d
Packit 8681c6
    template_update_attribute(tmpl, token_attr);
Packit 8681c6
    template_update_attribute(tmpl, priv_attr);
Packit 8681c6
    template_update_attribute(tmpl, mod_attr);
Packit 8681c6
    template_update_attribute(tmpl, label_attr);
Packit Service 8aa27d
    template_update_attribute(tmpl, unique_id_attr);
Packit 8681c6
Packit 8681c6
    /* the TEMPLATE 'owns' the attributes now.
Packit 8681c6
     * it is responsible for freeing them upon deletion...
Packit 8681c6
     */
Packit 8681c6
    return CKR_OK;
Packit 8681c6
}
Packit 8681c6
Packit 8681c6
Packit 8681c6
/* template_update_attribute()
Packit 8681c6
 *
Packit 8681c6
 * modifies an existing attribute or adds a new attribute to the template
Packit 8681c6
 *
Packit 8681c6
 * Returns:  TRUE on success, FALSE on failure
Packit 8681c6
 */
Packit 8681c6
CK_RV template_update_attribute(TEMPLATE *tmpl, CK_ATTRIBUTE *new_attr)
Packit 8681c6
{
Packit 8681c6
    DL_NODE *node = NULL;
Packit 8681c6
    CK_ATTRIBUTE *attr = NULL;
Packit 8681c6
Packit 8681c6
    if (!tmpl || !new_attr) {
Packit 8681c6
        TRACE_ERROR("Invalid function arguments.\n");
Packit 8681c6
        return CKR_FUNCTION_FAILED;
Packit 8681c6
    }
Packit 8681c6
    node = tmpl->attribute_list;
Packit 8681c6
Packit 8681c6
    /* if the attribute already exists in the list, remove it.
Packit 8681c6
     * this algorithm will limit an attribute to appearing at most
Packit 8681c6
     * once in the list
Packit 8681c6
     */
Packit 8681c6
    while (node != NULL) {
Packit 8681c6
        attr = (CK_ATTRIBUTE *) node->data;
Packit 8681c6
Packit 8681c6
        if (new_attr->type == attr->type) {
Packit 8681c6
            free(attr);
Packit 8681c6
            tmpl->attribute_list =
Packit 8681c6
                dlist_remove_node(tmpl->attribute_list, node);
Packit 8681c6
            break;
Packit 8681c6
        }
Packit 8681c6
Packit 8681c6
        node = node->next;
Packit 8681c6
    }
Packit 8681c6
Packit 8681c6
    /* add the new attribute */
Packit 8681c6
    tmpl->attribute_list = dlist_add_as_first(tmpl->attribute_list, new_attr);
Packit 8681c6
Packit 8681c6
    return CKR_OK;
Packit 8681c6
}
Packit 8681c6
Packit 8681c6
Packit 8681c6
/* template_validate_attribute()
Packit 8681c6
 *
Packit 8681c6
 * essentially a group of if-then-else-switch clauses.  separated from
Packit 8681c6
 * template_validate_attributes() to make that routine more readable
Packit 8681c6
 */
Packit 8681c6
CK_RV template_validate_attribute(STDLL_TokData_t *tokdata, TEMPLATE *tmpl,
Packit 8681c6
                                  CK_ATTRIBUTE *attr, CK_ULONG class,
Packit 8681c6
                                  CK_ULONG subclass, CK_ULONG mode)
Packit 8681c6
{
Packit 8681c6
    if (class == CKO_DATA) {
Packit 8681c6
        return data_object_validate_attribute(tmpl, attr, mode);
Packit 8681c6
    } else if (class == CKO_CERTIFICATE) {
Packit 8681c6
        if (subclass == CKC_X_509)
Packit 8681c6
            return cert_x509_validate_attribute(tmpl, attr, mode);
Packit 8681c6
        else
Packit 8681c6
            return cert_vendor_validate_attribute(tmpl, attr, mode);
Packit 8681c6
    } else if (class == CKO_PUBLIC_KEY) {
Packit 8681c6
        switch (subclass) {
Packit 8681c6
        case CKK_RSA:
Packit 8681c6
            return rsa_publ_validate_attribute(tokdata, tmpl, attr, mode);
Packit 8681c6
        case CKK_DSA:
Packit 8681c6
            return dsa_publ_validate_attribute(tokdata, tmpl, attr, mode);
Packit 8681c6
        case CKK_ECDSA:
Packit 8681c6
            return ecdsa_publ_validate_attribute(tokdata, tmpl, attr, mode);
Packit 8681c6
        case CKK_DH:
Packit 8681c6
            return dh_publ_validate_attribute(tokdata, tmpl, attr, mode);
Packit 8681c6
        case CKK_KEA:
Packit 8681c6
            return kea_publ_validate_attribute(tokdata, tmpl, attr, mode);
Packit 8681c6
        case CKK_IBM_PQC_DILITHIUM:
Packit 8681c6
            return ibm_dilithium_publ_validate_attribute(tokdata, tmpl, attr, mode);
Packit 8681c6
        default:
Packit 8681c6
            TRACE_ERROR("%s\n", ock_err(ERR_ATTRIBUTE_VALUE_INVALID));
Packit 8681c6
            return CKR_ATTRIBUTE_VALUE_INVALID; // unknown key type
Packit 8681c6
        }
Packit 8681c6
    } else if (class == CKO_PRIVATE_KEY) {
Packit 8681c6
        switch (subclass) {
Packit 8681c6
        case CKK_RSA:
Packit 8681c6
            return rsa_priv_validate_attribute(tokdata, tmpl, attr, mode);
Packit 8681c6
        case CKK_DSA:
Packit 8681c6
            return dsa_priv_validate_attribute(tokdata, tmpl, attr, mode);
Packit 8681c6
        case CKK_ECDSA:
Packit 8681c6
            return ecdsa_priv_validate_attribute(tokdata, tmpl, attr, mode);
Packit 8681c6
        case CKK_DH:
Packit 8681c6
            return dh_priv_validate_attribute(tokdata, tmpl, attr, mode);
Packit 8681c6
        case CKK_KEA:
Packit 8681c6
            return kea_priv_validate_attribute(tokdata, tmpl, attr, mode);
Packit 8681c6
        case CKK_IBM_PQC_DILITHIUM:
Packit 8681c6
            return ibm_dilithium_priv_validate_attribute(tokdata, tmpl, attr, mode);
Packit 8681c6
        default:
Packit 8681c6
            TRACE_ERROR("%s\n", ock_err(ERR_ATTRIBUTE_VALUE_INVALID));
Packit 8681c6
            return CKR_ATTRIBUTE_VALUE_INVALID; // unknown key type
Packit 8681c6
        }
Packit 8681c6
    } else if (class == CKO_SECRET_KEY) {
Packit 8681c6
        switch (subclass) {
Packit 8681c6
        case CKK_GENERIC_SECRET:
Packit 8681c6
            return generic_secret_validate_attribute(tokdata, tmpl, attr, mode);
Packit 8681c6
        case CKK_RC2:
Packit 8681c6
            return rc2_validate_attribute(tokdata, tmpl, attr, mode);
Packit 8681c6
        case CKK_RC4:
Packit 8681c6
            return rc4_validate_attribute(tokdata, tmpl, attr, mode);
Packit 8681c6
        case CKK_RC5:
Packit 8681c6
            return rc5_validate_attribute(tokdata, tmpl, attr, mode);
Packit 8681c6
        case CKK_DES:
Packit 8681c6
            return des_validate_attribute(tokdata, tmpl, attr, mode);
Packit 8681c6
        case CKK_DES2:
Packit 8681c6
            return des2_validate_attribute(tokdata, tmpl, attr, mode);
Packit 8681c6
        case CKK_DES3:
Packit 8681c6
            return des3_validate_attribute(tokdata, tmpl, attr, mode);
Packit 8681c6
        case CKK_CAST:
Packit 8681c6
            return cast_validate_attribute(tokdata, tmpl, attr, mode);
Packit 8681c6
        case CKK_CAST3:
Packit 8681c6
            return cast3_validate_attribute(tokdata, tmpl, attr, mode);
Packit 8681c6
        case CKK_CAST5:
Packit 8681c6
            return cast5_validate_attribute(tokdata, tmpl, attr, mode);
Packit 8681c6
        case CKK_IDEA:
Packit 8681c6
            return idea_validate_attribute(tokdata, tmpl, attr, mode);
Packit 8681c6
#if !(NOCDMF)
Packit 8681c6
        case CKK_CDMF:
Packit 8681c6
            return cdmf_validate_attribute(tokdata, tmpl, attr, mode);
Packit 8681c6
#endif
Packit 8681c6
        case CKK_SKIPJACK:
Packit 8681c6
            return skipjack_validate_attribute(tokdata, tmpl, attr, mode);
Packit 8681c6
        case CKK_BATON:
Packit 8681c6
            return baton_validate_attribute(tokdata, tmpl, attr, mode);
Packit 8681c6
        case CKK_JUNIPER:
Packit 8681c6
            return juniper_validate_attribute(tokdata, tmpl, attr, mode);
Packit 8681c6
        case CKK_AES:
Packit 8681c6
            return aes_validate_attribute(tokdata, tmpl, attr, mode);
Packit 8681c6
        default:
Packit 8681c6
            TRACE_ERROR("%s\n", ock_err(ERR_ATTRIBUTE_VALUE_INVALID));
Packit 8681c6
            return CKR_ATTRIBUTE_VALUE_INVALID; // unknown key type
Packit 8681c6
        }
Packit 8681c6
    } else if (class == CKO_HW_FEATURE) {
Packit 8681c6
        if (subclass >= CKH_VENDOR_DEFINED)
Packit 8681c6
            return CKR_OK;
Packit 8681c6
        switch (subclass) {
Packit 8681c6
        case CKH_CLOCK:
Packit 8681c6
            return clock_validate_attribute(tmpl, attr, mode);
Packit 8681c6
        case CKH_MONOTONIC_COUNTER:
Packit 8681c6
            return counter_validate_attribute(tmpl, attr, mode);
Packit 8681c6
        default:
Packit 8681c6
            TRACE_ERROR("%s\n", ock_err(ERR_ATTRIBUTE_VALUE_INVALID));
Packit 8681c6
            return CKR_ATTRIBUTE_VALUE_INVALID;
Packit 8681c6
        }
Packit 8681c6
    } else if (class == CKO_DOMAIN_PARAMETERS) {
Packit 8681c6
        switch (subclass) {
Packit 8681c6
        case CKK_DSA:
Packit 8681c6
            return dp_dsa_validate_attribute(tmpl, attr, mode);
Packit 8681c6
        case CKK_DH:
Packit 8681c6
            return dp_dh_validate_attribute(tmpl, attr, mode);
Packit 8681c6
        case CKK_X9_42_DH:
Packit 8681c6
            return dp_x9dh_validate_attribute(tmpl, attr, mode);
Packit 8681c6
        default:
Packit 8681c6
            TRACE_ERROR("%s\n", ock_err(ERR_ATTRIBUTE_VALUE_INVALID));
Packit 8681c6
            return CKR_ATTRIBUTE_VALUE_INVALID;
Packit 8681c6
        }
Packit Service 8aa27d
    } else if (class == CKO_PROFILE) {
Packit Service 8aa27d
        return profile_object_validate_attribute(tmpl, attr, mode);
Packit 8681c6
    }
Packit 8681c6
Packit 8681c6
    TRACE_ERROR("%s\n", ock_err(ERR_ATTRIBUTE_VALUE_INVALID));
Packit 8681c6
Packit 8681c6
    return CKR_ATTRIBUTE_VALUE_INVALID; // default fallthru
Packit 8681c6
}
Packit 8681c6
Packit 8681c6
Packit 8681c6
/* template_validate_attributes()
Packit 8681c6
 *
Packit 8681c6
 * walk through the list of attributes in the template validating each one
Packit 8681c6
 */
Packit 8681c6
CK_RV template_validate_attributes(STDLL_TokData_t *tokdata, TEMPLATE *tmpl,
Packit 8681c6
                                   CK_ULONG class, CK_ULONG subclass,
Packit 8681c6
                                   CK_ULONG mode)
Packit 8681c6
{
Packit 8681c6
    DL_NODE *node;
Packit 8681c6
    CK_RV rc = CKR_OK;
Packit 8681c6
Packit 8681c6
    node = tmpl->attribute_list;
Packit 8681c6
Packit 8681c6
    while (node) {
Packit 8681c6
        CK_ATTRIBUTE *attr = (CK_ATTRIBUTE *) node->data;
Packit 8681c6
Packit 8681c6
        rc = template_validate_attribute(tokdata, tmpl, attr, class,
Packit 8681c6
                                         subclass, mode);
Packit 8681c6
        if (rc != CKR_OK) {
Packit 8681c6
            TRACE_DEVEL("template_validate_attribute failed.\n");
Packit 8681c6
            return rc;
Packit 8681c6
        }
Packit 8681c6
        node = node->next;
Packit 8681c6
    }
Packit 8681c6
Packit 8681c6
    return CKR_OK;
Packit 8681c6
}
Packit 8681c6
Packit 8681c6
Packit 8681c6
/* template_validate_base_attribute() */
Packit 8681c6
CK_RV template_validate_base_attribute(TEMPLATE *tmpl, CK_ATTRIBUTE *attr,
Packit 8681c6
                                       CK_ULONG mode)
Packit 8681c6
{
Packit 8681c6
    if (!tmpl || !attr) {
Packit 8681c6
        TRACE_ERROR("Invalid function arguments.\n");
Packit 8681c6
        return CKR_FUNCTION_FAILED;
Packit 8681c6
    }
Packit 8681c6
    switch (attr->type) {
Packit 8681c6
    case CKA_CLASS:
Packit 8681c6
        if ((mode & (MODE_CREATE | MODE_DERIVE | MODE_KEYGEN | MODE_UNWRAP)) !=
Packit 8681c6
            0)
Packit 8681c6
            return CKR_OK;
Packit 8681c6
        break;
Packit 8681c6
    case CKA_TOKEN:
Packit 8681c6
        if ((mode & (MODE_CREATE | MODE_COPY | MODE_DERIVE | MODE_KEYGEN |
Packit 8681c6
                     MODE_UNWRAP)) != 0)
Packit 8681c6
            return CKR_OK;
Packit 8681c6
        break;
Packit 8681c6
    case CKA_PRIVATE:
Packit 8681c6
        if ((mode & (MODE_CREATE | MODE_COPY | MODE_DERIVE | MODE_KEYGEN |
Packit 8681c6
                     MODE_UNWRAP)) != 0)
Packit 8681c6
            return CKR_OK;
Packit 8681c6
        break;
Packit 8681c6
    case CKA_ALWAYS_AUTHENTICATE:
Packit 8681c6
        if ((mode & (MODE_CREATE | MODE_COPY | MODE_DERIVE | MODE_KEYGEN |
Packit 8681c6
                     MODE_UNWRAP)) != 0)
Packit 8681c6
            return CKR_OK;
Packit 8681c6
        break;
Packit 8681c6
    case CKA_LABEL:
Packit 8681c6
        return CKR_OK;
Packit 8681c6
    case CKA_IBM_OPAQUE:
Packit 8681c6
        /* Allow this attribute to be modified in order to support
Packit 8681c6
         * migratable keys on secure key tokens.
Packit 8681c6
         */
Packit 8681c6
        if ((mode & (MODE_COPY | MODE_MODIFY)) != 0)
Packit 8681c6
            return CKR_OK;
Packit 8681c6
        break;
Packit 8681c6
    case CKA_MODIFIABLE:
Packit 8681c6
        if ((mode & (MODE_CREATE | MODE_COPY | MODE_DERIVE | MODE_KEYGEN |
Packit 8681c6
                     MODE_UNWRAP)) != 0)
Packit 8681c6
            return CKR_OK;
Packit 8681c6
        break;
Packit Service 8aa27d
    case CKA_UNIQUE_ID:
Packit Service 8aa27d
        if ((mode & (MODE_CREATE | MODE_COPY | MODE_DERIVE | MODE_KEYGEN |
Packit Service 8aa27d
                     MODE_UNWRAP)) != 0)
Packit Service 8aa27d
        TRACE_ERROR("%s\n", ock_err(ERR_ATTRIBUTE_READ_ONLY));
Packit Service 8aa27d
        return CKR_ATTRIBUTE_READ_ONLY;
Packit Service 8aa27d
        break;
Packit 8681c6
    default:
Packit Service 8aa27d
        TRACE_ERROR("%s: %lx\n", ock_err(ERR_TEMPLATE_INCONSISTENT),
Packit Service 8aa27d
                    attr->type);
Packit 8681c6
        return CKR_TEMPLATE_INCONSISTENT;
Packit 8681c6
    }
Packit 8681c6
Packit 8681c6
    TRACE_ERROR("%s\n", ock_err(ERR_ATTRIBUTE_READ_ONLY));
Packit 8681c6
Packit 8681c6
    return CKR_ATTRIBUTE_READ_ONLY;
Packit 8681c6
}