The FreeRADIUS server $Id: a4e7f8c94e8038b7627bcf52356b7ed15955c1d7 $
Loading...
Searching...
No Matches
utils.c
Go to the documentation of this file.
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17/**
18 * $Id: 2a52d162414690214097944a345d0168f23a5652 $
19 *
20 * @file tls/utils.c
21 * @brief TLS utility functions
22 *
23 * @copyright 2018 The FreeRADIUS server project
24 */
25
26#include "utils.h"
27#include <openssl/ssl.h>
28
29/** PKEY types (friendly names)
30 *
31 */
33 { L("DH"), EVP_PKEY_DH },
34 { L("DSA"), EVP_PKEY_DSA },
35 { L("EC"), EVP_PKEY_EC },
36 { L("RSA"), EVP_PKEY_RSA }
37};
39
40/** Returns a friendly identifier for the public key type of a certificate
41 *
42 * @param[in] cert The X509 cert to return the type of.
43 * @return the type string.
44 */
45char const *fr_tls_utils_x509_pkey_type(X509 *cert)
46{
47 EVP_PKEY *pkey;
48 int pkey_type;
49 char const *type_str;
50
51 if (!cert) return NULL;
52
53 pkey = X509_get_pubkey(cert);
54 if (!pkey) return NULL;
55
56 pkey_type = EVP_PKEY_type(EVP_PKEY_id(pkey));
57 type_str = fr_table_str_by_value(pkey_types, pkey_type, OBJ_nid2sn(pkey_type));
58 EVP_PKEY_free(pkey);
59
60 return type_str;
61}
62
63/** Returns the OpenSSL keyblock size
64 *
65 * @copyright (c) 2002-2016, Jouni Malinen ("j@w1.fi") and contributors
66 * All Rights Reserved.
67 *
68 * These programs are licensed under the BSD license (the one with
69 * advertisement clause removed).
70 *
71 * this function shamelessly stolen from from
72 * hostap:src/crypto/tls_openssl.c:openssl_get_keyblock_size()
73 *
74 * @param[in] request The current request.
75 * @param[in] ssl The current SSL session.
76 * @return
77 * - -1 problem with the session.
78 * - >=0 length of the block.
79 */
81{
82 const EVP_CIPHER *c;
83 const EVP_MD *h;
84 const SSL_CIPHER *ssl_cipher;
85 int cipher, digest;
86 int mac_key_len, enc_key_len, fixed_iv_len;
87
88 ssl_cipher = SSL_get_current_cipher(ssl);
89 if (!ssl_cipher)
90 return -1;
91 cipher = SSL_CIPHER_get_cipher_nid(ssl_cipher);
92 digest = SSL_CIPHER_get_digest_nid(ssl_cipher);
93 RDEBUG3("(TLS) OpenSSL: cipher nid %d digest nid %d", cipher, digest);
94 if (cipher < 0 || digest < 0)
95 return -1;
96 c = EVP_get_cipherbynid(cipher);
97 if (!c)
98 return -1;
99 enc_key_len = EVP_CIPHER_key_length(c);
100 if (EVP_CIPHER_mode(c) == EVP_CIPH_GCM_MODE ||
101 EVP_CIPHER_mode(c) == EVP_CIPH_CCM_MODE)
102 fixed_iv_len = 4; /* only part of IV from PRF */
103 else
104 fixed_iv_len = EVP_CIPHER_iv_length(c);
105 if (digest == NID_undef) {
106 RDEBUG3("(TLS) OpenSSL: no digest in use (e.g., AEAD)");
107 mac_key_len = 0;
108 } else {
109 h = EVP_get_digestbynid(digest);
110 if (!h)
111 return -1;
112 mac_key_len = EVP_MD_size(h);
113 }
114
115 RDEBUG2("(TLS) OpenSSL: keyblock size: key_len=%d MD_size=%d IV_len=%d",
116 mac_key_len, enc_key_len, fixed_iv_len);
117 return 2 * (mac_key_len + enc_key_len + fixed_iv_len);
118}
119
120/** Convert OpenSSL's ASN1_TIME to an epoch time
121 *
122 * @param[out] out Where to write the time_t.
123 * @param[in] asn1 The ASN1_TIME to convert.
124 * @return
125 * - 0 success.
126 * - -1 on failure.
127 */
128int fr_tls_utils_asn1time_to_epoch(time_t *out, ASN1_TIME const *asn1)
129{
130 struct tm t;
131
132 /*
133 * ASN1_TIME_to_tm() would convert the current time instead.
134 */
135 if (!asn1) {
136 fr_strerror_const("ASN1 time not present");
137 return -1;
138 }
139
140 if (ASN1_TIME_to_tm(asn1, &t) != 1) {
141 fr_strerror_const("Invalid ASN1 time");
142 return -1;
143 }
144
145 /* ASN1_TIME is UTC, so get the UTC time */
146 *out = timegm(&t);
147
148 return 0;
149}
150
151/** Return the static private key password we have configured
152 *
153 * @note This is used as a callback to OpenSSL's PEM_read_PrivateKey function.
154
155 * @param[out] buf Where to write the password to.
156 * @param[in] size The length of buf.
157 * @param[in] rwflag
158 * - 0 if password used for decryption.
159 * - 1 if password used for encryption.
160 * @param[in] u The static password.
161 * @return
162 * - 0 on error.
163 * - >0 on success (the length of the password).
164 */
165int fr_utils_get_private_key_password(char *buf, int size, UNUSED int rwflag, void *u)
166{
167 char *pass;
168 size_t len;
169
170 if (!u) {
171 ERROR("Private key encrypted but no private_key_password configured");
172 return 0;
173 }
174
175 pass = talloc_get_type_abort(u, char);
176 len = talloc_array_length(pass); /* Len includes \0 */
177 if (len > (size_t)size) {
178 ERROR("Password too long. Maximum length is %i bytes", size - 1);
179 return -1;
180 }
181 memcpy(buf, pass, len); /* Copy complete password including \0 byte */
182
183 return (int)(len - 1);
184}
static uint8_t request[4]
#define L(_str)
Helper for initialising arrays of string literals.
Definition build.h:245
#define UNUSED
Definition build.h:415
#define NUM_ELEMENTS(_t)
Definition build.h:437
#define ERROR(fmt,...)
Definition dhcpclient.c:40
#define RDEBUG3(fmt,...)
Definition log.h:360
#define RDEBUG2(fmt,...)
#define fr_table_str_by_value(_table, _number, _def)
Convert an integer to a string.
Definition table.h:804
An element in a lexicographically sorted array of name to num mappings.
Definition table.h:49
#define fr_strerror_const(_msg)
Definition strerror.h:223
int fr_utils_get_private_key_password(char *buf, int size, UNUSED int rwflag, void *u)
Return the static private key password we have configured.
Definition utils.c:165
char const * fr_tls_utils_x509_pkey_type(X509 *cert)
Returns a friendly identifier for the public key type of a certificate.
Definition utils.c:45
int fr_tls_utils_asn1time_to_epoch(time_t *out, ASN1_TIME const *asn1)
Convert OpenSSL's ASN1_TIME to an epoch time.
Definition utils.c:128
int fr_tls_utils_keyblock_size_get(request_t *request, SSL *ssl)
Returns the OpenSSL keyblock size.
Definition utils.c:80
static size_t pkey_types_len
Definition utils.c:38
static fr_table_num_sorted_t const pkey_types[]
PKEY types (friendly names)
Definition utils.c:32
static fr_sbuff_err_t char ** out
Definition value.h:1061
static fr_sbuff_err_t char size_t * len
Definition value.h:1061