openssl 1.1.0 compatibility (Bug 497230)
authorebensza <bence.janos.szabo@ericsson.com>
Thu, 7 Jul 2016 12:07:30 +0000 (14:07 +0200)
committerebensza <bence.janos.szabo@ericsson.com>
Thu, 7 Jul 2016 12:07:30 +0000 (14:07 +0200)
Signed-off-by: ebensza <bence.janos.szabo@ericsson.com>
compiler2/Int.cc
core/Addfunc.cc
core/Integer.cc
core/RInt.hh
core/Textbuf.cc
mctr2/cli/config_read.y

index 41b1457aa733d1baf3909b9524fec4f97997ffc2..7809967114a5d7768905d41466ed1c3fc1640fc7 100644 (file)
@@ -314,7 +314,6 @@ int_val_t int_val_t::operator*(const int_val_t& right) const
         BIGNUM *left_bn = to_openssl();
         BIGNUM *right_bn = right.to_openssl();
         BN_CTX *ctx = BN_CTX_new();
-        BN_CTX_init(ctx);
         BN_mul(left_bn, left_bn, right_bn, ctx);
         BN_CTX_free(ctx);
         BN_free(right_bn);
@@ -328,7 +327,6 @@ int_val_t int_val_t::operator*(const int_val_t& right) const
     } else {
       BIGNUM *this_bn = to_openssl();
       BN_CTX *ctx = BN_CTX_new();
-      BN_CTX_init(ctx);
       BN_mul(this_bn, this_bn, right.get_val_openssl(), ctx);
       BN_CTX_free(ctx);
       return int_val_t(this_bn);
@@ -337,7 +335,6 @@ int_val_t int_val_t::operator*(const int_val_t& right) const
     BIGNUM *result = BN_new();
     BIGNUM *right_bn;
     BN_CTX *ctx = BN_CTX_new();
-    BN_CTX_init(ctx);
     right_bn = right.native_flag ? right.to_openssl()
       : right.get_val_openssl();
     BN_mul(result, val.openssl, right_bn, ctx);
@@ -359,7 +356,6 @@ int_val_t int_val_t::operator/(const int_val_t& right) const
     } else {
       BIGNUM *left_bn = to_openssl();
       BN_CTX *ctx = BN_CTX_new();
-      BN_CTX_init(ctx);
       BN_div(left_bn, NULL, left_bn, right.get_val_openssl(), ctx);
       BN_CTX_free(ctx);
       if (BN_num_bits(left_bn) <= (int)sizeof(long long) * 8 - 1) {
@@ -376,7 +372,6 @@ int_val_t int_val_t::operator/(const int_val_t& right) const
     BIGNUM *result = BN_new();
     BIGNUM *right_bn;
     BN_CTX *ctx = BN_CTX_new();
-    BN_CTX_init(ctx);
     right_bn = right.is_native() ? right.to_openssl() :
       right.get_val_openssl();
     BN_div(result, NULL, val.openssl, right_bn, ctx);
index 5c8c25c918fe5362abdc6e45c1d03f32a35347ea..b428d4fbfa6ca2170a275c0f49bf5df628313c1a 100644 (file)
@@ -461,10 +461,11 @@ OCTETSTRING int2oct(const INTEGER& value, int length)
         "does not fit in %d octet%s.", (const char *)value_str, length,
         length > 1 ? "s" : "");
     } else {
+      unsigned char* tmp = (unsigned char*)Malloc(bytes * sizeof(unsigned char));
+      BN_bn2bin(value_tmp, tmp);
       for (int i = length - 1; i >= 0; i--) {
-        if (value_tmp->top) {
-          octets_ptr[i] = value_tmp->d[0] & 0xff;
-          BN_rshift(value_tmp, value_tmp, 8);
+        if (bytes-length+i >= 0) {
+          octets_ptr[i] = tmp[bytes-length+i] & 0xff;
         }
         else { // we used up all of the bignum; zero the beginning and quit
           memset(octets_ptr, 0, i+1);
@@ -472,6 +473,7 @@ OCTETSTRING int2oct(const INTEGER& value, int length)
         }
       }
       BN_free(value_tmp);
+      Free(tmp);
       return ret_val;
     }
   }
index 34fd70033655d8b56e4830740adb52631b0e05c8..9ac1c32d853b23c680d5743638a72e14eb4df051 100644 (file)
@@ -395,7 +395,6 @@ INTEGER INTEGER::operator*(const INTEGER& other_value) const
         BIGNUM *this_int = to_openssl(val.native);
         BIGNUM *other_value_int = to_openssl(other_value.val.native);
         BN_CTX *ctx = BN_CTX_new();
-        BN_CTX_init(ctx);
         BN_mul(this_int, this_int, other_value_int, ctx);
         BN_CTX_free(ctx);
         BN_free(other_value_int);
@@ -409,7 +408,6 @@ INTEGER INTEGER::operator*(const INTEGER& other_value) const
     } else {
       BIGNUM *this_int = to_openssl(val.native);
       BN_CTX *ctx = BN_CTX_new();
-      BN_CTX_init(ctx);
       BN_mul(this_int, this_int, other_value.val.openssl, ctx);
       BN_CTX_free(ctx);
       return INTEGER(this_int);
@@ -418,7 +416,6 @@ INTEGER INTEGER::operator*(const INTEGER& other_value) const
     BIGNUM *result = BN_new();
     BIGNUM *other_value_int = NULL;
     BN_CTX *ctx = BN_CTX_new();
-    BN_CTX_init(ctx);
     other_value_int = other_value.native_flag
       ? to_openssl(other_value.val.native) : other_value.val.openssl;
     BN_mul(result, val.openssl, other_value_int, ctx);
@@ -447,7 +444,6 @@ INTEGER INTEGER::operator/(const INTEGER& other_value) const
     } else {
       BIGNUM *this_int = to_openssl(val.native);
       BN_CTX *ctx = BN_CTX_new();
-      BN_CTX_init(ctx);
       BN_div(this_int, NULL, this_int, other_value.val.openssl, ctx);
       BN_CTX_free(ctx);
       if (BN_num_bits(this_int) <= (int)sizeof(int) * 8 - 1) {
@@ -464,7 +460,6 @@ INTEGER INTEGER::operator/(const INTEGER& other_value) const
     BIGNUM *result = BN_new();
     BIGNUM *other_value_int = NULL;
     BN_CTX *ctx = BN_CTX_new();
-    BN_CTX_init(ctx);
     other_value_int = other_value.native_flag
       ? to_openssl(other_value.val.native) : other_value.val.openssl;
     BN_div(result, NULL, val.openssl, other_value_int, ctx);
@@ -597,23 +592,25 @@ long long int INTEGER::get_long_long_val() const
 {
   must_bound("Using the value of an unbound integer variable.");
   if (likely(native_flag)) return val.native;
-  size_t slot_size = sizeof(BN_ULONG);
   bool is_negative = BN_is_negative(val.openssl);
   long long int ret_val = 0;
-  if (unlikely(val.openssl->top == 0)) return 0;
+  if (unlikely(BN_is_zero(val.openssl))) return 0;
   // It feels so bad accessing a BIGNUM directly, but faster than string
   // conversion...
-  else if (likely(val.openssl->top == 1))
-    return !is_negative ? val.openssl->d[0] : -val.openssl->d[0];
-  ret_val = val.openssl->d[val.openssl->top - 1];
-  // From now, shift by 8.
-  for (int i = val.openssl->top - 2; i >= 0; i--) {
-    for (int j = slot_size - 1; j >= 0; j--) {
-      unsigned char tmp = (val.openssl->d[i] >> 8 * j) & 0xff;
-      ret_val <<= 8;
-      ret_val += tmp;
-    }
+  // I know, I had to fix this... Bence
+  if (BN_num_bytes(val.openssl) <= sizeof(BN_ULONG)) {
+    return !is_negative ? BN_get_word(val.openssl) : -BN_get_word(val.openssl);
   }
+  
+  unsigned num_bytes = BN_num_bytes(val.openssl);
+  unsigned char* tmp = (unsigned char*)Malloc(num_bytes * sizeof(unsigned char));
+  BN_bn2bin(val.openssl, tmp);
+  ret_val = tmp[0] & 0xff;
+  for (int i = 1; i < num_bytes; i++) {
+    ret_val <<= 8;
+    ret_val += tmp[i] & 0xff;
+  }
+  Free(tmp);
   return !is_negative ? ret_val : -ret_val;
 }
 
@@ -1250,14 +1247,14 @@ int INTEGER::RAW_encode_openssl(const TTCN_Typedescriptor_t& p_td,
   int val_bits = 0, len_bits = 0; // only for IntX
   BIGNUM *D = BN_new();
   BN_copy(D, val.openssl);
-  boolean neg_sgbit = (D->neg) && (p_td.raw->comp == SG_SG_BIT);
+  boolean neg_sgbit = (BN_is_negative(D)) && (p_td.raw->comp == SG_SG_BIT);
   if (!is_bound()) {
     TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_UNBOUND,
       "Encoding an unbound value.");
     BN_clear(D);
     neg_sgbit = FALSE;
   }
-  if ((D->neg) && (p_td.raw->comp == SG_NO)) {
+  if ((BN_is_negative(D)) && (p_td.raw->comp == SG_NO)) {
     TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_SIGN_ERR,
       "Unsigned encoding of a negative number: %s", p_td.name);
     BN_set_negative(D, 0);
@@ -1307,12 +1304,17 @@ int INTEGER::RAW_encode_openssl(const TTCN_Typedescriptor_t& p_td,
   } else {
     bc = myleaf.body.leaf.data_array;
   }
-  boolean twos_compl = (D->neg) && !neg_sgbit;
+  boolean twos_compl = (BN_is_negative(D)) && !neg_sgbit;
   // Conversion to 2's complement.
   if (twos_compl) {
     BN_set_negative(D, 0);
-    for (int a = 0; a < D->dmax; a++) D->d[a] = ~D->d[a];
+    unsigned num_bytes = BN_num_bytes(D);
+    unsigned char* tmp = (unsigned char*)Malloc(num_bytes * sizeof(unsigned char));
+    BN_bn2bin(D, tmp);
+    for (int a = 0; a < num_bytes; a++) tmp[a] = ~tmp[a];
+    BN_bin2bn(tmp, num_bytes, D);
     BN_add_word(D, 1);
+    Free(tmp);
   }
   if (p_td.raw->fieldlength == RAW_INTX) {
     int i = 0;
@@ -1320,13 +1322,17 @@ int INTEGER::RAW_encode_openssl(const TTCN_Typedescriptor_t& p_td,
     // of the value, too
     val_bits = length * 8 - len_bits;
     // first, encode the value
+    unsigned num_bytes = BN_num_bytes(D);
+    unsigned char* tmp = (unsigned char*)Malloc(num_bytes * sizeof(unsigned char));
+    BN_bn2bin(D, tmp);
     do {
-      bc[i] = (D->top ? D->d[0] : (twos_compl ? 0xFF : 0)) & INTX_MASKS[val_bits > 8 ? 8 : val_bits];
+      bc[i] = (num_bytes-i > 0 ? tmp[num_bytes - (i + 1)] : (twos_compl ? 0xFF : 0)) & INTX_MASKS[val_bits > 8 ? 8 : val_bits];
       ++i;
-      BN_rshift(D, D, 8);
       val_bits -= 8;
     }
     while (val_bits > 0);
+    Free(tmp);
+    BN_free(D);
     if (neg_sgbit) {
       // the sign bit is the first bit after the length
       unsigned char mask = 0x80 >> len_bits % 8;
@@ -1364,15 +1370,17 @@ int INTEGER::RAW_encode_openssl(const TTCN_Typedescriptor_t& p_td,
   }
   else {
     int num_bytes = BN_num_bytes(D);
+    unsigned char* tmp = (unsigned char*)Malloc(num_bytes * sizeof(unsigned char));
+    BN_bn2bin(D, tmp);
     for (int a = 0; a < length; a++) {
       if (twos_compl && num_bytes - 1 < a) bc[a] = 0xff;
-      else bc[a] = (D->top ? D->d[0] : 0) & 0xff;
-      BN_rshift(D, D, 8);
+      else bc[a] = (num_bytes - a > 0 ? tmp[num_bytes - (a + 1)] : 0) & 0xff;
     }
     if (neg_sgbit) {
       unsigned char mask = 0x01 << (p_td.raw->fieldlength - 1) % 8;
       bc[length - 1] |= mask;
     }
+    Free(tmp);
     BN_free(D);
     myleaf.length = p_td.raw->fieldlength;
   }
@@ -1557,7 +1565,7 @@ int INTEGER::RAW_decode(const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& buff,
         }
         else {
           native_flag = TRUE;
-          val.native = BN_is_negative(D) ? -D->d[0] : D->d[0];
+          val.native = BN_is_negative(D) ? -BN_get_word(D) : BN_get_word(D);
           BN_free(D);
         }
         Free(data);
@@ -1682,7 +1690,7 @@ int INTEGER::get_nof_digits()
     BIGNUM *x = BN_new();
     BN_copy(x, val.openssl);
     if (BN_is_zero(x)) return 1;
-    x->neg = 0;
+    BN_set_negative(x, 1);
     while (!BN_is_zero(x)) {
       ++digits;
       BN_div_word(x, 10);
index bbd0e313235f12db36e364d94d8179be59cfe44a..b8b3721d9e65d5cb696ff6c64f8ccbde33b616eb 100644 (file)
@@ -14,8 +14,7 @@
 #ifndef RInt_HH
 #define RInt_HH
 
-struct bignum_st;
-typedef bignum_st BIGNUM;
+#include <openssl/bn.h>
 
 typedef int RInt;
 
index b7d9e37139e3540a3070a22674adad7912f907ce..b4bff812cd3ecd3edf5a80e6fab216c6228ecc4f 100644 (file)
@@ -126,19 +126,24 @@ void Text_Buf::push_int(const int_val_t& value)
     unsigned num_bytes = (num_bits / 7)+1;
     Reallocate(buf_len + num_bytes);
     unsigned char *buf = (unsigned char *)data_ptr + buf_begin + buf_len;
+    // Alloc once, free once
+    unsigned char* buf2 = (unsigned char*)Malloc(BN_num_bytes(D) * sizeof(unsigned char));
     for (unsigned i = num_bytes - 1; ; i--) {
+      BN_bn2bin(D, buf2); // TODO: query once and then get the 7 loads
+      unsigned temp_num_bytes = BN_num_bytes(D);
       // Seven bits at a time, except the first byte has only 6 payload bits
       if (i > 0) {
-        buf[i] = D->d[0] & 0x7f;
+        buf[i] = buf2[temp_num_bytes-1] & 0x7f;
         if (!BN_rshift(D, D, 7)) return;
       } else {
-        buf[i] = (D->top ? D->d[0] : 0) & 0x3f;
+          buf[i] = (BN_is_zero(D) ? 0 : buf2[temp_num_bytes-1]) & 0x3f;
       }
       if (i < num_bytes - 1) buf[i] |= 0x80;
       if (i == 0) break;
     }
     if (BN_is_negative(D)) buf[0] |= 0x40; // Put in the sign bit
     BN_free(D);
+    Free(buf2);
     buf_len += num_bytes;
   }
 }
@@ -186,7 +191,8 @@ boolean Text_Buf::safe_pull_int(int_val_t& value)
     if (BN_num_bits(D) > (RInt)sizeof(RInt) * 8 - 1) {
       value = int_val_t(D);
     } else {
-      value = int_val_t(neg ? -D->d[0] : D->d[0]);
+      BN_ULONG num = BN_get_word(D); // BN_ULONG is unsigned long
+      value = int_val_t(neg ? -num : num);
       BN_free(D);
     }
   } else {
index 16135a81bc486fe256ef56d4e75177f574a744dc..4acf98185d63aa7bb2b3016d3f24ee1b3e65a23b 100644 (file)
@@ -402,7 +402,7 @@ IntegerValue:
        {
          $$ = BN_new();
     BN_CTX *ctx = BN_CTX_new();
-    BN_CTX_init(ctx);
+    //BN_CTX_init(ctx);
     BN_mul($$, $1, $3, ctx);
     BN_CTX_free(ctx);
     BN_free($1);
@@ -418,7 +418,7 @@ IntegerValue:
       $$ = BN_0;
     } else {
       BN_CTX *ctx = BN_CTX_new();
-      BN_CTX_init(ctx);
+      //BN_CTX_init(ctx);
       BN_div($$, NULL, $1, $3, ctx);
       BN_CTX_free(ctx);
       BN_free(BN_0);
This page took 0.032552 seconds and 5 git commands to generate.