⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tal_kprs.c

📁 Next BIOS Source code : Extensible Firmware Interface
💻 C
📖 第 1 页 / 共 2 页
字号:
                    }                   
                }
                OutData_ptr[0].Length = KeyDataLength;
                cssm_memcpy(OutData_ptr[0].Data, 
                            KeyBlob_ptr->KeyData.Data+KeyDataOffset,
                            OutData_ptr[0].Length);
                break;
            }
        default:
            TAL_SetError(CSSM_CSP_KEY_FORMAT_INCORRECT);
            return CSSM_FAIL;
        } /* switch FORMAT */
        break;
#endif /*CSSM_BIS*/
    /*######################### end not CSSM_BIS ##########################*/

    default:
        TAL_SetError(CSSM_CSP_INVALID_KEYCLASS);
        return CSSM_FAIL;
    } /* switch KeyClass */

    return CSSM_OK;
}

/*-----------------------------------------------------------------------------
 * TAL_CreateKeyBlob
 *
 *Description:
 *	Given a KeyBlob_ptr structure and a set of key parts, the keydata
 *	portion of the KeyBlob_ptr is formulated based on the values in the
 *	key header. The key parts are represented as an array of CSSM_DATA
 *	structures, and are required to be in the order specified for each
 *	key type below.
 *
 *	Keyblobs to be created must be unwrapped, use the current header
 *	format, and have the actual key data present. Requesting key handle
 *	or label references will cause an error.
 *
 *  Input key data is assumed to be little-ENDIAN byte order.
 *
 *Supported Keyblob Types:
 *	BlobDescription: CSSM_KEYCLASS_SESSION_KEY, CSSM_KEYBLOB_RAW_FORMAT_NONE
 *      AlgorithmId: Any
 *     Data Order: N/A
 *
 *	BlobDescription: CSSM_KEYCLASS_PUBLIC_KEY, CSSM_KEYBLOB_RAW_FORMAT_PKCS1
 *      AlgorithmId: CSSM_ALGID_RSA
 *     Data Order: modulus, public exponent
 *
 *	BlobDescription: CSSM_KEYCLASS_PRIVATE_KEY, CSSM_KEYBLOB_RAW_FORMAT_PKCS1
 *      AlgorithmId: CSSM_ALGID_RSA
 *     Data Order: modulus, public exponent, ...
 *
 *	BlobDescription: CSSM_KEYCLASS_PUBLIC_KEY, CSSM_KEYBLOB_RAW_FORMAT_FIPS186
 *      AlgorithmId: CSSM_ALGID_DSA
 *     Data Order: p, q, g, y
 *
 *	BlobDescription: CSSM_KEYCLASS_PRIVATE_KEY, CSSM_KEYBLOB_RAW_FORMAT_FIPS186
 *      AlgorithmId: CSSM_ALGID_DSA
 *     Data Order: p, q, g, x
 *
 *	BlobDescription: CSSM_KEYCLASS_PRIVATE_KEY, CSSM_KEYBLOB_RAW_FORMAT_PKCS8
 *      AlgorithmId: CSSM_ALGID_DSA/CSSM_ALGID_DSA
 *     Data Order: PrivateKey
 *
 *Parameters:
 *  KeyBlob_ptr(output) - A pointer to a KeyBlob_ptr buffer. Caller has to
 *                        allocate this buffer.
 *	InData_ptr(input) - A pointer to number of CSSM_DATA input buffers. 
 *	InDataCount(input) - contains the number of InData_ptr
 *
 *Returns:
 *  CSSM_OK:   The KeyBlob_ptr is created.
 *  CSSM_FAIL: Can not create the key blob form InData_ptr
 *---------------------------------------------------------------------------*/
CSSM_RETURN TAL_CreateKeyBlob(CSSM_KEY_PTR KeyBlob_ptr,
                              const CSSM_DATA_PTR InData_ptr,
                              uint32 InDataCount)
{
    uint8	*destPtr = NULL;
    sint32  rtnCode;
    CSSM_KEYHEADER_PTR header = NULL;

    /* Check parameters */
    if (InData_ptr == NULL)
    {
        TAL_SetError(CSSM_CSP_INVALID_DATA_POINTER);
        return CSSM_FAIL;
    }
    if (InDataCount == 0)
    {
        TAL_SetError(CSSM_CSP_INVALID_DATA_COUNT);
        return CSSM_FAIL;
    }
    /* Verify the key structure */
    if (TAL_ValidateInKeyParam(KeyBlob_ptr) != CSSM_OK)
        return CSSM_FAIL;

    destPtr = KeyBlob_ptr->KeyData.Data;
    header = &KeyBlob_ptr->KeyHeader;

    /* Check the header's magic number, the key isn't wrapped and
       the data is actually present */
    if ((header->HeaderVersion != CSSM_KEYHEADER_VERSION) ||
        (header->WrapAlgorithmId != CSSM_ALGID_NONE) ||
        ((header->BlobType != CSSM_KEYBLOB_RAW) &&
         (header->BlobType != CSSM_KEYBLOB_RAW_BERDER)))
	{
        TAL_SetError(CSSM_CSP_KEY_BLOBTYPE_INCORRECT);
        return CSSM_FAIL;
    }
    /* Create the key for each key class and format */
    switch (header->KeyClass)
    {
    case CSSM_KEYCLASS_PUBLIC_KEY:
        switch (header->Format)
        {
        case CSSM_KEYBLOB_RAW_FORMAT_FIPS186:
            if ((rtnCode = TAL_Build_FIPS186_Key(KeyBlob_ptr,
                                              InData_ptr,
                                              InDataCount)) != CSSM_OK)
            {
                TAL_SetError(rtnCode);
                return CSSM_FAIL;
            }
            break;

    /*######################### bgn not CSSM_BIS ##########################*/
#ifndef CSSM_BIS
        case CSSM_KEYBLOB_RAW_FORMAT_PKCS1:
            if ((rtnCode = TAL_Build_PKCS1_Key(KeyBlob_ptr,
                                              InData_ptr,
                                              InDataCount)) != CSSM_OK)
            {
                TAL_SetError(rtnCode);
                return CSSM_FAIL;
            }
            break;

        /* CSSM_BLOCDESC_PKCS3
         * This format is used with keys generated as specified in PKCS #3. The
         * public value y is the only value that is placed in the KeyBlob_ptr.
         * This mode is equivalent to CSSM_BLOBDESC_RAW in since there is no 
         * formatting data.
         */
        case CSSM_KEYBLOB_RAW_FORMAT_PKCS3:
            /* PKCS #3 is specific to Diffie-Hellman keys */
            if (header->AlgorithmId != CSSM_ALGID_DH)
            {
                TAL_SetError(CSSM_CSP_KEY_FORMAT_INCORRECT);
                return CSSM_FAIL;
            }
            /* Make sure the correct number of buffers were passed */
            if (InDataCount != 1) {
                TAL_SetError(CSSM_CSP_INVALID_DATA_COUNT);              
                return CSSM_FAIL;
            }
            /* Construct the key data */
            cssm_memcpy(destPtr,InData_ptr[0].Data,InData_ptr[0].Length);
            KeyBlob_ptr->KeyData.Length = InData_ptr[0].Length;
            break;
#endif /*CSSM_BIS*/
    /*######################### end not CSSM_BIS ##########################*/

        default:
            TAL_SetError(CSSM_CSP_KEY_FORMAT_INCORRECT);
            return CSSM_FAIL;
        } /* switch Format */
        break;

    /*######################### end not CSSM_BIS ##########################*/
#ifndef  CSSM_BIS

    case CSSM_KEYCLASS_SESSION_KEY:
        switch (header->Format)
        {
        case CSSM_KEYBLOB_RAW_FORMAT_NONE:
            if ((rtnCode = TAL_Build_RAW_Key(KeyBlob_ptr,
                                             InData_ptr,
                                             InDataCount)) != CSSM_OK)
            {
                TAL_SetError(rtnCode);
                return CSSM_FAIL;
            }
            break;

        default:
            TAL_SetError(CSSM_CSP_KEY_FORMAT_INCORRECT);
            return CSSM_FAIL;
        } /* switch Format */
        break;

    case CSSM_KEYCLASS_PRIVATE_KEY:
        switch (header->Format)
        {
        case CSSM_KEYBLOB_RAW_FORMAT_PKCS1:
            if ((rtnCode = TAL_Build_PKCS1_Key(KeyBlob_ptr,
                                              InData_ptr,
                                              InDataCount)) != CSSM_OK)
            {
                TAL_SetError(rtnCode);
                return CSSM_FAIL;
            }
            break;

        case CSSM_KEYBLOB_RAW_FORMAT_FIPS186:
            if ((rtnCode = TAL_Build_FIPS186_Key(KeyBlob_ptr,
                                                InData_ptr,
                                                InDataCount)) != CSSM_OK)
            {
                TAL_SetError(rtnCode);
                return CSSM_FAIL;
            }
            break;

#ifdef CAP_KEYPAIR_GEN
        case CSSM_KEYBLOB_RAW_FORMAT_PKCS8:
            {
                uint32	NeedSize;
                sint32	rtnCode;

                /* Make sure correct number of buffers were passed */
                if (InDataCount != 1) {
                    TAL_SetError(CSSM_CSP_INVALID_DATA_COUNT);              
                    return CSSM_FAIL;
                }
                /* Try to create the key.  */
                /* Get the output size, if there is no out keydata buffer. */
                rtnCode = TAL_Create_PKCS8_RawKey(KeyBlob_ptr, &NeedSize,
                                            header->AlgorithmId, InData_ptr);

                /* Make sure the output buffer has space and is big enough,
                   then copy to output buffer. */
                if (rtnCode == CSSM_CSP_NOT_ENOUGH_BUFFER)
                {
                    if (KeyBlob_ptr->KeyData.Length == 0)
                    {
                        if ((KeyBlob_ptr->KeyData.Data =
                                cssm_calloc(1, NeedSize, NULL)) == NULL)
                        {
                            TAL_SetError(CSSM_CSP_MEMORY_ERROR);
                            return CSSM_FAIL;
                        }
                        KeyBlob_ptr->KeyData.Length = NeedSize;
                        rtnCode = TAL_Create_PKCS8_RawKey(KeyBlob_ptr, NULL,
                                              header->AlgorithmId, InData_ptr);
                    }
                    else
                    {
                        TAL_SetError(CSSM_CSP_NOT_ENOUGH_BUFFER);
                        return CSSM_FAIL;           
                    }                   
                }
                if (rtnCode != CSSM_OK)
                {
                    TAL_SetError(CSSM_CSP_INVALID_KEY);
                    return CSSM_FAIL;
                }
                break;
            }
#endif /* CAP_KEYPAIR_GEN */

        default:
            TAL_SetError(CSSM_CSP_KEY_FORMAT_INCORRECT);
            return CSSM_FAIL;
        } /* switch Format */
        break;

#endif /*CSSM_BIS*/
    /*######################### end not CSSM_BIS ##########################*/

    default:
        TAL_SetError(CSSM_CSP_INVALID_KEYCLASS);
        return CSSM_FAIL;
    } /* switch KeyClass*/

    return CSSM_OK;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -