ssl_init.c revision 330141
1/* 2 * ssl_init.c Common OpenSSL initialization code for the various 3 * programs which use it. 4 * 5 * Moved from ntpd/ntp_crypto.c crypto_setup() 6 */ 7#ifdef HAVE_CONFIG_H 8# include <config.h> 9#endif 10#include <ctype.h> 11#include <ntp.h> 12#include <ntp_debug.h> 13#include <lib_strbuf.h> 14 15#ifdef OPENSSL 16# include "openssl/cmac.h" 17# include "openssl/crypto.h" 18# include "openssl/err.h" 19# include "openssl/evp.h" 20# include "openssl/opensslv.h" 21# include "libssl_compat.h" 22 23# define CMAC_LENGTH 16 24# define CMAC "AES128CMAC" 25 26int ssl_init_done; 27 28#if OPENSSL_VERSION_NUMBER < 0x10100000L 29 30static void 31atexit_ssl_cleanup(void) 32{ 33 if (!ssl_init_done) { 34 return; 35 } 36 37 ssl_init_done = FALSE; 38 EVP_cleanup(); 39 ERR_free_strings(); 40} 41 42void 43ssl_init(void) 44{ 45 init_lib(); 46 47 if ( ! ssl_init_done) { 48 ERR_load_crypto_strings(); 49 OpenSSL_add_all_algorithms(); 50 atexit(&atexit_ssl_cleanup); 51 ssl_init_done = TRUE; 52 } 53} 54 55#else /* OPENSSL_VERSION_NUMBER >= 0x10100000L */ 56 57void 58ssl_init(void) 59{ 60 init_lib(); 61 ssl_init_done = TRUE; 62} 63 64#endif /* OPENSSL_VERSION_NUMBER */ 65 66 67void 68ssl_check_version(void) 69{ 70 u_long v; 71 72 v = OpenSSL_version_num(); 73 if ((v ^ OPENSSL_VERSION_NUMBER) & ~0xff0L) { 74 msyslog(LOG_WARNING, 75 "OpenSSL version mismatch. Built against %lx, you have %lx", 76 (u_long)OPENSSL_VERSION_NUMBER, v); 77 fprintf(stderr, 78 "OpenSSL version mismatch. Built against %lx, you have %lx\n", 79 (u_long)OPENSSL_VERSION_NUMBER, v); 80 } 81 82 INIT_SSL(); 83} 84 85#else /* !OPENSSL */ 86# define MD5_LENGTH 16 87#endif /* OPENSSL */ 88 89 90/* 91 * keytype_from_text returns OpenSSL NID for digest by name, and 92 * optionally the associated digest length. 93 * 94 * Used by ntpd authreadkeys(), ntpq and ntpdc keytype() 95 */ 96int 97keytype_from_text( 98 const char * text, 99 size_t * pdigest_len 100 ) 101{ 102 int key_type; 103 u_int digest_len; 104#ifdef OPENSSL /* --*-- OpenSSL code --*-- */ 105 const u_long max_digest_len = MAX_MAC_LEN - sizeof(keyid_t); 106 char * upcased; 107 char * pch; 108 EVP_MD const * md; 109 110 /* 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 115 * past behavior. 116 */ 117 INIT_SSL(); 118 119 /* get name in uppercase */ 120 LIB_GETBUF(upcased); 121 strlcpy(upcased, text, LIB_BUFLENGTH); 122 123 for (pch = upcased; '\0' != *pch; pch++) { 124 *pch = (char)toupper((unsigned char)*pch); 125 } 126 127 key_type = OBJ_sn2nid(upcased); 128 129 if (!key_type && !strncmp(CMAC, upcased, strlen(CMAC) + 1)) { 130 key_type = NID_cmac; 131 132 if (debug) { 133 fprintf(stderr, "%s:%d:%s():%s:key\n", 134 __FILE__, __LINE__, __func__, CMAC); 135 } 136 } 137#else 138 139 key_type = 0; 140#endif 141 142 if (!key_type && 'm' == tolower((unsigned char)text[0])) { 143 key_type = NID_md5; 144 } 145 146 if (!key_type) { 147 return 0; 148 } 149 150 if (NULL != pdigest_len) { 151#ifdef OPENSSL 152 md = EVP_get_digestbynid(key_type); 153 digest_len = (md) ? EVP_MD_size(md) : 0; 154 155 if (!md || digest_len <= 0) { 156 if (key_type == NID_cmac) { 157 digest_len = CMAC_LENGTH; 158 159 if (debug) { 160 fprintf(stderr, "%s:%d:%s():%s:len\n", 161 __FILE__, __LINE__, __func__, CMAC); 162 } 163 } else { 164 fprintf(stderr, 165 "key type %s is not supported by OpenSSL\n", 166 keytype_name(key_type)); 167 msyslog(LOG_ERR, 168 "key type %s is not supported by OpenSSL\n", 169 keytype_name(key_type)); 170 return 0; 171 } 172 } 173 174 if (digest_len > max_digest_len) { 175 fprintf(stderr, 176 "key type %s %u octet digests are too big, max %lu\n", 177 keytype_name(key_type), digest_len, 178 max_digest_len); 179 msyslog(LOG_ERR, 180 "key type %s %u octet digests are too big, max %lu", 181 keytype_name(key_type), digest_len, 182 max_digest_len); 183 return 0; 184 } 185#else 186 digest_len = MD5_LENGTH; 187#endif 188 *pdigest_len = digest_len; 189 } 190 191 return key_type; 192} 193 194 195/* 196 * keytype_name returns OpenSSL short name for digest by NID. 197 * 198 * Used by ntpq and ntpdc keytype() 199 */ 200const char * 201keytype_name( 202 int nid 203 ) 204{ 205 static const char unknown_type[] = "(unknown key type)"; 206 const char *name; 207 208#ifdef OPENSSL 209 INIT_SSL(); 210 name = OBJ_nid2sn(nid); 211 212 if (NID_cmac == nid) { 213 name = CMAC; 214 215 if (debug) { 216 fprintf(stderr, "%s:%d:%s():%s:nid\n", 217 __FILE__, __LINE__, __func__, CMAC); 218 } 219 } else 220 if (NULL == name) { 221 name = unknown_type; 222 } 223#else /* !OPENSSL follows */ 224 if (NID_md5 == nid) 225 name = "MD5"; 226 else 227 name = unknown_type; 228#endif 229 return name; 230} 231 232 233/* 234 * Use getpassphrase() if configure.ac detected it, as Suns that 235 * have it truncate the password in getpass() to 8 characters. 236 */ 237#ifdef HAVE_GETPASSPHRASE 238# define getpass(str) getpassphrase(str) 239#endif 240 241/* 242 * getpass_keytype() -- shared between ntpq and ntpdc, only vaguely 243 * related to the rest of ssl_init.c. 244 */ 245char * 246getpass_keytype( 247 int keytype 248 ) 249{ 250 char pass_prompt[64 + 11 + 1]; /* 11 for " Password: " */ 251 252 snprintf(pass_prompt, sizeof(pass_prompt), 253 "%.64s Password: ", keytype_name(keytype)); 254 255 return getpass(pass_prompt); 256} 257 258