2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
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 obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
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 EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
20 // pkcs8.cpp - PKCS8 key wrap/unwrap support.
25 #include "AppleCSPUtils.h"
26 #include "AppleCSPKeys.h"
27 #include <SecurityNssAsn1/keyTemplates.h>
28 #include <SecurityNssAsn1/SecNssCoder.h>
29 #include <SecurityNssAsn1/nssUtils.h>
30 #include "AppleCSPSession.h"
31 #include <Security/cssmapple.h>
34 * Given a key in PKCS8 format, fill in the following
37 * CSSM_KEYBLOB_FORMAT Format
38 * CSSM_ALGORITHMS AlgorithmId
39 * uint32 LogicalKeySizeInBits
41 void AppleCSPSession::pkcs8InferKeyHeader(
45 * Incoming key blob is a PrivateKeyInfo. Take it apart
46 * to get its algorithm info, from which we infer other
49 NSS_PrivateKeyInfo privKeyInfo
;
51 CSSM_DATA
&keyData
= key
.KeyData
;
53 memset(&privKeyInfo
, 0, sizeof(privKeyInfo
));
54 if(coder
.decodeItem(keyData
, NSS_PrivateKeyInfoTemplate
,
56 errorLog0("pkcs8InferKeyHeader decode error\n");
57 CssmError::throwMe(CSSMERR_CSP_INVALID_KEY
);
60 CSSM_KEYHEADER
&hdr
= key
.KeyHeader
;
61 if(!cssmOidToAlg(&privKeyInfo
.algorithm
.algorithm
,
63 errorLog0("pkcs8InferKeyHeader unknown algorithm\n");
64 CssmError::throwMe(CSSMERR_CSP_INVALID_ALGORITHM
);
67 switch(hdr
.AlgorithmId
) {
69 hdr
.Format
= CSSM_KEYBLOB_RAW_FORMAT_PKCS8
;
72 hdr
.Format
= CSSM_KEYBLOB_RAW_FORMAT_FIPS186
;
76 hdr
.Format
= CSSM_KEYBLOB_RAW_FORMAT_NONE
;
81 * Find someone whoe knows about this key and ask them the
84 CSPKeyInfoProvider
*provider
= infoProvider(key
);
85 if(provider
== NULL
) {
86 errorLog0("pkcs8InferKeyHeader no info provider\n");
87 /* but we got this far, so don't abort */
90 CSSM_KEY_SIZE keySize
;
91 provider
->QueryKeySizeInBits(keySize
);
92 hdr
.LogicalKeySizeInBits
= keySize
.LogicalKeySizeInBits
;
97 * When doing a PKCS8 wrap operation on a reference key, this
98 * is used to infer the blob type to obtain before the encryption.
100 CSSM_KEYBLOB_FORMAT
pkcs8RawKeyFormat(
101 CSSM_ALGORITHMS keyAlg
)
105 return CSSM_KEYBLOB_RAW_FORMAT_PKCS8
;
107 return CSSM_KEYBLOB_RAW_FORMAT_FIPS186
;
110 return CSSM_KEYBLOB_RAW_FORMAT_NONE
;