x_bignum.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include <openssl/asn1t.h>
  12. #include <openssl/bn.h>
  13. /*
  14. * Custom primitive type for BIGNUM handling. This reads in an ASN1_INTEGER
  15. * as a BIGNUM directly. Currently it ignores the sign which isn't a problem
  16. * since all BIGNUMs used are non negative and anything that looks negative
  17. * is normally due to an encoding error.
  18. */
  19. #define BN_SENSITIVE 1
  20. static int bn_new(ASN1_VALUE **pval, const ASN1_ITEM *it);
  21. static int bn_secure_new(ASN1_VALUE **pval, const ASN1_ITEM *it);
  22. static void bn_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
  23. static int bn_i2c(const ASN1_VALUE **pval, unsigned char *cont, int *putype,
  24. const ASN1_ITEM *it);
  25. static int bn_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
  26. int utype, char *free_cont, const ASN1_ITEM *it);
  27. static int bn_secure_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
  28. int utype, char *free_cont, const ASN1_ITEM *it);
  29. static int bn_print(BIO *out, const ASN1_VALUE **pval, const ASN1_ITEM *it,
  30. int indent, const ASN1_PCTX *pctx);
  31. static ASN1_PRIMITIVE_FUNCS bignum_pf = {
  32. NULL, 0,
  33. bn_new,
  34. bn_free,
  35. 0,
  36. bn_c2i,
  37. bn_i2c,
  38. bn_print
  39. };
  40. static ASN1_PRIMITIVE_FUNCS cbignum_pf = {
  41. NULL, 0,
  42. bn_secure_new,
  43. bn_free,
  44. 0,
  45. bn_secure_c2i,
  46. bn_i2c,
  47. bn_print
  48. };
  49. ASN1_ITEM_start(BIGNUM)
  50. ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &bignum_pf, 0, "BIGNUM"
  51. ASN1_ITEM_end(BIGNUM)
  52. ASN1_ITEM_start(CBIGNUM)
  53. ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &cbignum_pf, BN_SENSITIVE, "CBIGNUM"
  54. ASN1_ITEM_end(CBIGNUM)
  55. static int bn_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
  56. {
  57. *pval = (ASN1_VALUE *)BN_new();
  58. if (*pval != NULL)
  59. return 1;
  60. else
  61. return 0;
  62. }
  63. static int bn_secure_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
  64. {
  65. *pval = (ASN1_VALUE *)BN_secure_new();
  66. if (*pval != NULL)
  67. return 1;
  68. else
  69. return 0;
  70. }
  71. static void bn_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
  72. {
  73. if (!*pval)
  74. return;
  75. if (it->size & BN_SENSITIVE)
  76. BN_clear_free((BIGNUM *)*pval);
  77. else
  78. BN_free((BIGNUM *)*pval);
  79. *pval = NULL;
  80. }
  81. static int bn_i2c(const ASN1_VALUE **pval, unsigned char *cont, int *putype,
  82. const ASN1_ITEM *it)
  83. {
  84. BIGNUM *bn;
  85. int pad;
  86. if (!*pval)
  87. return -1;
  88. bn = (BIGNUM *)*pval;
  89. /* If MSB set in an octet we need a padding byte */
  90. if (BN_num_bits(bn) & 0x7)
  91. pad = 0;
  92. else
  93. pad = 1;
  94. if (cont) {
  95. if (pad)
  96. *cont++ = 0;
  97. BN_bn2bin(bn, cont);
  98. }
  99. return pad + BN_num_bytes(bn);
  100. }
  101. static int bn_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
  102. int utype, char *free_cont, const ASN1_ITEM *it)
  103. {
  104. BIGNUM *bn;
  105. if (*pval == NULL && !bn_new(pval, it))
  106. return 0;
  107. bn = (BIGNUM *)*pval;
  108. if (!BN_bin2bn(cont, len, bn)) {
  109. bn_free(pval, it);
  110. return 0;
  111. }
  112. return 1;
  113. }
  114. static int bn_secure_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
  115. int utype, char *free_cont, const ASN1_ITEM *it)
  116. {
  117. if (!*pval)
  118. bn_secure_new(pval, it);
  119. return bn_c2i(pval, cont, len, utype, free_cont, it);
  120. }
  121. static int bn_print(BIO *out, const ASN1_VALUE **pval, const ASN1_ITEM *it,
  122. int indent, const ASN1_PCTX *pctx)
  123. {
  124. if (!BN_print(out, *(BIGNUM **)pval))
  125. return 0;
  126. if (BIO_puts(out, "\n") <= 0)
  127. return 0;
  128. return 1;
  129. }