2 * Copyright (c) 2003-2006,2008,2010-2012 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
23 * SecNssCoder.cpp: simple C++ wrapper for PLArenaPool and the
24 * high-level ANS1 encode/decode routines.
27 #include <TargetConditionals.h>
30 #include "SecNssCoder.h"
31 #include <Security/cssmerr.h>
32 #include <security_utilities/utilities.h>
33 #include <security_asn1/secasn1.h>
35 #include <security_utilities/simulatecrash_assert.h>
38 #define THROW_ENABLE 1
40 /* disable link against Security framework when true */
41 #define THROW_ENABLE 0
45 #define THROW_ERROR Security::CssmError::throwMe(CSSMERR_CSSM_MEMORY_ERROR)
50 SecNssCoder::SecNssCoder(PRUint32 chunkSize
/* = SNC_CHUNKSIZE_DEF */)
53 mPool
= PORT_NewArena(chunkSize
);
59 SecNssCoder::~SecNssCoder()
63 * Note: we're asking for a memory zero here, but
64 * PORT_FreeArena doesn't do that (yet).
66 PORT_FreeArena(mPool
, PR_TRUE
);
71 PRErrorCode
SecNssCoder::decode(
72 const void *src
, // BER-encoded source
74 const SecAsn1Template
*templ
,
79 assert(mPool
!= NULL
);
80 prtn
= SEC_ASN1Decode(mPool
, dest
, templ
, (const char *)src
, len
);
89 PRErrorCode
SecNssCoder::encodeItem(
91 const SecAsn1Template
*templ
,
94 assert(mPool
!= NULL
);
99 SECItem
*rtnItem
= SEC_ASN1EncodeItem(mPool
, &dest
, src
, templ
);
100 if(rtnItem
== NULL
) {
101 return PR_GetError();
108 void *SecNssCoder::malloc(size_t len
)
110 assert(mPool
!= NULL
);
111 void *rtn
= PORT_ArenaAlloc(mPool
, len
);
118 /* allocate space for num copies of specified type */
119 void *SecNssCoder::malloc_T(
120 size_t unit_bytesize
,
123 if (num_units
>=SIZE_MAX
/unit_bytesize
) {
127 return malloc(unit_bytesize
* num_units
);
131 /* malloc item.Data, set item.Length */
132 void SecNssCoder::allocItem(
136 item
.Data
= (uint8
*)malloc(len
);
140 /* malloc and copy */
141 void SecNssCoder::allocCopyItem(
146 allocItem(dest
, len
);
147 memmove(dest
.Data
, src
, len
);
151 * This is pretty much a copy of SEC_ASN1EncodeItem, with a Allocator
152 * malloc replacing the sec_asn1e_allocate_item to alloc the output data.
154 PRErrorCode
SecNssEncodeItem(
156 const SecAsn1Template
*templ
,
157 Security::Allocator
&alloc
,
160 unsigned long encoding_length
= 0;
166 rv
= SEC_ASN1Encode (src
, templ
,
167 sec_asn1e_encode_item_count
, &encoding_length
);
168 if (rv
!= SECSuccess
) {
169 return PR_GetError();
173 dest = sec_asn1e_allocate_item (poolp, dest, encoding_length);
177 dest
.Data
= (uint8
*)alloc
.malloc(encoding_length
);
179 /* end replacement */
181 rv
= SEC_ASN1Encode (src
, templ
, sec_asn1e_encode_item_store
, &dest
);
182 if (rv
!= SECSuccess
) {
183 return PR_GetError();
186 assert(encoding_length
== dest
.Length
);
190 PRErrorCode
SecNssEncodeItemOdata(
192 const SecAsn1Template
*templ
,
193 CssmOwnedData
&odata
)
195 Allocator
&alloc
= odata
.allocator
;
199 prtn
= SecNssEncodeItem(src
, templ
, alloc
, sitem
);
203 odata
.set(sitem
.Data
, sitem
.Length
);
207 #endif /* TARGET_OS_MAC */