diff options
Diffstat (limited to 'src/lib/libcrypto/asn1/x_bignum.c')
| -rw-r--r-- | src/lib/libcrypto/asn1/x_bignum.c | 69 |
1 files changed, 43 insertions, 26 deletions
diff --git a/src/lib/libcrypto/asn1/x_bignum.c b/src/lib/libcrypto/asn1/x_bignum.c index 9cf3204a1b..bc74164fdc 100644 --- a/src/lib/libcrypto/asn1/x_bignum.c +++ b/src/lib/libcrypto/asn1/x_bignum.c | |||
| @@ -10,7 +10,7 @@ | |||
| 10 | * are met: | 10 | * are met: |
| 11 | * | 11 | * |
| 12 | * 1. Redistributions of source code must retain the above copyright | 12 | * 1. Redistributions of source code must retain the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer. | 13 | * notice, this list of conditions and the following disclaimer. |
| 14 | * | 14 | * |
| 15 | * 2. Redistributions in binary form must reproduce the above copyright | 15 | * 2. Redistributions in binary form must reproduce the above copyright |
| 16 | * notice, this list of conditions and the following disclaimer in | 16 | * notice, this list of conditions and the following disclaimer in |
| @@ -72,11 +72,14 @@ | |||
| 72 | static int bn_new(ASN1_VALUE **pval, const ASN1_ITEM *it); | 72 | static int bn_new(ASN1_VALUE **pval, const ASN1_ITEM *it); |
| 73 | static void bn_free(ASN1_VALUE **pval, const ASN1_ITEM *it); | 73 | static void bn_free(ASN1_VALUE **pval, const ASN1_ITEM *it); |
| 74 | 74 | ||
| 75 | static int bn_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype, const ASN1_ITEM *it); | 75 | static int bn_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype, |
| 76 | static int bn_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len, int utype, char *free_cont, const ASN1_ITEM *it); | 76 | const ASN1_ITEM *it); |
| 77 | static int bn_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len, | ||
| 78 | int utype, char *free_cont, const ASN1_ITEM *it); | ||
| 77 | 79 | ||
| 78 | static ASN1_PRIMITIVE_FUNCS bignum_pf = { | 80 | static ASN1_PRIMITIVE_FUNCS bignum_pf = { |
| 79 | NULL, 0, | 81 | NULL, |
| 82 | 0, | ||
| 80 | bn_new, | 83 | bn_new, |
| 81 | bn_free, | 84 | bn_free, |
| 82 | 0, | 85 | 0, |
| @@ -85,55 +88,69 @@ static ASN1_PRIMITIVE_FUNCS bignum_pf = { | |||
| 85 | }; | 88 | }; |
| 86 | 89 | ||
| 87 | ASN1_ITEM_start(BIGNUM) | 90 | ASN1_ITEM_start(BIGNUM) |
| 88 | ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &bignum_pf, 0, "BIGNUM" | 91 | ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &bignum_pf, 0, "BIGNUM" |
| 89 | ASN1_ITEM_end(BIGNUM) | 92 | ASN1_ITEM_end(BIGNUM) |
| 90 | 93 | ||
| 91 | ASN1_ITEM_start(CBIGNUM) | 94 | ASN1_ITEM_start(CBIGNUM) |
| 92 | ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &bignum_pf, BN_SENSITIVE, "BIGNUM" | 95 | ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &bignum_pf, BN_SENSITIVE, "BIGNUM" |
| 93 | ASN1_ITEM_end(CBIGNUM) | 96 | ASN1_ITEM_end(CBIGNUM) |
| 94 | 97 | ||
| 95 | static int bn_new(ASN1_VALUE **pval, const ASN1_ITEM *it) | 98 | static int |
| 99 | bn_new(ASN1_VALUE **pval, const ASN1_ITEM *it) | ||
| 96 | { | 100 | { |
| 97 | *pval = (ASN1_VALUE *)BN_new(); | 101 | *pval = (ASN1_VALUE *)BN_new(); |
| 98 | if(*pval) return 1; | 102 | if (*pval) |
| 99 | else return 0; | 103 | return 1; |
| 104 | else | ||
| 105 | return 0; | ||
| 100 | } | 106 | } |
| 101 | 107 | ||
| 102 | static void bn_free(ASN1_VALUE **pval, const ASN1_ITEM *it) | 108 | static void |
| 109 | bn_free(ASN1_VALUE **pval, const ASN1_ITEM *it) | ||
| 103 | { | 110 | { |
| 104 | if(!*pval) return; | 111 | if (!*pval) |
| 105 | if(it->size & BN_SENSITIVE) BN_clear_free((BIGNUM *)*pval); | 112 | return; |
| 106 | else BN_free((BIGNUM *)*pval); | 113 | if (it->size & BN_SENSITIVE) |
| 114 | BN_clear_free((BIGNUM *)*pval); | ||
| 115 | else | ||
| 116 | BN_free((BIGNUM *)*pval); | ||
| 107 | *pval = NULL; | 117 | *pval = NULL; |
| 108 | } | 118 | } |
| 109 | 119 | ||
| 110 | static int bn_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype, const ASN1_ITEM *it) | 120 | static int |
| 121 | bn_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype, const ASN1_ITEM *it) | ||
| 111 | { | 122 | { |
| 112 | BIGNUM *bn; | 123 | BIGNUM *bn; |
| 113 | int pad; | 124 | int pad; |
| 114 | if(!*pval) return -1; | 125 | |
| 126 | if (!*pval) | ||
| 127 | return -1; | ||
| 115 | bn = (BIGNUM *)*pval; | 128 | bn = (BIGNUM *)*pval; |
| 116 | /* If MSB set in an octet we need a padding byte */ | 129 | /* If MSB set in an octet we need a padding byte */ |
| 117 | if(BN_num_bits(bn) & 0x7) pad = 0; | 130 | if (BN_num_bits(bn) & 0x7) |
| 118 | else pad = 1; | 131 | pad = 0; |
| 119 | if(cont) { | 132 | else |
| 120 | if(pad) *cont++ = 0; | 133 | pad = 1; |
| 134 | if (cont) { | ||
| 135 | if (pad) | ||
| 136 | *cont++ = 0; | ||
| 121 | BN_bn2bin(bn, cont); | 137 | BN_bn2bin(bn, cont); |
| 122 | } | 138 | } |
| 123 | return pad + BN_num_bytes(bn); | 139 | return pad + BN_num_bytes(bn); |
| 124 | } | 140 | } |
| 125 | 141 | ||
| 126 | static int bn_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len, | 142 | static int |
| 127 | int utype, char *free_cont, const ASN1_ITEM *it) | 143 | bn_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len, int utype, |
| 144 | char *free_cont, const ASN1_ITEM *it) | ||
| 128 | { | 145 | { |
| 129 | BIGNUM *bn; | 146 | BIGNUM *bn; |
| 130 | if(!*pval) bn_new(pval, it); | 147 | |
| 131 | bn = (BIGNUM *)*pval; | 148 | if (!*pval) |
| 132 | if(!BN_bin2bn(cont, len, bn)) { | 149 | bn_new(pval, it); |
| 150 | bn = (BIGNUM *)*pval; | ||
| 151 | if (!BN_bin2bn(cont, len, bn)) { | ||
| 133 | bn_free(pval, it); | 152 | bn_free(pval, it); |
| 134 | return 0; | 153 | return 0; |
| 135 | } | 154 | } |
| 136 | return 1; | 155 | return 1; |
| 137 | } | 156 | } |
| 138 | |||
| 139 | |||
