1f9fbec18Smcpowers /* ***** BEGIN LICENSE BLOCK *****
2f9fbec18Smcpowers * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3f9fbec18Smcpowers *
4f9fbec18Smcpowers * The contents of this file are subject to the Mozilla Public License Version
5f9fbec18Smcpowers * 1.1 (the "License"); you may not use this file except in compliance with
6f9fbec18Smcpowers * the License. You may obtain a copy of the License at
7f9fbec18Smcpowers * http://www.mozilla.org/MPL/
8f9fbec18Smcpowers *
9f9fbec18Smcpowers * Software distributed under the License is distributed on an "AS IS" basis,
10f9fbec18Smcpowers * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11f9fbec18Smcpowers * for the specific language governing rights and limitations under the
12f9fbec18Smcpowers * License.
13f9fbec18Smcpowers *
14f9fbec18Smcpowers * The Original Code is the Netscape security libraries.
15f9fbec18Smcpowers *
16f9fbec18Smcpowers * The Initial Developer of the Original Code is
17f9fbec18Smcpowers * Netscape Communications Corporation.
18f9fbec18Smcpowers * Portions created by the Initial Developer are Copyright (C) 1994-2000
19f9fbec18Smcpowers * the Initial Developer. All Rights Reserved.
20f9fbec18Smcpowers *
21f9fbec18Smcpowers * Contributor(s):
22f9fbec18Smcpowers *
23f9fbec18Smcpowers * Alternatively, the contents of this file may be used under the terms of
24f9fbec18Smcpowers * either the GNU General Public License Version 2 or later (the "GPL"), or
25f9fbec18Smcpowers * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26f9fbec18Smcpowers * in which case the provisions of the GPL or the LGPL are applicable instead
27f9fbec18Smcpowers * of those above. If you wish to allow use of your version of this file only
28f9fbec18Smcpowers * under the terms of either the GPL or the LGPL, and not to allow others to
29f9fbec18Smcpowers * use your version of this file under the terms of the MPL, indicate your
30f9fbec18Smcpowers * decision by deleting the provisions above and replace them with the notice
31f9fbec18Smcpowers * and other provisions required by the GPL or the LGPL. If you do not delete
32f9fbec18Smcpowers * the provisions above, a recipient may use your version of this file under
33f9fbec18Smcpowers * the terms of any one of the MPL, the GPL or the LGPL.
34f9fbec18Smcpowers *
35f9fbec18Smcpowers * ***** END LICENSE BLOCK ***** */
36f9fbec18Smcpowers /*
37f9fbec18Smcpowers * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
38f9fbec18Smcpowers * Use is subject to license terms.
39f9fbec18Smcpowers *
40f9fbec18Smcpowers * Sun elects to use this software under the MPL license.
41f9fbec18Smcpowers */
42f9fbec18Smcpowers
43f9fbec18Smcpowers /*
44f9fbec18Smcpowers * Support routines for SECItem data structure.
45f9fbec18Smcpowers *
46f9fbec18Smcpowers * $Id: secitem.c,v 1.14 2006/05/22 22:24:34 wtchang%redhat.com Exp $
47f9fbec18Smcpowers */
48f9fbec18Smcpowers
49f9fbec18Smcpowers #include <sys/types.h>
50f9fbec18Smcpowers #include <sys/systm.h>
51f9fbec18Smcpowers #include <sys/param.h>
52f9fbec18Smcpowers #ifdef _KERNEL
53f9fbec18Smcpowers #include <sys/kmem.h>
54f9fbec18Smcpowers #else
55f9fbec18Smcpowers #include <string.h>
56f9fbec18Smcpowers #include <strings.h>
57f9fbec18Smcpowers #include <assert.h>
58f9fbec18Smcpowers #endif
59f9fbec18Smcpowers #include "ec.h"
60f9fbec18Smcpowers #include "ecl-curve.h"
61f9fbec18Smcpowers #include "ecc_impl.h"
62f9fbec18Smcpowers
63f9fbec18Smcpowers void SECITEM_FreeItem(SECItem *, PRBool);
64f9fbec18Smcpowers
65f9fbec18Smcpowers SECItem *
SECITEM_AllocItem(PRArenaPool * arena,SECItem * item,unsigned int len,int kmflag)66f9fbec18Smcpowers SECITEM_AllocItem(PRArenaPool *arena, SECItem *item, unsigned int len,
67f9fbec18Smcpowers int kmflag)
68f9fbec18Smcpowers {
69f9fbec18Smcpowers SECItem *result = NULL;
70f9fbec18Smcpowers void *mark = NULL;
71f9fbec18Smcpowers
72f9fbec18Smcpowers if (arena != NULL) {
73f9fbec18Smcpowers mark = PORT_ArenaMark(arena);
74f9fbec18Smcpowers }
75f9fbec18Smcpowers
76f9fbec18Smcpowers if (item == NULL) {
77f9fbec18Smcpowers if (arena != NULL) {
78f9fbec18Smcpowers result = PORT_ArenaZAlloc(arena, sizeof(SECItem), kmflag);
79f9fbec18Smcpowers } else {
80f9fbec18Smcpowers result = PORT_ZAlloc(sizeof(SECItem), kmflag);
81f9fbec18Smcpowers }
82f9fbec18Smcpowers if (result == NULL) {
83f9fbec18Smcpowers goto loser;
84f9fbec18Smcpowers }
85f9fbec18Smcpowers } else {
86f9fbec18Smcpowers PORT_Assert(item->data == NULL);
87f9fbec18Smcpowers result = item;
88f9fbec18Smcpowers }
89f9fbec18Smcpowers
90f9fbec18Smcpowers result->len = len;
91f9fbec18Smcpowers if (len) {
92f9fbec18Smcpowers if (arena != NULL) {
93f9fbec18Smcpowers result->data = PORT_ArenaAlloc(arena, len, kmflag);
94f9fbec18Smcpowers } else {
95f9fbec18Smcpowers result->data = PORT_Alloc(len, kmflag);
96f9fbec18Smcpowers }
97f9fbec18Smcpowers if (result->data == NULL) {
98f9fbec18Smcpowers goto loser;
99f9fbec18Smcpowers }
100f9fbec18Smcpowers } else {
101f9fbec18Smcpowers result->data = NULL;
102f9fbec18Smcpowers }
103f9fbec18Smcpowers
104f9fbec18Smcpowers if (mark) {
105f9fbec18Smcpowers PORT_ArenaUnmark(arena, mark);
106f9fbec18Smcpowers }
107f9fbec18Smcpowers return(result);
108f9fbec18Smcpowers
109f9fbec18Smcpowers loser:
110f9fbec18Smcpowers if ( arena != NULL ) {
111f9fbec18Smcpowers if (mark) {
112f9fbec18Smcpowers PORT_ArenaRelease(arena, mark);
113f9fbec18Smcpowers }
114f9fbec18Smcpowers if (item != NULL) {
115f9fbec18Smcpowers item->data = NULL;
116f9fbec18Smcpowers item->len = 0;
117f9fbec18Smcpowers }
118f9fbec18Smcpowers } else {
119f9fbec18Smcpowers if (result != NULL) {
120f9fbec18Smcpowers SECITEM_FreeItem(result, (item == NULL) ? PR_TRUE : PR_FALSE);
121f9fbec18Smcpowers }
122f9fbec18Smcpowers /*
123f9fbec18Smcpowers * If item is not NULL, the above has set item->data and
124f9fbec18Smcpowers * item->len to 0.
125f9fbec18Smcpowers */
126f9fbec18Smcpowers }
127f9fbec18Smcpowers return(NULL);
128f9fbec18Smcpowers }
129f9fbec18Smcpowers
130f9fbec18Smcpowers SECStatus
SECITEM_CopyItem(PRArenaPool * arena,SECItem * to,const SECItem * from,int kmflag)131f9fbec18Smcpowers SECITEM_CopyItem(PRArenaPool *arena, SECItem *to, const SECItem *from,
132f9fbec18Smcpowers int kmflag)
133f9fbec18Smcpowers {
134f9fbec18Smcpowers to->type = from->type;
135f9fbec18Smcpowers if (from->data && from->len) {
136f9fbec18Smcpowers if ( arena ) {
137f9fbec18Smcpowers to->data = (unsigned char*) PORT_ArenaAlloc(arena, from->len,
138f9fbec18Smcpowers kmflag);
139f9fbec18Smcpowers } else {
140f9fbec18Smcpowers to->data = (unsigned char*) PORT_Alloc(from->len, kmflag);
141f9fbec18Smcpowers }
142*c40a6cd7SToomas Soome
143f9fbec18Smcpowers if (!to->data) {
144f9fbec18Smcpowers return SECFailure;
145f9fbec18Smcpowers }
146f9fbec18Smcpowers PORT_Memcpy(to->data, from->data, from->len);
147f9fbec18Smcpowers to->len = from->len;
148f9fbec18Smcpowers } else {
149f9fbec18Smcpowers to->data = 0;
150f9fbec18Smcpowers to->len = 0;
151f9fbec18Smcpowers }
152f9fbec18Smcpowers return SECSuccess;
153f9fbec18Smcpowers }
154f9fbec18Smcpowers
155f9fbec18Smcpowers void
SECITEM_FreeItem(SECItem * zap,PRBool freeit)156f9fbec18Smcpowers SECITEM_FreeItem(SECItem *zap, PRBool freeit)
157f9fbec18Smcpowers {
158f9fbec18Smcpowers if (zap) {
159f9fbec18Smcpowers #ifdef _KERNEL
160f9fbec18Smcpowers kmem_free(zap->data, zap->len);
161f9fbec18Smcpowers #else
162f9fbec18Smcpowers free(zap->data);
163f9fbec18Smcpowers #endif
164f9fbec18Smcpowers zap->data = 0;
165f9fbec18Smcpowers zap->len = 0;
166f9fbec18Smcpowers if (freeit) {
167f9fbec18Smcpowers #ifdef _KERNEL
168f9fbec18Smcpowers kmem_free(zap, sizeof (SECItem));
169f9fbec18Smcpowers #else
170f9fbec18Smcpowers free(zap);
171f9fbec18Smcpowers #endif
172f9fbec18Smcpowers }
173f9fbec18Smcpowers }
174f9fbec18Smcpowers }
175