libtommath: Fix possible integer overflow CVE-2023-36328
[heimdal.git] / kdc / ca.c
blob4402c44677fb0528faaf56f14056dfe8bc83a8fd
1 /*
2 * Copyright (c) 2019 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #include "kdc_locl.h"
35 #include <hex.h>
36 #include <rfc2459_asn1.h>
37 #include "../lib/hx509/hx_locl.h"
39 #include <stdarg.h>
42 * This file implements a singular utility function `kdc_issue_certificate()'
43 * for certificate issuance for kx509 and bx509, which takes a principal name,
44 * an `hx509_request' resulting from parsing a CSR and possibly adding
45 * SAN/EKU/KU extensions, the start/end times of request's authentication
46 * method, and whether to include a full certificate chain in the result.
50 * Get a configuration sub-tree for kx509 based on what's being requested and
51 * by whom.
53 * We have a number of cases:
55 * - default certificate (no CSR used, or no certificate extensions requested)
56 * - for client principals
57 * - for service principals
58 * - client certificate requested (CSR used and client-y SANs/EKUs requested)
59 * - server certificate requested (CSR used and server-y SANs/EKUs requested)
60 * - mixed client/server certificate requested (...)
62 static krb5_error_code
63 get_cf(krb5_context context,
64 const char *app_name,
65 krb5_log_facility *logf,
66 hx509_request req,
67 krb5_principal cprinc,
68 const krb5_config_binding **cf)
70 krb5_error_code ret = ENOTSUP;
71 const char *realm = krb5_principal_get_realm(context, cprinc);
73 *cf = NULL;
74 if (strcmp(app_name, "kdc") == 0)
75 *cf = krb5_config_get_list(context, NULL, app_name, "realms", realm,
76 "kx509", NULL);
77 else
78 *cf = krb5_config_get_list(context, NULL, app_name, "realms", realm,
79 NULL);
80 if (*cf)
81 ret = 0;
82 if (ret) {
83 krb5_log_msg(context, logf, 3, NULL,
84 "No %s configuration for certification authority [%s] "
85 "realm %s -> kx509 -> ...", app_name,
86 strcmp(app_name, "bx509") == 0 ? "bx509" : "kx509",
87 realm);
88 krb5_set_error_message(context, KRB5KDC_ERR_POLICY,
89 "No %s configuration for certification authority [%s] "
90 "realm %s -> kx509 -> ...", app_name,
91 strcmp(app_name, "bx509") == 0 ? "bx509" : "kx509",
92 realm);
94 return ret;
98 * Build a certifate for `principal' and its CSR.
100 KDC_LIB_FUNCTION krb5_error_code KDC_LIB_CALL
101 kdc_issue_certificate(krb5_context context,
102 const char *app_name,
103 krb5_log_facility *logf,
104 hx509_request req,
105 krb5_principal cprinc,
106 krb5_times *auth_times,
107 time_t req_life,
108 int send_chain,
109 hx509_certs *out)
111 const krb5_config_binding *cf;
112 krb5_error_code ret = KRB5KDC_ERR_POLICY;
113 KRB5PrincipalName cprinc2;
115 *out = NULL;
116 cprinc2.principalName = cprinc->name;
117 cprinc2.realm = cprinc->realm;
119 /* Get configuration */
120 ret = get_cf(context, app_name, logf, req, cprinc, &cf);
121 if (ret == 0)
122 ret = _hx509_ca_issue_certificate(context->hx509ctx,
123 (const heim_config_binding *)cf,
124 logf, req, &cprinc2,
125 auth_times->starttime,
126 auth_times->endtime,
127 req_life,
128 send_chain,
129 out);
130 if (ret == EACCES)
131 ret = KRB5KDC_ERR_POLICY;
132 return ret;