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.
25 #include <opensslUtils/openRsaSnacc.h>
26 #include <Security/logging.h>
27 #include <Security/debugging.h>
28 #include <open_ssl/opensslUtils/opensslUtils.h>
29 #include <openssl/bn.h>
30 #include <openssl/dh.h>
31 #include <openssl/err.h>
33 #define dhMiscDebug(args...) debug("dhMisc", ## args)
37 * -- obtain CSSM key (there must only be one)
38 * -- validate keyClass - MUST be private! (DH public keys are never found
40 * -- validate keyUsage
41 * -- convert to DH *, allocating the DH key if necessary
44 const Context
&context
,
45 AppleCSPSession
&session
,
46 CSSM_KEYUSE usage
, // CSSM_KEYUSE_ENCRYPT, CSSM_KEYUSE_SIGN, etc.
47 bool &mallocdKey
) // RETURNED
50 context
.get
<CssmKey
>(CSSM_ATTRIBUTE_KEY
, CSSMERR_CSP_MISSING_ATTR_KEY
);
51 const CSSM_KEYHEADER
&hdr
= cssmKey
.KeyHeader
;
52 if(hdr
.AlgorithmId
!= CSSM_ALGID_DH
) {
53 CssmError::throwMe(CSSMERR_CSP_ALGID_MISMATCH
);
55 if(hdr
.KeyClass
!= CSSM_KEYCLASS_PRIVATE_KEY
) {
56 CssmError::throwMe(CSSMERR_CSP_INVALID_KEY_CLASS
);
58 cspValidateIntendedKeyUsage(&hdr
, usage
);
59 return cssmKeyToDh(cssmKey
, session
, mallocdKey
);
62 * Convert a CssmKey (Private only!) to an DH * key. May result in the
63 * creation of a new DH (when cssmKey is a raw key); allocdKey is true
64 * in that case in which case the caller generally has to free the allocd key).
67 const CssmKey
&cssmKey
,
68 AppleCSPSession
&session
,
69 bool &allocdKey
) // RETURNED
74 const CSSM_KEYHEADER
*hdr
= &cssmKey
.KeyHeader
;
75 if(hdr
->AlgorithmId
!= CSSM_ALGID_DH
) {
76 // someone else's key (should never happen)
77 CssmError::throwMe(CSSMERR_CSP_INVALID_ALGORITHM
);
79 assert(hdr
->KeyClass
== CSSM_KEYCLASS_PRIVATE_KEY
);
80 switch(hdr
->BlobType
) {
81 case CSSM_KEYBLOB_RAW
:
82 dhKey
= rawCssmKeyToDh(cssmKey
);
85 case CSSM_KEYBLOB_REFERENCE
:
87 BinaryKey
&binKey
= session
.lookupRefKey(cssmKey
);
88 DHBinaryKey
*dhBinKey
= dynamic_cast<DHBinaryKey
*>(&binKey
);
89 /* this cast failing means that this is some other
90 * kind of binary key */
91 if(dhBinKey
== NULL
) {
92 dhMiscDebug("cssmKeyToDh: wrong BinaryKey subclass\n");
93 CssmError::throwMe(CSSMERR_CSP_INVALID_KEY
);
95 assert(dhBinKey
->mDhKey
!= NULL
);
96 dhKey
= dhBinKey
->mDhKey
;
100 CssmError::throwMe(CSSMERR_CSP_KEY_BLOB_TYPE_INCORRECT
);
106 * Convert a raw CssmKey (Private only!) to a newly alloc'd DH key.
109 const CssmKey
&cssmKey
)
111 const CSSM_KEYHEADER
*hdr
= &cssmKey
.KeyHeader
;
113 if(hdr
->AlgorithmId
!= CSSM_ALGID_DH
) {
114 // someone else's key (should never happen)
115 CssmError::throwMe(CSSMERR_CSP_INVALID_ALGORITHM
);
117 assert(hdr
->BlobType
== CSSM_KEYBLOB_RAW
);
118 assert(hdr
->KeyClass
== CSSM_KEYCLASS_PRIVATE_KEY
);
119 if(hdr
->Format
!= DH_PRIV_KEY_FORMAT
) {
120 CssmError::throwMe(CSSMERR_CSP_INVALID_ATTR_PRIVATE_KEY_FORMAT
);
123 DH
*dhKey
= DH_new();
125 CssmError::throwMe(CSSMERR_CSP_MEMORY_ERROR
);
128 crtn
= DHPrivateKeyDecode(dhKey
,
129 cssmKey
.KeyData
.Data
,
130 cssmKey
.KeyData
.Length
);
132 CssmError::throwMe(crtn
);