]> git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_pluginlib/csputilities.cpp
Security-54.tar.gz
[apple/security.git] / cdsa / cdsa_pluginlib / csputilities.cpp
1 /*
2 * Copyright (c) 2000-2001 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 // csputilities - utility classes for CSP implementation
21 //
22 #include <Security/CSPsession.h>
23 #include <Security/cssmplugin.h>
24 #include <Security/memutils.h>
25 #include <stdio.h> //@@@ debug
26
27 using LowLevelMemoryUtilities::increment;
28
29
30 //
31 // Writer objects
32 //
33 CSPFullPluginSession::Writer::Writer(CssmData *v, uint32 n, CssmData *rem)
34 : vec(v), firstVec(v), lastVec(v + n - 1), remData(rem)
35 {
36 if (vec == NULL || n == 0)
37 CssmError::throwMe(CSSMERR_CSP_INVALID_OUTPUT_VECTOR); // CDSA p.253, amended
38 useData(vec);
39 written = 0;
40 }
41
42 void CSPFullPluginSession::Writer::allocate(size_t needed, CssmAllocator &alloc)
43 {
44 if (vec == firstVec && !*vec) { // initial null vector element, wants allocation there
45 *vec = makeBuffer(needed, alloc);
46 lastVec = vec; // ignore all subsequent buffers in vector
47 useData(vec);
48 } else {
49 // how much output space do we have left?
50 size_t size = currentSize;
51 for (CssmData *v = vec + 1; v <= lastVec; v++)
52 size += v->length();
53 if (size >= needed)
54 return; // we're fine
55 if (remData) {
56 if (!*remData) { // have overflow, can allocate
57 *remData = makeBuffer(needed - size, alloc);
58 return; // got it
59 }
60 if (size + remData->length() >= needed)
61 return; // will fit into overflow
62 }
63 // not enough buffer space, and can't allocate
64 CssmError::throwMe(CSSMERR_CSP_OUTPUT_LENGTH_ERROR);
65 }
66 }
67
68 void CSPFullPluginSession::Writer::nextBlock(void * &ptr, size_t &size)
69 {
70 ptr = currentBuffer;
71 size = currentSize;
72 }
73
74 void CSPFullPluginSession::Writer::use(size_t used)
75 {
76 assert(used <= currentSize);
77 written += used;
78 if (used < currentSize) {
79 currentBuffer = increment(currentBuffer, used);
80 currentSize -= used;
81 } else {
82 if (vec < lastVec) {
83 useData(vec++); // use next vector buffer
84 } else if (vec == lastVec && remData) {
85 useData(remData); // use remainder buffer
86 vec++; // mark used
87 #if !defined(NDEBUG) && 0
88 } else if (vec == lastVec) {
89 vec++;
90 } else if (vec > lastVec) {
91 assert(false); // 2nd try to overflow end
92 #endif !NDEBUG
93 } else {
94 currentBuffer = NULL; // no more output buffer
95 currentSize = 0;
96 }
97 }
98 }
99
100 void CSPFullPluginSession::Writer::put(void *addr, size_t size)
101 {
102 while (size > 0) {
103 void *p; size_t sz;
104 nextBlock(p, sz);
105 if (size < sz)
106 sz = size; // cap transfer
107 memcpy(p, addr, sz);
108 use(sz);
109 addr = increment(addr, sz);
110 size -= sz;
111 }
112 }
113
114 size_t CSPFullPluginSession::Writer::close()
115 {
116 return written;
117 }
118
119
120 //
121 // Common algorithm utilities
122 //
123 void CSPFullPluginSession::setKey(CssmKey &key,
124 const Context &context, CSSM_KEYCLASS keyClass,
125 CSSM_KEYATTR_FLAGS attrs, CSSM_KEYUSE use)
126 {
127 // general setup
128 memset(&key.KeyHeader, 0, sizeof(key.KeyHeader));
129 key.KeyHeader.HeaderVersion = CSSM_KEYHEADER_VERSION;
130 key.KeyHeader.CspId = plugin.myGuid();
131 key.KeyHeader.AlgorithmId = context.algorithm();
132 key.KeyHeader.KeyClass = keyClass;
133 key.KeyHeader.KeyUsage = use;
134 key.KeyHeader.KeyAttr = attrs;
135
136 // defaults (change as needed)
137 key.KeyHeader.WrapAlgorithmId = CSSM_ALGID_NONE;
138
139 // clear key data (standard says, "Always allocate this, ignore prior contents.")
140 key = CssmData();
141 }