2 * Copyright (c) 2003-2005 Apple Computer, 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 "SecNssCoder.h"
28 #include <Security/cssmerr.h>
29 #include <security_utilities/utilities.h>
30 #include <security_asn1/secasn1.h>
35 #define THROW_ENABLE 1
37 /* disable link against Security framework when true */
38 #define THROW_ENABLE 0
42 #define THROW_ERROR Security::CssmError::throwMe(CSSMERR_CSSM_MEMORY_ERROR)
47 SecNssCoder::SecNssCoder(PRUint32 chunkSize
/* = SNC_CHUNKSIZE_DEF */)
50 mPool
= PORT_NewArena(chunkSize
);
56 SecNssCoder::~SecNssCoder()
60 * Note: we're asking for a memory zero here, but
61 * PORT_FreeArena doesn't do that (yet).
63 PORT_FreeArena(mPool
, PR_TRUE
);
68 PRErrorCode
SecNssCoder::decode(
69 const void *src
, // BER-encoded source
71 const SecAsn1Template
*templ
,
76 assert(mPool
!= NULL
);
77 prtn
= SEC_ASN1Decode(mPool
, dest
, templ
, (const char *)src
, len
);
86 PRErrorCode
SecNssCoder::encodeItem(
88 const SecAsn1Template
*templ
,
91 assert(mPool
!= NULL
);
96 SECItem
*rtnItem
= SEC_ASN1EncodeItem(mPool
, &dest
, src
, templ
);
105 void *SecNssCoder::malloc(size_t len
)
107 assert(mPool
!= NULL
);
108 void *rtn
= PORT_ArenaAlloc(mPool
, len
);
115 /* malloc item.Data, set item.Length */
116 void SecNssCoder::allocItem(
120 item
.Data
= (uint8
*)malloc(len
);
124 /* malloc and copy */
125 void SecNssCoder::allocCopyItem(
130 allocItem(dest
, len
);
131 memmove(dest
.Data
, src
, len
);
135 * This is pretty much a copy of SEC_ASN1EncodeItem, with a Allocator
136 * malloc replacing the sec_asn1e_allocate_item to alloc the output data.
138 PRErrorCode
SecNssEncodeItem(
140 const SecAsn1Template
*templ
,
141 Security::Allocator
&alloc
,
144 unsigned long encoding_length
= 0;
150 rv
= SEC_ASN1Encode (src
, templ
,
151 sec_asn1e_encode_item_count
, &encoding_length
);
152 if (rv
!= SECSuccess
) {
153 return PR_GetError();
157 dest = sec_asn1e_allocate_item (poolp, dest, encoding_length);
161 dest
.Data
= (uint8
*)alloc
.malloc(encoding_length
);
163 /* end replacement */
165 rv
= SEC_ASN1Encode (src
, templ
, sec_asn1e_encode_item_store
, &dest
);
166 if (rv
!= SECSuccess
) {
167 return PR_GetError();
170 assert(encoding_length
== dest
.Length
);
174 PRErrorCode
SecNssEncodeItemOdata(
176 const SecAsn1Template
*templ
,
177 CssmOwnedData
&odata
)
179 Allocator
&alloc
= odata
.allocator
;
183 prtn
= SecNssEncodeItem(src
, templ
, alloc
, sitem
);
187 odata
.set(sitem
.Data
, sitem
.Length
);