|
|
|
@ -1011,6 +1011,7 @@ pgp_push_enc_crypt(pgp_output_t *output, pgp_crypt_t *pgp_crypt)
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
pgp_crypt_t *crypt;
|
|
|
|
|
unsigned raw;
|
|
|
|
|
} encrypt_se_ip_t;
|
|
|
|
|
|
|
|
|
|
static unsigned encrypt_se_ip_writer(const uint8_t *,
|
|
|
|
@ -1026,24 +1027,39 @@ static void encrypt_se_ip_destroyer(pgp_writer_t *);
|
|
|
|
|
\brief Push Encrypted SE IP Writer onto stack
|
|
|
|
|
*/
|
|
|
|
|
int
|
|
|
|
|
pgp_push_enc_se_ip(pgp_output_t *output, const pgp_key_t *pubkey, const char *cipher)
|
|
|
|
|
pgp_push_enc_se_ip(pgp_output_t *output, const pgp_keyring_t *pubkeys, const char *cipher, unsigned raw)
|
|
|
|
|
{
|
|
|
|
|
pgp_pk_sesskey_t *initial_sesskey = NULL;
|
|
|
|
|
pgp_pk_sesskey_t *encrypted_pk_sesskey;
|
|
|
|
|
encrypt_se_ip_t *se_ip;
|
|
|
|
|
pgp_crypt_t *encrypted;
|
|
|
|
|
uint8_t *iv;
|
|
|
|
|
unsigned n;
|
|
|
|
|
|
|
|
|
|
if ((se_ip = calloc(1, sizeof(*se_ip))) == NULL) {
|
|
|
|
|
(void) fprintf(stderr, "pgp_push_enc_se_ip: bad alloc\n");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Create and write encrypted PK session key */
|
|
|
|
|
if ((encrypted_pk_sesskey = pgp_create_pk_sesskey(pubkey, cipher)) == NULL) {
|
|
|
|
|
(void) fprintf(stderr, "pgp_push_enc_se_ip: null pk sesskey\n");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
pgp_write_pk_sesskey(output, encrypted_pk_sesskey);
|
|
|
|
|
for (n = 0; n < pubkeys->keyc; ++n) {
|
|
|
|
|
/* Create and write encrypted PK session key */
|
|
|
|
|
if ((encrypted_pk_sesskey =
|
|
|
|
|
pgp_create_pk_sesskey(&pubkeys->keys[n],
|
|
|
|
|
cipher, initial_sesskey)) == NULL) {
|
|
|
|
|
(void) fprintf(stderr, "pgp_push_enc_se_ip: null pk sesskey\n");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (initial_sesskey == NULL) {
|
|
|
|
|
initial_sesskey = encrypted_pk_sesskey;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pgp_write_pk_sesskey(output, encrypted_pk_sesskey);
|
|
|
|
|
|
|
|
|
|
if(encrypted_pk_sesskey != initial_sesskey){
|
|
|
|
|
free(encrypted_pk_sesskey);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Setup the se_ip */
|
|
|
|
|
if ((encrypted = calloc(1, sizeof(*encrypted))) == NULL) {
|
|
|
|
@ -1051,7 +1067,7 @@ pgp_push_enc_se_ip(pgp_output_t *output, const pgp_key_t *pubkey, const char *ci
|
|
|
|
|
(void) fprintf(stderr, "pgp_push_enc_se_ip: bad alloc\n");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
pgp_crypt_any(encrypted, encrypted_pk_sesskey->symm_alg);
|
|
|
|
|
pgp_crypt_any(encrypted, initial_sesskey->symm_alg);
|
|
|
|
|
if ((iv = calloc(1, encrypted->blocksize)) == NULL) {
|
|
|
|
|
free(se_ip);
|
|
|
|
|
free(encrypted);
|
|
|
|
@ -1059,16 +1075,17 @@ pgp_push_enc_se_ip(pgp_output_t *output, const pgp_key_t *pubkey, const char *ci
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
encrypted->set_iv(encrypted, iv);
|
|
|
|
|
encrypted->set_crypt_key(encrypted, &encrypted_pk_sesskey->key[0]);
|
|
|
|
|
encrypted->set_crypt_key(encrypted, &initial_sesskey->key[0]);
|
|
|
|
|
pgp_encrypt_init(encrypted);
|
|
|
|
|
|
|
|
|
|
se_ip->crypt = encrypted;
|
|
|
|
|
se_ip->raw = raw;
|
|
|
|
|
|
|
|
|
|
/* And push writer on stack */
|
|
|
|
|
pgp_writer_push(output, encrypt_se_ip_writer, NULL,
|
|
|
|
|
encrypt_se_ip_destroyer, se_ip);
|
|
|
|
|
/* tidy up */
|
|
|
|
|
free(encrypted_pk_sesskey);
|
|
|
|
|
free(initial_sesskey);
|
|
|
|
|
free(iv);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
@ -1089,19 +1106,29 @@ encrypt_se_ip_writer(const uint8_t *src,
|
|
|
|
|
pgp_memory_t *localmem;
|
|
|
|
|
unsigned ret = 1;
|
|
|
|
|
|
|
|
|
|
const uint8_t *zsrc;
|
|
|
|
|
unsigned zsrclen;
|
|
|
|
|
|
|
|
|
|
pgp_setup_memory_write(&litoutput, &litmem, bufsz);
|
|
|
|
|
pgp_setup_memory_write(&zoutput, &zmem, bufsz);
|
|
|
|
|
pgp_setup_memory_write(&output, &localmem, bufsz);
|
|
|
|
|
|
|
|
|
|
/* create literal data packet from source data */
|
|
|
|
|
pgp_write_litdata(litoutput, src, (const int)len, PGP_LDT_BINARY);
|
|
|
|
|
if (pgp_mem_len(litmem) <= len) {
|
|
|
|
|
(void) fprintf(stderr, "encrypt_se_ip_writer: bad len\n");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
if (!se_ip->raw) {
|
|
|
|
|
/* create literal data packet from source data */
|
|
|
|
|
pgp_write_litdata(litoutput, src, (const int)len, PGP_LDT_BINARY);
|
|
|
|
|
if (pgp_mem_len(litmem) <= len) {
|
|
|
|
|
(void) fprintf(stderr, "encrypt_se_ip_writer: bad len\n");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
zsrc = pgp_mem_data(litmem);
|
|
|
|
|
zsrclen = (unsigned)pgp_mem_len(litmem);
|
|
|
|
|
}else{
|
|
|
|
|
zsrc = src;
|
|
|
|
|
zsrclen = len;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* create compressed packet from literal data packet */
|
|
|
|
|
pgp_writez(zoutput, pgp_mem_data(litmem), (unsigned)pgp_mem_len(litmem));
|
|
|
|
|
pgp_writez(zoutput, zsrc, zsrclen);
|
|
|
|
|
|
|
|
|
|
/* create SE IP packet set from this compressed literal data */
|
|
|
|
|
pgp_write_se_ip_pktset(output, pgp_mem_data(zmem),
|
|
|
|
@ -1425,7 +1452,7 @@ pgp_push_stream_enc_se_ip(pgp_output_t *output, const pgp_key_t *pubkey, const c
|
|
|
|
|
"pgp_push_stream_enc_se_ip: bad alloc\n");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
encrypted_pk_sesskey = pgp_create_pk_sesskey(pubkey, cipher);
|
|
|
|
|
encrypted_pk_sesskey = pgp_create_pk_sesskey(pubkey, cipher, NULL);
|
|
|
|
|
pgp_write_pk_sesskey(output, encrypted_pk_sesskey);
|
|
|
|
|
|
|
|
|
|
/* Setup the se_ip */
|
|
|
|
|