Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

457 lignes
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 <string.h>
  58. #include <openssl/err.h>
  59. #include <openssl/mem.h>
  60. ASN1_INTEGER *ASN1_INTEGER_dup(const ASN1_INTEGER *x)
  61. { return M_ASN1_INTEGER_dup(x);}
  62. int ASN1_INTEGER_cmp(const ASN1_INTEGER *x, const ASN1_INTEGER *y)
  63. {
  64. int neg, ret;
  65. /* Compare signs */
  66. neg = x->type & V_ASN1_NEG;
  67. if (neg != (y->type & V_ASN1_NEG))
  68. {
  69. if (neg)
  70. return -1;
  71. else
  72. return 1;
  73. }
  74. ret = ASN1_STRING_cmp(x, y);
  75. if (neg)
  76. return -ret;
  77. else
  78. return ret;
  79. }
  80. /*
  81. * This converts an ASN1 INTEGER into its content encoding.
  82. * The internal representation is an ASN1_STRING whose data is a big endian
  83. * representation of the value, ignoring the sign. The sign is determined by
  84. * the type: V_ASN1_INTEGER for positive and V_ASN1_NEG_INTEGER for negative.
  85. *
  86. * Positive integers are no problem: they are almost the same as the DER
  87. * encoding, except if the first byte is >= 0x80 we need to add a zero pad.
  88. *
  89. * Negative integers are a bit trickier...
  90. * The DER representation of negative integers is in 2s complement form.
  91. * The internal form is converted by complementing each octet and finally
  92. * adding one to the result. This can be done less messily with a little trick.
  93. * If the internal form has trailing zeroes then they will become FF by the
  94. * complement and 0 by the add one (due to carry) so just copy as many trailing
  95. * zeros to the destination as there are in the source. The carry will add one
  96. * to the last none zero octet: so complement this octet and add one and finally
  97. * complement any left over until you get to the start of the string.
  98. *
  99. * Padding is a little trickier too. If the first bytes is > 0x80 then we pad
  100. * with 0xff. However if the first byte is 0x80 and one of the following bytes
  101. * is non-zero we pad with 0xff. The reason for this distinction is that 0x80
  102. * followed by optional zeros isn't padded.
  103. */
  104. int i2c_ASN1_INTEGER(ASN1_INTEGER *a, unsigned char **pp)
  105. {
  106. int pad=0,ret,i,neg;
  107. unsigned char *p,*n,pb=0;
  108. if (a == NULL) return(0);
  109. neg=a->type & V_ASN1_NEG;
  110. if (a->length == 0)
  111. ret=1;
  112. else
  113. {
  114. ret=a->length;
  115. i=a->data[0];
  116. if (!neg && (i > 127)) {
  117. pad=1;
  118. pb=0;
  119. } else if(neg) {
  120. if(i>128) {
  121. pad=1;
  122. pb=0xFF;
  123. } else if(i == 128) {
  124. /*
  125. * Special case: if any other bytes non zero we pad:
  126. * otherwise we don't.
  127. */
  128. for(i = 1; i < a->length; i++) if(a->data[i]) {
  129. pad=1;
  130. pb=0xFF;
  131. break;
  132. }
  133. }
  134. }
  135. ret+=pad;
  136. }
  137. if (pp == NULL) return(ret);
  138. p= *pp;
  139. if (pad) *(p++)=pb;
  140. if (a->length == 0) *(p++)=0;
  141. else if (!neg) memcpy(p,a->data,(unsigned int)a->length);
  142. else {
  143. /* Begin at the end of the encoding */
  144. n=a->data + a->length - 1;
  145. p += a->length - 1;
  146. i = a->length;
  147. /* Copy zeros to destination as long as source is zero */
  148. while(!*n) {
  149. *(p--) = 0;
  150. n--;
  151. i--;
  152. }
  153. /* Complement and increment next octet */
  154. *(p--) = ((*(n--)) ^ 0xff) + 1;
  155. i--;
  156. /* Complement any octets left */
  157. for(;i > 0; i--) *(p--) = *(n--) ^ 0xff;
  158. }
  159. *pp+=ret;
  160. return(ret);
  161. }
  162. /* Convert just ASN1 INTEGER content octets to ASN1_INTEGER structure */
  163. ASN1_INTEGER *c2i_ASN1_INTEGER(ASN1_INTEGER **a, const unsigned char **pp,
  164. long len)
  165. {
  166. ASN1_INTEGER *ret=NULL;
  167. const unsigned char *p, *pend;
  168. unsigned char *to,*s;
  169. int i;
  170. if ((a == NULL) || ((*a) == NULL))
  171. {
  172. if ((ret=M_ASN1_INTEGER_new()) == NULL) return(NULL);
  173. ret->type=V_ASN1_INTEGER;
  174. }
  175. else
  176. ret=(*a);
  177. p= *pp;
  178. pend = p + len;
  179. /* We must OPENSSL_malloc stuff, even for 0 bytes otherwise it
  180. * signifies a missing NULL parameter. */
  181. s=(unsigned char *)OPENSSL_malloc((int)len+1);
  182. if (s == NULL)
  183. {
  184. i=ERR_R_MALLOC_FAILURE;
  185. goto err;
  186. }
  187. to=s;
  188. if(!len) {
  189. /* Strictly speaking this is an illegal INTEGER but we
  190. * tolerate it.
  191. */
  192. ret->type=V_ASN1_INTEGER;
  193. } else if (*p & 0x80) /* a negative number */
  194. {
  195. ret->type=V_ASN1_NEG_INTEGER;
  196. if ((*p == 0xff) && (len != 1)) {
  197. p++;
  198. len--;
  199. }
  200. i = len;
  201. p += i - 1;
  202. to += i - 1;
  203. while((!*p) && i) {
  204. *(to--) = 0;
  205. i--;
  206. p--;
  207. }
  208. /* Special case: if all zeros then the number will be of
  209. * the form FF followed by n zero bytes: this corresponds to
  210. * 1 followed by n zero bytes. We've already written n zeros
  211. * so we just append an extra one and set the first byte to
  212. * a 1. This is treated separately because it is the only case
  213. * where the number of bytes is larger than len.
  214. */
  215. if(!i) {
  216. *s = 1;
  217. s[len] = 0;
  218. len++;
  219. } else {
  220. *(to--) = (*(p--) ^ 0xff) + 1;
  221. i--;
  222. for(;i > 0; i--) *(to--) = *(p--) ^ 0xff;
  223. }
  224. } else {
  225. ret->type=V_ASN1_INTEGER;
  226. if ((*p == 0) && (len != 1))
  227. {
  228. p++;
  229. len--;
  230. }
  231. memcpy(s,p,(int)len);
  232. }
  233. if (ret->data != NULL) OPENSSL_free(ret->data);
  234. ret->data=s;
  235. ret->length=(int)len;
  236. if (a != NULL) (*a)=ret;
  237. *pp=pend;
  238. return(ret);
  239. err:
  240. OPENSSL_PUT_ERROR(ASN1, c2i_ASN1_INTEGER, i);
  241. if ((ret != NULL) && ((a == NULL) || (*a != ret)))
  242. M_ASN1_INTEGER_free(ret);
  243. return(NULL);
  244. }
  245. /* This is a version of d2i_ASN1_INTEGER that ignores the sign bit of
  246. * ASN1 integers: some broken software can encode a positive INTEGER
  247. * with its MSB set as negative (it doesn't add a padding zero).
  248. */
  249. ASN1_INTEGER *d2i_ASN1_UINTEGER(ASN1_INTEGER **a, const unsigned char **pp,
  250. long length)
  251. {
  252. ASN1_INTEGER *ret=NULL;
  253. const unsigned char *p;
  254. unsigned char *s;
  255. long len;
  256. int inf,tag,xclass;
  257. int i;
  258. if ((a == NULL) || ((*a) == NULL))
  259. {
  260. if ((ret=M_ASN1_INTEGER_new()) == NULL) return(NULL);
  261. ret->type=V_ASN1_INTEGER;
  262. }
  263. else
  264. ret=(*a);
  265. p= *pp;
  266. inf=ASN1_get_object(&p,&len,&tag,&xclass,length);
  267. if (inf & 0x80)
  268. {
  269. i=ASN1_R_BAD_OBJECT_HEADER;
  270. goto err;
  271. }
  272. if (tag != V_ASN1_INTEGER)
  273. {
  274. i=ASN1_R_EXPECTING_AN_INTEGER;
  275. goto err;
  276. }
  277. /* We must OPENSSL_malloc stuff, even for 0 bytes otherwise it
  278. * signifies a missing NULL parameter. */
  279. s=(unsigned char *)OPENSSL_malloc((int)len+1);
  280. if (s == NULL)
  281. {
  282. i=ERR_R_MALLOC_FAILURE;
  283. goto err;
  284. }
  285. ret->type=V_ASN1_INTEGER;
  286. if(len) {
  287. if ((*p == 0) && (len != 1))
  288. {
  289. p++;
  290. len--;
  291. }
  292. memcpy(s,p,(int)len);
  293. p+=len;
  294. }
  295. if (ret->data != NULL) OPENSSL_free(ret->data);
  296. ret->data=s;
  297. ret->length=(int)len;
  298. if (a != NULL) (*a)=ret;
  299. *pp=p;
  300. return(ret);
  301. err:
  302. OPENSSL_PUT_ERROR(ASN1, d2i_ASN1_UINTEGER, i);
  303. if ((ret != NULL) && ((a == NULL) || (*a != ret)))
  304. M_ASN1_INTEGER_free(ret);
  305. return(NULL);
  306. }
  307. int ASN1_INTEGER_set(ASN1_INTEGER *a, long v)
  308. {
  309. int j,k;
  310. unsigned int i;
  311. unsigned char buf[sizeof(long)+1];
  312. long d;
  313. a->type=V_ASN1_INTEGER;
  314. if (a->length < (int)(sizeof(long)+1))
  315. {
  316. if (a->data != NULL)
  317. OPENSSL_free(a->data);
  318. if ((a->data=(unsigned char *)OPENSSL_malloc(sizeof(long)+1)) != NULL)
  319. memset((char *)a->data,0,sizeof(long)+1);
  320. }
  321. if (a->data == NULL)
  322. {
  323. OPENSSL_PUT_ERROR(ASN1, ASN1_INTEGER_set, ERR_R_MALLOC_FAILURE);
  324. return(0);
  325. }
  326. d=v;
  327. if (d < 0)
  328. {
  329. d= -d;
  330. a->type=V_ASN1_NEG_INTEGER;
  331. }
  332. for (i=0; i<sizeof(long); i++)
  333. {
  334. if (d == 0) break;
  335. buf[i]=(int)d&0xff;
  336. d>>=8;
  337. }
  338. j=0;
  339. for (k=i-1; k >=0; k--)
  340. a->data[j++]=buf[k];
  341. a->length=j;
  342. return(1);
  343. }
  344. long ASN1_INTEGER_get(const ASN1_INTEGER *a)
  345. {
  346. int neg=0,i;
  347. long r=0;
  348. if (a == NULL) return(0L);
  349. i=a->type;
  350. if (i == V_ASN1_NEG_INTEGER)
  351. neg=1;
  352. else if (i != V_ASN1_INTEGER)
  353. return -1;
  354. if (a->length > (int)sizeof(long))
  355. {
  356. /* hmm... a bit ugly, return all ones */
  357. return -1;
  358. }
  359. if (a->data == NULL)
  360. return 0;
  361. for (i=0; i<a->length; i++)
  362. {
  363. r<<=8;
  364. r|=(unsigned char)a->data[i];
  365. }
  366. if (neg) r= -r;
  367. return(r);
  368. }
  369. ASN1_INTEGER *BN_to_ASN1_INTEGER(const BIGNUM *bn, ASN1_INTEGER *ai)
  370. {
  371. ASN1_INTEGER *ret;
  372. int len,j;
  373. if (ai == NULL)
  374. ret=M_ASN1_INTEGER_new();
  375. else
  376. ret=ai;
  377. if (ret == NULL)
  378. {
  379. OPENSSL_PUT_ERROR(ASN1, BN_to_ASN1_INTEGER, ASN1_R_NESTED_ASN1_ERROR);
  380. goto err;
  381. }
  382. if (BN_is_negative(bn) && !BN_is_zero(bn))
  383. ret->type = V_ASN1_NEG_INTEGER;
  384. else ret->type=V_ASN1_INTEGER;
  385. j=BN_num_bits(bn);
  386. len=((j == 0)?0:((j/8)+1));
  387. if (ret->length < len+4)
  388. {
  389. unsigned char *new_data=OPENSSL_realloc(ret->data, len+4);
  390. if (!new_data)
  391. {
  392. OPENSSL_PUT_ERROR(ASN1, BN_to_ASN1_INTEGER, ERR_R_MALLOC_FAILURE);
  393. goto err;
  394. }
  395. ret->data=new_data;
  396. }
  397. ret->length=BN_bn2bin(bn,ret->data);
  398. /* Correct zero case */
  399. if(!ret->length)
  400. {
  401. ret->data[0] = 0;
  402. ret->length = 1;
  403. }
  404. return(ret);
  405. err:
  406. if (ret != ai) M_ASN1_INTEGER_free(ret);
  407. return(NULL);
  408. }
  409. BIGNUM *ASN1_INTEGER_to_BN(const ASN1_INTEGER *ai, BIGNUM *bn)
  410. {
  411. BIGNUM *ret;
  412. if ((ret=BN_bin2bn(ai->data,ai->length,bn)) == NULL)
  413. OPENSSL_PUT_ERROR(ASN1, ASN1_INTEGER_to_BN, ASN1_R_BN_LIB);
  414. else if(ai->type == V_ASN1_NEG_INTEGER)
  415. BN_set_negative(ret, 1);
  416. return(ret);
  417. }