@@ -40,6 +40,8 @@ along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
4040#include < openssl/x509.h>
4141#include < openssl/pkcs7.h>
4242#include < openssl/asn1t.h>
43+ #include < openssl/bn.h>
44+ #include < openssl/rsa.h>
4345
4446// //////////////////////////////////////////////////////////////////////////////
4547
@@ -1488,27 +1490,27 @@ bool MCDeploySignLoadPVK(MCStringRef p_filename, MCStringRef p_passphrase, EVP_P
14881490 // Compute the passkey. This is done by taking the first 16 bytes
14891491 // of SHA1(salt & passphrase).
14901492 uint8_t t_passkey[EVP_MAX_KEY_LENGTH ];
1491- EVP_MD_CTX t_md;
1492- if (t_success && EVP_DigestInit (& t_md, EVP_sha1 ()))
1493+ MCAutoCustomPointer< EVP_MD_CTX , EVP_MD_CTX_free> t_md = EVP_MD_CTX_new () ;
1494+ if (t_success && EVP_DigestInit (* t_md, EVP_sha1 ()))
14931495 {
14941496 MCAutoStringRefAsCString t_passphrase;
14951497 t_success = t_passphrase . Lock (p_passphrase);
14961498
1497- EVP_DigestUpdate (& t_md, t_salt, t_header . salt_length);
1498- EVP_DigestUpdate (& t_md, *t_passphrase, strlen (*t_passphrase));
1499- EVP_DigestFinal (& t_md, t_passkey, NULL );
1499+ EVP_DigestUpdate (* t_md, t_salt, t_header . salt_length);
1500+ EVP_DigestUpdate (* t_md, *t_passphrase, strlen (*t_passphrase));
1501+ EVP_DigestFinal (* t_md, t_passkey, NULL );
15001502 }
15011503
15021504 // Now, first we see if the PVK can be decrypted using the strong form
15031505 // of password generation - that is the first 16 bytes of the hash we
15041506 // just made.
1505- EVP_CIPHER_CTX t_cipher;
1507+ MCAutoCustomPointer< EVP_CIPHER_CTX , EVP_CIPHER_CTX_free> t_cipher = EVP_CIPHER_CTX_new () ;
15061508 int t_cipher_output;
15071509 t_cipher_output = 0 ;
1508- if (t_success && EVP_DecryptInit (& t_cipher, EVP_rc4 (), t_passkey, nil))
1510+ if (t_success && EVP_DecryptInit (* t_cipher, EVP_rc4 (), t_passkey, nil))
15091511 {
1510- EVP_DecryptUpdate (& t_cipher, t_new_key_data, &t_cipher_output, t_key_data, t_header . key_length);
1511- EVP_DecryptFinal (& t_cipher, t_new_key_data + t_cipher_output, &t_cipher_output);
1512+ EVP_DecryptUpdate (* t_cipher, t_new_key_data, &t_cipher_output, t_key_data, t_header . key_length);
1513+ EVP_DecryptFinal (* t_cipher, t_new_key_data + t_cipher_output, &t_cipher_output);
15121514 }
15131515
15141516 // Check to see if 'RSA2' is the first four bytes of the output, and if
@@ -1518,10 +1520,10 @@ bool MCDeploySignLoadPVK(MCStringRef p_filename, MCStringRef p_passphrase, EVP_P
15181520 {
15191521 t_cipher_output = 0 ;
15201522 MCMemoryClear (t_passkey + 5 , 11 );
1521- if (EVP_DecryptInit (& t_cipher, EVP_rc4 (), t_passkey, nil))
1523+ if (EVP_DecryptInit (* t_cipher, EVP_rc4 (), t_passkey, nil))
15221524 {
1523- EVP_DecryptUpdate (& t_cipher, t_new_key_data, &t_cipher_output, t_key_data, t_header . key_length);
1524- EVP_DecryptFinal (& t_cipher, t_new_key_data + t_cipher_output, &t_cipher_output);
1525+ EVP_DecryptUpdate (* t_cipher, t_new_key_data, &t_cipher_output, t_key_data, t_header . key_length);
1526+ EVP_DecryptFinal (* t_cipher, t_new_key_data + t_cipher_output, &t_cipher_output);
15251527 }
15261528 }
15271529
@@ -1575,24 +1577,36 @@ bool MCDeploySignLoadPVK(MCStringRef p_filename, MCStringRef p_passphrase, EVP_P
15751577 // byte sequences in little-endian order.
15761578
15771579 // The exponent is first - this is just a 32-bit number, so we deal with it explicitly.
1580+ typedef MCAutoCustomPointer<BIGNUM , BN_free> MCAutoBignum;
1581+ MCAutoBignum t_rsa_e;
15781582 if (t_success)
15791583 {
1580- t_rsa -> e = BN_new ();
1581- if (t_rsa -> e == nil ||
1582- !BN_set_word (t_rsa -> e, t_rsa_header . exponent))
1584+ t_rsa_e = BN_new ();
1585+ if (!t_rsa_e || !BN_set_word (&t_rsa_e, t_rsa_header.exponent ))
15831586 t_success = MCDeployThrowOpenSSL (kMCDeployErrorNoMemory );
15841587 }
15851588
15861589 // The rest of the numbers for the RSA2 key need special processing.
1590+ MCAutoBignum t_rsa_n;
1591+ MCAutoBignum t_rsa_p;
1592+ MCAutoBignum t_rsa_q;
1593+ MCAutoBignum t_rsa_dmp1;
1594+ MCAutoBignum t_rsa_dmq1;
1595+ MCAutoBignum t_rsa_iqmp;
1596+ MCAutoBignum t_rsa_d;
15871597 if (t_success)
1588- if (!read_le_bignum (t_rsa_data, t_key_byte_length, t_rsa -> n ) ||
1589- !read_le_bignum (t_rsa_data, t_key_byte_length / 2 , t_rsa -> p ) ||
1590- !read_le_bignum (t_rsa_data, t_key_byte_length / 2 , t_rsa -> q ) ||
1591- !read_le_bignum (t_rsa_data, t_key_byte_length / 2 , t_rsa -> dmp1 ) ||
1592- !read_le_bignum (t_rsa_data, t_key_byte_length / 2 , t_rsa -> dmq1 ) ||
1593- !read_le_bignum (t_rsa_data, t_key_byte_length / 2 , t_rsa -> iqmp ) ||
1594- !read_le_bignum (t_rsa_data, t_key_byte_length, t_rsa -> d ))
1598+ if (!read_le_bignum (t_rsa_data, t_key_byte_length, &t_rsa_n ) ||
1599+ !read_le_bignum (t_rsa_data, t_key_byte_length / 2 , &t_rsa_p ) ||
1600+ !read_le_bignum (t_rsa_data, t_key_byte_length / 2 , &t_rsa_q ) ||
1601+ !read_le_bignum (t_rsa_data, t_key_byte_length / 2 , &t_rsa_dmp1 ) ||
1602+ !read_le_bignum (t_rsa_data, t_key_byte_length / 2 , &t_rsa_dmq1 ) ||
1603+ !read_le_bignum (t_rsa_data, t_key_byte_length / 2 , &t_rsa_iqmp ) ||
1604+ !read_le_bignum (t_rsa_data, t_key_byte_length, &t_rsa_d ))
15951605 t_success = MCDeployThrowOpenSSL (kMCDeployErrorNoMemory );
1606+
1607+ RSA_set0_key (t_rsa, t_rsa_n.Release (), t_rsa_e.Release (), t_rsa_d.Release ());
1608+ RSA_set0_factors (t_rsa, t_rsa_p.Release (), t_rsa_q.Release ());
1609+ RSA_set0_crt_params (t_rsa, t_rsa_dmp1.Release (), t_rsa_dmq1.Release (), t_rsa_iqmp.Release ());
15961610
15971611 // We now have the RSA key, so wrap it in an EVP_PKEY and return.
15981612 EVP_PKEY *t_pkey;
0 commit comments