security/nss/lib/util/dersubr.c

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 #include "secder.h"
michael@0 6 #include <limits.h>
michael@0 7 #include "secerr.h"
michael@0 8
michael@0 9 int
michael@0 10 DER_LengthLength(PRUint32 len)
michael@0 11 {
michael@0 12 if (len > 127) {
michael@0 13 if (len > 255) {
michael@0 14 if (len > 65535L) {
michael@0 15 if (len > 16777215L) {
michael@0 16 return 5;
michael@0 17 } else {
michael@0 18 return 4;
michael@0 19 }
michael@0 20 } else {
michael@0 21 return 3;
michael@0 22 }
michael@0 23 } else {
michael@0 24 return 2;
michael@0 25 }
michael@0 26 } else {
michael@0 27 return 1;
michael@0 28 }
michael@0 29 }
michael@0 30
michael@0 31 unsigned char *
michael@0 32 DER_StoreHeader(unsigned char *buf, unsigned int code, PRUint32 len)
michael@0 33 {
michael@0 34 unsigned char b[4];
michael@0 35
michael@0 36 b[0] = (unsigned char)(len >> 24);
michael@0 37 b[1] = (unsigned char)(len >> 16);
michael@0 38 b[2] = (unsigned char)(len >> 8);
michael@0 39 b[3] = (unsigned char)len;
michael@0 40 if ((code & DER_TAGNUM_MASK) == DER_SET
michael@0 41 || (code & DER_TAGNUM_MASK) == DER_SEQUENCE)
michael@0 42 code |= DER_CONSTRUCTED;
michael@0 43 *buf++ = code;
michael@0 44 if (len > 127) {
michael@0 45 if (len > 255) {
michael@0 46 if (len > 65535) {
michael@0 47 if (len > 16777215) {
michael@0 48 *buf++ = 0x84;
michael@0 49 *buf++ = b[0];
michael@0 50 *buf++ = b[1];
michael@0 51 *buf++ = b[2];
michael@0 52 *buf++ = b[3];
michael@0 53 } else {
michael@0 54 *buf++ = 0x83;
michael@0 55 *buf++ = b[1];
michael@0 56 *buf++ = b[2];
michael@0 57 *buf++ = b[3];
michael@0 58 }
michael@0 59 } else {
michael@0 60 *buf++ = 0x82;
michael@0 61 *buf++ = b[2];
michael@0 62 *buf++ = b[3];
michael@0 63 }
michael@0 64 } else {
michael@0 65 *buf++ = 0x81;
michael@0 66 *buf++ = b[3];
michael@0 67 }
michael@0 68 } else {
michael@0 69 *buf++ = b[3];
michael@0 70 }
michael@0 71 return buf;
michael@0 72 }
michael@0 73
michael@0 74 /*
michael@0 75 * XXX This should be rewritten, generalized, to take a long instead
michael@0 76 * of a PRInt32.
michael@0 77 */
michael@0 78 SECStatus
michael@0 79 DER_SetInteger(PLArenaPool *arena, SECItem *it, PRInt32 i)
michael@0 80 {
michael@0 81 unsigned char bb[4];
michael@0 82 unsigned len;
michael@0 83
michael@0 84 bb[0] = (unsigned char) (i >> 24);
michael@0 85 bb[1] = (unsigned char) (i >> 16);
michael@0 86 bb[2] = (unsigned char) (i >> 8);
michael@0 87 bb[3] = (unsigned char) (i);
michael@0 88
michael@0 89 /*
michael@0 90 ** Small integers are encoded in a single byte. Larger integers
michael@0 91 ** require progressively more space.
michael@0 92 */
michael@0 93 if (i < -128) {
michael@0 94 if (i < -32768L) {
michael@0 95 if (i < -8388608L) {
michael@0 96 len = 4;
michael@0 97 } else {
michael@0 98 len = 3;
michael@0 99 }
michael@0 100 } else {
michael@0 101 len = 2;
michael@0 102 }
michael@0 103 } else if (i > 127) {
michael@0 104 if (i > 32767L) {
michael@0 105 if (i > 8388607L) {
michael@0 106 len = 4;
michael@0 107 } else {
michael@0 108 len = 3;
michael@0 109 }
michael@0 110 } else {
michael@0 111 len = 2;
michael@0 112 }
michael@0 113 } else {
michael@0 114 len = 1;
michael@0 115 }
michael@0 116 it->data = (unsigned char*) PORT_ArenaAlloc(arena, len);
michael@0 117 if (!it->data) {
michael@0 118 return SECFailure;
michael@0 119 }
michael@0 120 it->len = len;
michael@0 121 PORT_Memcpy(it->data, bb + (4 - len), len);
michael@0 122 return SECSuccess;
michael@0 123 }
michael@0 124
michael@0 125 /*
michael@0 126 * XXX This should be rewritten, generalized, to take an unsigned long instead
michael@0 127 * of a PRUint32.
michael@0 128 */
michael@0 129 SECStatus
michael@0 130 DER_SetUInteger(PLArenaPool *arena, SECItem *it, PRUint32 ui)
michael@0 131 {
michael@0 132 unsigned char bb[5];
michael@0 133 int len;
michael@0 134
michael@0 135 bb[0] = 0;
michael@0 136 bb[1] = (unsigned char) (ui >> 24);
michael@0 137 bb[2] = (unsigned char) (ui >> 16);
michael@0 138 bb[3] = (unsigned char) (ui >> 8);
michael@0 139 bb[4] = (unsigned char) (ui);
michael@0 140
michael@0 141 /*
michael@0 142 ** Small integers are encoded in a single byte. Larger integers
michael@0 143 ** require progressively more space.
michael@0 144 */
michael@0 145 if (ui > 0x7f) {
michael@0 146 if (ui > 0x7fff) {
michael@0 147 if (ui > 0x7fffffL) {
michael@0 148 if (ui >= 0x80000000L) {
michael@0 149 len = 5;
michael@0 150 } else {
michael@0 151 len = 4;
michael@0 152 }
michael@0 153 } else {
michael@0 154 len = 3;
michael@0 155 }
michael@0 156 } else {
michael@0 157 len = 2;
michael@0 158 }
michael@0 159 } else {
michael@0 160 len = 1;
michael@0 161 }
michael@0 162
michael@0 163 it->data = (unsigned char *)PORT_ArenaAlloc(arena, len);
michael@0 164 if (it->data == NULL) {
michael@0 165 return SECFailure;
michael@0 166 }
michael@0 167
michael@0 168 it->len = len;
michael@0 169 PORT_Memcpy(it->data, bb + (sizeof(bb) - len), len);
michael@0 170
michael@0 171 return SECSuccess;
michael@0 172 }
michael@0 173
michael@0 174 /*
michael@0 175 ** Convert a der encoded *signed* integer into a machine integral value.
michael@0 176 ** If an underflow/overflow occurs, sets error code and returns min/max.
michael@0 177 */
michael@0 178 long
michael@0 179 DER_GetInteger(const SECItem *it)
michael@0 180 {
michael@0 181 long ival = 0;
michael@0 182 unsigned len = it->len;
michael@0 183 unsigned char *cp = it->data;
michael@0 184 unsigned long overflow = 0x1ffUL << (((sizeof(ival) - 1) * 8) - 1);
michael@0 185 unsigned long ofloinit;
michael@0 186
michael@0 187 PORT_Assert(len);
michael@0 188 if (!len) {
michael@0 189 PORT_SetError(SEC_ERROR_INPUT_LEN);
michael@0 190 return 0;
michael@0 191 }
michael@0 192
michael@0 193 if (*cp & 0x80)
michael@0 194 ival = -1L;
michael@0 195 ofloinit = ival & overflow;
michael@0 196
michael@0 197 while (len) {
michael@0 198 if ((ival & overflow) != ofloinit) {
michael@0 199 PORT_SetError(SEC_ERROR_BAD_DER);
michael@0 200 if (ival < 0) {
michael@0 201 return LONG_MIN;
michael@0 202 }
michael@0 203 return LONG_MAX;
michael@0 204 }
michael@0 205 ival = ival << 8;
michael@0 206 ival |= *cp++;
michael@0 207 --len;
michael@0 208 }
michael@0 209 return ival;
michael@0 210 }
michael@0 211
michael@0 212 /*
michael@0 213 ** Convert a der encoded *unsigned* integer into a machine integral value.
michael@0 214 ** If an overflow occurs, sets error code and returns max.
michael@0 215 */
michael@0 216 unsigned long
michael@0 217 DER_GetUInteger(SECItem *it)
michael@0 218 {
michael@0 219 unsigned long ival = 0;
michael@0 220 unsigned len = it->len;
michael@0 221 unsigned char *cp = it->data;
michael@0 222 unsigned long overflow = 0xffUL << ((sizeof(ival) - 1) * 8);
michael@0 223
michael@0 224 PORT_Assert(len);
michael@0 225 if (!len) {
michael@0 226 PORT_SetError(SEC_ERROR_INPUT_LEN);
michael@0 227 return 0;
michael@0 228 }
michael@0 229
michael@0 230 /* Cannot put a negative value into an unsigned container. */
michael@0 231 if (*cp & 0x80) {
michael@0 232 PORT_SetError(SEC_ERROR_BAD_DER);
michael@0 233 return 0;
michael@0 234 }
michael@0 235
michael@0 236 while (len) {
michael@0 237 if (ival & overflow) {
michael@0 238 PORT_SetError(SEC_ERROR_BAD_DER);
michael@0 239 return ULONG_MAX;
michael@0 240 }
michael@0 241 ival = ival << 8;
michael@0 242 ival |= *cp++;
michael@0 243 --len;
michael@0 244 }
michael@0 245 return ival;
michael@0 246 }

mercurial