Newer
Older
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
// unsigned char *encrypted_key_mat = NULL;
/* THIS PROBABLY NEEDS TO BE CHANGED TO BE MORE DYNAMIC AND HAVE THE REAL LENGTH*/
unsigned char encrypted_key_mat[1024];
int encrypted_key_mat_len = 0; //[1] = {0};
unsigned char envelope_iv[EVP_MAX_IV_LENGTH];
// unsigned char *envelope_iv = NULL;
unsigned char **encrypted_envelope_keys = OPENSSL_malloc(1 * sizeof(unsigned char *));
// unsigned char encrypted_envelope_keys[1][128]
int encrypted_envelope_key_len[1] = {0};
unsigned char *p,*d;
int n;
/* I'm not sure about this buffer size... got it by printing it out when
running code... should probably be doing things better :'(
Also, note that unsigned chars are used in various places, but this buffer
is a char?!??!
HACK The size of this buffer should be different...
It is dangerous right now and we risk overflowing later on...
*/
char temp_buff[21848] = {0};
if (s->state == SPP_ST_CW_PRXY_MAT_A) {
n = spp_pack_proxy_key_mat(s, temp_buff);
/* Encrypt using envelopes. What this means is that the data we are
sending will be encrypted with a randomly generated shared secret key.
The shared secret key is then encrypted via the RSA pub key of the
destination.
*/
d = (unsigned char *)s->init_buf->data;
p = &(d[4]);
/* Need to free this later on still */
//key_mat = malloc(n * sizeof(unsigned char *));
pub_key = X509_get_pubkey(SSL_get_peer_certificate(s));
pub_keys[0] = pub_key;
encrypted_envelope_keys[0] = OPENSSL_malloc(RSA_size(pub_keys[0]->pkey.rsa));
memset(envelope_iv, 0, sizeof envelope_iv); /* per RFC 1510 */
/* seal the envelope */
encrypted_key_mat_len = envelope_seal(
pub_keys,
temp_buff,
n,
encrypted_envelope_keys,
&encrypted_envelope_key_len,
envelope_iv,
encrypted_key_mat,
&s->proxy_key_mat_shared_secret,
&s->proxy_key_mat_shared_secret_len);
/* store the shared secret */ // Already stored
//memcpy(s->proxy_key_mat_shared_secret, shared_secret, sizeof(shared_secret));
*(d++)=SPP_MT_PROXY_KEY_MATERIAL;
/* calculate the size of the payload */
n = 4; /* to store length of encrypted envelope key and destination ID*/
n += encrypted_envelope_key_len[0]; /* to store the encrypted envelope key */
n += EVP_MAX_IV_LENGTH; /* to store the iv */
n += 3; /* to store the length of the encrypted data */
n += encrypted_key_mat_len; /* to store the encrypted key material */
// printf("total legnth of message: %d\n", n);
l2n3(n,d);
//p = &(((unsigned char *)s->init_buf->data)[4]);
// p = &(d[4]);
/* If we are server, we send to client (1), otherwise we send to server (2)*/
s1n(s->server == 0 ? 2 : 1, d);
/* Now we need to handle writing encryption stuff! */
/* write the length of the encrypted key */
l2n3(encrypted_envelope_key_len[0], d);
// l2n3(1, d);
/* write the encrypted envelope key */
memcpy(d, encrypted_envelope_keys[0], encrypted_envelope_key_len[0]);
/* free the allocated mem */
OPENSSL_free(encrypted_envelope_keys[0]);
/* advance pointer! */
d += encrypted_envelope_key_len[0];
memcpy(d, envelope_iv, EVP_MAX_IV_LENGTH);
/* advance pointer */
d += EVP_MAX_IV_LENGTH;
/* write the legnth of encrypted key material */
l2n3(encrypted_key_mat_len, d);
/* write the encrypted key material */
memcpy(d, encrypted_key_mat, encrypted_key_mat_len);
/* advance pointer! */
d += encrypted_key_mat_len;
// printf("after copying relevant stuff (is %d bytes)...\n", n);
// spp_print_buffer(p, n);
// memcpy(&(d2[4]), temp_buff, n);
s->state=SPP_ST_CW_PRXY_MAT_B;
/* number of bytes to write */
/*
Here we should realy ensure that we are writing the size of the
encrypted key material
*/
s->init_num=n+4;
s->init_off=0;
#ifdef DEBUG
printf("Sending end key material, n=%d\n", n);
spp_print_buffer((unsigned char *)s->init_buf->data, s->init_num);
#endif
}
/* SPP_ST_CW_PRXY_MAT_B */
return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
err:
return(-1);
}
int spp_send_end_key_material_server(SSL *s) {
unsigned char *p,*d;
int n;
unsigned char iv[EVP_MAX_IV_LENGTH];
unsigned char encrypted_key_mat[2048];
int encrypted_key_mat_len = 0;
char temp_buff[21848] = {0};
if (s->state == SPP_ST_CW_PRXY_MAT_A) {
n = spp_pack_proxy_key_mat(s, temp_buff);
/* 0 out per some rfc.
iv is randomized in spp_encrypt_key_mat_server()
*/
memset(iv, 0, EVP_MAX_IV_LENGTH);
if (s->proxy_key_mat_shared_secret == NULL) {
goto err;
}
#ifdef DEBUG
printf("Proxy key material shared secret (server):\n");
spp_print_buffer(s->proxy_key_mat_shared_secret, s->proxy_key_mat_shared_secret_len);
#endif
encrypted_key_mat_len = spp_encrypt_key_mat_server(
s->proxy_key_mat_shared_secret,
s->proxy_key_mat_shared_secret_len,
iv,
temp_buff,
n,
encrypted_key_mat
);
// Release the shared secret
OPENSSL_cleanse(s->proxy_key_mat_shared_secret,s->proxy_key_mat_shared_secret_len);
OPENSSL_free(s->proxy_key_mat_shared_secret);
s->proxy_key_mat_shared_secret = NULL;
s->proxy_key_mat_shared_secret_len = 0;
#ifdef DEBUG
printf("server->client key material:\n");
spp_print_buffer(temp_buff, n);
printf("server->client encrypted_key_mat:\n");
spp_print_buffer(encrypted_key_mat, encrypted_key_mat_len);
#endif
d = (unsigned char *)s->init_buf->data;
p = &(d[4]);
/* Now we need to copy relevant info into the full buffer */
n = encrypted_key_mat_len+1; /* the actual encrypted key material and 1 byte for the proxy_id*/
n += 3; /* for the length of the encrypted key material */
n += 3; /* for the length of the iv */
n += EVP_MAX_IV_LENGTH; /* for the length of the iv HACK: This should be dynamic? */
s1n(s->server == 0 ? 2 : 1, p);
/* copy in the length of the encrypted key material */
l2n3(encrypted_key_mat_len, p);
/* copy in the encrypted key material */
memcpy(p, encrypted_key_mat, encrypted_key_mat_len);
/* advance pointer */
p += encrypted_key_mat_len;
/* copy in the length of the iv */
l2n3(EVP_MAX_IV_LENGTH, p);
/* copy in the actual iv */
memcpy(p, iv, EVP_MAX_IV_LENGTH);
*(d++)=SPP_MT_PROXY_KEY_MATERIAL;
l2n3(n,d);
s->state=SPP_ST_CW_PRXY_MAT_B;
/* number of bytes to write */
s->init_num=n+4;
s->init_off=0;
#ifdef DEBUG
printf("Sending end key material, n=%d\n", n);
spp_print_buffer((unsigned char *)s->init_buf->data, s->init_num);
#endif
}
/* SPP_ST_CW_PRXY_MAT_B */
return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
err:
printf("Error in spp_send_end_key_material_server\n");
return(-1);
}
int spp_send_end_key_material(SSL *s) {
unsigned char *p,*d;
int n,i;
if (s->state == SPP_ST_CW_PRXY_MAT_A) {
d=(unsigned char *)s->init_buf->data;
p= &(d[4]);
n = 0;
s1n(s->server == 0 ? 2 : 1, p);
for (i = 0; i < s->slices_len; i++) {
s1n(s->slices[i]->slice_id, p);
s2n(EVP_MAX_KEY_LENGTH, p);
memcpy(p, s->slices[i]->read_mat, EVP_MAX_KEY_LENGTH);
p += EVP_MAX_KEY_LENGTH;
s2n(EVP_MAX_KEY_LENGTH, p);
memcpy(p, s->slices[i]->write_mat, EVP_MAX_KEY_LENGTH);
p += EVP_MAX_KEY_LENGTH;
}
n = p-d-4;
/* Encrypt using the master key previously negotiated. */
/*if ((cipher=OPENSSL_malloc(sizeof(EVP_CIPHER_CTX))) == NULL)
goto err;
EVP_CIPHER_CTX_init(cipher);
if(!EVP_EncryptInit_ex(cipher, c, NULL, s->session->master_key, aesIV)) {
goto err;
}*/
*(d++)=SPP_MT_PROXY_KEY_MATERIAL;
l2n3(n,d);
s->state=SPP_ST_CW_PRXY_MAT_B;
/* number of bytes to write */
s->init_num=n+4;
s->init_off=0;
#ifdef DEBUG
printf("Sending end key material, n=%d\n", n);
spp_print_buffer((unsigned char *)s->init_buf->data, s->init_num);
#endif
}
/* SPP_ST_CW_PRXY_MAT_B */
return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
}
int spp_get_end_key_material_client(SSL *s) {
unsigned char key_mat[SSL3_RT_MAX_PLAIN_LENGTH] = {0};
unsigned char *encrypted_key_mat;//[21848] = {0};
int encrypted_key_mat_len, key_mat_len = 0;
unsigned char *iv;//[EVP_MAX_IV_LENGTH];
int iv_len = 0;
unsigned char *p,*d;
int ok;
long n;
int id;
n=s->method->ssl_get_message(s,
SPP_ST_CR_PRXY_MAT_A,
SPP_ST_CR_PRXY_MAT_B,
SPP_MT_PROXY_KEY_MATERIAL,
SSL3_RT_MAX_PLAIN_LENGTH,
&ok);
if (!ok) return((int)n);
/* unpack our data */
p = d = (unsigned char*)s->init_msg;
/* Get the ID */
n1s(p, id);
if (id != 1) {
/* Material not intended for client */
return -1;
}
/* encrypted key mat len */
n2l3(p, encrypted_key_mat_len);
/* get the encrypted key material */
//memcpy(encrypted_key_mat, p, encrypted_key_mat_len);
encrypted_key_mat = p;
// printf("client get key material, encrypted key mat:\n");
// spp_print_buffer(encrypted_key_mat, encrypted_key_mat_len);
/* advance pointer */
p += encrypted_key_mat_len;
/* get the length of the iv */
n2l3(p, iv_len);
/* get the iv itself */
//memcpy(iv, p, iv_len);
iv = p;
p+= iv_len;
/* Make sure the message length is sane. */
if (p-d != n) {
goto err;
}
if (s->proxy_key_mat_shared_secret == NULL) {
goto err;
}
#ifdef DEBUG
printf("Proxy key material shared secret (client):\n");
spp_print_buffer(s->proxy_key_mat_shared_secret, s->proxy_key_mat_shared_secret_len);
#endif
/* decrypt the key material */
key_mat_len = spp_decrypt_key_mat_client(
s->proxy_key_mat_shared_secret,
s->proxy_key_mat_shared_secret_len,
iv,
encrypted_key_mat,
encrypted_key_mat_len,
key_mat);
// Release the shared secret
OPENSSL_cleanse(s->proxy_key_mat_shared_secret,s->proxy_key_mat_shared_secret_len);
OPENSSL_free(s->proxy_key_mat_shared_secret);
s->proxy_key_mat_shared_secret = NULL;
s->proxy_key_mat_shared_secret_len = 0;
#ifdef DEBUG
printf("key mat len: %d\n", key_mat_len);
printf("client get key material, key mat:\n");
spp_print_buffer(key_mat, key_mat_len);
#endif
return spp_unpack_proxy_key_mat(s, key_mat, key_mat_len);
err:
printf("Error in spp_get_end_key_material_client\n");
return -1;
}
int spp_get_end_key_material_server(SSL *s) {
unsigned char key_mat[SSL3_RT_MAX_PLAIN_LENGTH] = {0};
int *key_mat_len = 0;
EVP_PKEY *private_key = NULL;
unsigned char *key_mat_envelope = NULL, *d;
int encrypted_envelope_key_len = 0;
unsigned char *envelope_iv;//[EVP_MAX_IV_LENGTH];
unsigned char *encrypted_envelope_key;//[128] = {0};
int encrypted_key_mat_len = 0;
unsigned char *encrypted_key_mat;//[2048]; /* HACK size... */
int ok,id;
long n;
n=s->method->ssl_get_message(s,
SPP_ST_CR_PRXY_MAT_A,
SPP_ST_CR_PRXY_MAT_B,
SPP_MT_PROXY_KEY_MATERIAL,
SSL3_RT_MAX_PLAIN_LENGTH,
&ok);
if (!ok) return((int)n);
// printf("length of msg received by server: %d\n", n);
/* we need to decrypt the message first... */
// printf("getting private key\n");
private_key = s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey;
key_mat_envelope = d = (unsigned char *)s->init_msg;
// printf("Payload from client:\n");
// spp_print_buffer(key_mat_envelope, n);
/* Get the ID */
n1s(key_mat_envelope, id);
if (id != 2) {
/* Material not intended for server */
return -1;
}
/* get length of encrypted envelope key */
n2l3(key_mat_envelope, encrypted_envelope_key_len);
/* now pull out the encrypted envelope key */
//memcpy(encrypted_envelope_key, key_mat_envelope, encrypted_envelope_key_len); // );
encrypted_envelope_key = key_mat_envelope;
// printf("encrypted envelope key:\n");
// spp_print_buffer(encrypted_envelope_key, encrypted_envelope_key_len);
/* advance pointer! */
key_mat_envelope += encrypted_envelope_key_len;
/* read iv */
// printf("reading iv\n");
//memcpy(envelope_iv, key_mat_envelope, EVP_MAX_IV_LENGTH);
envelope_iv = key_mat_envelope;
// printf("envelope_iv:\n");
// // printf("%s\n", envelope_iv);
// spp_print_buffer(envelope_iv, EVP_MAX_IV_LENGTH);
/* advance pointer! */
key_mat_envelope += EVP_MAX_IV_LENGTH;
/* get legnth of encrypted key material */
n2l3(key_mat_envelope, encrypted_key_mat_len);
/* pull the encrypted key material out! */
//memcpy(encrypted_key_mat, key_mat_envelope, encrypted_key_mat_len);
encrypted_key_mat = key_mat_envelope;
key_mat_envelope += encrypted_key_mat_len;
// printf("Encrypted key material:\n");
// spp_print_buffer(encrypted_key_mat, encrypted_key_mat_len);
if (n != (key_mat_envelope - d)) {
goto err;
}
#ifdef DEBUG
printf("opening envelope!\n");
#endif
key_mat_len = envelope_open(
private_key,
encrypted_key_mat,
encrypted_key_mat_len,
encrypted_envelope_key,
encrypted_envelope_key_len,
envelope_iv,
key_mat,
&s->proxy_key_mat_shared_secret,
&s->proxy_key_mat_shared_secret_len);
// printf("key mat len: %d\n", key_mat_len);
// spp_print_buffer(key_mat, key_mat_len);
/* if we are lucky, key_mat conatins the unencrypted key material! */
return spp_unpack_proxy_key_mat(s, key_mat, key_mat_len);
err:
printf("Error in spp_get_end_key_material_server\n");
return -1;
}
/* Old method to be removed. This is an unencrypted proxykeymat */
int spp_get_end_key_material(SSL *s) {
unsigned char *param,*p;
int ok;
long n;
int id,slice_id,len;
SPP_SLICE *slice;
n=s->method->ssl_get_message(s,
SPP_ST_CR_PRXY_MAT_A,
SPP_ST_CR_PRXY_MAT_B,
SPP_MT_PROXY_KEY_MATERIAL,
SSL3_RT_MAX_PLAIN_LENGTH,
&ok);
if (!ok) return((int)n);
param=p=(unsigned char *)s->init_msg;
/* Server or client identifier */
//printf("Message header %d, %d, %d, %d\n", p[0], p[1], p[2], p[3]);
n1s(p, id);
if (id != 1 && id != 2) {
goto err;
}
/* More to read */
while (p-param < n) {
n1s(p, slice_id);
//printf("Slice %d received\n", slice_id);
slice = SPP_get_slice_by_id(s, slice_id);
if (slice == NULL)
goto err;
n2s(p, len);
if (len > EVP_MAX_KEY_LENGTH)
goto err;
memcpy(slice->other_read_mat, p, len);
p += len;
n2s(p, len);
if (len > EVP_MAX_KEY_LENGTH)
goto err;
memcpy(slice->other_write_mat, p, len);
p += len;
slice->write_access = 1;
slice->read_access = 1;
}
/* Should now have read the full message. */
if (p-param != n) {
printf("Did not read the whole message, %d != %d\n", (int)(p-param), n);
goto err;
}
/* Check to make sure we have material for all slices.
* and generate the contexts. */
for (n = 0; n < s->slices_len; n++) {
if (s->slices[n]->write_access == 0 || s->slices[n]->read_access == 0) {
printf("Slice %d missing\n", s->slices[n]->slice_id);
goto err;
}
/* Do not init yet. Save this for on sending the change cipher state message. */
/*if (spp_init_slice_st(s, s->slices[n]) <= 0) {
printf("Slice %d init failure\n", s->slices[n]->slice_id);
goto err;
}*/
}
return 1;
err:
return(-1);
}
/* Open an envelope.
Also return the shared secret key!
TODO Proper error handling
*/
int envelope_open(EVP_PKEY *priv_key, unsigned char *ciphertext, int ciphertext_len,
unsigned char *encrypted_key, int encrypted_key_len, unsigned char *iv,
unsigned char *plaintext, unsigned char **shared_secret_key, int *shared_secret_key_len)
{
EVP_CIPHER_CTX *ctx;
int len;
int plaintext_len;
/* Create and initialise the context */
#ifdef DEBUG
printf("Create and initialise the context\n");
#endif
if(!(ctx = EVP_CIPHER_CTX_new())) {
// handleErrors();
printf("envelope_open error 1\n");
}
#ifdef DEBUG
printf("Initialise the decryption operation.\n");
#endif
/* Initialise the decryption operation. The asymmetric private key is
* provided and priv_key, whilst the encrypted session key is held in
* encrypted_key */
if(1 != spp_OpenInit(ctx, EVP_aes_256_cbc(), encrypted_key,
encrypted_key_len, iv, priv_key, shared_secret_key, shared_secret_key_len)) {
//handleErrors();
printf("envelope_open error 2\n");
}
/* Provide the message to be decrypted, and obtain the plaintext output.
* EVP_OpenUpdate can be called multiple times if necessary
*/
// printf("Provide the message to be decrypted, and obtain the plaintext output.\n");
if(1 != EVP_OpenUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len)) {
// handleErrors();
printf("envelope_open error 3\n");
}
plaintext_len = len;
/* Finalise the decryption. Further plaintext bytes may be written at
* this stage.
*/
// printf("Finalise the decryption.\n");
if(1 != EVP_OpenFinal(ctx, plaintext + len, &len)) {
// handleErrors();
printf("envelope_open error 4\n");
}
plaintext_len += len;
/* Clean up */
// printf("clean up\n");
EVP_CIPHER_CTX_free(ctx);
return plaintext_len;
}
/*
Create an envelope, encrypt with pub key, etc...
Also returns the shared secret key.
TODO Proper error handling
*/
int envelope_seal(EVP_PKEY **pub_key, unsigned char *plaintext, int plaintext_len,
unsigned char **encrypted_key, int *encrypted_key_len, unsigned char *iv,
unsigned char *ciphertext, unsigned char **shared_secret_key, int *shared_secret_key_len)
{
EVP_CIPHER_CTX *ctx;
int ciphertext_len;
// unsigned char *ek;
int len;
/* Create and initialise the context */
if(!(ctx = EVP_CIPHER_CTX_new())) printf("ERROR\n");//handleErrors();
// ek = malloc(EVP_PKEY_size(pub_key));
/* Initialise the envelope seal operation. This operation generates
* a key for the provided cipher, and then encrypts that key a number
* of times (one for each public key provided in the pub_key array). In
* this example the array size is just one. This operation also
* generates an IV and places it in iv. */
// printf("Running SealInit\n");
if(1 != spp_SealInit(ctx, EVP_aes_256_cbc(), encrypted_key, encrypted_key_len, iv, pub_key, 1, shared_secret_key, shared_secret_key_len)) {
printf("ERROR2\n");
// handleErrors();
}
/* Provide the message to be encrypted, and obtain the encrypted output.
* EVP_SealUpdate can be called multiple times if necessary
*/
if(1 != EVP_SealUpdate(ctx, ciphertext, &len, plaintext, plaintext_len)) {
printf("ERROR3\n");
// handleErrors();
}
ciphertext_len = len;
/* Finalise the encryption. Further ciphertext bytes may be written at
* this stage.
*/
if(1 != EVP_SealFinal(ctx, ciphertext + len, &len)) {
printf("ERROR4\n");
// handleErrors();
}
ciphertext_len += len;
/* Clean up */
EVP_CIPHER_CTX_free(ctx);
return ciphertext_len;
}
/*
HACK Custom version of EVP_OpenInit that also returns the shared secret key.
TODO Proper error handling
*/
int spp_OpenInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
const unsigned char *ek, int ekl, const unsigned char *iv,
EVP_PKEY *priv, unsigned char **shared_secret, int *shared_secret_key_len)
{
unsigned char *key=NULL;
int i,size=0,ret=0;
if(type) {
EVP_CIPHER_CTX_init(ctx);
if(!EVP_DecryptInit_ex(ctx,type,NULL, NULL,NULL))
{
printf("what's up?\n");
return 0;
}
}
if(!priv) return 1;
if (priv->type != EVP_PKEY_RSA) {
EVPerr(EVP_F_EVP_OPENINIT,EVP_R_PUBLIC_KEY_NOT_RSA);
goto err;
}
size=RSA_size(priv->pkey.rsa);
*shared_secret=key=(unsigned char *)OPENSSL_malloc(size+2);
*shared_secret_key_len=size+2;
if (key == NULL)
{
/* ERROR */
EVPerr(EVP_F_EVP_OPENINIT,ERR_R_MALLOC_FAILURE);
goto err;
}
i=EVP_PKEY_decrypt_old(key,ek,ekl,priv);
//memcpy(shared_secret, key, (size + 2));
if ((i <= 0) || !EVP_CIPHER_CTX_set_key_length(ctx, i)) {
/* ERROR */
goto err;
}
if(!EVP_DecryptInit_ex(ctx,NULL,NULL,key,iv)) goto err;
ret=1;
err:
// Do not free or cleanse the key. Instead, it is returned in shared_secret.
//if (key != NULL) OPENSSL_cleanse(key,size);
/* Before we free the key, let's copy it out to the shared_secret... */
//OPENSSL_free(key);
return(ret);
}
/* TODO Proper error handling */
int spp_decrypt_key_mat_client(
unsigned char *symmetric_key,
int symmetric_key_len,
unsigned char *iv,
unsigned char *cipher_text,
int cipher_text_len,
unsigned char *plain_text) {
EVP_CIPHER_CTX *ctx;
EVP_CIPHER *type = EVP_aes_256_cbc();
int plain_text_len;
int len;
if (!(ctx = EVP_CIPHER_CTX_new())) printf("ERROR1\n");
if (type) {
EVP_CIPHER_CTX_init(ctx);
if (!EVP_DecryptInit_ex(ctx, type, NULL, symmetric_key, iv)) return 0;
}
if (1 != EVP_DecryptUpdate(ctx, plain_text, &len, cipher_text, cipher_text_len)) {
printf("ERROR2\n");
}
plain_text_len = len;
/* finalize decrpytion */
int i;
i = EVP_DecryptFinal_ex(ctx, plain_text + len, &len);
if (i) {
i = EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, NULL);
}
plain_text_len += len;
EVP_CIPHER_CTX_free(ctx);
return plain_text_len;
}
/* TODO Proper error handling */
int spp_encrypt_key_mat_server(
unsigned char *symmetric_key,
int symmetric_key_len,
unsigned char *iv,
unsigned char *plain_text,
int plain_text_len,
unsigned char *cipher_text) {
EVP_CIPHER_CTX *ctx;
EVP_CIPHER *type = EVP_aes_256_cbc();
int ciphertext_len;
// unsigned char *ek;
int len;
/* Create and initialise the context */
if(!(ctx = EVP_CIPHER_CTX_new())) printf("ERROR\n");//handleErrors();
if(type) {
EVP_CIPHER_CTX_init(ctx);
/* gotta initialize the context properly i guess */
if(!EVP_EncryptInit_ex(ctx,type,NULL,NULL,NULL)) return 0;
}
/* generate random iv */
if (EVP_CIPHER_CTX_iv_length(ctx)) {
RAND_pseudo_bytes(iv,EVP_CIPHER_CTX_iv_length(ctx));
}
if(!EVP_EncryptInit_ex(ctx,type,NULL,symmetric_key,iv)) return 0;
if(1 != EVP_EncryptUpdate(ctx, cipher_text, &len, plain_text, plain_text_len)) {
printf("ERROR3\n");
// handleErrors();
}
// Finalize
ciphertext_len = len;
/* Finalise the encryption. Further ciphertext bytes may be written at
* this stage.
*/
int i;
i = EVP_EncryptFinal_ex(ctx,cipher_text + len, &len);
if (i)
i = EVP_EncryptInit_ex(ctx,NULL,NULL,NULL,NULL);
if (1 != i) {
printf("ERROR4\n");
}
ciphertext_len += len;
/* Clean up */
EVP_CIPHER_CTX_free(ctx);
return ciphertext_len;
}
/*
HACK
Custom version of EVP_SealInit that also returns the shared secret key that's
generated.
HACK NO IDEA IF REUSING THE SHARED SECRET KEY LATER ON COMPROMISES THE INTEGRITY
OF THE ENCRYPTION ALGORITHM!!!!!!
*/
int spp_SealInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type, unsigned char **ek,
int *ekl, unsigned char *iv, EVP_PKEY **pubk, int npubk, unsigned char **key, int *key_len)
{
// unsigned char key[EVP_MAX_KEY_LENGTH];
int i;
//
BIO *bio_out = NULL;
//
if(type) {
EVP_CIPHER_CTX_init(ctx);
if(!EVP_EncryptInit_ex(ctx,type,NULL,NULL,NULL)) return 0;
}
if ((npubk <= 0) || !pubk)
return 1;
*key_len = EVP_CIPHER_CTX_key_length(ctx);
#ifdef DEBUG
printf("Key length = %d\n", *key_len);
#endif
*key = (unsigned char*)OPENSSL_malloc(*key_len);
if (EVP_CIPHER_CTX_rand_key(ctx, *key) <= 0)
return 0;
if (EVP_CIPHER_CTX_iv_length(ctx))
RAND_pseudo_bytes(iv,EVP_CIPHER_CTX_iv_length(ctx));
if(!EVP_EncryptInit_ex(ctx,NULL,NULL,*key,iv)) return 0;
for (i=0; i<npubk; i++)
{
// printf("public key %d is %d bits\n", i, EVP_PKEY_bits(pubk[i]));
// printf("pubk[i]-type: %d\n", pubk[i]->type);
// printf("ekl[i] = %d\n", ekl[i]);
// printf("Public key:\n");
// bio_out = BIO_new_fp(stdout,BIO_NOCLOSE);
// printf("Key: %s\n", key);
// EVP_PKEY_print_public(bio_out, pubk[i], 4, NULL);
ekl[i]=EVP_PKEY_encrypt_old(ek[i],*key,EVP_CIPHER_CTX_key_length(ctx),
pubk[i]);
if (ekl[i] <= 0) return(-1);
}
return(npubk);
}
int spp_unpack_proxy_key_mat(SSL *s, unsigned char *p, long n) {
int len, slice_id;
SPP_SLICE *slice;
unsigned char *param=p;
/* More to read */
while (p-param < n) {
n1s(p, slice_id);
//printf("Slice %d received\n", slice_id);
slice = SPP_get_slice_by_id(s, slice_id);
if (slice == NULL) {
printf("Invalid slice id: %d\n", slice_id);
goto err;
}
n2s(p, len);
if (len > EVP_MAX_KEY_LENGTH)
goto err;
memcpy(slice->other_read_mat, p, len);
p += len;
n2s(p, len);
if (len > EVP_MAX_KEY_LENGTH)
goto err;
memcpy(slice->other_write_mat, p, len);
p += len;
slice->write_access = 1;
slice->read_access = 1;
}
/* Should now have read the full message. */
if (p-param != n) {
printf("Did not read the whole message, %d != %d\n", p-param, n);
goto err;
}
/* Check to make sure we have material for all slices.
* and generate the contexts. */
for (n = 0; n < s->slices_len; n++) {
if (s->slices[n]->write_access == 0 || s->slices[n]->read_access == 0) {
printf("Slice %d missing\n", s->slices[n]->slice_id);
goto err;
}
/* Do not init yet. Save this for on sending the change cipher state message. */
/*if (spp_init_slice_st(s, s->slices[n]) <= 0) {
printf("Slice %d init failure\n", s->slices[n]->slice_id);
goto err;
}*/
}
return 1;
err:
printf("Error in unpacking key material\n");
return -1;
}
/* TODO Make this work and use it in spp_send_end_key_material_client/server !*/
int spp_pack_proxy_key_mat(SSL *s, unsigned char *proxy_key_mat) {
int n, i = 0;
unsigned char *p, *d;
/* I'm not sure about this buffer size... got it by printing it out when
running code... should probably be doing things better :'(
Also, note that unsigned chars are used in various places, but this buffer
is a char?!??!
HACK The size of this buffer should be different...
It is dangerous right now and we risk overflowing later on...
*/
// char temp_buff[21848] = {0};
/* NOTE: data in the init_buf is really a char?!?! s->init_buf->data; */
d=(unsigned char *)proxy_key_mat;
// p= &(d[4]);
// p= &(d[0]);
p = d;
n = 0;
for (i = 0; i < s->slices_len; i++) {
s1n(s->slices[i]->slice_id, p);
s2n(EVP_MAX_KEY_LENGTH, p);
memcpy(p, s->slices[i]->read_mat, EVP_MAX_KEY_LENGTH);
p += EVP_MAX_KEY_LENGTH;
s2n(EVP_MAX_KEY_LENGTH, p);
memcpy(p, s->slices[i]->write_mat, EVP_MAX_KEY_LENGTH);
p += EVP_MAX_KEY_LENGTH;
}
// n = p-d -4;
n = p - d;
return n;
}
void spp_print_buffer(unsigned char *buf, int len) {
printf("(%d) ", len);
while (len-- > 0) {
if (len == 0)
printf("%x",(*(buf++))&0xff);
else
printf("%x:",(*(buf++))&0xff);
}
printf("\n");
}