Browse Source

Merge pull request #203 from lzha101/openssl_compat

Fix OpenSSL 1.1.0 compatibility issue in sign_tool and urts_sim

Signed-off-by: Zhang Lili Z lili.z.zhang@intel.com
lzha101 6 years ago
parent
commit
3ccaec34f4

+ 20 - 2
sdk/sign_tool/SignTool/parse_key_file.cpp

@@ -47,6 +47,22 @@
 #include <assert.h>
 #include <openssl/pem.h>
 
+
+#if OPENSSL_VERSION_NUMBER < 0x10100000L
+void RSA_get0_key(const RSA *rsa, const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
+{
+    assert(rsa != NULL);
+
+    if(n != NULL)
+        *n = rsa->n;
+    if(e != NULL)
+        *e = rsa->e;
+    if(d != NULL)
+        *d = rsa->d;
+}
+#endif
+
+
 //parse_key_file():
 //       parse the RSA key file
 //Return Value:
@@ -100,13 +116,15 @@ bool parse_key_file(int mode, const char *key_path, RSA **prsa, int *pkey_type)
     }
 
     // Check the key size and exponent
-    if(BN_num_bytes(rsa->n) != N_SIZE_IN_BYTES)
+    const BIGNUM *n = NULL, *e = NULL;
+    RSA_get0_key(rsa, &n, &e, NULL);
+    if(BN_num_bytes(n) != N_SIZE_IN_BYTES)
     {
         se_trace(SE_TRACE_ERROR, INVALID_KEYSIZE_ERROR);
         RSA_free(rsa);
         return false;
     }
-    char *p = BN_bn2dec(rsa->e);
+    char *p = BN_bn2dec(e);
     if(memcmp(p, "3", 2))
     {
         se_trace(SE_TRACE_ERROR, INVALID_EXPONENT_ERROR);

+ 4 - 1
sdk/sign_tool/SignTool/parse_key_file.h

@@ -34,6 +34,7 @@
 #ifndef _PARSE_KEY_FILE_H_
 #define _PARSE_KEY_FILE_H_
 
+#include <openssl/rsa.h>
 
 #define N_SIZE_IN_BYTES    384
 #define E_SIZE_IN_BYTES    4
@@ -47,7 +48,9 @@ typedef enum _key_type_t
     PUBLIC_KEY 
 } key_type_t;
 
-#include <openssl/rsa.h>
+#if OPENSSL_VERSION_NUMBER < 0x10100000L
+void RSA_get0_key(const RSA *rsa, const BIGNUM **n, const BIGNUM **e, const BIGNUM **d);
+#endif
 
 bool parse_key_file(int mode, const char *key_path, RSA **prsa, int *pkey_type);
 

+ 13 - 5
sdk/sign_tool/SignTool/sign_tool.cpp

@@ -47,6 +47,7 @@
 #include <openssl/rsa.h>
 #include <openssl/evp.h>
 #include <openssl/err.h>
+#include <openssl/crypto.h>
 
 #include "metadata.h"
 #include "manage_metadata.h"
@@ -245,8 +246,10 @@ static bool fill_enclave_css(const RSA *rsa, const char **path,
     //if rsa is not NULL, fill the public key part
     if(rsa)
     {
-        int exponent_size = BN_num_bytes(rsa->e);
-        int modulus_size = BN_num_bytes(rsa->n);
+        const BIGNUM *e = NULL, *n = NULL;
+        RSA_get0_key(rsa, &n, &e, NULL);
+        int exponent_size = BN_num_bytes(e);
+        int modulus_size = BN_num_bytes(n);
         
         if(modulus_size > SE_KEY_SIZE)
             return false;
@@ -260,12 +263,12 @@ static bool fill_enclave_css(const RSA *rsa, const char **path,
         exponent_size = (uint32_t)(ROUND_TO(exponent_size, sizeof(uint32_t)) / sizeof(uint32_t));
         modulus_size = (uint32_t)(ROUND_TO(modulus_size, sizeof(uint32_t)) / sizeof(uint32_t));
         
-        if(BN_bn2bin(rsa->n, modulus) != SE_KEY_SIZE)
+        if(BN_bn2bin(n, modulus) != SE_KEY_SIZE)
         {
             free(modulus);
             return false;
         }
-        if(BN_bn2bin(rsa->e, (unsigned char *)&css->key.exponent) != 1)
+        if(BN_bn2bin(e, (unsigned char *)&css->key.exponent) != 1)
         {
             free(modulus);
             return false;
@@ -1024,8 +1027,12 @@ int main(int argc, char* argv[])
     RSA *rsa = NULL;
     memset(&metadata_raw, 0, sizeof(metadata_raw));
 
+#if OPENSSL_VERSION_NUMBER < 0x10100000L
     OpenSSL_add_all_algorithms();
     ERR_load_crypto_strings();
+#else
+    OPENSSL_init_crypto(0, NULL);
+#endif
 
 
     //Parse command line
@@ -1124,10 +1131,11 @@ clear_return:
     if(res == -1 && path[DUMPFILE])
         remove(path[DUMPFILE]);
     
+#if OPENSSL_VERSION_NUMBER < 0x10100000L    
     EVP_cleanup();
     CRYPTO_cleanup_all_ex_data();
     ERR_remove_thread_state(NULL);
     ERR_free_strings();
-    
+#endif
     return res;
 }

+ 7 - 1
sdk/simulation/urtssim/enclave_creator_sim.cpp

@@ -47,22 +47,28 @@
 
 #include <openssl/evp.h>
 #include <openssl/err.h>
-
+#include <openssl/crypto.h>
 
 __attribute__((constructor))
 static void init_openssl(void)
 {
+#if OPENSSL_VERSION_NUMBER < 0x10100000L
     OpenSSL_add_all_algorithms();
     ERR_load_crypto_strings();
+#else
+    OPENSSL_init_crypto(0, NULL);
+#endif
 }
 
 __attribute__((destructor))
 static void cleanup_openssl(void)
 {
+#if OPENSSL_VERSION_NUMBER < 0x10100000L
     EVP_cleanup();
     CRYPTO_cleanup_all_ex_data();
     ERR_remove_thread_state(NULL);
     ERR_free_strings();
+#endif
 }