您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

455 行
12 KiB

  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.] */
  56. #include <openssl/asn1.h>
  57. #include <openssl/err.h>
  58. #include <openssl/mem.h>
  59. ASN1_INTEGER *ASN1_INTEGER_dup(const ASN1_INTEGER *x)
  60. { return M_ASN1_INTEGER_dup(x);}
  61. int ASN1_INTEGER_cmp(const ASN1_INTEGER *x, const ASN1_INTEGER *y)
  62. {
  63. int neg, ret;
  64. /* Compare signs */
  65. neg = x->type & V_ASN1_NEG;
  66. if (neg != (y->type & V_ASN1_NEG))
  67. {
  68. if (neg)
  69. return -1;
  70. else
  71. return 1;
  72. }
  73. ret = ASN1_STRING_cmp(x, y);
  74. if (neg)
  75. return -ret;
  76. else
  77. return ret;
  78. }
  79. /*
  80. * This converts an ASN1 INTEGER into its content encoding.
  81. * The internal representation is an ASN1_STRING whose data is a big endian
  82. * representation of the value, ignoring the sign. The sign is determined by
  83. * the type: V_ASN1_INTEGER for positive and V_ASN1_NEG_INTEGER for negative.
  84. *
  85. * Positive integers are no problem: they are almost the same as the DER
  86. * encoding, except if the first byte is >= 0x80 we need to add a zero pad.
  87. *
  88. * Negative integers are a bit trickier...
  89. * The DER representation of negative integers is in 2s complement form.
  90. * The internal form is converted by complementing each octet and finally
  91. * adding one to the result. This can be done less messily with a little trick.
  92. * If the internal form has trailing zeroes then they will become FF by the
  93. * complement and 0 by the add one (due to carry) so just copy as many trailing
  94. * zeros to the destination as there are in the source. The carry will add one
  95. * to the last none zero octet: so complement this octet and add one and finally
  96. * complement any left over until you get to the start of the string.
  97. *
  98. * Padding is a little trickier too. If the first bytes is > 0x80 then we pad
  99. * with 0xff. However if the first byte is 0x80 and one of the following bytes
  100. * is non-zero we pad with 0xff. The reason for this distinction is that 0x80
  101. * followed by optional zeros isn't padded.
  102. */
  103. int i2c_ASN1_INTEGER(ASN1_INTEGER *a, unsigned char **pp)
  104. {
  105. int pad=0,ret,i,neg;
  106. unsigned char *p,*n,pb=0;
  107. if (a == NULL) return(0);
  108. neg=a->type & V_ASN1_NEG;
  109. if (a->length == 0)
  110. ret=1;
  111. else
  112. {
  113. ret=a->length;
  114. i=a->data[0];
  115. if (!neg && (i > 127)) {
  116. pad=1;
  117. pb=0;
  118. } else if(neg) {
  119. if(i>128) {
  120. pad=1;
  121. pb=0xFF;
  122. } else if(i == 128) {
  123. /*
  124. * Special case: if any other bytes non zero we pad:
  125. * otherwise we don't.
  126. */
  127. for(i = 1; i < a->length; i++) if(a->data[i]) {
  128. pad=1;
  129. pb=0xFF;
  130. break;
  131. }
  132. }
  133. }
  134. ret+=pad;
  135. }
  136. if (pp == NULL) return(ret);
  137. p= *pp;
  138. if (pad) *(p++)=pb;
  139. if (a->length == 0) *(p++)=0;
  140. else if (!neg) memcpy(p,a->data,(unsigned int)a->length);
  141. else {
  142. /* Begin at the end of the encoding */
  143. n=a->data + a->length - 1;
  144. p += a->length - 1;
  145. i = a->length;
  146. /* Copy zeros to destination as long as source is zero */
  147. while(!*n) {
  148. *(p--) = 0;
  149. n--;
  150. i--;
  151. }
  152. /* Complement and increment next octet */
  153. *(p--) = ((*(n--)) ^ 0xff) + 1;
  154. i--;
  155. /* Complement any octets left */
  156. for(;i > 0; i--) *(p--) = *(n--) ^ 0xff;
  157. }
  158. *pp+=ret;
  159. return(ret);
  160. }
  161. /* Convert just ASN1 INTEGER content octets to ASN1_INTEGER structure */
  162. ASN1_INTEGER *c2i_ASN1_INTEGER(ASN1_INTEGER **a, const unsigned char **pp,
  163. long len)
  164. {
  165. ASN1_INTEGER *ret=NULL;
  166. const unsigned char *p, *pend;
  167. unsigned char *to,*s;
  168. int i;
  169. if ((a == NULL) || ((*a) == NULL))
  170. {
  171. if ((ret=M_ASN1_INTEGER_new()) == NULL) return(NULL);
  172. ret->type=V_ASN1_INTEGER;
  173. }
  174. else
  175. ret=(*a);
  176. p= *pp;
  177. pend = p + len;
  178. /* We must OPENSSL_malloc stuff, even for 0 bytes otherwise it
  179. * signifies a missing NULL parameter. */
  180. s=(unsigned char *)OPENSSL_malloc((int)len+1);
  181. if (s == NULL)
  182. {
  183. i=ERR_R_MALLOC_FAILURE;
  184. goto err;
  185. }
  186. to=s;
  187. if(!len) {
  188. /* Strictly speaking this is an illegal INTEGER but we
  189. * tolerate it.
  190. */
  191. ret->type=V_ASN1_INTEGER;
  192. } else if (*p & 0x80) /* a negative number */
  193. {
  194. ret->type=V_ASN1_NEG_INTEGER;
  195. if ((*p == 0xff) && (len != 1)) {
  196. p++;
  197. len--;
  198. }
  199. i = len;
  200. p += i - 1;
  201. to += i - 1;
  202. while((!*p) && i) {
  203. *(to--) = 0;
  204. i--;
  205. p--;
  206. }
  207. /* Special case: if all zeros then the number will be of
  208. * the form FF followed by n zero bytes: this corresponds to
  209. * 1 followed by n zero bytes. We've already written n zeros
  210. * so we just append an extra one and set the first byte to
  211. * a 1. This is treated separately because it is the only case
  212. * where the number of bytes is larger than len.
  213. */
  214. if(!i) {
  215. *s = 1;
  216. s[len] = 0;
  217. len++;
  218. } else {
  219. *(to--) = (*(p--) ^ 0xff) + 1;
  220. i--;
  221. for(;i > 0; i--) *(to--) = *(p--) ^ 0xff;
  222. }
  223. } else {
  224. ret->type=V_ASN1_INTEGER;
  225. if ((*p == 0) && (len != 1))
  226. {
  227. p++;
  228. len--;
  229. }
  230. memcpy(s,p,(int)len);
  231. }
  232. if (ret->data != NULL) OPENSSL_free(ret->data);
  233. ret->data=s;
  234. ret->length=(int)len;
  235. if (a != NULL) (*a)=ret;
  236. *pp=pend;
  237. return(ret);
  238. err:
  239. OPENSSL_PUT_ERROR(ASN1, c2i_ASN1_INTEGER, i);
  240. if ((ret != NULL) && ((a == NULL) || (*a != ret)))
  241. M_ASN1_INTEGER_free(ret);
  242. return(NULL);
  243. }
  244. /* This is a version of d2i_ASN1_INTEGER that ignores the sign bit of
  245. * ASN1 integers: some broken software can encode a positive INTEGER
  246. * with its MSB set as negative (it doesn't add a padding zero).
  247. */
  248. ASN1_INTEGER *d2i_ASN1_UINTEGER(ASN1_INTEGER **a, const unsigned char **pp,
  249. long length)
  250. {
  251. ASN1_INTEGER *ret=NULL;
  252. const unsigned char *p;
  253. unsigned char *s;
  254. long len;
  255. int inf,tag,xclass;
  256. int i;
  257. if ((a == NULL) || ((*a) == NULL))
  258. {
  259. if ((ret=M_ASN1_INTEGER_new()) == NULL) return(NULL);
  260. ret->type=V_ASN1_INTEGER;
  261. }
  262. else
  263. ret=(*a);
  264. p= *pp;
  265. inf=ASN1_get_object(&p,&len,&tag,&xclass,length);
  266. if (inf & 0x80)
  267. {
  268. i=ASN1_R_BAD_OBJECT_HEADER;
  269. goto err;
  270. }
  271. if (tag != V_ASN1_INTEGER)
  272. {
  273. i=ASN1_R_EXPECTING_AN_INTEGER;
  274. goto err;
  275. }
  276. /* We must OPENSSL_malloc stuff, even for 0 bytes otherwise it
  277. * signifies a missing NULL parameter. */
  278. s=(unsigned char *)OPENSSL_malloc((int)len+1);
  279. if (s == NULL)
  280. {
  281. i=ERR_R_MALLOC_FAILURE;
  282. goto err;
  283. }
  284. ret->type=V_ASN1_INTEGER;
  285. if(len) {
  286. if ((*p == 0) && (len != 1))
  287. {
  288. p++;
  289. len--;
  290. }
  291. memcpy(s,p,(int)len);
  292. p+=len;
  293. }
  294. if (ret->data != NULL) OPENSSL_free(ret->data);
  295. ret->data=s;
  296. ret->length=(int)len;
  297. if (a != NULL) (*a)=ret;
  298. *pp=p;
  299. return(ret);
  300. err:
  301. OPENSSL_PUT_ERROR(ASN1, d2i_ASN1_UINTEGER, i);
  302. if ((ret != NULL) && ((a == NULL) || (*a != ret)))
  303. M_ASN1_INTEGER_free(ret);
  304. return(NULL);
  305. }
  306. int ASN1_INTEGER_set(ASN1_INTEGER *a, long v)
  307. {
  308. int j,k;
  309. unsigned int i;
  310. unsigned char buf[sizeof(long)+1];
  311. long d;
  312. a->type=V_ASN1_INTEGER;
  313. if (a->length < (int)(sizeof(long)+1))
  314. {
  315. if (a->data != NULL)
  316. OPENSSL_free(a->data);
  317. if ((a->data=(unsigned char *)OPENSSL_malloc(sizeof(long)+1)) != NULL)
  318. memset((char *)a->data,0,sizeof(long)+1);
  319. }
  320. if (a->data == NULL)
  321. {
  322. OPENSSL_PUT_ERROR(ASN1, ASN1_INTEGER_set, ERR_R_MALLOC_FAILURE);
  323. return(0);
  324. }
  325. d=v;
  326. if (d < 0)
  327. {
  328. d= -d;
  329. a->type=V_ASN1_NEG_INTEGER;
  330. }
  331. for (i=0; i<sizeof(long); i++)
  332. {
  333. if (d == 0) break;
  334. buf[i]=(int)d&0xff;
  335. d>>=8;
  336. }
  337. j=0;
  338. for (k=i-1; k >=0; k--)
  339. a->data[j++]=buf[k];
  340. a->length=j;
  341. return(1);
  342. }
  343. long ASN1_INTEGER_get(const ASN1_INTEGER *a)
  344. {
  345. int neg=0,i;
  346. long r=0;
  347. if (a == NULL) return(0L);
  348. i=a->type;
  349. if (i == V_ASN1_NEG_INTEGER)
  350. neg=1;
  351. else if (i != V_ASN1_INTEGER)
  352. return -1;
  353. if (a->length > (int)sizeof(long))
  354. {
  355. /* hmm... a bit ugly, return all ones */
  356. return -1;
  357. }
  358. if (a->data == NULL)
  359. return 0;
  360. for (i=0; i<a->length; i++)
  361. {
  362. r<<=8;
  363. r|=(unsigned char)a->data[i];
  364. }
  365. if (neg) r= -r;
  366. return(r);
  367. }
  368. ASN1_INTEGER *BN_to_ASN1_INTEGER(const BIGNUM *bn, ASN1_INTEGER *ai)
  369. {
  370. ASN1_INTEGER *ret;
  371. int len,j;
  372. if (ai == NULL)
  373. ret=M_ASN1_INTEGER_new();
  374. else
  375. ret=ai;
  376. if (ret == NULL)
  377. {
  378. OPENSSL_PUT_ERROR(ASN1, BN_to_ASN1_INTEGER, ASN1_R_NESTED_ASN1_ERROR);
  379. goto err;
  380. }
  381. if (BN_is_negative(bn))
  382. ret->type = V_ASN1_NEG_INTEGER;
  383. else ret->type=V_ASN1_INTEGER;
  384. j=BN_num_bits(bn);
  385. len=((j == 0)?0:((j/8)+1));
  386. if (ret->length < len+4)
  387. {
  388. unsigned char *new_data=OPENSSL_realloc(ret->data, len+4);
  389. if (!new_data)
  390. {
  391. OPENSSL_PUT_ERROR(ASN1, BN_to_ASN1_INTEGER, ERR_R_MALLOC_FAILURE);
  392. goto err;
  393. }
  394. ret->data=new_data;
  395. }
  396. ret->length=BN_bn2bin(bn,ret->data);
  397. /* Correct zero case */
  398. if(!ret->length)
  399. {
  400. ret->data[0] = 0;
  401. ret->length = 1;
  402. }
  403. return(ret);
  404. err:
  405. if (ret != ai) M_ASN1_INTEGER_free(ret);
  406. return(NULL);
  407. }
  408. BIGNUM *ASN1_INTEGER_to_BN(const ASN1_INTEGER *ai, BIGNUM *bn)
  409. {
  410. BIGNUM *ret;
  411. if ((ret=BN_bin2bn(ai->data,ai->length,bn)) == NULL)
  412. OPENSSL_PUT_ERROR(ASN1, ASN1_INTEGER_to_BN, ASN1_R_BN_LIB);
  413. else if(ai->type == V_ASN1_NEG_INTEGER)
  414. BN_set_negative(ret, 1);
  415. return(ret);
  416. }