1/*
2 * Copyright (c) 2003,2011-2012,2014 Apple Inc. All Rights Reserved.
3 *
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please
7 * obtain a copy of the License at http://www.apple.com/publicsource and
8 * read it before using this file.
9 *
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
12 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
13 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
14 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
15 * Please see the License for the specific language governing rights and
16 * limitations under the License.
17 */
18/*
19 * AppleCSPKeys.cpp - Key support
20 */
21
22#include "AppleCSPKeys.h"
23#include "AppleCSPUtils.h"
24/*
25 * CSPKeyInfoProvider for symmetric keys.
26 */
27CSPKeyInfoProvider *SymmetricKeyInfoProvider::provider(
28		const CssmKey 	&cssmKey,
29		AppleCSPSession	&session)
30{
31	if(cssmKey.blobType() != CSSM_KEYBLOB_RAW) {
32		errorLog0("KeyInfoProvider deals only with RAW keys!\n");
33		CssmError::throwMe(CSSMERR_CSP_INTERNAL_ERROR);
34	}
35	if(cssmKey.keyClass() != CSSM_KEYCLASS_SESSION_KEY) {
36		/* that's all we need to know */
37		return NULL;
38	}
39	return new SymmetricKeyInfoProvider(cssmKey, session);
40}
41
42SymmetricKeyInfoProvider::SymmetricKeyInfoProvider(
43	const CssmKey 	&cssmKey,
44	AppleCSPSession	&session) :
45		CSPKeyInfoProvider(cssmKey, session)
46{
47}
48
49/* cook up a Binary key */
50void SymmetricKeyInfoProvider::CssmKeyToBinary(
51	CssmKey				*paramKey,	// ignored
52	CSSM_KEYATTR_FLAGS	&attrFlags,	// IN/OUT
53	BinaryKey 			**binKey)
54{
55	CASSERT(mKey.keyClass() == CSSM_KEYCLASS_SESSION_KEY);
56	SymmetricBinaryKey *symBinKey = new SymmetricBinaryKey(
57		mKey.KeyHeader.LogicalKeySizeInBits);
58	copyCssmData(mKey,
59		symBinKey->mKeyData,
60		symBinKey->mAllocator);
61	*binKey = symBinKey;
62}
63
64/* obtain key size in bits */
65void SymmetricKeyInfoProvider::QueryKeySizeInBits(
66	CSSM_KEY_SIZE &keySize)
67{
68	/* FIXME - do we ever need to calculate RC2 effective size here? */
69	keySize.LogicalKeySizeInBits = keySize.EffectiveKeySizeInBits =
70		(uint32)(mKey.length() * 8);
71}
72
73/*
74 * Obtain blob suitable for hashing in CSSM_APPLECSP_KEYDIGEST
75 * passthrough.
76 */
77bool SymmetricKeyInfoProvider::getHashableBlob(
78	Allocator 	&allocator,
79	CssmData		&blob)			// blob to hash goes here
80{
81	/*
82	 * This is trivial: the raw key is already in the "proper" format.
83	 */
84	assert(mKey.blobType() == CSSM_KEYBLOB_RAW);
85	const CssmData &keyBlob = CssmData::overlay(mKey.KeyData);
86	copyCssmData(keyBlob, blob, allocator);
87	return true;
88}
89
90