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