aboutsummaryrefslogtreecommitdiffstats
path: root/src/gnutls_hooks.c
blob: 7c638fbd35ac140790ac93b190aa439d8f822157 (plain)
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 Christopher Powell
2001-11-28
='#n151'>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 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
/**
 *  Copyright 2004-2005 Paul Querna
 *  Copyright 2007 Nikos Mavrogiannopoulos
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */

#include "mod_gnutls.h"
#include "http_vhost.h"
#include "ap_mpm.h"

#if APR_HAS_THREADS
# if GNUTLS_VERSION_MAJOR <= 2 && GNUTLS_VERSION_MINOR < 11
#include <gcrypt.h>
GCRY_THREAD_OPTION_PTHREAD_IMPL;
# endif
#endif

#if !USING_2_1_RECENT
extern server_rec *ap_server_conf;
#endif

#if MOD_GNUTLS_DEBUG
static apr_file_t *debug_log_fp;
#endif

static int mpm_is_threaded;
static gnutls_datum session_ticket_key = { NULL, 0 };

static int mgs_cert_verify(request_rec * r, mgs_handle_t * ctxt);
/* use side==0 for server and side==1 for client */
static void mgs_add_common_cert_vars(request_rec * r, gnutls_x509_crt_t cert,
				     int side,
				     int export_certificates_enabled);
static void mgs_add_common_pgpcert_vars(request_rec * r, gnutls_openpgp_crt_t cert,
				     int side,
				     int export_certificates_enabled);

static apr_status_t mgs_cleanup_pre_config(void *data)
{
    gnutls_free(session_ticket_key.data);
    session_ticket_key.data = NULL;
    session_ticket_key.size = 0;
    gnutls_global_deinit();
    return APR_SUCCESS;
}

#if MOD_GNUTLS_DEBUG
static void gnutls_debug_log_all(int level, const char *str)
{
    apr_file_printf(debug_log_fp, "<%d> %s\n", level, str);
}
#define _gnutls_log apr_file_printf
#else
# define _gnutls_log(...) 
#endif

int
mgs_hook_pre_config(apr_pool_t * pconf,
		    apr_pool_t * plog, apr_pool_t * ptemp)
{
int ret;

#if MOD_GNUTLS_DEBUG
    apr_file_open(&debug_log_fp, "/tmp/gnutls_debug",
		  APR_APPEND | APR_WRITE | APR_CREATE, APR_OS_DEFAULT,
		  pconf);

    _gnutls_log(debug_log_fp, "%s: %d\n", __func__, __LINE__);

    gnutls_global_set_log_level(9);
    gnutls_global_set_log_function(gnutls_debug_log_all);
    _gnutls_log(debug_log_fp, "gnutls: %s\n", gnutls_check_version(NULL));
#endif

#if APR_HAS_THREADS
    ap_mpm_query(AP_MPMQ_IS_THREADED, &mpm_is_threaded);
#if (GNUTLS_VERSION_MAJOR == 2 && GNUTLS_VERSION_MINOR < 11) || GNUTLS_VERSION_MAJOR < 2
    if (mpm_is_threaded) {
	gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
    }
#endif
#else
    mpm_is_threaded = 0;
#endif


    if (gnutls_check_version(LIBGNUTLS_VERSION)==NULL) {
        _gnutls_log(debug_log_fp, "gnutls_check_version() failed. Required: gnutls-%s Found: gnutls-%s\n", 
          LIBGNUTLS_VERSION, gnutls_check_version(NULL));
        return -3;
    }

    ret = gnutls_global_init();
    if (ret < 0) {
        _gnutls_log(debug_log_fp, "gnutls_global_init: %s\n", gnutls_strerror(ret));
        return -3;
    }
    
    ret = gnutls_session_ticket_key_generate( &session_ticket_key);
    if (ret < 0) {
        _gnutls_log(debug_log_fp, "gnutls_session_ticket_key_generate: %s\n", gnutls_strerror(ret));
    }

    apr_pool_cleanup_register(pconf, NULL, mgs_cleanup_pre_config,
			      apr_pool_cleanup_null);


    return OK;
}

static int mgs_select_virtual_server_cb(gnutls_session_t session)
{
    mgs_handle_t *ctxt;
    mgs_srvconf_rec *tsc;
    int ret;
    int cprio[2];

    _gnutls_log(debug_log_fp, "%s: %d\n", __func__, __LINE__);

    ctxt = gnutls_transport_get_ptr(session);

    /* find the virtual server */
    tsc = mgs_find_sni_server(session);

    if (tsc != NULL)
	ctxt->sc = tsc;

    gnutls_certificate_server_set_request(session,
					      ctxt->sc->client_verify_mode);

    /* set the new server credentials 
     */

    gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE,
			   ctxt->sc->certs);

    gnutls_credentials_set(session, GNUTLS_CRD_ANON, ctxt->sc->anon_creds);

#ifdef ENABLE_SRP
    if (ctxt->sc->srp_tpasswd_conf_file != NULL
	&& ctxt->sc->srp_tpasswd_file != NULL) {
	gnutls_credentials_set(session, GNUTLS_CRD_SRP,
			       ctxt->sc->srp_creds);
    }
#endif

    /* update the priorities - to avoid negotiating a ciphersuite that is not
     * enabled on this virtual server. Note that here we ignore the version
     * negotiation.
     */   
    ret = gnutls_priority_set(session, ctxt->sc->priorities);
    /* actually it shouldn't fail since we have checked at startup */
    if (ret < 0)
	return ret;

    /* If both certificate types are not present disallow them from
     * being negotiated.
     */
    if (ctxt->sc->certs_x509[0] != NULL && ctxt->sc->cert_pgp == NULL) {
        cprio[0] = GNUTLS_CRT_X509;
        cprio[1] = 0;
        gnutls_certificate_type_set_priority( session, cprio);
    } else if (ctxt->sc->cert_pgp != NULL && ctxt->sc->certs_x509[0]==NULL) {
        cprio[0] = GNUTLS_CRT_OPENPGP;
        cprio[1] = 0;
        gnutls_certificate_type_set_priority( session, cprio);
    }

    return 0;
}

static int cert_retrieve_fn(gnutls_session_t session, gnutls_retr_st * ret)
{
    mgs_handle_t *ctxt;

    _gnutls_log(debug_log_fp, "%s: %d\n", __func__, __LINE__);
    ctxt = gnutls_transport_get_ptr(session);

    if (ctxt == NULL)
        return GNUTLS_E_INTERNAL_ERROR;

    if (gnutls_certificate_type_get( session) == GNUTLS_CRT_X509) {
        ret->type = GNUTLS_CRT_X509;
        ret->ncerts = ctxt->sc->certs_x509_num;
        ret->deinit_all = 0;

        ret->cert.x509 = ctxt->sc->certs_x509;
        ret->key.x509 = ctxt->sc->privkey_x509;
        
        return 0;
    } else if (gnutls_certificate_type_get( session) == GNUTLS_CRT_OPENPGP) {
        ret->type = GNUTLS_CRT_OPENPGP;
        ret->ncerts = 1;
        ret->deinit_all = 0;

        ret->cert.pgp = ctxt->sc->cert_pgp;
        ret->key.pgp = ctxt->sc->privkey_pgp;

        return 0;
    
    }

    return GNUTLS_E_INTERNAL_ERROR;
}

/* 2048-bit group parameters from SRP specification */
const char static_dh_params[] = "-----BEGIN DH PARAMETERS-----\n"
    "MIIBBwKCAQCsa9tBMkqam/Fm3l4TiVgvr3K2ZRmH7gf8MZKUPbVgUKNzKcu0oJnt\n"
    "gZPgdXdnoT3VIxKrSwMxDc1/SKnaBP1Q6Ag5ae23Z7DPYJUXmhY6s2YaBfvV+qro\n"
    "KRipli8Lk7hV+XmT7Jde6qgNdArb9P90c1nQQdXDPqcdKB5EaxR3O8qXtDoj+4AW\n"
    "dr0gekNsZIHx0rkHhxdGGludMuaI+HdIVEUjtSSw1X1ep3onddLs+gMs+9v1L7N4\n"
    "YWAnkATleuavh05zA85TKZzMBBx7wwjYKlaY86jQw4JxrjX46dv7tpS1yAPYn3rk\n"
    "Nd4jbVJfVHWbZeNy/NaO8g+nER+eSv9zAgEC\n"
    "-----END DH PARAMETERS-----\n";

/* Read the common name or the alternative name of the certificate.
 * We only support a single name per certificate.
 *
 * Returns negative on error.
 */
static int read_crt_cn(server_rec * s, apr_pool_t * p,
		       gnutls_x509_crt_t cert, char **cert_cn)
{
    int rv = 0, i;
    size_t data_len;


    _gnutls_log(debug_log_fp,   "%s: %d\n", __func__, __LINE__);
    *cert_cn = NULL;

    data_len = 0;
    rv = gnutls_x509_crt_get_dn_by_oid(cert,
				       GNUTLS_OID_X520_COMMON_NAME,
				       0, 0, NULL, &data_len);

    if (rv == GNUTLS_E_SHORT_MEMORY_BUFFER && data_len > 1) {
	*cert_cn = apr_palloc(p, data_len);
	rv = gnutls_x509_crt_get_dn_by_oid(cert,
					   GNUTLS_OID_X520_COMMON_NAME, 0,
					   0, *cert_cn, &data_len);
    } else {			/* No CN return subject alternative name */
	ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
		     "No common name found in certificate for '%s:%d'. Looking for subject alternative name...",
		     s->server_hostname, s->port);
	rv = 0;
	/* read subject alternative name */
	for (i = 0; !(rv < 0); i++) {
	    data_len = 0;
	    rv = gnutls_x509_crt_get_subject_alt_name(cert, i,
						      NULL, &data_len,
						      NULL);

	    if (rv == GNUTLS_E_SHORT_MEMORY_BUFFER && data_len > 1) {
		/* FIXME: not very efficient. What if we have several alt names
		 * before DNSName?
		 */
		*cert_cn = apr_palloc(p, data_len + 1);

		rv = gnutls_x509_crt_get_subject_alt_name(cert, i,
							  *cert_cn,
							  &data_len, NULL);
		(*cert_cn)[data_len] = 0;

		if (rv == GNUTLS_SAN_DNSNAME)
		    break;
	    }
	}
    }

    return rv;
}

static int read_pgpcrt_cn(server_rec * s, apr_pool_t * p,
		       gnutls_openpgp_crt_t cert, char **cert_cn)
{
    int rv = 0;
    size_t data_len;


    _gnutls_log(debug_log_fp, "%s: %d\n", __func__, __LINE__);
    *cert_cn = NULL;

    data_len = 0;
    rv = gnutls_openpgp_crt_get_name(cert, 0, NULL, &data_len);

    if (rv == GNUTLS_E_SHORT_MEMORY_BUFFER && data_len > 1) {
	*cert_cn = apr_palloc(p, data_len);
	rv = gnutls_openpgp_crt_get_name(cert, 0, *cert_cn, &data_len);
    } else {			/* No CN return subject alternative name */
	ap_log_error(APLOG_MARK, APLOG_INFO, 0, s,
		     "No name found in PGP certificate for '%s:%d'.",
		     s->server_hostname, s->port);
    }

    return rv;
}


int
mgs_hook_post_config(apr_pool_t * p, apr_pool_t * plog,
		     apr_pool_t * ptemp, server_rec * base_server)
{
    int rv;
    server_rec *s;
    gnutls_dh_params_t dh_params = NULL;
    gnutls_rsa_params_t rsa_params = NULL;
    mgs_srvconf_rec *sc;
    mgs_srvconf_rec *sc_base;
    void *data = NULL;
    int first_run = 0;
    const char *userdata_key = "mgs_init";

    _gnutls_log(debug_log_fp, "%s: %d\n", __func__, __LINE__);
    apr_pool_userdata_get(&data, userdata_key, base_server->process->pool);
    if (data == NULL) {
	first_run = 1;
	apr_pool_userdata_set((const void *) 1, userdata_key,
			      apr_pool_cleanup_null,
			      base_server->process->pool);
    }


    {
	s = base_server;
	sc_base =
	    (mgs_srvconf_rec *) ap_get_module_config(s->module_config,
						     &gnutls_module);

	gnutls_dh_params_init(&dh_params);

	if (sc_base->dh_params == NULL) {
	    gnutls_datum pdata = { (void *) static_dh_params, sizeof(static_dh_params) };
            /* loading defaults */
            rv = gnutls_dh_params_import_pkcs3(dh_params, &pdata,
					       GNUTLS_X509_FMT_PEM);

            if (rv < 0) {
  	        ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, s,
		     "GnuTLS: Unable to load DH Params: (%d) %s",
		     rv, gnutls_strerror(rv));
                exit(rv);
            }
        } else dh_params = sc_base->dh_params;

        if (sc_base->rsa_params != NULL) 
            rsa_params = sc_base->rsa_params;

	/* else not an error but RSA-EXPORT ciphersuites are not available 
	 */

	rv = mgs_cache_post_config(p, s, sc_base);
	if (rv != 0) {
	    ap_log_error(APLOG_MARK, APLOG_STARTUP, rv, s,
			 "GnuTLS: Post Config for GnuTLSCache Failed."
			 " Shutting Down.");
	    exit(-1);
	}

	for (s = base_server; s; s = s->next) {
	    void *load = NULL;
	    sc = (mgs_srvconf_rec *) ap_get_module_config(s->module_config,
							  &gnutls_module);
	    sc->cache_type = sc_base->cache_type;
	    sc->cache_config = sc_base->cache_config;

            /* Check if the priorities have been set */
            if (sc->priorities == NULL && sc->enabled == GNUTLS_ENABLED_TRUE) {
                ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, s,
		     "GnuTLS: Host '%s:%d' is missing the GnuTLSPriorities directive!",
			     s->server_hostname, s->port);
		exit(-1);
            }
            
            /* Check if DH or RSA params have been set per host */
            if (sc->rsa_params != NULL)
                load = sc->rsa_params;
            else if (rsa_params) load = rsa_params;
            
            if (load != NULL)
		gnutls_certificate_set_rsa_export_params(sc->certs, load);


            load = NULL;
            if (sc->dh_params != NULL)
                load = sc->dh_params;
            else if (dh_params) load = dh_params;
            
            if (load != NULL) { /* not needed but anyway */
	        gnutls_certificate_set_dh_params(sc->certs, load);
	        gnutls_anon_set_server_dh_params(sc->anon_creds, load);
            }

	    gnutls_certificate_server_set_retrieve_function(sc->certs,
							    cert_retrieve_fn);

#ifdef ENABLE_SRP
	    if (sc->srp_tpasswd_conf_file != NULL
		&& sc->srp_tpasswd_file != NULL) {
		rv = gnutls_srp_set_server_credentials_file(sc->srp_creds,
							    sc->
							    srp_tpasswd_file,
							    sc->
							    srp_tpasswd_conf_file);

		if (rv < 0 && sc->enabled == GNUTLS_ENABLED_TRUE) {
		    ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, s,
				 "[GnuTLS] - Host '%s:%d' is missing a "
				 "SRP password or conf File!",
				 s->server_hostname, s->port);
		    exit(-1);
		}
	    }
#endif

	    if (sc->certs_x509[0] == NULL &&
	        sc->cert_pgp == NULL &&
		sc->enabled == GNUTLS_ENABLED_TRUE) {
		ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, s,
			     "[GnuTLS] - Host '%s:%d' is missing a "
			     "Certificate File!", s->server_hostname,
			     s->port);
		exit(-1);
	    }

	    if (sc->enabled == GNUTLS_ENABLED_TRUE && 
	      ((sc->certs_x509[0] != NULL && sc->privkey_x509 == NULL) ||
	      (sc->cert_pgp != NULL && sc->privkey_pgp == NULL))) {
		ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, s,
			     "[GnuTLS] - Host '%s:%d' is missing a "
			     "Private Key File!",
			     s->server_hostname, s->port);
		exit(-1);
	    }

	    if (sc->enabled == GNUTLS_ENABLED_TRUE) {
		rv = read_crt_cn(s, p, sc->certs_x509[0], &sc->cert_cn);
		if (rv < 0 && sc->cert_pgp != NULL)  /* try openpgp certificate */
    		    rv = read_pgpcrt_cn(s, p, sc->cert_pgp, &sc->cert_cn);

		if (rv < 0) {
		    ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, s,
				 "[GnuTLS] - Cannot find a certificate for host '%s:%d'!",
				 s->server_hostname, s->port);
		    sc->cert_cn = NULL;
		    continue;
		}
	    }
	}
    }

    ap_add_version_component(p, "mod_gnutls/" MOD_GNUTLS_VERSION);

    return OK;
}

void mgs_hook_child_init(apr_pool_t * p, server_rec * s)
{
    apr_status_t rv = APR_SUCCESS;
    mgs_srvconf_rec *sc = ap_get_module_config(s->module_config,
					       &gnutls_module);

    _gnutls_log(debug_log_fp,   "%s: %d\n", __func__, __LINE__);
    if (sc->cache_type != mgs_cache_none) {
	rv = mgs_cache_child_init(p, s, sc);
	if (rv != APR_SUCCESS) {
	    ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s,
			 "[GnuTLS] - Failed to run Cache Init");
	}
    } else {
	ap_log_error(APLOG_MARK, APLOG_CRIT, 0, s,
		     "[GnuTLS] - No Cache Configured. Hint: GnuTLSCache");
    }
}

const char *mgs_hook_http_scheme(const request_rec * r)
{
    mgs_srvconf_rec *sc;
    
    if (r == NULL)
        return NULL;
    
    sc =
	(mgs_srvconf_rec *) ap_get_module_config(r->server->module_config,
						 &gnutls_module);

    _gnutls_log(debug_log_fp,   "%s: %d\n", __func__, __LINE__);
    if (sc->enabled == GNUTLS_ENABLED_FALSE) {
	return NULL;
    }

    return "https";
}

apr_port_t mgs_hook_default_port(const request_rec * r)
{
    mgs_srvconf_rec *sc;
    
    if (r == NULL)
        return 0;
    
    sc =
	(mgs_srvconf_rec *) ap_get_module_config(r->server->module_config,
						 &gnutls_module);

    _gnutls_log(debug_log_fp,   "%s: %d\n", __func__, __LINE__);
    if (sc->enabled == GNUTLS_ENABLED_FALSE) {
	return 0;
    }

    return 443;
}

#define MAX_HOST_LEN 255

#if USING_2_1_RECENT
typedef struct {
    mgs_handle_t *ctxt;
    mgs_srvconf_rec *sc;
    const char *sni_name;
} vhost_cb_rec;

static int vhost_cb(void *baton, conn_rec * conn, server_rec * s)
{
    mgs_srvconf_rec *tsc;
    vhost_cb_rec *x = baton;

    _gnutls_log(debug_log_fp,   "%s: %d\n", __func__, __LINE__);
    tsc = (mgs_srvconf_rec *) ap_get_module_config(s->module_config,
						   &gnutls_module);

    if (tsc->enabled != GNUTLS_ENABLED_TRUE || tsc->cert_cn == NULL) {
	return 0;
    }

    /* The CN can contain a * -- this will match those too. */
    if (ap_strcasecmp_match(x->sni_name, tsc->cert_cn) == 0) {
	/* found a match */
#if MOD_GNUTLS_DEBUG
	ap_log_error(APLOG_MARK, APLOG_DEBUG, 0,
		     x->ctxt->c->base_server,
		     "GnuTLS: Virtual Host CB: "
		     "'%s' == '%s'", tsc->cert_cn, x->sni_name);
#endif
	/* Because we actually change the server used here, we need to reset
	 * things like ClientVerify.
	 */
	x->sc = tsc;
	/* Shit. Crap. Dammit. We *really* should rehandshake here, as our
	 * certificate structure *should* change when the server changes. 
	 * acccckkkkkk. 
	 */
	return 1;
    } else {
#if MOD_GNUTLS_DEBUG
	ap_log_error(APLOG_MARK, APLOG_DEBUG, 0,
		     x->ctxt->c->base_server,
		     "GnuTLS: Virtual Host CB: "
		     "'%s' != '%s'", tsc->cert_cn, x->sni_name);
#endif

    }
    return 0;
}
#endif

mgs_srvconf_rec *mgs_find_sni_server(gnutls_session_t session)
{
    int rv;
    unsigned int sni_type;
    size_t data_len = MAX_HOST_LEN;
    char sni_name[MAX_HOST_LEN];
    mgs_handle_t *ctxt;
#if USING_2_1_RECENT
    vhost_cb_rec cbx;
#else
    server_rec *s;
    mgs_srvconf_rec *tsc;
#endif

    if (session == NULL)
        return NULL;

    _gnutls_log(debug_log_fp,   "%s: %d\n", __func__, __LINE__);
    ctxt = gnutls_transport_get_ptr(session);

    rv = gnutls_server_name_get(ctxt->session, sni_name,
				&data_len, &sni_type, 0);

    if (rv != 0) {
	return NULL;
    }

    if (sni_type != GNUTLS_NAME_DNS) {
	ap_log_error(APLOG_MARK, APLOG_CRIT, 0,
		     ctxt->c->base_server,
		     "GnuTLS: Unknown type '%d' for SNI: "
		     "'%s'", sni_type, sni_name);
	return NULL;
    }

    /**
     * Code in the Core already sets up the c->base_server as the base
     * for this IP/Port combo.  Trust that the core did the 'right' thing.
     */
#if USING_2_1_RECENT
    cbx.ctxt = ctxt;
    cbx.sc = NULL;
    cbx.sni_name = sni_name;

    rv = ap_vhost_iterate_given_conn(ctxt->c, vhost_cb, &cbx);
    if (rv == 1) {
	return cbx.sc;
    }
#else
    for (s = ap_server_conf; s; s = s->next) {

	tsc = (mgs_srvconf_rec *) ap_get_module_config(s->module_config,
						       &gnutls_module);
	if (tsc->enabled != GNUTLS_ENABLED_TRUE) {
	    continue;
	}
#if MOD_GNUTLS_DEBUG
	ap_log_error(APLOG_MARK, APLOG_DEBUG, 0,
		     ctxt->c->base_server,
		     "GnuTLS: sni-x509 cn: %s/%d pk: %s s: 0x%08X s->n: 0x%08X  sc: 0x%08X",
		     tsc->cert_cn, rv,
		     gnutls_pk_algorithm_get_name
		     (gnutls_x509_privkey_get_pk_algorithm
		      (ctxt->sc->privkey_x509)), (unsigned int) s,
		     (unsigned int) s->next, (unsigned int) tsc);
#endif
	/* The CN can contain a * -- this will match those too. */
	if (ap_strcasecmp_match(sni_name, tsc->cert_cn) == 0) {
#if MOD_GNUTLS_DEBUG
	    ap_log_error(APLOG_MARK, APLOG_DEBUG, 0,
			 ctxt->c->base_server,
			 "GnuTLS: Virtual Host: "
			 "'%s' == '%s'", tsc->cert_cn, sni_name);
#endif
	    return tsc;
	}
    }
#endif
    return NULL;
}


static const int protocol_priority[] = {
    GNUTLS_TLS1_1, GNUTLS_TLS1_0, GNUTLS_SSL3, 0
};


static mgs_handle_t *create_gnutls_handle(apr_pool_t * pool, conn_rec * c)
{
    mgs_handle_t *ctxt;
    mgs_srvconf_rec *sc =
	(mgs_srvconf_rec *) ap_get_module_config(c->base_server->
						 module_config,
						 &gnutls_module);

    _gnutls_log(debug_log_fp,   "%s: %d\n", __func__, __LINE__);
    ctxt = apr_pcalloc(pool, sizeof(*ctxt));
    ctxt->c = c;
    ctxt->sc = sc;
    ctxt->status = 0;

    ctxt->input_rc = APR_SUCCESS;
    ctxt->input_bb = apr_brigade_create(c->pool, c->bucket_alloc);
    ctxt->input_cbuf.length = 0;

    ctxt->output_rc = APR_SUCCESS;
    ctxt->output_bb = apr_brigade_create(c->pool, c->bucket_alloc);
    ctxt->output_blen = 0;
    ctxt->output_length = 0;

    gnutls_init(&ctxt->session, GNUTLS_SERVER);
    if (session_ticket_key.data != NULL && ctxt->sc->tickets != 0)
        gnutls_session_ticket_enable_server(ctxt->session, &session_ticket_key);

    /* because we don't set any default priorities here (we set later at
     * the user hello callback) we need to at least set this in order for
     * gnutls to be able to read packets.
     */
    gnutls_protocol_set_priority(ctxt->session, protocol_priority);

    gnutls_handshake_set_post_client_hello_function(ctxt->session,
						    mgs_select_virtual_server_cb);

    mgs_cache_session_init(ctxt);

    return ctxt;
}

int mgs_hook_pre_connection(conn_rec * c, void *csd)
{
    mgs_handle_t *ctxt;
    mgs_srvconf_rec *sc;

    _gnutls_log(debug_log_fp,   "%s: %d\n", __func__, __LINE__);
    
    if (c == NULL)
        return DECLINED;
    
    sc =
	(mgs_srvconf_rec *) ap_get_module_config(c->base_server->
						 module_config,
						 &gnutls_module);

    if (!(sc && (sc->enabled == GNUTLS_ENABLED_TRUE))) {
	return DECLINED;
    }

    if(c->remote_addr->hostname)
      /* Connection initiated by Apache (mod_proxy) => ignore */
      return OK;

    ctxt = create_gnutls_handle(c->pool, c);

    ap_set_module_config(c->conn_config, &gnutls_module, ctxt);

    gnutls_transport_set_pull_function(ctxt->session, mgs_transport_read);
    gnutls_transport_set_push_function(ctxt->session, mgs_transport_write);
    gnutls_transport_set_ptr(ctxt->session, ctxt);

    ctxt->input_filter =
	ap_add_input_filter(GNUTLS_INPUT_FILTER_NAME, ctxt, NULL, c);
    ctxt->output_filter =
	ap_add_output_filter(GNUTLS_OUTPUT_FILTER_NAME, ctxt, NULL, c);

    return OK;
}

int mgs_hook_fixups(request_rec * r)
{
    unsigned char sbuf[GNUTLS_MAX_SESSION_ID];
    char buf[AP_IOBUFSIZE];
    const char *tmp;
    size_t len;
    mgs_handle_t *ctxt;
    int rv = OK;

    if (r == NULL)
        return DECLINED;

    _gnutls_log(debug_log_fp,   "%s: %d\n", __func__, __LINE__);
    apr_table_t *env = r->subprocess_env;

    ctxt =
	ap_get_module_config(r->connection->conn_config, &gnutls_module);

    if (!ctxt || ctxt->session == NULL) {
	return DECLINED;
    }

    apr_table_setn(env, "HTTPS", "on");

    apr_table_setn(env, "SSL_VERSION_LIBRARY",
		   "GnuTLS/" LIBGNUTLS_VERSION);
    apr_table_setn(env, "SSL_VERSION_INTERFACE",
		   "mod_gnutls/" MOD_GNUTLS_VERSION);

    apr_table_setn(env, "SSL_PROTOCOL",
		   gnutls_protocol_get_name(gnutls_protocol_get_version
					    (ctxt->session)));

    /* should have been called SSL_CIPHERSUITE instead */
    apr_table_setn(env, "SSL_CIPHER",
		   gnutls_cipher_suite_get_name(gnutls_kx_get
						(ctxt->session),
						gnutls_cipher_get(ctxt->
								  session),
						gnutls_mac_get(ctxt->
							       session)));

    apr_table_setn(env, "SSL_COMPRESS_METHOD",
		   gnutls_compression_get_name(gnutls_compression_get
					       (ctxt->session)));

#ifdef ENABLE_SRP
    tmp = gnutls_srp_server_get_username(ctxt->session);
    apr_table_setn(env, "SSL_SRP_USER", (tmp!=NULL)?tmp:"");
#endif

    if (apr_table_get(env, "SSL_CLIENT_VERIFY") == NULL)
	apr_table_setn(env, "SSL_CLIENT_VERIFY", "NONE");

    unsigned int key_size =
	8 * gnutls_cipher_get_key_size(gnutls_cipher_get(ctxt->session));
    tmp = apr_psprintf(r->pool, "%u", key_size);

    apr_table_setn(env, "SSL_CIPHER_USEKEYSIZE", tmp);

    apr_table_setn(env, "SSL_CIPHER_ALGKEYSIZE", tmp);

    apr_table_setn(env, "SSL_CIPHER_EXPORT",
		   (key_size <= 40) ? "true" : "false");

    len = sizeof(sbuf);
    gnutls_session_get_id(ctxt->session, sbuf, &len);
    tmp = mgs_session_id2sz(sbuf, len, buf, sizeof(buf));
    apr_table_setn(env, "SSL_SESSION_ID", apr_pstrdup(r->pool, tmp));

    if (gnutls_certificate_type_get( ctxt->session) == GNUTLS_CRT_X509)
        mgs_add_common_cert_vars(r, ctxt->sc->certs_x509[0], 0,
			     ctxt->sc->export_certificates_enabled);
    else if (gnutls_certificate_type_get( ctxt->session) == GNUTLS_CRT_OPENPGP)
        mgs_add_common_pgpcert_vars(r, ctxt->sc->cert_pgp, 0,
			     ctxt->sc->export_certificates_enabled);

    return rv;
}

int mgs_hook_authz(request_rec * r)
{
    int rv;
    mgs_handle_t *ctxt;
    mgs_dirconf_rec *dc;
    
    if (r == NULL)
        return DECLINED;
    
    dc = ap_get_module_config(r->per_dir_config,
					       &gnutls_module);

    _gnutls_log(debug_log_fp,   "%s: %d\n", __func__, __LINE__);
    ctxt =
	ap_get_module_config(r->connection->conn_config, &gnutls_module);

    if (!ctxt || ctxt->session == NULL) {
	return DECLINED;
    }

    if (dc->client_verify_mode == GNUTLS_CERT_IGNORE) {
	ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
		      "GnuTLS: Directory set to Ignore Client Certificate!");
    } else {
	if (ctxt->sc->client_verify_mode < dc->client_verify_mode) {
	    ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
			  "GnuTLS: Attempting to rehandshake with peer. %d %d",
			  ctxt->sc->client_verify_mode,
			  dc->client_verify_mode);

            /* If we already have a client certificate, there's no point in
             * re-handshaking... */
            rv = mgs_cert_verify(r, ctxt);
            if (rv != DECLINED && rv != HTTP_FORBIDDEN)
                return rv;

	    gnutls_certificate_server_set_request(ctxt->session,
						  dc->client_verify_mode);

	    if (mgs_rehandshake(ctxt) != 0) {
		return HTTP_FORBIDDEN;
	    }
	} else if (ctxt->sc->client_verify_mode == GNUTLS_CERT_IGNORE) {
#if MOD_GNUTLS_DEBUG
	    ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r,
			  "GnuTLS: Peer is set to IGNORE");
#endif
	    return DECLINED;
	}
	rv = mgs_cert_verify(r, ctxt);
	if (rv != DECLINED &&
	    (rv != HTTP_FORBIDDEN ||
	     dc->client_verify_mode == GNUTLS_CERT_REQUIRE)) {
	    return rv;
	}
    }

    return DECLINED;
}

/* variables that are not sent by default:
 *
 * SSL_CLIENT_CERT 	string 	PEM-encoded client certificate
 * SSL_SERVER_CERT 	string 	PEM-encoded client certificate
 */

/* side is either 0 for SERVER or 1 for CLIENT
 */
#define MGS_SIDE ((side==0)?"SSL_SERVER":"SSL_CLIENT")
static void
mgs_add_common_cert_vars(request_rec * r, gnutls_x509_crt_t cert, int side,
			 int export_certificates_enabled)
{
    unsigned char sbuf[64];	/* buffer to hold serials */
    char buf[AP_IOBUFSIZE];
    const char *tmp;
    char *tmp2;
    size_t len;
    int ret, i;

    if (r == NULL)
        return;

    apr_table_t *env = r->subprocess_env;

    _gnutls_log(debug_log_fp,   "%s: %d\n", __func__, __LINE__);
    if (export_certificates_enabled != 0) {
	char cert_buf[10 * 1024];
	len = sizeof(cert_buf);

	if (gnutls_x509_crt_export
	    (cert, GNUTLS_X509_FMT_PEM, cert_buf, &len) >= 0)
	    apr_table_setn(env,
			   apr_pstrcat(r->pool, MGS_SIDE, "_CERT", NULL),
			   apr_pstrmemdup(r->pool, cert_buf, len));

    }

    len = sizeof(buf);
    gnutls_x509_crt_get_dn(cert, buf, &len);
    apr_table_setn(env, apr_pstrcat(r->pool, MGS_SIDE, "_S_DN", NULL),
		   apr_pstrmemdup(r->pool, buf, len));

    len = sizeof(buf);
    gnutls_x509_crt_get_issuer_dn(cert, buf, &len);
    apr_table_setn(env, apr_pstrcat(r->pool, MGS_SIDE, "_I_DN", NULL),
		   apr_pstrmemdup(r->pool, buf, len));

    len = sizeof(sbuf);
    gnutls_x509_crt_get_serial(cert, sbuf, &len);
    tmp = mgs_session_id2sz(sbuf, len, buf, sizeof(buf));
    apr_table_setn(env, apr_pstrcat(r->pool, MGS_SIDE, "_M_SERIAL", NULL),
		   apr_pstrdup(r->pool, tmp));

    ret = gnutls_x509_crt_get_version(cert);
    if (ret > 0)
	apr_table_setn(env,
		       apr_pstrcat(r->pool, MGS_SIDE, "_M_VERSION", NULL),
		       apr_psprintf(r->pool, "%u", ret));

    apr_table_setn(env,
       apr_pstrcat(r->pool, MGS_SIDE, "_CERT_TYPE", NULL), "X.509");

    tmp =
	mgs_time2sz(gnutls_x509_crt_get_expiration_time
		    (cert), buf, sizeof(buf));
    apr_table_setn(env, apr_pstrcat(r->pool, MGS_SIDE, "_V_END", NULL),
		   apr_pstrdup(r->pool, tmp));

    tmp =
	mgs_time2sz(gnutls_x509_crt_get_activation_time
		    (cert), buf, sizeof(buf));
    apr_table_setn(env, apr_pstrcat(r->pool, MGS_SIDE, "_V_START", NULL),
		   apr_pstrdup(r->pool, tmp));

    ret = gnutls_x509_crt_get_signature_algorithm(cert);
    if (ret >= 0) {
	apr_table_setn(env, apr_pstrcat(r->pool, MGS_SIDE, "_A_SIG", NULL),
		       gnutls_sign_algorithm_get_name(ret));
    }

    ret = gnutls_x509_crt_get_pk_algorithm(cert, NULL);
    if (ret >= 0) {
	apr_table_setn(env, apr_pstrcat(r->pool, MGS_SIDE, "_A_KEY", NULL),
		       gnutls_pk_algorithm_get_name(ret));
    }

    /* export all the alternative names (DNS, RFC822 and URI) */
    for (i = 0; !(ret < 0); i++) {
	len = 0;
	ret = gnutls_x509_crt_get_subject_alt_name(cert, i,
						   NULL, &len, NULL);

	if (ret == GNUTLS_E_SHORT_MEMORY_BUFFER && len > 1) {
	    tmp2 = apr_palloc(r->pool, len + 1);

	    ret =
		gnutls_x509_crt_get_subject_alt_name(cert, i, tmp2, &len,
						     NULL);
	    tmp2[len] = 0;

	    if (ret == GNUTLS_SAN_DNSNAME) {
		apr_table_setn(env,
		       apr_psprintf(r->pool, "%s_S_AN%u", MGS_SIDE, i), 
		       apr_psprintf(r->pool, "DNSNAME:%s", tmp2));
	    } else if (ret == GNUTLS_SAN_RFC822NAME) {
		apr_table_setn(env,
		       apr_psprintf(r->pool, "%s_S_AN%u", MGS_SIDE, i), 
		       apr_psprintf(r->pool, "RFC822NAME:%s", tmp2));
	    } else if (ret == GNUTLS_SAN_URI) {
		apr_table_setn(env,
		       apr_psprintf(r->pool, "%s_S_AN%u", MGS_SIDE, i), 
		       apr_psprintf(r->pool, "URI:%s", tmp2));
            } else {
		apr_table_setn(env,
		       apr_psprintf(r->pool, "%s_S_AN%u", MGS_SIDE, i), 
		       "UNSUPPORTED");
            }
	}
    }
}

static void
mgs_add_common_pgpcert_vars(request_rec * r, gnutls_openpgp_crt_t cert, int side,
			 int export_certificates_enabled)
{
    unsigned char sbuf[64];	/* buffer to hold serials */
    char buf[AP_IOBUFSIZE];
    const char *tmp;
    size_t len;
    int ret;
    
    if (r == NULL)
        return;

    _gnutls_log(debug_log_fp,   "%s: %d\n", __func__, __LINE__);
    apr_table_t *env = r->subprocess_env;

    if (export_certificates_enabled != 0) {
	char cert_buf[10 * 1024];
	len = sizeof(cert_buf);

	if (gnutls_openpgp_crt_export
	    (cert, GNUTLS_OPENPGP_FMT_BASE64, cert_buf, &len) >= 0)
	    apr_table_setn(env,
			   apr_pstrcat(r->pool, MGS_SIDE, "_CERT", NULL),
			   apr_pstrmemdup(r->pool, cert_buf, len));

    }

    len = sizeof(buf);
    gnutls_openpgp_crt_get_name(cert, 0, buf, &len);
    apr_table_setn(env, apr_pstrcat(r->pool, MGS_SIDE, "_NAME", NULL),
		   apr_pstrmemdup(r->pool, buf, len));

    len = sizeof(sbuf);
    gnutls_openpgp_crt_get_fingerprint(cert, sbuf, &len);
    tmp = mgs_session_id2sz(sbuf, len, buf, sizeof(buf));
    apr_table_setn(env, apr_pstrcat(r->pool, MGS_SIDE, "_FINGERPRINT", NULL),
		   apr_pstrdup(r->pool, tmp));

    ret = gnutls_openpgp_crt_get_version(cert);
    if (ret > 0)
	apr_table_setn(env,
		       apr_pstrcat(r->pool, MGS_SIDE, "_M_VERSION", NULL),
		       apr_psprintf(r->pool, "%u", ret));

    apr_table_setn(env,
       apr_pstrcat(r->pool, MGS_SIDE, "_CERT_TYPE", NULL), "OPENPGP");

    tmp =
	mgs_time2sz(gnutls_openpgp_crt_get_expiration_time
		    (cert), buf, sizeof(buf));
    apr_table_setn(env, apr_pstrcat(r->pool, MGS_SIDE, "_V_END", NULL),
		   apr_pstrdup(r->pool, tmp));

    tmp =
	mgs_time2sz(gnutls_openpgp_crt_get_creation_time
		    (cert), buf, sizeof(buf));
    apr_table_setn(env, apr_pstrcat(r->pool, MGS_SIDE, "_V_START", NULL),
		   apr_pstrdup(r->pool, tmp));

    ret = gnutls_openpgp_crt_get_pk_algorithm(cert, NULL);
    if (ret >= 0) {
	apr_table_setn(env, apr_pstrcat(r->pool, MGS_SIDE, "_A_KEY", NULL),
		       gnutls_pk_algorithm_get_name(ret));
    }

}

/* TODO: Allow client sending a X.509 certificate chain */
static int mgs_cert_verify(request_rec * r, mgs_handle_t * ctxt)
{
    const gnutls_datum_t *cert_list;
    unsigned int cert_list_size, status;
    int rv = GNUTLS_E_NO_CERTIFICATE_FOUND, ret;
    unsigned int ch_size = 0;
    union {
      gnutls_x509_crt_t x509[MAX_CHAIN_SIZE];
      gnutls_openpgp_crt_t pgp;
    } cert;
    apr_time_t expiration_time, cur_time;

    if (r == NULL || ctxt == NULL || ctxt->session == NULL)
        return HTTP_FORBIDDEN;

    _gnutls_log(debug_log_fp, "%s: %d\n", __func__, __LINE__);
    cert_list =
	gnutls_certificate_get_peers(ctxt->session, &cert_list_size);

    if (cert_list == NULL || cert_list_size == 0) {
	/* It is perfectly OK for a client not to send a certificate if on REQUEST mode
	 */
	if (ctxt->sc->client_verify_mode == GNUTLS_CERT_REQUEST)
	    return OK;

	/* no certificate provided by the client, but one was required. */
	ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r,
		      "GnuTLS: Failed to Verify Peer: "
		      "Client did not submit a certificate");
	return HTTP_FORBIDDEN;
    }

    if (gnutls_certificate_type_get( ctxt->session) == GNUTLS_CRT_X509) {
        ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
            "GnuTLS: A Chain of %d certificate(s) was provided for validation", cert_list_size);

        for (ch_size = 0; ch_size<cert_list_size; ch_size++) {
            gnutls_x509_crt_init(&cert.x509[ch_size]);
            rv = gnutls_x509_crt_import(cert.x509[ch_size], &cert_list[ch_size], GNUTLS_X509_FMT_DER);
            // When failure to import, leave the loop
            if ( rv != GNUTLS_E_SUCCESS ) {
                if (ch_size < 1) {
                    ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r,
                            "GnuTLS: Failed to Verify Peer: "
                            "Failed to import peer certificates.");
                    ret = HTTP_FORBIDDEN;
                    goto exit;
                }
                ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r,
                        "GnuTLS: Failed to import some peer certificates. Using %d certificates", 
                        ch_size);
                rv = GNUTLS_E_SUCCESS;
                break;
            }
        }
    } else if (gnutls_certificate_type_get( ctxt->session) == GNUTLS_CRT_OPENPGP) {
        if (cert_list_size > 1) {
	    ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r,
		      "GnuTLS: Failed to Verify Peer: "
		      "Chained Client Certificates are not supported.");
            return HTTP_FORBIDDEN;
        }

        gnutls_openpgp_crt_init(&cert.pgp);
        rv = gnutls_openpgp_crt_import(cert.pgp, &cert_list[0], GNUTLS_OPENPGP_FMT_RAW);

    } else return HTTP_FORBIDDEN;
 
    if (rv < 0) {
       ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r,
		      "GnuTLS: Failed to Verify Peer: "
		      "Failed to import peer certificates.");
       ret = HTTP_FORBIDDEN;
       goto exit;
    }

    if (gnutls_certificate_type_get( ctxt->session) == GNUTLS_CRT_X509) {
        apr_time_ansi_put(&expiration_time,
		      gnutls_x509_crt_get_expiration_time(cert.x509[0]));

        ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
            "GnuTLS: Verifying list of  %d certificate(s)", ch_size);
        rv = gnutls_x509_crt_list_verify(cert.x509, ch_size, 
                ctxt->sc->ca_list, ctxt->sc->ca_list_size,
                NULL, 0, 0, &status);
    } else {
        apr_time_ansi_put(&expiration_time,
		      gnutls_openpgp_crt_get_expiration_time(cert.pgp));

        rv = gnutls_openpgp_crt_verify_ring(cert.pgp, ctxt->sc->pgp_list,
                      0, &status);
    }

    if (rv < 0) {
	ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r,
		      "GnuTLS: Failed to Verify Peer certificate: (%d) %s",
		      rv, gnutls_strerror(rv));
	if (rv == GNUTLS_E_NO_CERTIFICATE_FOUND)
	    ap_log_rerror(APLOG_MARK, APLOG_EMERG, 0, r,
		      "GnuTLS: No certificate was found for verification. Did you set the GnuTLSX509CAFile or GnuTLSPGPKeyringFile directives?");
	ret = HTTP_FORBIDDEN;
	goto exit;
    }

    /* TODO: X509 CRL Verification. */
    /* May add later if anyone needs it.
     */
    /* ret = gnutls_x509_crt_check_revocation(crt, crl_list, crl_list_size); */

    cur_time = apr_time_now();

    if (status & GNUTLS_CERT_SIGNER_NOT_FOUND) {
	ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r,
		      "GnuTLS: Could not find Signer for Peer Certificate");
    }

    if (status & GNUTLS_CERT_SIGNER_NOT_CA) {
	ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r,
		      "GnuTLS: Peer's Certificate signer is not a CA");
    }

    if (status & GNUTLS_CERT_INSECURE_ALGORITHM) {
	ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r,
		      "GnuTLS: Peer's Certificate is using insecure algorithms");
    }

    if (status & GNUTLS_CERT_EXPIRED || status & GNUTLS_CERT_NOT_ACTIVATED) {
	ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r,
		      "GnuTLS: Peer's Certificate signer is expired or not yet activated");
    }

    if (status & GNUTLS_CERT_INVALID) {
	ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r,
		      "GnuTLS: Peer Certificate is invalid.");
    } else if (status & GNUTLS_CERT_REVOKED) {
	ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r,
		      "GnuTLS: Peer Certificate is revoked.");
    }

    if (gnutls_certificate_type_get( ctxt->session) == GNUTLS_CRT_X509)
        mgs_add_common_cert_vars(r, cert.x509[0], 1,
			     ctxt->sc->export_certificates_enabled);
    else if (gnutls_certificate_type_get( ctxt->session) == GNUTLS_CRT_OPENPGP)
        mgs_add_common_pgpcert_vars(r, cert.pgp, 1,
			     ctxt->sc->export_certificates_enabled);

    {
	/* days remaining */
	unsigned long remain =
	    (apr_time_sec(expiration_time) -
	     apr_time_sec(cur_time)) / 86400;
	apr_table_setn(r->subprocess_env, "SSL_CLIENT_V_REMAIN",
		       apr_psprintf(r->pool, "%lu", remain));
    }

    if (status == 0) {
	apr_table_setn(r->subprocess_env, "SSL_CLIENT_VERIFY", "SUCCESS");
	ret = OK;
    } else {
	apr_table_setn(r->subprocess_env, "SSL_CLIENT_VERIFY", "FAILED");
	if (ctxt->sc->client_verify_mode == GNUTLS_CERT_REQUEST)
	    ret = OK;
	else
	    ret = HTTP_FORBIDDEN;
    }

  exit:
    if (gnutls_certificate_type_get( ctxt->session) == GNUTLS_CRT_X509) {
        int i;
        for (i=0; i<ch_size; i++) {
            gnutls_x509_crt_deinit(cert.x509[i]);
        }
    } else if (gnutls_certificate_type_get( ctxt->session) == GNUTLS_CRT_OPENPGP)
        gnutls_openpgp_crt_deinit(cert.pgp);
    return ret;


}