2 * Copyright (c) 2000-2001,2011,2014 Apple Inc. All Rights Reserved.
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
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.
20 // csputilities - utility classes for CSP implementation
22 #include <security_cdsa_plugin/CSPsession.h>
23 #include <security_cdsa_plugin/cssmplugin.h>
24 #include <security_utilities/memutils.h>
25 #include <security_cdsa_utilities/cssmdates.h>
27 using LowLevelMemoryUtilities::increment
;
33 CSPFullPluginSession::Writer::Writer(CssmData
*v
, uint32 n
, CssmData
*rem
)
34 : vec(v
), firstVec(v
), lastVec(v
+ n
- 1), remData(rem
)
36 if (vec
== NULL
|| n
== 0)
37 CssmError::throwMe(CSSMERR_CSP_INVALID_OUTPUT_VECTOR
); // CDSA p.253, amended
42 void CSPFullPluginSession::Writer::allocate(size_t needed
, Allocator
&alloc
)
45 return; // No output buffer space needed so we're done.
46 else if (vec
== firstVec
&& !*vec
) { // initial null vector element, wants allocation there
47 *vec
= makeBuffer(needed
, alloc
);
48 lastVec
= vec
; // ignore all subsequent buffers in vector
51 // how much output space do we have left?
52 size_t size
= currentSize
;
53 for (CssmData
*v
= vec
+ 1; v
<= lastVec
; v
++)
58 if (!*remData
) { // have overflow, can allocate
59 *remData
= makeBuffer(needed
- size
, alloc
);
62 if (size
+ remData
->length() >= needed
)
63 return; // will fit into overflow
65 // not enough buffer space, and can't allocate
66 CssmError::throwMe(CSSMERR_CSP_OUTPUT_LENGTH_ERROR
);
70 void CSPFullPluginSession::Writer::nextBlock(void * &ptr
, size_t &size
)
76 void CSPFullPluginSession::Writer::use(size_t used
)
78 assert(used
<= currentSize
);
80 if (used
< currentSize
) {
81 currentBuffer
= increment(currentBuffer
, used
);
85 useData(vec
++); // use next vector buffer
86 } else if (vec
== lastVec
&& remData
) {
87 useData(remData
); // use remainder buffer
89 #if !defined(NDEBUG) && 0
90 } else if (vec
== lastVec
) {
92 } else if (vec
> lastVec
) {
93 assert(false); // 2nd try to overflow end
96 currentBuffer
= NULL
; // no more output buffer
102 void CSPFullPluginSession::Writer::put(void *addr
, size_t size
)
108 sz
= size
; // cap transfer
111 addr
= increment(addr
, sz
);
116 size_t CSPFullPluginSession::Writer::close()
123 // Common algorithm utilities
125 void CSPFullPluginSession::setKey(CssmKey
&key
,
126 const Context
&context
, CSSM_KEYCLASS keyClass
,
127 CSSM_KEYATTR_FLAGS attrs
, CSSM_KEYUSE use
)
130 memset(&key
.KeyHeader
, 0, sizeof(key
.KeyHeader
));
131 key
.KeyHeader
.HeaderVersion
= CSSM_KEYHEADER_VERSION
;
132 key
.KeyHeader
.CspId
= plugin
.myGuid();
133 key
.KeyHeader
.AlgorithmId
= context
.algorithm();
134 key
.KeyHeader
.KeyClass
= keyClass
;
135 key
.KeyHeader
.KeyUsage
= use
;
136 key
.KeyHeader
.KeyAttr
= attrs
;
138 CssmDate
*theDate
= context
.get
<CssmDate
>(CSSM_ATTRIBUTE_START_DATE
);
140 key
.KeyHeader
.StartDate
= *theDate
;
142 theDate
= context
.get
<CssmDate
>(CSSM_ATTRIBUTE_END_DATE
);
144 key
.KeyHeader
.EndDate
= *theDate
;
147 // defaults (change as needed)
148 key
.KeyHeader
.WrapAlgorithmId
= CSSM_ALGID_NONE
;
150 // clear key data (standard says, "Always allocate this, ignore prior contents.")