1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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
14 * The Original Code is the Netscape security libraries.
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.
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.
35 * ***** END LICENSE BLOCK ***** */
42 DER_LengthLength(PRUint32 len)
47 if (len > 16777215L) {
64 DER_StoreHeader(unsigned char *buf, unsigned int code, PRUint32 len)
68 b[0] = (unsigned char)(len >> 24);
69 b[1] = (unsigned char)(len >> 16);
70 b[2] = (unsigned char)(len >> 8);
71 b[3] = (unsigned char)len;
72 if ((code & DER_TAGNUM_MASK) == DER_SET
73 || (code & DER_TAGNUM_MASK) == DER_SEQUENCE)
74 code |= DER_CONSTRUCTED;
107 * XXX This should be rewritten, generalized, to take a long instead
111 DER_SetInteger(PRArenaPool *arena, SECItem *it, PRInt32 i)
116 bb[0] = (unsigned char) (i >> 24);
117 bb[1] = (unsigned char) (i >> 16);
118 bb[2] = (unsigned char) (i >> 8);
119 bb[3] = (unsigned char) (i);
122 ** Small integers are encoded in a single byte. Larger integers
123 ** require progressively more space.
135 } else if (i > 127) {
148 it->data = (unsigned char*) PORT_ArenaAlloc(arena, len);
153 PORT_Memcpy(it->data, bb + (4 - len), len);
158 * XXX This should be rewritten, generalized, to take an unsigned long instead
162 DER_SetUInteger(PRArenaPool *arena, SECItem *it, PRUint32 ui)
168 bb[1] = (unsigned char) (ui >> 24);
169 bb[2] = (unsigned char) (ui >> 16);
170 bb[3] = (unsigned char) (ui >> 8);
171 bb[4] = (unsigned char) (ui);
174 ** Small integers are encoded in a single byte. Larger integers
175 ** require progressively more space.
179 if (ui > 0x7fffffL) {
180 if (ui >= 0x80000000L) {
195 it->data = (unsigned char *)PORT_ArenaAlloc(arena, len);
196 if (it->data == NULL) {
201 PORT_Memcpy(it->data, bb + (sizeof(bb) - len), len);
207 ** Convert a der encoded *signed* integer into a machine integral value.
208 ** If an underflow/overflow occurs, sets error code and returns min/max.
211 DER_GetInteger(SECItem *it)
214 unsigned len = it->len;
215 unsigned char *cp = it->data;
216 unsigned long overflow = 0x1ffUL << (((sizeof(ival) - 1) * 8) - 1);
217 unsigned long ofloinit;
221 ofloinit = ival & overflow;
224 if ((ival & overflow) != ofloinit) {
225 PORT_SetError(SEC_ERROR_BAD_DER);
239 ** Convert a der encoded *unsigned* integer into a machine integral value.
240 ** If an underflow/overflow occurs, sets error code and returns min/max.
243 DER_GetUInteger(SECItem *it)
245 unsigned long ival = 0;
246 unsigned len = it->len;
247 unsigned char *cp = it->data;
248 unsigned long overflow = 0xffUL << ((sizeof(ival) - 1) * 8);
250 /* Cannot put a negative value into an unsigned container. */
252 PORT_SetError(SEC_ERROR_BAD_DER);
257 if (ival & overflow) {
258 PORT_SetError(SEC_ERROR_BAD_DER);