]> git.saurik.com Git - apple/security.git/blob - AppleCSP/DiffieHellman/DH_keys.h
Security-54.1.tar.gz
[apple/security.git] / AppleCSP / DiffieHellman / DH_keys.h
1 /*
2 * Copyright (c) 2000-2002 Apple Computer, 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 obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
8 * 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 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.
16 */
17
18
19 /*
20 * DH_keys.h - Diffie-Hellman key pair support
21 */
22
23 #ifndef _DH_KEYS_H_
24 #define _DH_KEYS_H_
25
26 #include <AppleCSP/AppleCSPContext.h>
27 #include <AppleCSP/AppleCSPSession.h>
28 #include <DiffieHellman/DH_csp.h>
29 #include <openssl/dh.h>
30 #include <Security/context.h>
31 #include <opensslUtils/openRsaSnacc.h>
32 #include <Security/appleoids.h>
33
34 #define DH_PUB_KEY_FORMAT CSSM_KEYBLOB_RAW_FORMAT_PKCS3
35 #define DH_PRIV_KEY_FORMAT CSSM_KEYBLOB_RAW_FORMAT_PKCS3
36
37 #define DH_MIN_KEY_SIZE 512 /* FIXME */
38 #define DH_MAX_KEY_SIZE 2048
39
40 /*
41 * Diffie-Hellman version of a BinaryKey.
42 */
43 class DHBinaryKey : public BinaryKey {
44 public:
45 DHBinaryKey(DH *dhKey = NULL); // for private key
46 DHBinaryKey(const CSSM_DATA *pubBlob); // for public key
47 ~DHBinaryKey();
48 void generateKeyBlob(
49 CssmAllocator &allocator,
50 CssmData &blob,
51 CSSM_KEYBLOB_FORMAT &format);
52
53 void setPubBlob(const CSSM_DATA *pubBlob);
54 void setPubBlob(DH *privKey);
55
56 /*
57 * At most one of these is valid - a DH for a private key,
58 * CSSM_DATA for public.
59 */
60 DH *mDhKey;
61 CSSM_DATA mPubKey;
62 };
63
64 class DHKeyPairGenContext :
65 public AppleCSPContext, private AppleKeyPairGenContext {
66 public:
67 DHKeyPairGenContext(
68 AppleCSPSession &session,
69 const Context &) :
70 AppleCSPContext(session),
71 mGenAttrs(NULL) {}
72
73 ~DHKeyPairGenContext() { freeGenAttrs(); }
74
75 // no init functionality, but we need to implement it
76 void init(
77 const Context &,
78 bool) { }
79
80 // this one is specified in, and called from, CSPFullPluginSession
81 void generate(
82 const Context &context,
83 CssmKey &pubKey,
84 CssmKey &privKey);
85
86 // this one is specified in, and called from, AppleKeyPairGenContext
87 void generate(
88 const Context &context,
89 BinaryKey &pubBinKey,
90 BinaryKey &privBinKey,
91 uint32 &keySize);
92
93 // specified in, and called from, CSPFullPluginSessionÊ- generate parameters
94 void generate(
95 const Context &context,
96 uint32 bitSize,
97 CssmData &params,
98 uint32 &attrCount,
99 Context::Attr * &attrs);
100
101 /*
102 * Necessary to handle and deflect "context changed" notification which occurs
103 * after the strange return from "generate parameters", when the plugin adds
104 * the "returned" values to the Context.
105 */
106 bool changed(const Context &context) { return true; }
107
108 void dhGenParams(
109 uint32 keySizeInBits,
110 unsigned g, // probably should be BIGNUM
111 int privValueLength, // optional
112 DHParameter &algParams);
113
114 private:
115 /* gross hack to store attributes "returned" from GenParams */
116 Context::Attr *mGenAttrs;
117 void freeGenAttrs();
118 }; /* DHKeyPairGenContext */
119
120 /*
121 * CSPKeyInfoProvider for Diffie-Hellman keys
122 */
123 class DHKeyInfoProvider : public CSPKeyInfoProvider
124 {
125 public:
126 DHKeyInfoProvider(
127 const CssmKey &cssmKey);
128 ~DHKeyInfoProvider() { }
129 void CssmKeyToBinary(
130 BinaryKey **binKey); // RETURNED
131 void QueryKeySizeInBits(
132 CSSM_KEY_SIZE &keySize); // RETURNED
133 };
134
135 #endif /* _DH_KEYS_H_ */