2 * ssl_init.c Common OpenSSL initialization code for the various
3 * programs which use it.
5 * Moved from ntpd/ntp_crypto.c crypto_setup()
12 #include <ntp_debug.h>
13 #include <lib_strbuf.h>
16 # include <openssl/crypto.h>
17 # include <openssl/err.h>
18 # include <openssl/evp.h>
19 # include <openssl/opensslv.h>
20 # include "libssl_compat.h"
21 # ifdef HAVE_OPENSSL_CMAC_H
22 # include <openssl/cmac.h>
23 # define CMAC_LENGTH 16
24 # define CMAC "AES128CMAC"
25 # endif /*HAVE_OPENSSL_CMAC_H*/
28 #if OPENSSL_VERSION_NUMBER < 0x10100000L
31 atexit_ssl_cleanup(void)
37 ssl_init_done = FALSE;
47 if ( ! ssl_init_done) {
48 ERR_load_crypto_strings();
49 OpenSSL_add_all_algorithms();
50 atexit(&atexit_ssl_cleanup);
55 #else /* OPENSSL_VERSION_NUMBER >= 0x10100000L */
64 #endif /* OPENSSL_VERSION_NUMBER */
68 ssl_check_version(void)
72 v = OpenSSL_version_num();
73 if ((v ^ OPENSSL_VERSION_NUMBER) & ~0xff0L) {
75 "OpenSSL version mismatch. Built against %lx, you have %lx",
76 (u_long)OPENSSL_VERSION_NUMBER, v);
78 "OpenSSL version mismatch. Built against %lx, you have %lx\n",
79 (u_long)OPENSSL_VERSION_NUMBER, v);
86 # define MD5_LENGTH 16
91 * keytype_from_text returns OpenSSL NID for digest by name, and
92 * optionally the associated digest length.
94 * Used by ntpd authreadkeys(), ntpq and ntpdc keytype()
104 #ifdef OPENSSL /* --*-- OpenSSL code --*-- */
105 const u_long max_digest_len = MAX_MAC_LEN - sizeof(keyid_t);
111 * OpenSSL digest short names are capitalized, so uppercase the
112 * digest name before passing to OBJ_sn2nid(). If it is not
113 * recognized but matches our CMAC string use NID_cmac, or if
114 * it begins with 'M' or 'm' use NID_md5 to be consistent with
119 /* get name in uppercase */
121 strlcpy(upcased, text, LIB_BUFLENGTH);
123 for (pch = upcased; '\0' != *pch; pch++) {
124 *pch = (char)toupper((unsigned char)*pch);
127 key_type = OBJ_sn2nid(upcased);
130 if (!key_type && !strncmp(CMAC, upcased, strlen(CMAC) + 1)) {
134 fprintf(stderr, "%s:%d:%s():%s:key\n",
135 __FILE__, __LINE__, __func__, CMAC);
138 # endif /*ENABLE_CMAC*/
144 if (!key_type && 'm' == tolower((unsigned char)text[0])) {
152 if (NULL != pdigest_len) {
154 md = EVP_get_digestbynid(key_type);
155 digest_len = (md) ? EVP_MD_size(md) : 0;
157 if (!md || digest_len <= 0) {
159 if (key_type == NID_cmac) {
160 digest_len = CMAC_LENGTH;
163 fprintf(stderr, "%s:%d:%s():%s:len\n",
164 __FILE__, __LINE__, __func__, CMAC);
167 # endif /*ENABLE_CMAC*/
170 "key type %s is not supported by OpenSSL\n",
171 keytype_name(key_type));
173 "key type %s is not supported by OpenSSL\n",
174 keytype_name(key_type));
179 if (digest_len > max_digest_len) {
181 "key type %s %u octet digests are too big, max %lu\n",
182 keytype_name(key_type), digest_len,
185 "key type %s %u octet digests are too big, max %lu",
186 keytype_name(key_type), digest_len,
191 digest_len = MD5_LENGTH;
193 *pdigest_len = digest_len;
201 * keytype_name returns OpenSSL short name for digest by NID.
203 * Used by ntpq and ntpdc keytype()
210 static const char unknown_type[] = "(unknown key type)";
215 name = OBJ_nid2sn(nid);
218 if (NID_cmac == nid) {
222 fprintf(stderr, "%s:%d:%s():%s:nid\n",
223 __FILE__, __LINE__, __func__, CMAC);
226 # endif /*ENABLE_CMAC*/
230 #else /* !OPENSSL follows */
241 * Use getpassphrase() if configure.ac detected it, as Suns that
242 * have it truncate the password in getpass() to 8 characters.
244 #ifdef HAVE_GETPASSPHRASE
245 # define getpass(str) getpassphrase(str)
249 * getpass_keytype() -- shared between ntpq and ntpdc, only vaguely
250 * related to the rest of ssl_init.c.
257 char pass_prompt[64 + 11 + 1]; /* 11 for " Password: " */
259 snprintf(pass_prompt, sizeof(pass_prompt),
260 "%.64s Password: ", keytype_name(keytype));
262 return getpass(pass_prompt);