Skip to content

Commit 37028aa

Browse files
committed
Merge branch 'pvanstam-hexkeys' into develop
2 parents d1f7cad + fde4152 commit 37028aa

33 files changed

+689
-273
lines changed

src/agency.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,9 @@ liagency_t *copy_liagency(liagency_t *lea) {
7777
if (lea->hi3_ipstr) {
7878
copy->hi3_ipstr = strdup(lea->hi3_ipstr);
7979
}
80-
if (lea->encryptkey) {
81-
copy->encryptkey = strdup(lea->encryptkey);
80+
if (lea->encryptkey_len > 0) {
81+
// should be covered by the original memcpy, but just to be safe
82+
memcpy(copy->encryptkey, lea->encryptkey, lea->encryptkey_len);
8283
}
8384
return copy;
8485
}
@@ -101,9 +102,6 @@ void free_liagency(liagency_t *lea) {
101102
}
102103
if (lea->agencycc) {
103104
free(lea->agencycc);
104-
}
105-
if (lea->encryptkey) {
106-
free(lea->encryptkey);
107105
}
108106
free(lea);
109107
}

src/agency.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ typedef struct liagency {
7272
uint32_t digest_sign_hashlimit;
7373

7474
payload_encryption_method_t encrypt;
75-
char *encryptkey;
75+
uint8_t encryptkey[OPENLI_MAX_ENCRYPTKEY_LEN];
76+
size_t encryptkey_len;
7677
} liagency_t;
7778

7879
#define agency_equal(a, b) \
@@ -86,6 +87,7 @@ typedef struct liagency {
8687
a->handover_retry == b->handover_retry && \
8788
a->resend_window_kbs == b->resend_window_kbs && \
8889
a->digest_required == b->digest_required && \
90+
a->encryptkey_len == b->encryptkey_len && \
8991
a->time_fmt == b->time_fmt && \
9092
((!a->digest_required) || ( \
9193
a->digest_hash_timeout == b->digest_hash_timeout && \
@@ -97,16 +99,14 @@ typedef struct liagency {
9799
((a->agencycc == NULL && b->agencycc == NULL) || \
98100
(a->agencycc != NULL && b->agencycc != NULL && \
99101
strcmp(a->agencycc, b->agencycc) == 0)) && \
100-
((a->encryptkey == NULL && b->encryptkey == NULL) || \
101-
(a->encryptkey != NULL && b->encryptkey != NULL && \
102-
strcmp(a->encryptkey, b->encryptkey) == 0)) && \
103-
a->encrypt == b->encrypt \
102+
((a->encryptkey_len > 0 && b->encryptkey_len > 0) && \
103+
memcmp(a->encryptkey, b->encryptkey, a->encryptkey_len) == 0) \
104104
)
105105

106-
#endif
107106

108107
openli_integrity_hash_method_t map_digest_hash_method_string(char *str);
109108
void free_liagency(liagency_t *ag);
110109
liagency_t *copy_liagency(liagency_t *lea);
110+
#endif
111111

112112
// vim: set sw=4 tabstop=4 softtabstop=4 expandtab :

src/collector/collector.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2642,7 +2642,6 @@ int main(int argc, char *argv[]) {
26422642

26432643
glob->encoders[i].encrypt.byte_counter = 0;
26442644
glob->encoders[i].encrypt.byte_startts = 0;
2645-
glob->encoders[i].encrypt.evp_ctx = NULL;
26462645
glob->encoders[i].encrypt.saved_encryption_templates = NULL;
26472646
glob->encoders[i].seqtrackers = glob->seqtracker_threads;
26482647
glob->encoders[i].forwarders = glob->forwarding_threads;

src/collector/collector_publish.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "collector_publish.h"
3737
#include "emailiri.h"
3838
#include "export_buffer.h"
39+
#include "intercept.h"
3940

4041
int publish_openli_msg(void *pubsock, openli_export_recv_t *msg) {
4142

@@ -82,11 +83,20 @@ openli_export_recv_t *create_intercept_details_msg(intercept_common_t *common,
8283
}
8384
expmsg->data.cept.xid_count = common->xid_count;
8485

85-
if (common->encryptkey) {
86-
expmsg->data.cept.encryptkey = strdup(common->encryptkey);
87-
} else {
86+
if (common->encrypt != OPENLI_PAYLOAD_ENCRYPTION_NONE &&
87+
common->encryptkey_len > 0) {
88+
expmsg->data.cept.encryptkey_len = common->encryptkey_len;
89+
expmsg->data.cept.encryptkey =
90+
openli_dup_encryptkey_ptr(common->encryptkey, common->encryptkey_len);
91+
if (!expmsg->data.cept.encryptkey) {
92+
/* treat as no key or bail out; pick one policy */
93+
expmsg->data.cept.encryptkey_len = 0;
94+
}
95+
} else {
8896
expmsg->data.cept.encryptkey = NULL;
97+
expmsg->data.cept.encryptkey_len = 0;
8998
}
99+
90100
expmsg->data.cept.seqtrackerid = common->seqtrackerid;
91101

92102
// set the optional fields to suitable "null" values
@@ -112,7 +122,9 @@ void free_published_message(openli_export_recv_t *msg) {
112122
free(msg->data.cept.delivcc);
113123
}
114124
if (msg->data.cept.encryptkey) {
115-
free(msg->data.cept.encryptkey);
125+
openli_free_encryptkey_ptr(&msg->data.cept.encryptkey,
126+
msg->data.cept.encryptkey_len);
127+
msg->data.cept.encryptkey_len = 0;
116128
}
117129
if (msg->data.cept.username) {
118130
free(msg->data.cept.username);

src/collector/collector_publish.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,9 @@ typedef struct published_intercept_msg {
210210
char *delivcc;
211211
int seqtrackerid;
212212
payload_encryption_method_t encryptmethod;
213-
char *encryptkey;
214213
openli_timestamp_encoding_fmt_t timefmt;
214+
uint8_t *encryptkey;
215+
size_t encryptkey_len;
215216
uuid_t *xids;
216217
size_t xid_count;
217218
openli_intercept_types_t cepttype;

src/collector/collector_push_messaging.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <assert.h>
2929
#include <uthash.h>
3030
#include <arpa/inet.h>
31+
#include <string.h>
3132

3233
#include "logger.h"
3334
#include "collector.h"
@@ -54,13 +55,20 @@ static inline void update_intercept_common(intercept_common_t *found,
5455
found->tomediate = replace->tomediate;
5556
found->encrypt = replace->encrypt;
5657

57-
tmp = found->encryptkey;
58-
found->encryptkey = replace->encryptkey;
59-
replace->encryptkey = tmp;
60-
6158
tmp = found->targetagency;
6259
found->targetagency = replace->targetagency;
6360
replace->targetagency = tmp;
61+
62+
/* copy binary key + set length; clear when encryption is NONE */
63+
if (replace->encrypt != OPENLI_PAYLOAD_ENCRYPTION_NONE &&
64+
replace->encryptkey_len > 0) {
65+
found->encryptkey_len = openli_copy_encryptkey(
66+
found->encryptkey, OPENLI_MAX_ENCRYPTKEY_LEN,
67+
replace->encryptkey, replace->encryptkey_len);
68+
} else {
69+
openli_clear_encryptkey(found->encryptkey, OPENLI_MAX_ENCRYPTKEY_LEN,
70+
&found->encryptkey_len);
71+
}
6472
}
6573

6674
static int remove_rtp_stream(colthread_local_t *loc, char *rtpstreamkey) {

src/collector/collector_seqtracker.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "logger.h"
3434
#include "collector_base.h"
3535
#include "collector_publish.h"
36+
#include "intercept.h"
3637

3738
static inline void free_intercept_msg(exporter_intercept_msg_t *msg) {
3839
if (msg->liid) {
@@ -209,6 +210,7 @@ static void track_new_intercept(seqtracker_thread_data_t *seqdata,
209210
intstate->details.delivcc = strdup(cept->delivcc);
210211
intstate->details.authcc_len = strlen(cept->authcc);
211212
intstate->details.delivcc_len = strlen(cept->delivcc);
213+
212214
intstate->details.encryptmethod = cept->encryptmethod;
213215
intstate->details.timefmt = cept->timefmt;
214216
intstate->version ++;
@@ -223,8 +225,8 @@ static void track_new_intercept(seqtracker_thread_data_t *seqdata,
223225
intstate->details.liid_len = strlen(cept->liid);
224226
intstate->details.authcc_len = strlen(cept->authcc);
225227
intstate->details.delivcc_len = strlen(cept->delivcc);
226-
intstate->details.encryptmethod = cept->encryptmethod;
227228
intstate->details.timefmt = cept->timefmt;
229+
intstate->details.encryptmethod = cept->encryptmethod;
228230
intstate->cinsequencing = NULL;
229231
intstate->version = 0;
230232

@@ -283,9 +285,8 @@ static int modify_tracked_intercept(seqtracker_thread_data_t *seqdata,
283285
intstate->details.delivcc = strdup(msg->delivcc);
284286
intstate->details.delivcc_len = strlen(msg->delivcc);
285287

286-
intstate->details.encryptmethod = msg->encryptmethod;
287288
intstate->details.timefmt = msg->timefmt;
288-
289+
intstate->details.encryptmethod = msg->encryptmethod;
289290
remove_preencoded(seqdata, intstate);
290291
preencode_etsi_fields(seqdata, intstate);
291292
intstate->version ++;
@@ -367,7 +368,6 @@ static int run_encoding_job(seqtracker_thread_data_t *seqdata,
367368
job.cept_version = intstate->version;
368369
job.encryptmethod = intstate->details.encryptmethod;
369370
job.timefmt = intstate->details.timefmt;
370-
371371
if (recvd->type == OPENLI_EXPORT_IPMMCC ||
372372
recvd->type == OPENLI_EXPORT_IPCC ||
373373
recvd->type == OPENLI_EXPORT_UMTSCC ||

src/collector/email_worker.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -707,10 +707,18 @@ static void start_email_intercept(openli_email_worker_t *state,
707707
expmsg->data.cept.delivcc = strdup(em->common.delivcc);
708708
expmsg->data.cept.seqtrackerid = em->common.seqtrackerid;
709709
expmsg->data.cept.encryptmethod = em->common.encrypt;
710-
if (em->common.encryptkey) {
711-
expmsg->data.cept.encryptkey = strdup(em->common.encryptkey);
710+
if (em->common.encrypt != OPENLI_PAYLOAD_ENCRYPTION_NONE &&
711+
em->common.encryptkey_len > 0) {
712+
expmsg->data.cept.encryptkey_len = em->common.encryptkey_len;
713+
expmsg->data.cept.encryptkey =
714+
openli_dup_encryptkey_ptr(em->common.encryptkey,
715+
em->common.encryptkey_len);
716+
if (!expmsg->data.cept.encryptkey) {
717+
expmsg->data.cept.encryptkey_len = 0; /* OOM -> treat as no key */
718+
}
712719
} else {
713720
expmsg->data.cept.encryptkey = NULL;
721+
expmsg->data.cept.encryptkey_len = 0;
714722
}
715723

716724
publish_openli_msg(state->zmq_pubsocks[em->common.seqtrackerid],

src/collector/encoder_worker.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include "logger.h"
4141
#include "etsili_core.h"
4242
#include "encoder_worker.h"
43+
#include "intercept.h"
4344

4445
static int init_worker(openli_encoder_t *enc) {
4546
int zero = 0, rto = 10;
@@ -231,10 +232,6 @@ void destroy_encoder_worker(openli_encoder_t *enc) {
231232
zmq_close(enc->zmq_recvjobs[i]);
232233
}
233234

234-
if (enc->encrypt.evp_ctx) {
235-
EVP_CIPHER_CTX_free(enc->encrypt.evp_ctx);
236-
}
237-
238235
if (enc->zmq_control) {
239236
zmq_close(enc->zmq_control);
240237
}

src/collector/etsiencoding/encryptcontainer.c

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -209,12 +209,11 @@ static encoded_encrypt_template_t *lookup_encrypted_template(
209209

210210
int encrypt_aes_192_cbc(EVP_CIPHER_CTX *ctx, uint8_t *buf, uint16_t buflen,
211211
uint8_t *dest, uint16_t destlen, uint32_t seqno,
212-
char *encryptkey) {
212+
const uint8_t *encryptkey) {
213213

214214
uint8_t IV_128[16];
215215
uint8_t key[24];
216216
uint32_t swapseqno;
217-
size_t keylen = strlen(encryptkey);
218217
int len, i;
219218

220219
assert(buflen <= destlen);
@@ -226,11 +225,9 @@ int encrypt_aes_192_cbc(EVP_CIPHER_CTX *ctx, uint8_t *buf, uint16_t buflen,
226225
memcpy(&(IV_128[i]), &swapseqno, sizeof(uint32_t));
227226
}
228227

229-
if (keylen > 24) {
230-
keylen = 24;
231-
}
232-
memset(key, 0, 24);
233-
memcpy(key, encryptkey, keylen);
228+
/* The key is 24 bytes for AES-192. */
229+
memcpy(key, encryptkey, 24);
230+
234231

235232
/* Trust that we have correctly pre-padded the data to encrypt */
236233
EVP_CIPHER_CTX_set_padding(ctx, 0);
@@ -241,6 +238,17 @@ int encrypt_aes_192_cbc(EVP_CIPHER_CTX *ctx, uint8_t *buf, uint16_t buflen,
241238
return -1;
242239
}
243240

241+
/* ETSI-IP.nl uses AES-192-CBC with application-layer padding (if any).
242+
* Disable PKCS#7 inside the cipher so ciphertext length == input length. */
243+
EVP_CIPHER_CTX_set_padding(ctx, 0);
244+
245+
/* Sanity: with padding disabled, input MUST be a multiple of 16. */
246+
if ((buflen & 0x0F) != 0) {
247+
logger(LOG_INFO, "OpenLI: AES-192-CBC called with non-block-aligned input (%u bytes)", buflen);
248+
return -1;
249+
}
250+
251+
244252
if (EVP_EncryptUpdate(ctx, dest, &len, buf, (int)buflen) != 1) {
245253
logger(LOG_INFO, "OpenLI: unable to perform EVP encryption operation -- openssl error %s", ERR_error_string(ERR_get_error(), NULL));
246254
return -1;
@@ -251,6 +259,13 @@ int encrypt_aes_192_cbc(EVP_CIPHER_CTX *ctx, uint8_t *buf, uint16_t buflen,
251259
return -1;
252260
}
253261

262+
/* Cleanse local key copy */
263+
#if defined(__GLIBC__) && defined(_GNU_SOURCE)
264+
explicit_bzero(key, sizeof(key));
265+
#else
266+
volatile uint8_t *p = key;
267+
for (size_t i = 0; i < sizeof(key); ++i) p[i] = 0;
268+
#endif
254269
return 0;
255270

256271
}
@@ -388,7 +403,6 @@ int create_preencrypted_message_body(wandder_encoder_t *encoder,
388403
* mediator itself.
389404
*/
390405

391-
392406
/* Lookup the template for a message of this length and encryption method */
393407
tplate = lookup_encrypted_template(&(encrypt->saved_encryption_templates),
394408
enclen, job->encryptmethod, &is_new);

0 commit comments

Comments
 (0)