/security/nss/lib/util/dersubr.c

http://github.com/zpao/v8monkey · C · 266 lines · 187 code · 19 blank · 60 comment · 51 complexity · f1ebf2325621ef43f7906414b0baac1c MD5 · raw file

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