mirror of
https://github.com/deltachat/deltachat-core.git
synced 2025-10-05 10:39:27 +02:00
support symm. decryption with 'Encrypted session key' present
This commit is contained in:
parent
7eb96d0178
commit
94fd79aecb
1 changed files with 49 additions and 15 deletions
|
@ -3215,11 +3215,14 @@ uint8_t* pgp_s2k_do(const char* passphrase,
|
|||
// EDIT BY MR - parse Symmetric-Key Encrypted Session Key Packets (Tag 3)
|
||||
static int parse_sk_sesskey(pgp_region_t *region, pgp_stream_t *stream)
|
||||
{
|
||||
int success = 0;
|
||||
uint8_t version = 0, algo = 0;
|
||||
uint8_t s2k_spec = 0, s2k_hash_algo = 0, s2k_salt[PGP_SALT_SIZE], s2k_iter_id = 0;
|
||||
uint8_t *iv;
|
||||
uint8_t *key = NULL;
|
||||
int success = 0;
|
||||
uint8_t version = 0, algo = 0;
|
||||
pgp_crypt_t ci;
|
||||
uint8_t s2k_spec = 0, s2k_hash_algo = 0, s2k_salt[PGP_SALT_SIZE], s2k_iter_id = 0;
|
||||
uint8_t* encr_session_key = NULL, *decr_session_key = NULL;
|
||||
int session_key_bytes = 0;
|
||||
uint8_t* iv = NULL;
|
||||
uint8_t* key = NULL;
|
||||
|
||||
if( region == NULL || stream == NULL || stream->cbinfo.cryptinfo.symm_passphrase == NULL ) {
|
||||
goto cleanup;
|
||||
|
@ -3251,13 +3254,12 @@ static int parse_sk_sesskey(pgp_region_t *region, pgp_stream_t *stream)
|
|||
}
|
||||
|
||||
/* calculate the key from the passphrase */
|
||||
{
|
||||
pgp_crypt_t temp_crypt_info;
|
||||
pgp_crypt_any(&temp_crypt_info, algo);
|
||||
if( (key = pgp_s2k_do(stream->cbinfo.cryptinfo.symm_passphrase, temp_crypt_info.keysize,
|
||||
s2k_spec, s2k_hash_algo, s2k_salt, s2k_iter_id)) == NULL ) {
|
||||
goto cleanup;
|
||||
}
|
||||
if( !pgp_crypt_any(&ci, algo) ) {
|
||||
goto cleanup;
|
||||
}
|
||||
if( (key = pgp_s2k_do(stream->cbinfo.cryptinfo.symm_passphrase, ci.keysize,
|
||||
s2k_spec, s2k_hash_algo, s2k_salt, s2k_iter_id)) == NULL ) {
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
/* set up stream->decrypt so that PGP_PTAG_CT_SE_IP_DATA can decrypt the data
|
||||
|
@ -3271,7 +3273,35 @@ static int parse_sk_sesskey(pgp_region_t *region, pgp_stream_t *stream)
|
|||
algorithm identifier that specifies the symmetric-key encryption
|
||||
algorithm used to encrypt the following Symmetrically Encrypted Data
|
||||
packet, followed by the session key octets themselves. */
|
||||
goto cleanup; // TODO
|
||||
|
||||
/* read the data */
|
||||
session_key_bytes = region->length - region->readc;
|
||||
if( (encr_session_key=malloc(session_key_bytes)) == NULL
|
||||
|| (decr_session_key=malloc(session_key_bytes)) == NULL
|
||||
|| !limread(encr_session_key, session_key_bytes, region, stream) ) {
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
/* decrypt the read data */
|
||||
iv = calloc(1, stream->decrypt.blocksize); if( iv == NULL ) { goto cleanup; }
|
||||
ci.set_iv(&ci, iv);
|
||||
free(iv);
|
||||
iv = NULL;
|
||||
ci.set_crypt_key(&ci, key);
|
||||
pgp_encrypt_init(&ci);
|
||||
pgp_decrypt_se_ip(&ci, decr_session_key, encr_session_key, session_key_bytes); // TODO/TOCHECK: or pgp_decrypt_se()?
|
||||
ci.decrypt_finish(&ci);
|
||||
|
||||
/* use decrypted data as key */
|
||||
algo = decr_session_key[0];
|
||||
if( !pgp_crypt_any(&stream->decrypt, algo)
|
||||
|| stream->decrypt.keysize != (session_key_bytes-1/*skip algo identifier*/) ) {
|
||||
goto cleanup;
|
||||
}
|
||||
iv = calloc(1, stream->decrypt.blocksize); if( iv == NULL ) { goto cleanup; }
|
||||
stream->decrypt.set_iv(&stream->decrypt, iv);
|
||||
stream->decrypt.set_crypt_key(&stream->decrypt, &decr_session_key[1]/*skip algo identifier*/);
|
||||
pgp_encrypt_init(&stream->decrypt);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -3279,12 +3309,13 @@ static int parse_sk_sesskey(pgp_region_t *region, pgp_stream_t *stream)
|
|||
"[...] The S2K algorithm applied to the passphrase produces the session
|
||||
key for decrypting the file, using the symmetric cipher algorithm from
|
||||
the Symmetric-Key Encrypted Session Key packet." */
|
||||
pgp_crypt_any(&stream->decrypt, algo);
|
||||
if( !pgp_crypt_any(&stream->decrypt, algo) ) {
|
||||
goto cleanup;
|
||||
}
|
||||
iv = calloc(1, stream->decrypt.blocksize); if( iv == NULL ) { goto cleanup; }
|
||||
stream->decrypt.set_iv(&stream->decrypt, iv);
|
||||
stream->decrypt.set_crypt_key(&stream->decrypt, key);
|
||||
pgp_encrypt_init(&stream->decrypt);
|
||||
free(iv);
|
||||
}
|
||||
|
||||
success = 1;
|
||||
|
@ -3292,6 +3323,9 @@ static int parse_sk_sesskey(pgp_region_t *region, pgp_stream_t *stream)
|
|||
|
||||
cleanup:
|
||||
free(key);
|
||||
free(iv);
|
||||
free(encr_session_key);
|
||||
free(decr_session_key);
|
||||
return success;
|
||||
}
|
||||
// /EDIT BY MR
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue