]> git.saurik.com Git - apple/security.git/blame - Security/libsecurity_sd_cspdl/lib/SDContext.cpp
Security-57031.1.35.tar.gz
[apple/security.git] / Security / libsecurity_sd_cspdl / lib / SDContext.cpp
CommitLineData
b1ab9ed8 1/*
d8f41ccd 2 * Copyright (c) 2004,2011-2012,2014 Apple Inc. All Rights Reserved.
b1ab9ed8
A
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25//
26// SDContext - cryptographic contexts for the security server
27//
28#include "SDContext.h"
29
30#include "SDCSPSession.h"
31#include "SDKey.h"
32#include <security_utilities/debugging.h>
33
34#define ssCryptDebug(args...) secdebug("ssCrypt", ## args)
35
36using namespace SecurityServer;
37
38//
39// SDContext
40//
41SDContext::SDContext(SDCSPSession &session)
42: mSession(session), mContext(NULL)
43{
44}
45
46void SDContext::clearOutBuf()
47{
48 if(mOutBuf.Data) {
49 mSession.free(mOutBuf.Data);
50 mOutBuf.clear();
51 }
52}
53
54void SDContext::copyOutBuf(CssmData &out)
55{
56 if(out.length() < mOutBuf.length()) {
57 CssmError::throwMe(CSSMERR_CSP_OUTPUT_LENGTH_ERROR);
58 }
59 memmove(out.Data, mOutBuf.Data, mOutBuf.Length);
60 out.Length = mOutBuf.Length;
61 clearOutBuf();
62}
63
64void
65SDContext::init(const Context &context,
66 bool /* encoding */) // @@@ should be removed from API since it's already in mDirection
67{
68 mContext = &context;
69 clearOutBuf();
70}
71
72SecurityServer::ClientSession &
73SDContext::clientSession()
74{
75 return mSession.clientSession();
76}
77
78
79//
80// SDRandomContext -- Context for GenerateRandom operations
81//
82SDRandomContext::SDRandomContext(SDCSPSession &session) : SDContext(session) {}
83
84void
85SDRandomContext::init(const Context &context, bool encoding)
86{
87 SDContext::init(context, encoding);
88
89 // set/freeze output size
90 mOutSize = context.getInt(CSSM_ATTRIBUTE_OUTPUT_SIZE, CSSMERR_CSP_MISSING_ATTR_OUTPUT_SIZE);
91
92#if 0
93 // seed the PRNG (if specified)
94 if (const CssmCryptoData *seed = context.get<CssmCryptoData>(CSSM_ATTRIBUTE_SEED)) {
95 const CssmData &seedValue = (*seed)();
96 clientSession().seedRandom(seedValue);
97 }
98#endif
99}
100
101size_t
102SDRandomContext::outputSize(bool final, size_t inSize)
103{
104 return mOutSize;
105}
106
107void
108SDRandomContext::final(CssmData &out)
109{
110 clientSession().generateRandom(*mContext, out);
111}
112
113
114// signature contexts
115SDSignatureContext::SDSignatureContext(SDCSPSession &session)
116 : SDContext(session),
117 mKeyHandle(noKey),
118 mNullDigest(NULL),
119 mDigest(NULL)
120{
121 /* nothing else for now */
122}
123
124SDSignatureContext::~SDSignatureContext()
125{
126 delete mNullDigest;
127 delete mDigest;
128}
129
130void SDSignatureContext::init(const Context &context, bool signing)
131{
132 SDContext::init(context, signing);
133
134 /* reusable: skip everything except resetting digest state */
135 if((mNullDigest != NULL) || (mDigest != NULL)) {
136 if(mNullDigest != NULL) {
137 mNullDigest->digestInit();
138 }
139 return;
140 }
141
142 /* snag key from context */
143 const CssmKey &keyInContext =
144 context.get<const CssmKey>(CSSM_ATTRIBUTE_KEY,
145 CSSMERR_CSP_MISSING_ATTR_KEY);
146 mKeyHandle = mSession.lookupKey(keyInContext).keyHandle();
147
148 /* get digest alg and sig alg from Context.algorithm */
149 switch(context.algorithm()) {
150 /*** DSA ***/
151 case CSSM_ALGID_SHA1WithDSA:
152 mDigestAlg = CSSM_ALGID_SHA1;
153 mSigAlg = CSSM_ALGID_DSA;
154 break;
155 case CSSM_ALGID_DSA: // Raw
156 mDigestAlg = CSSM_ALGID_NONE;
157 mSigAlg = CSSM_ALGID_DSA;
158 break;
159 /*** RSA ***/
160 case CSSM_ALGID_SHA1WithRSA:
161 mDigestAlg = CSSM_ALGID_SHA1;
162 mSigAlg = CSSM_ALGID_RSA;
163 break;
164 case CSSM_ALGID_MD5WithRSA:
165 mDigestAlg = CSSM_ALGID_MD5;
166 mSigAlg = CSSM_ALGID_RSA;
167 break;
168 case CSSM_ALGID_MD2WithRSA:
169 mDigestAlg = CSSM_ALGID_MD2;
170 mSigAlg = CSSM_ALGID_RSA;
171 break;
172 case CSSM_ALGID_RSA: // Raw
173 mDigestAlg = CSSM_ALGID_NONE;
174 mSigAlg = CSSM_ALGID_RSA;
175 break;
176 /*** FEE ***/
177 case CSSM_ALGID_FEE_SHA1:
178 mDigestAlg = CSSM_ALGID_SHA1;
179 mSigAlg = CSSM_ALGID_FEE;
180 break;
181 case CSSM_ALGID_FEE_MD5:
182 mDigestAlg = CSSM_ALGID_MD5;
183 mSigAlg = CSSM_ALGID_FEE;
184 break;
185 case CSSM_ALGID_FEE: // Raw
186 mDigestAlg = CSSM_ALGID_NONE;
187 mSigAlg = CSSM_ALGID_FEE;
188 break;
189 /*** ECDSA ***/
190 case CSSM_ALGID_SHA1WithECDSA:
191 mDigestAlg = CSSM_ALGID_SHA1;
192 mSigAlg = CSSM_ALGID_ECDSA;
193 break;
194 case CSSM_ALGID_SHA224WithECDSA:
195 mDigestAlg = CSSM_ALGID_SHA224;
196 mSigAlg = CSSM_ALGID_ECDSA;
197 break;
198 case CSSM_ALGID_SHA256WithECDSA:
199 mDigestAlg = CSSM_ALGID_SHA256;
200 mSigAlg = CSSM_ALGID_ECDSA;
201 break;
202 case CSSM_ALGID_SHA384WithECDSA:
203 mDigestAlg = CSSM_ALGID_SHA384;
204 mSigAlg = CSSM_ALGID_ECDSA;
205 break;
206 case CSSM_ALGID_SHA512WithECDSA:
207 mDigestAlg = CSSM_ALGID_SHA512;
208 mSigAlg = CSSM_ALGID_ECDSA;
209 break;
210 case CSSM_ALGID_ECDSA: // Raw
211 mDigestAlg = CSSM_ALGID_NONE;
212 mSigAlg = CSSM_ALGID_ECDSA;
213 break;
214 default:
215 CssmError::throwMe(CSSMERR_CSP_INVALID_ALGORITHM);
216 }
217
218 /* set up mNullDigest or mDigest */
219 if(mDigestAlg == CSSM_ALGID_NONE) {
220 mNullDigest = new NullDigest();
221 }
222 else {
223 mDigest = new CssmClient::Digest(mSession.mRawCsp, mDigestAlg);
224 }
225}
226
227/*
228 * for raw sign/verify - optionally called after init.
229 * Note that in init (in this case), we set mDigestAlg to ALGID_NONE and set up
230