| OLD | NEW |
| 1 /* This Source Code Form is subject to the terms of the Mozilla Public | 1 /* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 * License, v. 2.0. If a copy of the MPL was not distributed with this | 2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | 4 |
| 5 #include "secder.h" | 5 #include "secder.h" |
| 6 #include <limits.h> | 6 #include <limits.h> |
| 7 #include "secerr.h" | 7 #include "secerr.h" |
| 8 | 8 |
| 9 int | 9 int |
| 10 DER_LengthLength(PRUint32 len) | 10 DER_LengthLength(PRUint32 len) |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 177 */ | 177 */ |
| 178 long | 178 long |
| 179 DER_GetInteger(SECItem *it) | 179 DER_GetInteger(SECItem *it) |
| 180 { | 180 { |
| 181 long ival = 0; | 181 long ival = 0; |
| 182 unsigned len = it->len; | 182 unsigned len = it->len; |
| 183 unsigned char *cp = it->data; | 183 unsigned char *cp = it->data; |
| 184 unsigned long overflow = 0x1ffUL << (((sizeof(ival) - 1) * 8) - 1); | 184 unsigned long overflow = 0x1ffUL << (((sizeof(ival) - 1) * 8) - 1); |
| 185 unsigned long ofloinit; | 185 unsigned long ofloinit; |
| 186 | 186 |
| 187 PORT_Assert(len); |
| 188 if (!len) { |
| 189 PORT_SetError(SEC_ERROR_INPUT_LEN); |
| 190 return 0; |
| 191 } |
| 192 |
| 187 if (*cp & 0x80) | 193 if (*cp & 0x80) |
| 188 ival = -1L; | 194 ival = -1L; |
| 189 ofloinit = ival & overflow; | 195 ofloinit = ival & overflow; |
| 190 | 196 |
| 191 while (len) { | 197 while (len) { |
| 192 if ((ival & overflow) != ofloinit) { | 198 if ((ival & overflow) != ofloinit) { |
| 193 PORT_SetError(SEC_ERROR_BAD_DER); | 199 PORT_SetError(SEC_ERROR_BAD_DER); |
| 194 if (ival < 0) { | 200 if (ival < 0) { |
| 195 return LONG_MIN; | 201 return LONG_MIN; |
| 196 } | 202 } |
| 197 return LONG_MAX; | 203 return LONG_MAX; |
| 198 } | 204 } |
| 199 ival = ival << 8; | 205 ival = ival << 8; |
| 200 ival |= *cp++; | 206 ival |= *cp++; |
| 201 --len; | 207 --len; |
| 202 } | 208 } |
| 203 return ival; | 209 return ival; |
| 204 } | 210 } |
| 205 | 211 |
| 206 /* | 212 /* |
| 207 ** Convert a der encoded *unsigned* integer into a machine integral value. | 213 ** Convert a der encoded *unsigned* integer into a machine integral value. |
| 208 ** If an underflow/overflow occurs, sets error code and returns min/max. | 214 ** If an overflow occurs, sets error code and returns max. |
| 209 */ | 215 */ |
| 210 unsigned long | 216 unsigned long |
| 211 DER_GetUInteger(SECItem *it) | 217 DER_GetUInteger(SECItem *it) |
| 212 { | 218 { |
| 213 unsigned long ival = 0; | 219 unsigned long ival = 0; |
| 214 unsigned len = it->len; | 220 unsigned len = it->len; |
| 215 unsigned char *cp = it->data; | 221 unsigned char *cp = it->data; |
| 216 unsigned long overflow = 0xffUL << ((sizeof(ival) - 1) * 8); | 222 unsigned long overflow = 0xffUL << ((sizeof(ival) - 1) * 8); |
| 217 | 223 |
| 224 PORT_Assert(len); |
| 225 if (!len) { |
| 226 PORT_SetError(SEC_ERROR_INPUT_LEN); |
| 227 return 0; |
| 228 } |
| 229 |
| 218 /* Cannot put a negative value into an unsigned container. */ | 230 /* Cannot put a negative value into an unsigned container. */ |
| 219 if (*cp & 0x80) { | 231 if (*cp & 0x80) { |
| 220 PORT_SetError(SEC_ERROR_BAD_DER); | 232 PORT_SetError(SEC_ERROR_BAD_DER); |
| 221 return 0; | 233 return 0; |
| 222 } | 234 } |
| 223 | 235 |
| 224 while (len) { | 236 while (len) { |
| 225 if (ival & overflow) { | 237 if (ival & overflow) { |
| 226 PORT_SetError(SEC_ERROR_BAD_DER); | 238 PORT_SetError(SEC_ERROR_BAD_DER); |
| 227 return ULONG_MAX; | 239 return ULONG_MAX; |
| 228 } | 240 } |
| 229 ival = ival << 8; | 241 ival = ival << 8; |
| 230 ival |= *cp++; | 242 ival |= *cp++; |
| 231 --len; | 243 --len; |
| 232 } | 244 } |
| 233 return ival; | 245 return ival; |
| 234 } | 246 } |
| OLD | NEW |