1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
diff --git a/tgl/mtproto-client.c b/tgl/mtproto-client.c
index 075decc..0f6c3f2 100644
--- a/tgl/mtproto-client.c
+++ b/tgl/mtproto-client.c
@@ -143,7 +143,9 @@ static int decrypt_buffer[ENCRYPT_BUFFER_INTS];
static int encrypt_packet_buffer (struct tgl_state *TLS, struct tgl_dc *DC) {
RSA *key = TLS->rsa_key_loaded[DC->rsa_key_idx];
- return tgl_pad_rsa_encrypt (TLS, (char *) packet_buffer, (packet_ptr - packet_buffer) * 4, (char *) encrypt_buffer, ENCRYPT_BUFFER_INTS * 4, key->n, key->e);
+ const BIGNUM *n, *e;
+ RSA_get0_key(key, &n, &e, NULL);
+ return tgl_pad_rsa_encrypt (TLS, (char *) packet_buffer, (packet_ptr - packet_buffer) * 4, (char *) encrypt_buffer, ENCRYPT_BUFFER_INTS * 4, n, e);
}
static int encrypt_packet_buffer_aes_unauth (const char server_nonce[16], const char hidden_client_nonce[32]) {
diff --git a/tgl/mtproto-common.c b/tgl/mtproto-common.c
index f3b6582..b782256 100644
--- a/tgl/mtproto-common.c
+++ b/tgl/mtproto-common.c
@@ -178,10 +178,12 @@ int tgl_serialize_bignum (BIGNUM *b, char *buffer, int maxlen) {
long long tgl_do_compute_rsa_key_fingerprint (RSA *key) {
static char tempbuff[4096];
static unsigned char sha[20];
- assert (key->n && key->e);
- int l1 = tgl_serialize_bignum (key->n, tempbuff, 4096);
+ const BIGNUM *n, *e;
+ RSA_get0_key(key, &n, &e, NULL);
+ assert (n && e);
+ int l1 = tgl_serialize_bignum (n, tempbuff, 4096);
assert (l1 > 0);
- int l2 = tgl_serialize_bignum (key->e, tempbuff + l1, 4096 - l1);
+ int l2 = tgl_serialize_bignum (e, tempbuff + l1, 4096 - l1);
assert (l2 > 0 && l1 + l2 <= 4096);
SHA1 ((unsigned char *)tempbuff, l1 + l2, sha);
return *(long long *)(sha + 12);
@@ -258,21 +260,20 @@ int tgl_pad_rsa_encrypt (struct tgl_state *TLS, char *from, int from_len, char *
assert (size >= chunks * 256);
assert (RAND_pseudo_bytes ((unsigned char *) from + from_len, pad) >= 0);
int i;
- BIGNUM x, y;
- BN_init (&x);
- BN_init (&y);
+ BIGNUM *x = BN_new();
+ BIGNUM *y = BN_new();
rsa_encrypted_chunks += chunks;
for (i = 0; i < chunks; i++) {
- BN_bin2bn ((unsigned char *) from, 255, &x);
- assert (BN_mod_exp (&y, &x, E, N, TLS->BN_ctx) == 1);
- unsigned l = 256 - BN_num_bytes (&y);
+ BN_bin2bn ((unsigned char *) from, 255, x);
+ assert (BN_mod_exp (y, x, E, N, TLS->BN_ctx) == 1);
+ unsigned l = 256 - BN_num_bytes (y);
assert (l <= 256);
memset (to, 0, l);
- BN_bn2bin (&y, (unsigned char *) to + l);
+ BN_bn2bin (y, (unsigned char *) to + l);
to += 256;
}
- BN_free (&x);
- BN_free (&y);
+ BN_free (x);
+ BN_free (y);
return chunks * 256;
}
@@ -285,26 +286,25 @@ int tgl_pad_rsa_decrypt (struct tgl_state *TLS, char *from, int from_len, char *
assert (bits >= 2041 && bits <= 2048);
assert (size >= chunks * 255);
int i;
- BIGNUM x, y;
- BN_init (&x);
- BN_init (&y);
+ BIGNUM *x = BN_new();
+ BIGNUM *y = BN_new();
for (i = 0; i < chunks; i++) {
++rsa_decrypted_chunks;
- BN_bin2bn ((unsigned char *) from, 256, &x);
- assert (BN_mod_exp (&y, &x, D, N, TLS->BN_ctx) == 1);
- int l = BN_num_bytes (&y);
+ BN_bin2bn ((unsigned char *) from, 256, x);
+ assert (BN_mod_exp (y, x, D, N, TLS->BN_ctx) == 1);
+ int l = BN_num_bytes (y);
if (l > 255) {
- BN_free (&x);
- BN_free (&y);
+ BN_free (x);
+ BN_free (y);
return -1;
}
assert (l >= 0 && l <= 255);
memset (to, 0, 255 - l);
- BN_bn2bin (&y, (unsigned char *) to + 255 - l);
+ BN_bn2bin (y, (unsigned char *) to + 255 - l);
to += 255;
}
- BN_free (&x);
- BN_free (&y);
+ BN_free (x);
+ BN_free (y);
return chunks * 255;
}
|