1/*
2 * Copyright (c) 2011-12 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 * Copyright (c) 2010 Kungliga Tekniska Högskolan
26 * (Royal Institute of Technology, Stockholm, Sweden).
27 * All rights reserved.
28 *
29 * Portions Copyright (c) 2010 Apple Inc. All rights reserved.
30 *
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
33 * are met:
34 *
35 * 1. Redistributions of source code must retain the above copyright
36 *    notice, this list of conditions and the following disclaimer.
37 *
38 * 2. Redistributions in binary form must reproduce the above copyright
39 *    notice, this list of conditions and the following disclaimer in the
40 *    documentation and/or other materials provided with the distribution.
41 *
42 * 3. Neither the name of the Institute nor the names of its contributors
43 *    may be used to endorse or promote products derived from this software
44 *    without specific prior written permission.
45 *
46 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 * SUCH DAMAGE.
57 */
58
59#include "ossl-config.h"
60
61#include <errno.h>
62#include <stdio.h>
63#include <stdlib.h>
64
65#include "krb5-types.h"
66#include "rfc2459_asn1.h"
67
68#include "ossl-bn.h"
69#include "ossl-common.h"
70
71#ifdef HAVE_CDSA
72
73static CSSM_CSP_HANDLE cspHandle;
74
75static CSSM_VERSION vers = { 2, 0 };
76static const CSSM_GUID guid = { 0xFADE, 0, 0, { 1, 2, 3, 4, 5, 6, 7, 0 } };
77
78const CSSM_DATA _cs_labelData = { 7, (void *)"noLabel" };
79
80static void *cssmMalloc(CSSM_SIZE size, void *alloc)
81{
82	return (malloc(size));
83}
84
85
86static void cssmFree(void *ptr, void *alloc)
87{
88	free(ptr);
89}
90
91
92static void *cssmRealloc(void *ptr, CSSM_SIZE size, void *alloc)
93{
94	return (realloc(ptr, size));
95}
96
97
98static void *cssmCalloc(uint32 num, CSSM_SIZE size, void *alloc)
99{
100	return (calloc(num, size));
101}
102
103
104static CSSM_API_MEMORY_FUNCS cssm_memory_funcs =
105{
106	cssmMalloc,
107	cssmFree,
108	cssmRealloc,
109	cssmCalloc,
110	NULL
111};
112
113CSSM_CSP_HANDLE
114_cs_get_cdsa_csphandle(void)
115{
116	CSSM_PVC_MODE pvcPolicy = CSSM_PVC_NONE;
117	CSSM_RETURN ret;
118
119	if (cspHandle) {
120		return (cspHandle);
121	}
122
123	ret = CSSM_Init(&vers, CSSM_PRIVILEGE_SCOPE_NONE,
124		&guid, CSSM_KEY_HIERARCHY_NONE,
125		&pvcPolicy, NULL);
126	if (ret != CSSM_OK) {
127		fprintf(stderr, "CSSM_Init failed\n");
128		abort();
129	}
130
131	ret = CSSM_ModuleLoad(&gGuidAppleCSP, CSSM_KEY_HIERARCHY_NONE, NULL, NULL);
132	if (ret) {
133		fprintf(stderr, "CSSM_ModuleLoad failed\n");
134		abort();
135	}
136
137	ret = CSSM_ModuleAttach(&gGuidAppleCSP, &vers, &cssm_memory_funcs,
138		0, CSSM_SERVICE_CSP, 0,
139		CSSM_KEY_HIERARCHY_NONE,
140		NULL, 0, NULL, &cspHandle);
141	if (ret) {
142		fprintf(stderr, "CSSM_ModuleAttach failed\n");
143		abort();
144	}
145
146	return (cspHandle);
147}
148
149
150#endif /* HAVE_CDSA */
151
152
153int
154_cs_BN_to_integer(BIGNUM *bn, heim_integer *integer)
155{
156	integer->length = BN_num_bytes(bn);
157	integer->data = malloc(integer->length);
158	if (integer->data == NULL) {
159		return (ENOMEM);
160	}
161	BN_bn2bin(bn, integer->data);
162	integer->negative = BN_is_negative(bn);
163	return (0);
164}
165
166
167BIGNUM *
168_cs_integer_to_BN(const heim_integer *i, BIGNUM *bn)
169{
170	bn = BN_bin2bn(i->data, i->length, bn);
171	if (bn) {
172		BN_set_negative(bn, i->negative);
173	}
174	return (bn);
175}
176