summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Nikias Bassen2023-07-05 10:30:52 +0200
committerGravatar Nikias Bassen2023-07-05 10:30:52 +0200
commite57b6e7163277c6a63f22a7e2942cf666cf71a80 (patch)
tree56c083f63bffb238a1aabd8f43a42489c19570d6
parent474fd9284b76d8ddd3a3aec41cbca3cc48271cc1 (diff)
downloadlibimobiledevice-e57b6e7163277c6a63f22a7e2942cf666cf71a80.tar.gz
libimobiledevice-e57b6e7163277c6a63f22a7e2942cf666cf71a80.tar.bz2
Updated OpenSSL-specific code to use OpenSSL 3.0+ API
-rw-r--r--common/userpref.c25
-rw-r--r--src/idevice.c34
-rw-r--r--tools/idevicebackup.c18
3 files changed, 69 insertions, 8 deletions
diff --git a/common/userpref.c b/common/userpref.c
index 11e28ba..b64c703 100644
--- a/common/userpref.c
+++ b/common/userpref.c
@@ -435,6 +435,10 @@ userpref_error_t pair_record_generate_keys_and_certs(plist_t pair_record, key_da
435 debug_info("Generating keys and certificates..."); 435 debug_info("Generating keys and certificates...");
436 436
437#if defined(HAVE_OPENSSL) 437#if defined(HAVE_OPENSSL)
438#if OPENSSL_VERSION_NUMBER >= 0x30000000L
439 EVP_PKEY* root_pkey = EVP_RSA_gen(2048);
440 EVP_PKEY* host_pkey = EVP_RSA_gen(2048);
441#else
438 BIGNUM *e = BN_new(); 442 BIGNUM *e = BN_new();
439 RSA* root_keypair = RSA_new(); 443 RSA* root_keypair = RSA_new();
440 RSA* host_keypair = RSA_new(); 444 RSA* host_keypair = RSA_new();
@@ -451,6 +455,7 @@ userpref_error_t pair_record_generate_keys_and_certs(plist_t pair_record, key_da
451 455
452 EVP_PKEY* host_pkey = EVP_PKEY_new(); 456 EVP_PKEY* host_pkey = EVP_PKEY_new();
453 EVP_PKEY_assign_RSA(host_pkey, host_keypair); 457 EVP_PKEY_assign_RSA(host_pkey, host_keypair);
458#endif
454 459
455 /* generate root certificate */ 460 /* generate root certificate */
456 X509* root_cert = X509_new(); 461 X509* root_cert = X509_new();
@@ -561,12 +566,22 @@ userpref_error_t pair_record_generate_keys_and_certs(plist_t pair_record, key_da
561 } 566 }
562 } 567 }
563 568
564 RSA *pubkey = NULL; 569 EVP_PKEY *pubkey = NULL;
565 { 570 {
566 BIO *membp = BIO_new_mem_buf(public_key.data, public_key.size); 571 BIO *membp = BIO_new_mem_buf(public_key.data, public_key.size);
567 if (!PEM_read_bio_RSAPublicKey(membp, &pubkey, NULL, NULL)) { 572#if OPENSSL_VERSION_NUMBER >= 0x30000000L
573 if (!PEM_read_bio_PUBKEY(membp, &pubkey, NULL, NULL)) {
568 debug_info("WARNING: Could not read public key"); 574 debug_info("WARNING: Could not read public key");
569 } 575 }
576#else
577 RSA *rsa_pubkey = NULL;
578 if (!PEM_read_bio_RSAPublicKey(membp, &rsa_pubkey, NULL, NULL)) {
579 debug_info("WARNING: Could not read public key");
580 } else {
581 pubkey = EVP_PKEY_new();
582 EVP_PKEY_assign_RSA(pubkey, rsa_pubkey);
583 }
584#endif
570 BIO_free(membp); 585 BIO_free(membp);
571 } 586 }
572 587
@@ -588,10 +603,7 @@ userpref_error_t pair_record_generate_keys_and_certs(plist_t pair_record, key_da
588 X509_set1_notAfter(dev_cert, asn1time); 603 X509_set1_notAfter(dev_cert, asn1time);
589 ASN1_TIME_free(asn1time); 604 ASN1_TIME_free(asn1time);
590 605
591 EVP_PKEY* pkey = EVP_PKEY_new(); 606 X509_set_pubkey(dev_cert, pubkey);
592 EVP_PKEY_assign_RSA(pkey, pubkey);
593 X509_set_pubkey(dev_cert, pkey);
594 EVP_PKEY_free(pkey);
595 607
596 X509_add_ext_helper(dev_cert, NID_subject_key_identifier, (char*)"hash"); 608 X509_add_ext_helper(dev_cert, NID_subject_key_identifier, (char*)"hash");
597 X509_add_ext_helper(dev_cert, NID_key_usage, (char*)"critical,digitalSignature,keyEncipherment"); 609 X509_add_ext_helper(dev_cert, NID_key_usage, (char*)"critical,digitalSignature,keyEncipherment");
@@ -618,6 +630,7 @@ userpref_error_t pair_record_generate_keys_and_certs(plist_t pair_record, key_da
618 X509V3_EXT_cleanup(); 630 X509V3_EXT_cleanup();
619 X509_free(dev_cert); 631 X509_free(dev_cert);
620 632
633 EVP_PKEY_free(pubkey);
621 EVP_PKEY_free(root_pkey); 634 EVP_PKEY_free(root_pkey);
622 EVP_PKEY_free(host_pkey); 635 EVP_PKEY_free(host_pkey);
623 636
diff --git a/src/idevice.c b/src/idevice.c
index 8545317..719cd28 100644
--- a/src/idevice.c
+++ b/src/idevice.c
@@ -1057,18 +1057,33 @@ static void internal_ssl_cleanup(ssl_data_t ssl_data)
1057} 1057}
1058 1058
1059#ifdef HAVE_OPENSSL 1059#ifdef HAVE_OPENSSL
1060#if OPENSSL_VERSION_NUMBER >= 0x30000000L
1061static long ssl_idevice_bio_callback(BIO *b, int oper, const char *argp, size_t len, int argi, long argl, int retvalue, size_t *processed)
1062#else
1060static long ssl_idevice_bio_callback(BIO *b, int oper, const char *argp, int argi, long argl, long retvalue) 1063static long ssl_idevice_bio_callback(BIO *b, int oper, const char *argp, int argi, long argl, long retvalue)
1064#endif
1061{ 1065{
1066 ssize_t bytes = 0;
1062 idevice_connection_t conn = (idevice_connection_t)BIO_get_callback_arg(b); 1067 idevice_connection_t conn = (idevice_connection_t)BIO_get_callback_arg(b);
1068#if OPENSSL_VERSION_NUMBER < 0x30000000L
1063 size_t len = (size_t)argi; 1069 size_t len = (size_t)argi;
1070 size_t *processed = (size_t*)&bytes;
1071#endif
1064 switch (oper) { 1072 switch (oper) {
1065 case (BIO_CB_READ|BIO_CB_RETURN): 1073 case (BIO_CB_READ|BIO_CB_RETURN):
1066 return argp ? (long)internal_ssl_read(conn, (char *)argp, len) : 0; 1074 if (argp) {
1075 bytes = internal_ssl_read(conn, (char *)argp, len);
1076 *processed = bytes;
1077 return (long)bytes;
1078 }
1079 return 0;
1067 case (BIO_CB_PUTS|BIO_CB_RETURN): 1080 case (BIO_CB_PUTS|BIO_CB_RETURN):
1068 len = strlen(argp); 1081 len = strlen(argp);
1069 // fallthrough 1082 // fallthrough
1070 case (BIO_CB_WRITE|BIO_CB_RETURN): 1083 case (BIO_CB_WRITE|BIO_CB_RETURN):
1071 return (long)internal_ssl_write(conn, argp, len); 1084 bytes = internal_ssl_write(conn, argp, len);
1085 *processed = bytes;
1086 return (long)bytes;
1072 default: 1087 default:
1073 return retvalue; 1088 return retvalue;
1074 } 1089 }
@@ -1079,7 +1094,11 @@ static BIO *ssl_idevice_bio_new(idevice_connection_t conn)
1079 BIO *b = BIO_new(BIO_s_null()); 1094 BIO *b = BIO_new(BIO_s_null());
1080 if (!b) return NULL; 1095 if (!b) return NULL;
1081 BIO_set_callback_arg(b, (char *)conn); 1096 BIO_set_callback_arg(b, (char *)conn);
1097#if OPENSSL_VERSION_NUMBER >= 0x30000000L
1098 BIO_set_callback_ex(b, ssl_idevice_bio_callback);
1099#else
1082 BIO_set_callback(b, ssl_idevice_bio_callback); 1100 BIO_set_callback(b, ssl_idevice_bio_callback);
1101#endif
1083 return b; 1102 return b;
1084} 1103}
1085 1104
@@ -1257,6 +1276,16 @@ LIBIMOBILEDEVICE_API idevice_error_t idevice_connection_enable_ssl(idevice_conne
1257 X509_free(rootCert); 1276 X509_free(rootCert);
1258 free(root_cert.data); 1277 free(root_cert.data);
1259 1278
1279#if OPENSSL_VERSION_NUMBER >= 0x30000000L
1280 EVP_PKEY* rootPrivKey = NULL;
1281 membp = BIO_new_mem_buf(root_privkey.data, root_privkey.size);
1282 PEM_read_bio_PrivateKey(membp, &rootPrivKey, NULL, NULL);
1283 BIO_free(membp);
1284 if (SSL_CTX_use_PrivateKey(ssl_ctx, rootPrivKey) != 1) {
1285 debug_info("WARNING: Could not load RootPrivateKey");
1286 }
1287 EVP_PKEY_free(rootPrivKey);
1288#else
1260 RSA* rootPrivKey = NULL; 1289 RSA* rootPrivKey = NULL;
1261 membp = BIO_new_mem_buf(root_privkey.data, root_privkey.size); 1290 membp = BIO_new_mem_buf(root_privkey.data, root_privkey.size);
1262 PEM_read_bio_RSAPrivateKey(membp, &rootPrivKey, NULL, NULL); 1291 PEM_read_bio_RSAPrivateKey(membp, &rootPrivKey, NULL, NULL);
@@ -1265,6 +1294,7 @@ LIBIMOBILEDEVICE_API idevice_error_t idevice_connection_enable_ssl(idevice_conne
1265 debug_info("WARNING: Could not load RootPrivateKey"); 1294 debug_info("WARNING: Could not load RootPrivateKey");
1266 } 1295 }
1267 RSA_free(rootPrivKey); 1296 RSA_free(rootPrivKey);
1297#endif
1268 free(root_privkey.data); 1298 free(root_privkey.data);
1269 1299
1270 SSL *ssl = SSL_new(ssl_ctx); 1300 SSL *ssl = SSL_new(ssl_ctx);
diff --git a/tools/idevicebackup.c b/tools/idevicebackup.c
index 1684666..5694c12 100644
--- a/tools/idevicebackup.c
+++ b/tools/idevicebackup.c
@@ -34,6 +34,9 @@
34#include <getopt.h> 34#include <getopt.h>
35#if defined(HAVE_OPENSSL) 35#if defined(HAVE_OPENSSL)
36#include <openssl/sha.h> 36#include <openssl/sha.h>
37#if OPENSSL_VERSION_NUMBER >= 0x30000000L
38#include <openssl/evp.h>
39#endif
37#elif defined(HAVE_GNUTLS) 40#elif defined(HAVE_GNUTLS)
38#include <gcrypt.h> 41#include <gcrypt.h>
39#elif defined(HAVE_MBEDTLS) 42#elif defined(HAVE_MBEDTLS)
@@ -113,7 +116,11 @@ static int compare_hash(const unsigned char *hash1, const unsigned char *hash2,
113static void _sha1_update(void* context, const char* data, size_t len) 116static void _sha1_update(void* context, const char* data, size_t len)
114{ 117{
115#if defined(HAVE_OPENSSL) 118#if defined(HAVE_OPENSSL)
119#if OPENSSL_VERSION_NUMBER >= 0x30000000L
120 EVP_DigestUpdate(context, data, len);
121#else
116 SHA1_Update(context, data, len); 122 SHA1_Update(context, data, len);
123#endif
117#elif defined(HAVE_GNUTLS) 124#elif defined(HAVE_GNUTLS)
118 gcry_md_write(context, data, len); 125 gcry_md_write(context, data, len);
119#elif defined(HAVE_MBEDTLS) 126#elif defined(HAVE_MBEDTLS)
@@ -124,9 +131,15 @@ static void _sha1_update(void* context, const char* data, size_t len)
124static void compute_datahash(const char *path, const char *destpath, uint8_t greylist, const char *domain, const char *appid, const char *version, unsigned char *hash_out) 131static void compute_datahash(const char *path, const char *destpath, uint8_t greylist, const char *domain, const char *appid, const char *version, unsigned char *hash_out)
125{ 132{
126#if defined(HAVE_OPENSSL) 133#if defined(HAVE_OPENSSL)
134#if OPENSSL_VERSION_NUMBER >= 0x30000000L
135 EVP_MD_CTX* sha1 = EVP_MD_CTX_new();
136 EVP_DigestInit(sha1, EVP_sha1());
137 void* psha1 = sha1;
138#else
127 SHA_CTX sha1; 139 SHA_CTX sha1;
128 SHA1_Init(&sha1); 140 SHA1_Init(&sha1);
129 void* psha1 = &sha1; 141 void* psha1 = &sha1;
142#endif
130#elif defined(HAVE_GNUTLS) 143#elif defined(HAVE_GNUTLS)
131 gcry_md_hd_t hd = NULL; 144 gcry_md_hd_t hd = NULL;
132 gcry_md_open(&hd, GCRY_MD_SHA1, 0); 145 gcry_md_open(&hd, GCRY_MD_SHA1, 0);
@@ -180,7 +193,12 @@ static void compute_datahash(const char *path, const char *destpath, uint8_t gre
180 _sha1_update(psha1, "(null)", 6); 193 _sha1_update(psha1, "(null)", 6);
181 } 194 }
182#if defined(HAVE_OPENSSL) 195#if defined(HAVE_OPENSSL)
196#if OPENSSL_VERSION_NUMBER >= 0x30000000L
197 EVP_DigestFinal(sha1, hash_out, NULL);
198 EVP_MD_CTX_destroy(sha1);
199#else
183 SHA1_Final(hash_out, &sha1); 200 SHA1_Final(hash_out, &sha1);
201#endif
184#elif defined(HAVE_GNUTLS) 202#elif defined(HAVE_GNUTLS)
185 unsigned char *newhash = gcry_md_read(hd, GCRY_MD_SHA1); 203 unsigned char *newhash = gcry_md_read(hd, GCRY_MD_SHA1);
186 memcpy(hash_out, newhash, 20); 204 memcpy(hash_out, newhash, 20);