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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 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
|
/*
* Copyright (C) 2000, 2004, 2005 Free Software Foundation
*
* Author: Nikos Mavroyanopoulos
*
* This file is part of GNUTLS.
*
* The GNUTLS library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA
*
*/
/* Contains functions that are supposed to pack and unpack session data,
* before and after they are sent to the database backend.
*/
#include <gnutls_int.h>
#ifdef ENABLE_SRP
# include <auth_srp.h>
#endif
#ifdef ENABLE_PSK
# include <auth_psk.h>
#endif
#include <auth_anon.h>
#include <auth_cert.h>
#include <gnutls_errors.h>
#include <gnutls_auth_int.h>
#include <gnutls_session_pack.h>
#include <gnutls_datum.h>
#include <gnutls_num.h>
#define PACK_HEADER_SIZE 1
#define MAX_SEC_PARAMS 7+MAX_SRP_USERNAME+MAX_SERVER_NAME_EXTENSIONS*(3+MAX_SERVER_NAME_SIZE)+165
static int pack_certificate_auth_info (gnutls_session_t,
gnutls_datum_t * packed_session);
static int unpack_certificate_auth_info (gnutls_session_t,
const gnutls_datum_t *
packed_session);
static int unpack_srp_auth_info (gnutls_session_t session,
const gnutls_datum * packed_session);
static int pack_srp_auth_info (gnutls_session_t session,
gnutls_datum * packed_session);
static int unpack_psk_auth_info (gnutls_session_t session,
const gnutls_datum * packed_session);
static int pack_psk_auth_info (gnutls_session_t session,
gnutls_datum * packed_session);
static int unpack_anon_auth_info (gnutls_session_t session,
const gnutls_datum * packed_session);
static int pack_anon_auth_info (gnutls_session_t session,
gnutls_datum * packed_session);
static int unpack_security_parameters (gnutls_session_t session,
const gnutls_datum * packed_session);
static int pack_security_parameters (gnutls_session_t session,
gnutls_datum * packed_session);
/* Since auth_info structures contain malloced data, this function
* is required in order to pack these structures in a vector in
* order to store them to the DB.
*
* packed_session will contain the session data.
*
* The data will be in a platform independent format.
*/
int
_gnutls_session_pack (gnutls_session_t session,
gnutls_datum_t * packed_session)
{
int ret;
if (packed_session == NULL)
{
gnutls_assert ();
return GNUTLS_E_INTERNAL_ERROR;
}
switch (gnutls_auth_get_type (session))
{
#ifdef ENABLE_SRP
case GNUTLS_CRD_SRP:
ret = pack_srp_auth_info (session, packed_session);
if (ret < 0)
{
gnutls_assert ();
return ret;
}
break;
#endif
#ifdef ENABLE_PSK
case GNUTLS_CRD_PSK:
ret = pack_psk_auth_info (session, packed_session);
if (ret < 0)
{
gnutls_assert ();
return ret;
}
break;
#endif
#ifdef ENABLE_ANON
case GNUTLS_CRD_ANON:
ret = pack_anon_auth_info (session, packed_session);
if (ret < 0)
{
gnutls_assert ();
return ret;
}
break;
#endif
case GNUTLS_CRD_CERTIFICATE:
ret = pack_certificate_auth_info (session, packed_session);
if (ret < 0)
{
gnutls_assert ();
return ret;
}
break;
default:
return GNUTLS_E_INTERNAL_ERROR;
}
/* Auth_info structures copied. Now copy security_parameters_st.
* packed_session must have allocated space for the security parameters.
*/
ret = pack_security_parameters (session, packed_session);
if (ret < 0)
{
gnutls_assert ();
_gnutls_free_datum (packed_session);
return ret;
}
return 0;
}
/* Load session data from a buffer.
*/
int
_gnutls_session_unpack (gnutls_session_t session,
const gnutls_datum_t * packed_session)
{
int ret;
if (packed_session == NULL || packed_session->size == 0)
{
gnutls_assert ();
return GNUTLS_E_INTERNAL_ERROR;
}
if (session->key->auth_info != NULL)
{
_gnutls_free_auth_info (session);
}
switch (packed_session->data[0])
{
#ifdef ENABLE_SRP
case GNUTLS_CRD_SRP:
ret = unpack_srp_auth_info (session, packed_session);
if (ret < 0)
{
gnutls_assert ();
return ret;
}
break;
#endif
#ifdef ENABLE_PSK
case GNUTLS_CRD_PSK:
ret = unpack_psk_auth_info (session, packed_session);
if (ret < 0)
{
gnutls_assert ();
return ret;
}
break;
#endif
#ifdef ENABLE_ANON
case GNUTLS_CRD_ANON:
ret = unpack_anon_auth_info (session, packed_session);
if (ret < 0)
{
gnutls_assert ();
return ret;
}
break;
#endif
case GNUTLS_CRD_CERTIFICATE:
ret = unpack_certificate_auth_info (session, packed_session);
if (ret < 0)
{
gnutls_assert ();
return ret;
}
break;
default:
gnutls_assert ();
return GNUTLS_E_INTERNAL_ERROR;
}
/* Auth_info structures copied. Now copy security_parameters_st.
* packed_session must have allocated space for the security parameters.
*/
ret = unpack_security_parameters (session, packed_session);
if (ret < 0)
{
gnutls_assert ();
return ret;
}
return 0;
}
/* Format:
* 1 byte the credentials type
* 4 bytes the size of the whole structure
* DH stuff
* 2 bytes the size of secret key in bits
* 4 bytes the size of the prime
* x bytes the prime
* 4 bytes the size of the generator
* x bytes the generator
* 4 bytes the size of the public key
* x bytes the public key
* RSA stuff
* 4 bytes the size of the modulus
* x bytes the modulus
* 4 bytes the size of the exponent
* x bytes the exponent
* CERTIFICATES
* 4 bytes the length of the certificate list
* 4 bytes the size of first certificate
* x bytes the certificate
* and so on...
*/
static int
pack_certificate_auth_info (gnutls_session_t session,
gnutls_datum_t * packed_session)
{
unsigned int pos = 0, i;
int cert_size, pack_size;
cert_auth_info_t info = _gnutls_get_auth_info (session);
if (info == NULL && session->key->auth_info_size != 0)
{
gnutls_assert ();
return GNUTLS_E_INVALID_REQUEST;
}
if (info)
{
cert_size = 4;
for (i = 0; i < info->ncerts; i++)
cert_size += 4 + info->raw_certificate_list[i].size;
pack_size = 2 + 4 + info->dh.prime.size +
4 + info->dh.generator.size + 4 + info->dh.public_key.size +
4 + info->rsa_export.modulus.size +
4 + info->rsa_export.exponent.size + cert_size;
}
else
pack_size = 0;
packed_session->size = PACK_HEADER_SIZE + pack_size + sizeof (uint32_t);
/* calculate the size and allocate the data.
*/
packed_session->data =
gnutls_malloc (packed_session->size + MAX_SEC_PARAMS);
if (packed_session->data == NULL)
{
gnutls_assert ();
return GNUTLS_E_MEMORY_ERROR;
}
packed_session->data[0] = GNUTLS_CRD_CERTIFICATE;
_gnutls_write_uint32 (pack_size, &packed_session->data[PACK_HEADER_SIZE]);
pos += 4 + PACK_HEADER_SIZE;
if (pack_size > 0)
{
_gnutls_write_uint16 (info->dh.secret_bits, &packed_session->data[pos]);
pos += 2;
_gnutls_write_datum32 (&packed_session->data[pos], info->dh.prime);
pos += 4 + info->dh.prime.size;
_gnutls_write_datum32 (&packed_session->data[pos], info->dh.generator);
pos += 4 + info->dh.generator.size;
_gnutls_write_datum32 (&packed_session->data[pos], info->dh.public_key);
pos += 4 + info->dh.public_key.size;
_gnutls_write_datum32 (&packed_session->data[pos],
info->rsa_export.modulus);
pos += 4 + info->rsa_export.modulus.size;
_gnutls_write_datum32 (&packed_session->data[pos],
info->rsa_export.exponent);
pos += 4 + info->rsa_export.exponent.size;
_gnutls_write_uint32 (info->ncerts, &packed_session->data[pos]);
pos += 4;
for (i = 0; i < info->ncerts; i++)
{
_gnutls_write_datum32 (&packed_session->data[pos],
info->raw_certificate_list[i]);
pos += sizeof (uint32_t) + info->raw_certificate_list[i].size;
}
}
return 0;
}
/* Upack certificate info.
*/
static int
unpack_certificate_auth_info (gnutls_session_t session,
const gnutls_datum_t * packed_session)
{
int pos = 0, size, ret;
unsigned int i, j;
size_t pack_size;
cert_auth_info_t info;
if (packed_session->data[0] != GNUTLS_CRD_CERTIFICATE)
{
gnutls_assert ();
return GNUTLS_E_INVALID_REQUEST;
}
pack_size = _gnutls_read_uint32 (&packed_session->data[PACK_HEADER_SIZE]);
pos += PACK_HEADER_SIZE + 4;
if (pack_size == 0)
return 0; /* nothing to be done */
/* a simple check for integrity */
if (pack_size + PACK_HEADER_SIZE + 4 > packed_session->size)
{
gnutls_assert ();
return GNUTLS_E_INVALID_REQUEST;
}
/* client and server have the same auth_info here
*/
ret =
_gnutls_auth_info_set (session, GNUTLS_CRD_CERTIFICATE,
sizeof (cert_auth_info_st), 1);
if (ret < 0)
{
gnutls_assert ();
return ret;
}
info = _gnutls_get_auth_info (session);
if (info == NULL)
{
gnutls_assert ();
return GNUTLS_E_INTERNAL_ERROR;
}
info->dh.secret_bits = _gnutls_read_uint16 (&packed_session->data[pos]);
pos += 2;
size = _gnutls_read_uint32 (&packed_session->data[pos]);
pos += 4;
ret = _gnutls_set_datum (&info->dh.prime, &packed_session->data[pos], size);
if (ret < 0)
{
gnutls_assert ();
goto error;
}
pos += size;
size = _gnutls_read_uint32 (&packed_session->data[pos]);
pos += 4;
ret =
_gnutls_set_datum (&info->dh.generator, &packed_session->data[pos], size);
if (ret < 0)
{
gnutls_assert ();
goto error;
}
pos += size;
size = _gnutls_read_uint32 (&packed_session->data[pos]);
pos += 4;
ret =
_gnutls_set_datum (&info->dh.public_key, &packed_session->data[pos],
size);
if (ret < 0)
{
gnutls_assert ();
goto error;
}
pos += size;
size = _gnutls_read_uint32 (&packed_session->data[pos]);
pos += 4;
ret =
_gnutls_set_datum (&info->rsa_export.modulus,
&packed_session->data[pos], size);
if (ret < 0)
{
gnutls_assert ();
goto error;
}
pos += size;
size = _gnutls_read_uint32 (&packed_session->data[pos]);
pos += 4;
ret =
_gnutls_set_datum (&info->rsa_export.exponent,
&packed_session->data[pos], size);
if (ret < 0)
{
gnutls_assert ();
goto error;
}
pos += size;
info->ncerts = _gnutls_read_uint32 (&packed_session->data[pos]);
pos += 4;
if (info->ncerts > 0)
{
info->raw_certificate_list =
gnutls_calloc (1, sizeof (gnutls_datum_t) * info->ncerts);
if (info->raw_certificate_list == NULL)
{
gnutls_assert ();
ret = GNUTLS_E_MEMORY_ERROR;
goto error;
}
}
for (i = 0; i < info->ncerts; i++)
{
size = _gnutls_read_uint32 (&packed_session->data[pos]);
pos += sizeof (uint32_t);
ret =
_gnutls_set_datum (&info->raw_certificate_list[i],
&packed_session->data[pos], size);
pos += size;
if (ret < 0)
{
gnutls_assert ();
goto error;
}
}
return 0;
error:
_gnutls_free_datum (&info->dh.prime);
_gnutls_free_datum (&info->dh.generator);
_gnutls_free_datum (&info->dh.public_key);
_gnutls_free_datum (&info->rsa_export.modulus);
_gnutls_free_datum (&info->rsa_export.exponent);
for (j = 0; j < i; j++)
_gnutls_free_datum (&info->raw_certificate_list[j]);
gnutls_free (info->raw_certificate_list);
return ret;
}
#ifdef ENABLE_SRP
/* Packs the SRP session authentication data.
*/
/* Format:
* 1 byte the credentials type
* 4 bytes the size of the SRP username (x)
* x bytes the SRP username
*/
static int
pack_srp_auth_info (gnutls_session_t session, gnutls_datum * packed_session)
{
srp_server_auth_info_t info = _gnutls_get_auth_info (session);
int pack_size;
if (info == NULL && session->key->auth_info_size != 0)
{
gnutls_assert ();
return GNUTLS_E_INVALID_REQUEST;
}
if (info && info->username)
pack_size = strlen (info->username) + 1; /* include the terminating null */
else
pack_size = 0;
packed_session->size = PACK_HEADER_SIZE + pack_size + sizeof (uint32_t);
/* calculate the size and allocate the data.
*/
packed_session->data =
gnutls_malloc (packed_session->size + MAX_SEC_PARAMS);
if (packed_session->data == NULL)
{
gnutls_assert ();
return GNUTLS_E_MEMORY_ERROR;
}
packed_session->data[0] = GNUTLS_CRD_SRP;
_gnutls_write_uint32 (pack_size, &packed_session->data[PACK_HEADER_SIZE]);
if (pack_size > 0)
memcpy (&packed_session->data[PACK_HEADER_SIZE + sizeof (uint32_t)],
info->username, pack_size + 1);
return 0;
}
static int
unpack_srp_auth_info (gnutls_session_t session,
const gnutls_datum * packed_session)
{
size_t username_size;
int ret;
srp_server_auth_info_t info;
if (packed_session->data[0] != GNUTLS_CRD_SRP)
{
gnutls_assert ();
return GNUTLS_E_INVALID_REQUEST;
}
username_size =
_gnutls_read_uint32 (&packed_session->data[PACK_HEADER_SIZE]);
if (username_size == 0)
return 0; /* nothing to be done */
/* a simple check for integrity */
if (username_size + 4 + PACK_HEADER_SIZE > packed_session->size)
{
gnutls_assert ();
return GNUTLS_E_INVALID_REQUEST;
}
ret =
_gnutls_auth_info_set (session, GNUTLS_CRD_SRP,
sizeof (srp_server_auth_info_st), 1);
if (ret < 0)
{
gnutls_assert ();
return ret;
}
info = _gnutls_get_auth_info (session);
if (info == NULL)
{
gnutls_assert ();
return GNUTLS_E_INTERNAL_ERROR;
}
memcpy (info->username,
&packed_session->data[PACK_HEADER_SIZE + sizeof (uint32_t)],
username_size);
return 0;
}
#endif
#ifdef ENABLE_ANON
/* Packs the ANON session authentication data.
*/
/* Format:
* 1 byte the credentials type
* 4 bytes the size of the whole structure
* 2 bytes the size of secret key in bits
* 4 bytes the size of the prime
* x bytes the prime
* 4 bytes the size of the generator
* x bytes the generator
* 4 bytes the size of the public key
* x bytes the public key
*/
static int
pack_anon_auth_info (gnutls_session_t session, gnutls_datum * packed_session)
{
anon_auth_info_t info = _gnutls_get_auth_info (session);
int pos = 0;
size_t pack_size;
if (info == NULL && session->key->auth_info_size != 0)
{
gnutls_assert ();
return GNUTLS_E_INVALID_REQUEST;
}
if (info)
pack_size = 2 + 4 * 3 + info->dh.prime.size +
info->dh.generator.size + info->dh.public_key.size;
else
pack_size = 0;
packed_session->size = PACK_HEADER_SIZE + pack_size + sizeof (uint32_t);
/* calculate the size and allocate the data.
*/
packed_session->data =
gnutls_malloc (packed_session->size + MAX_SEC_PARAMS);
if (packed_session->data == NULL)
{
gnutls_assert ();
return GNUTLS_E_MEMORY_ERROR;
}
packed_session->data[0] = GNUTLS_CRD_ANON;
_gnutls_write_uint32 (pack_size, &packed_session->data[PACK_HEADER_SIZE]);
pos += 4 + PACK_HEADER_SIZE;
if (pack_size > 0)
{
_gnutls_write_uint16 (info->dh.secret_bits, &packed_session->data[pos]);
pos += 2;
_gnutls_write_datum32 (&packed_session->data[pos], info->dh.prime);
pos += 4 + info->dh.prime.size;
_gnutls_write_datum32 (&packed_session->data[pos], info->dh.generator);
pos += 4 + info->dh.generator.size;
_gnutls_write_datum32 (&packed_session->data[pos], info->dh.public_key);
pos += 4 + info->dh.public_key.size;
}
return 0;
}
static int
unpack_anon_auth_info (gnutls_session_t session,
const gnutls_datum * packed_session)
{
size_t pack_size;
int pos = 0, size, ret;
anon_auth_info_t info;
if (packed_session->data[0] != GNUTLS_CRD_ANON)
{
gnutls_assert ();
return GNUTLS_E_INVALID_REQUEST;
}
pack_size = _gnutls_read_uint32 (&packed_session->data[PACK_HEADER_SIZE]);
pos += PACK_HEADER_SIZE + 4;
if (pack_size == 0)
return 0; /* nothing to be done */
/* a simple check for integrity */
if (pack_size + PACK_HEADER_SIZE + 4 > packed_session->size)
{
gnutls_assert ();
return GNUTLS_E_INVALID_REQUEST;
}
/* client and serer have the same auth_info here
*/
ret =
_gnutls_auth_info_set (session, GNUTLS_CRD_ANON,
sizeof (anon_auth_info_st), 1);
if (ret < 0)
{
gnutls_assert ();
return ret;
}
info = _gnutls_get_auth_info (session);
if (info == NULL)
{
gnutls_assert ();
return GNUTLS_E_INTERNAL_ERROR;
}
info->dh.secret_bits = _gnutls_read_uint16 (&packed_session->data[pos]);
pos += 2;
size = _gnutls_read_uint32 (&packed_session->data[pos]);
pos += 4;
ret = _gnutls_set_datum (&info->dh.prime, &packed_session->data[pos], size);
if (ret < 0)
{
gnutls_assert ();
goto error;
}
pos += size;
size = _gnutls_read_uint32 (&packed_session->data[pos]);
pos += 4;
ret =
_gnutls_set_datum (&info->dh.generator, &packed_session->data[pos], size);
if (ret < 0)
{
gnutls_assert ();
goto error;
}
pos += size;
size = _gnutls_read_uint32 (&packed_session->data[pos]);
pos += 4;
ret =
_gnutls_set_datum (&info->dh.public_key, &packed_session->data[pos],
size);
if (ret < 0)
{
gnutls_assert ();
goto error;
}
pos += size;
return 0;
error:
_gnutls_free_datum (&info->dh.prime);
_gnutls_free_datum (&info->dh.generator);
_gnutls_free_datum (&info->dh.public_key);
return ret;
}
#endif /* ANON */
#ifdef ENABLE_PSK
/* Packs the PSK session authentication data.
*/
/* Format:
* 1 byte the credentials type
* 4 bytes the size of the whole structure
* 4 bytes the size of the PSK username (x)
* x bytes the PSK username
* 2 bytes the size of secret key in bits
* 4 bytes the size of the prime
* x bytes the prime
* 4 bytes the size of the generator
* x bytes the generator
* 4 bytes the size of the public key
* x bytes the public key
*/
static int
pack_psk_auth_info (gnutls_session_t session, gnutls_datum * packed_session)
{
psk_auth_info_t info;
int pack_size, username_size = 0, pos;
info = _gnutls_get_auth_info (session);
if (info == NULL && session->key->auth_info_size != 0)
{
gnutls_assert ();
return GNUTLS_E_INVALID_REQUEST;
}
if (info)
{
username_size = strlen (info->username) + 1; /* include the terminating null */
pack_size = username_size +
2 + 4 * 3 + info->dh.prime.size + info->dh.generator.size +
info->dh.public_key.size;
}
else
pack_size = 0;
packed_session->size = PACK_HEADER_SIZE + pack_size + sizeof (uint32_t);
/* calculate the size and allocate the data.
*/
packed_session->data =
gnutls_malloc (packed_session->size + MAX_SEC_PARAMS);
if (packed_session->data == NULL)
{
gnutls_assert ();
return GNUTLS_E_MEMORY_ERROR;
}
pos = 0;
packed_session->data[pos] = GNUTLS_CRD_PSK;
pos++;
_gnutls_write_uint32 (pack_size, &packed_session->data[pos]);
pos += 4;
if (pack_size > 0)
{
_gnutls_write_uint32 (username_size, &packed_session->data[pos]);
pos += 4;
memcpy (&packed_session->data[pos], info->username, username_size);
pos += username_size;
_gnutls_write_uint16 (info->dh.secret_bits, &packed_session->data[pos]);
pos += 2;
_gnutls_write_datum32 (&packed_session->data[pos], info->dh.prime);
pos += 4 + info->dh.prime.size;
_gnutls_write_datum32 (&packed_session->data[pos], info->dh.generator);
pos += 4 + info->dh.generator.size;
_gnutls_write_datum32 (&packed_session->data[pos], info->dh.public_key);
pos += 4 + info->dh.public_key.size;
}
return 0;
}
static int
unpack_psk_auth_info (gnutls_session_t session,
const gnutls_datum * packed_session)
{
size_t username_size;
size_t pack_size;
int pos = 0, size, ret;
psk_auth_info_t info;
if (packed_session->data[0] != GNUTLS_CRD_PSK)
{
gnutls_assert ();
return GNUTLS_E_INVALID_REQUEST;
}
pack_size = _gnutls_read_uint32 (&packed_session->data[PACK_HEADER_SIZE]);
pos += PACK_HEADER_SIZE + 4;
if (pack_size == 0)
return 0; /* nothing to be done */
/* a simple check for integrity */
if (pack_size + PACK_HEADER_SIZE + 4 > packed_session->size)
{
gnutls_assert ();
return GNUTLS_E_INVALID_REQUEST;
}
/* client and serer have the same auth_info here
*/
ret =
_gnutls_auth_info_set (session, GNUTLS_CRD_PSK,
sizeof (psk_auth_info_st), 1);
if (ret < 0)
{
gnutls_assert ();
return ret;
}
info = _gnutls_get_auth_info (session);
if (info == NULL)
{
gnutls_assert ();
return GNUTLS_E_INTERNAL_ERROR;
}
username_size = _gnutls_read_uint32 (&packed_session->data[pos]);
pos += 4;
memcpy (info->username, &packed_session->data[pos], username_size);
pos += username_size;
info->dh.secret_bits = _gnutls_read_uint16 (&packed_session->data[pos]);
pos += 2;
size = _gnutls_read_uint32 (&packed_session->data[pos]);
pos += 4;
ret = _gnutls_set_datum (&info->dh.prime, &packed_session->data[pos], size);
if (ret < 0)
{
gnutls_assert ();
goto error;
}
pos += size;
size = _gnutls_read_uint32 (&packed_session->data[pos]);
pos += 4;
ret =
_gnutls_set_datum (&info->dh.generator, &packed_session->data[pos], size);
if (ret < 0)
{
gnutls_assert ();
goto error;
}
pos += size;
size = _gnutls_read_uint32 (&packed_session->data[pos]);
pos += 4;
ret =
_gnutls_set_datum (&info->dh.public_key, &packed_session->data[pos],
size);
if (ret < 0)
{
gnutls_assert ();
goto error;
}
pos += size;
return 0;
error:
_gnutls_free_datum (&info->dh.prime);
_gnutls_free_datum (&info->dh.generator);
_gnutls_free_datum (&info->dh.public_key);
return ret;
}
#endif
/* Packs the security parameters.
*/
/* Format:
* 4 bytes the total security data size
* 1 byte the entity type (client/server)
* 1 byte the key exchange algorithm used
* 1 byte the read cipher algorithm
* 1 byte the read mac algorithm
* 1 byte the read compression algorithm
*
* 1 byte the write cipher algorithm
* 1 byte the write mac algorithm
* 1 byte the write compression algorithm
*
* 1 byte the certificate type
* 1 byte the protocol version
*
* 2 bytes the cipher suite
*
* 48 bytes the master secret
*
* 32 bytes the client random
* 32 bytes the server random
*
* 1 byte the session ID size
* x bytes the session ID (32 bytes max)
*
* 4 bytes a timestamp
* -------------------
* MAX: 165 bytes
*
* EXTENSIONS:
* 2 bytes the record send size
* 2 bytes the record recv size
*
* 1 byte the SRP username size
* x bytes the SRP username (MAX_SRP_USERNAME)
*
* 2 bytes the number of server name extensions (up to MAX_SERVER_NAME_EXTENSIONS)
* 1 byte the first name type
* 2 bytes the size of the first name
* x bytes the first name (MAX_SERVER_NAME_SIZE)
* and so on...
*
* --------------------
* MAX: 7+MAX_SRP_USERNAME+MAX_SERVER_NAME_EXTENSIONS*(3+MAX_SERVER_NAME_SIZE)
*/
static int
pack_security_parameters (gnutls_session_t session,
gnutls_datum * packed_session)
{
int pos = 0;
size_t len, init, i;
/* move after the auth info stuff.
*/
init =
_gnutls_read_uint32 (&packed_session->data[PACK_HEADER_SIZE]) + 4 +
PACK_HEADER_SIZE;
pos = init + 4; /* make some space to write later the size */
packed_session->data[pos++] = session->security_parameters.entity;
packed_session->data[pos++] = session->security_parameters.kx_algorithm;
packed_session->data[pos++] =
session->security_parameters.read_bulk_cipher_algorithm;
packed_session->data[pos++] =
session->security_parameters.read_mac_algorithm;
packed_session->data[pos++] =
session->security_parameters.read_compression_algorithm;
packed_session->data[pos++] =
session->security_parameters.write_bulk_cipher_algorithm;
packed_session->data[pos++] =
session->security_parameters.write_mac_algorithm;
packed_session->data[pos++] =
session->security_parameters.write_compression_algorithm;
packed_session->data[pos++] =
session->security_parameters.current_cipher_suite.suite[0];
packed_session->data[pos++] =
session->security_parameters.current_cipher_suite.suite[1];
packed_session->data[pos++] = session->security_parameters.cert_type;
packed_session->data[pos++] = session->security_parameters.version;
memcpy (&packed_session->data[pos],
session->security_parameters.master_secret, TLS_MASTER_SIZE);
pos += TLS_MASTER_SIZE;
memcpy (&packed_session->data[pos],
session->security_parameters.client_random, TLS_RANDOM_SIZE);
pos += TLS_RANDOM_SIZE;
memcpy (&packed_session->data[pos],
session->security_parameters.server_random, TLS_RANDOM_SIZE);
pos += TLS_RANDOM_SIZE;
packed_session->data[pos++] = session->security_parameters.session_id_size;
memcpy (&packed_session->data[pos], session->security_parameters.session_id,
session->security_parameters.session_id_size);
pos += session->security_parameters.session_id_size;
_gnutls_write_uint32 (session->security_parameters.timestamp,
&packed_session->data[pos]);
pos += 4;
/* Extensions */
_gnutls_write_uint16 (session->security_parameters.max_record_send_size,
&packed_session->data[pos]);
pos += 2;
_gnutls_write_uint16 (session->security_parameters.max_record_recv_size,
&packed_session->data[pos]);
pos += 2;
/* SRP */
len =
strlen ((char *) session->security_parameters.extensions.srp_username);
packed_session->data[pos++] = len;
memcpy (&packed_session->data[pos],
session->security_parameters.extensions.srp_username, len);
pos += len;
_gnutls_write_uint16 (session->security_parameters.extensions.
server_names_size, &packed_session->data[pos]);
pos += 2;
for (i = 0; i < session->security_parameters.extensions.server_names_size;
i++)
{
packed_session->data[pos++] =
session->security_parameters.extensions.server_names[i].type;
_gnutls_write_uint16 (session->security_parameters.extensions.
server_names[i].name_length,
&packed_session->data[pos]);
pos += 2;
memcpy (&packed_session->data[pos],
session->security_parameters.extensions.server_names[i].name,
session->security_parameters.extensions.server_names[i].
name_length);
pos +=
session->security_parameters.extensions.server_names[i].name_length;
}
/* write the total size */
_gnutls_write_uint32 (pos - init - 4, &packed_session->data[init]);
packed_session->size += pos - init;
return 0;
}
static int
unpack_security_parameters (gnutls_session_t session,
const gnutls_datum * packed_session)
{
size_t pack_size, init, i;
int pos = 0, len;
time_t timestamp = time (0);
/* skip the auth info stuff */
init =
_gnutls_read_uint32 (&packed_session->data[PACK_HEADER_SIZE]) + 4 +
PACK_HEADER_SIZE;
pos = init;
pack_size = _gnutls_read_uint32 (&packed_session->data[pos]);
pos += 4;
if (pack_size == 0)
return GNUTLS_E_INVALID_REQUEST;
/* a simple check for integrity */
if (pack_size > MAX_SEC_PARAMS)
{
gnutls_assert ();
return GNUTLS_E_INVALID_REQUEST;
}
session->internals.resumed_security_parameters.entity =
packed_session->data[pos++];
session->internals.resumed_security_parameters.kx_algorithm =
packed_session->data[pos++];
session->internals.resumed_security_parameters.read_bulk_cipher_algorithm =
packed_session->data[pos++];
session->internals.resumed_security_parameters.read_mac_algorithm =
packed_session->data[pos++];
session->internals.resumed_security_parameters.read_compression_algorithm =
packed_session->data[pos++];
session->internals.resumed_security_parameters.write_bulk_cipher_algorithm =
packed_session->data[pos++];
session->internals.resumed_security_parameters.write_mac_algorithm =
packed_session->data[pos++];
session->internals.resumed_security_parameters.write_compression_algorithm =
packed_session->data[pos++];
session->internals.resumed_security_parameters.current_cipher_suite.
suite[0] = packed_session->data[pos++];
session->internals.resumed_security_parameters.current_cipher_suite.
suite[1] = packed_session->data[pos++];
session->internals.resumed_security_parameters.cert_type =
packed_session->data[pos++];
session->internals.resumed_security_parameters.version =
packed_session->data[pos++];
memcpy (session->internals.resumed_security_parameters.master_secret,
&packed_session->data[pos], TLS_MASTER_SIZE);
pos += TLS_MASTER_SIZE;
memcpy (session->internals.resumed_security_parameters.client_random,
&packed_session->data[pos], TLS_RANDOM_SIZE);
pos += TLS_RANDOM_SIZE;
memcpy (session->internals.resumed_security_parameters.server_random,
&packed_session->data[pos], TLS_RANDOM_SIZE);
pos += TLS_RANDOM_SIZE;
session->internals.resumed_security_parameters.session_id_size =
packed_session->data[pos++];
memcpy (session->internals.resumed_security_parameters.session_id,
&packed_session->data[pos],
session->internals.resumed_security_parameters.session_id_size);
pos += session->internals.resumed_security_parameters.session_id_size;
session->internals.resumed_security_parameters.timestamp =
_gnutls_read_uint32 (&packed_session->data[pos]);
pos += 4;
if (timestamp - session->internals.resumed_security_parameters.timestamp >
session->internals.expire_time
|| session->internals.resumed_security_parameters.timestamp > timestamp)
{
gnutls_assert ();
return GNUTLS_E_EXPIRED;
}
/* Extensions */
session->internals.resumed_security_parameters.max_record_send_size =
_gnutls_read_uint16 (&packed_session->data[pos]);
pos += 2;
session->internals.resumed_security_parameters.max_record_recv_size =
_gnutls_read_uint16 (&packed_session->data[pos]);
pos += 2;
/* SRP */
len = packed_session->data[pos++]; /* srp username length */
memcpy (session->internals.resumed_security_parameters.extensions.
srp_username, &packed_session->data[pos], len);
session->internals.resumed_security_parameters.extensions.
srp_username[len] = 0;
pos += len;
session->internals.resumed_security_parameters.extensions.
server_names_size = _gnutls_read_uint16 (&packed_session->data[pos]);
pos += 2;
for (i = 0;
i <
session->internals.resumed_security_parameters.extensions.
server_names_size; i++)
{
session->internals.resumed_security_parameters.extensions.
server_names[i].type = packed_session->data[pos++];
session->internals.resumed_security_parameters.extensions.
server_names[i].name_length =
_gnutls_read_uint16 (&packed_session->data[pos]);
pos += 2;
memcpy (session->internals.resumed_security_parameters.extensions.
server_names[i].name, &packed_session->data[pos],
session->internals.resumed_security_parameters.extensions.
server_names[i].name_length);
pos +=
session->internals.resumed_security_parameters.extensions.
server_names[i].name_length;
}
return 0;
}
|