1/*
2 * Copyright (c) 2004,2011,2014 Apple Inc. All Rights Reserved.
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// SDFactory -- The factory for Security Server context objects
27//
28#include "SDFactory.h"
29
30#include "SDContext.h"
31
32
33//
34// SDFactory -- The factory for Security Server context objects
35//
36bool SDFactory::setup(SDCSPSession &session, CSPFullPluginSession::CSPContext * &cspCtx,
37					  const Context &context, bool encoding)
38{
39	if (cspCtx)
40		return false;	// not ours or already set
41
42	switch (context.type())
43	{
44	case CSSM_ALGCLASS_SIGNATURE:
45		cspCtx = new SDSignatureContext(session);
46		return true;
47	case CSSM_ALGCLASS_MAC:
48		cspCtx = new SDMACContext(session);
49		return true;
50	case CSSM_ALGCLASS_DIGEST:
51		cspCtx = new SDDigestContext(session);
52		return true;
53	case CSSM_ALGCLASS_SYMMETRIC:
54	case CSSM_ALGCLASS_ASYMMETRIC:
55		cspCtx = new SDCryptContext(session); // @@@ Could also be wrap/unwrap
56		return true;
57	case CSSM_ALGCLASS_RANDOMGEN:
58		cspCtx = new SDRandomContext(session); // @@@ Should go.
59		return true;
60	}
61
62	return false;
63
64#if 0
65	/* FIXME - qualify by ALGCLASS as well to avoid MAC */
66	switch (context.algorithm()) {
67	case CSSM_ALGID_MD5:
68		cspCtx = new MD5Context(session);
69		return true;
70	case CSSM_ALGID_SHA1:
71		cspCtx = new SHA1Context(session);
72		return true;
73	}
74	return false;
75
76    if (ctx)
77        CssmError::throwMe(CSSM_ERRCODE_INTERNAL_ERROR);	// won't support re-definition
78    switch (context.algorithm()) {
79        case CSSM_ALGID_ROTTY_ROT_16:
80            ctx = new SDContext(16);
81            return true;
82        case CSSM_ALGID_ROTTY_ROT_37:
83            ctx = new SDContext(37);
84            return true;
85    }
86#endif
87    return false;
88}
89