1*eabc0478Schristos /* $NetBSD: ntp-keygen.c,v 1.16 2024/08/18 20:47:27 christos Exp $ */ 2abb0f93cSkardel 3abb0f93cSkardel /* 4abb0f93cSkardel * Program to generate cryptographic keys for ntp clients and servers 5abb0f93cSkardel * 6abb0f93cSkardel * This program generates password encrypted data files for use with the 7abb0f93cSkardel * Autokey security protocol and Network Time Protocol Version 4. Files 8abb0f93cSkardel * are prefixed with a header giving the name and date of creation 9abb0f93cSkardel * followed by a type-specific descriptive label and PEM-encoded data 10abb0f93cSkardel * structure compatible with programs of the OpenSSL library. 11abb0f93cSkardel * 12abb0f93cSkardel * All file names are like "ntpkey_<type>_<hostname>.<filestamp>", where 13abb0f93cSkardel * <type> is the file type, <hostname> the generating host name and 14abb0f93cSkardel * <filestamp> the generation time in NTP seconds. The NTP programs 15abb0f93cSkardel * expect generic names such as "ntpkey_<type>_whimsy.udel.edu" with the 16abb0f93cSkardel * association maintained by soft links. Following is a list of file 17abb0f93cSkardel * types; the first line is the file name and the second link name. 18abb0f93cSkardel * 19abb0f93cSkardel * ntpkey_MD5key_<hostname>.<filestamp> 20abb0f93cSkardel * MD5 (128-bit) keys used to compute message digests in symmetric 21abb0f93cSkardel * key cryptography 22abb0f93cSkardel * 23abb0f93cSkardel * ntpkey_RSAhost_<hostname>.<filestamp> 24abb0f93cSkardel * ntpkey_host_<hostname> 25abb0f93cSkardel * RSA private/public host key pair used for public key signatures 26abb0f93cSkardel * 27abb0f93cSkardel * ntpkey_RSAsign_<hostname>.<filestamp> 28abb0f93cSkardel * ntpkey_sign_<hostname> 29abb0f93cSkardel * RSA private/public sign key pair used for public key signatures 30abb0f93cSkardel * 31abb0f93cSkardel * ntpkey_DSAsign_<hostname>.<filestamp> 32abb0f93cSkardel * ntpkey_sign_<hostname> 33abb0f93cSkardel * DSA Private/public sign key pair used for public key signatures 34abb0f93cSkardel * 35abb0f93cSkardel * Available digest/signature schemes 36abb0f93cSkardel * 37abb0f93cSkardel * RSA: RSA-MD2, RSA-MD5, RSA-SHA, RSA-SHA1, RSA-MDC2, EVP-RIPEMD160 38abb0f93cSkardel * DSA: DSA-SHA, DSA-SHA1 39abb0f93cSkardel * 40abb0f93cSkardel * ntpkey_XXXcert_<hostname>.<filestamp> 41abb0f93cSkardel * ntpkey_cert_<hostname> 42abb0f93cSkardel * X509v3 certificate using RSA or DSA public keys and signatures. 43abb0f93cSkardel * XXX is a code identifying the message digest and signature 44abb0f93cSkardel * encryption algorithm 45abb0f93cSkardel * 46abb0f93cSkardel * Identity schemes. The key type par is used for the challenge; the key 47abb0f93cSkardel * type key is used for the response. 48abb0f93cSkardel * 49abb0f93cSkardel * ntpkey_IFFkey_<groupname>.<filestamp> 50abb0f93cSkardel * ntpkey_iffkey_<groupname> 51abb0f93cSkardel * Schnorr (IFF) identity parameters and keys 52abb0f93cSkardel * 53abb0f93cSkardel * ntpkey_GQkey_<groupname>.<filestamp>, 54abb0f93cSkardel * ntpkey_gqkey_<groupname> 55abb0f93cSkardel * Guillou-Quisquater (GQ) identity parameters and keys 56abb0f93cSkardel * 57abb0f93cSkardel * ntpkey_MVkeyX_<groupname>.<filestamp>, 58abb0f93cSkardel * ntpkey_mvkey_<groupname> 59abb0f93cSkardel * Mu-Varadharajan (MV) identity parameters and keys 60abb0f93cSkardel * 61abb0f93cSkardel * Note: Once in a while because of some statistical fluke this program 62abb0f93cSkardel * fails to generate and verify some cryptographic data, as indicated by 63abb0f93cSkardel * exit status -1. In this case simply run the program again. If the 64abb0f93cSkardel * program does complete with exit code 0, the data are correct as 65abb0f93cSkardel * verified. 66abb0f93cSkardel * 67abb0f93cSkardel * These cryptographic routines are characterized by the prime modulus 68abb0f93cSkardel * size in bits. The default value of 512 bits is a compromise between 69abb0f93cSkardel * cryptographic strength and computing time and is ordinarily 70abb0f93cSkardel * considered adequate for this application. The routines have been 71abb0f93cSkardel * tested with sizes of 256, 512, 1024 and 2048 bits. Not all message 72abb0f93cSkardel * digest and signature encryption schemes work with sizes less than 512 73abb0f93cSkardel * bits. The computing time for sizes greater than 2048 bits is 74abb0f93cSkardel * prohibitive on all but the fastest processors. An UltraSPARC Blade 75abb0f93cSkardel * 1000 took something over nine minutes to generate and verify the 76abb0f93cSkardel * values with size 2048. An old SPARC IPC would take a week. 77abb0f93cSkardel * 78abb0f93cSkardel * The OpenSSL library used by this program expects a random seed file. 79abb0f93cSkardel * As described in the OpenSSL documentation, the file name defaults to 80abb0f93cSkardel * first the RANDFILE environment variable in the user's home directory 81abb0f93cSkardel * and then .rnd in the user's home directory. 82abb0f93cSkardel */ 83abb0f93cSkardel #ifdef HAVE_CONFIG_H 84abb0f93cSkardel # include <config.h> 85abb0f93cSkardel #endif 86abb0f93cSkardel #include <string.h> 87abb0f93cSkardel #include <stdio.h> 88abb0f93cSkardel #include <stdlib.h> 89abb0f93cSkardel #include <unistd.h> 90abb0f93cSkardel #include <sys/stat.h> 91abb0f93cSkardel #include <sys/time.h> 92abb0f93cSkardel #include <sys/types.h> 932950cc38Schristos 942950cc38Schristos #include "ntp.h" 95abb0f93cSkardel #include "ntp_random.h" 96abb0f93cSkardel #include "ntp_stdlib.h" 97abb0f93cSkardel #include "ntp_assert.h" 983123f114Skardel #include "ntp_libopts.h" 992950cc38Schristos #include "ntp_unixtime.h" 100abb0f93cSkardel #include "ntp-keygen-opts.h" 101abb0f93cSkardel 102abb0f93cSkardel #ifdef OPENSSL 103ccc794f0Schristos #include "openssl/asn1.h" 104abb0f93cSkardel #include "openssl/bn.h" 105ccc794f0Schristos #include "openssl/crypto.h" 106abb0f93cSkardel #include "openssl/evp.h" 107abb0f93cSkardel #include "openssl/err.h" 108abb0f93cSkardel #include "openssl/rand.h" 109ccc794f0Schristos #include "openssl/opensslv.h" 110abb0f93cSkardel #include "openssl/pem.h" 111ccc794f0Schristos #include "openssl/x509.h" 112abb0f93cSkardel #include "openssl/x509v3.h" 113abb0f93cSkardel #include <openssl/objects.h> 11403cfe0ffSchristos #include "libssl_compat.h" 115abb0f93cSkardel #endif /* OPENSSL */ 116abb0f93cSkardel #include <ssl_applink.c> 117abb0f93cSkardel 118e19314b7Schristos #define _UC(str) ((char *)(intptr_t)(str)) 119abb0f93cSkardel /* 120abb0f93cSkardel * Cryptodefines 121abb0f93cSkardel */ 122abb0f93cSkardel #define MD5KEYS 10 /* number of keys generated of each type */ 123abb0f93cSkardel #define MD5SIZE 20 /* maximum key size */ 1242950cc38Schristos #ifdef AUTOKEY 125abb0f93cSkardel #define PLEN 512 /* default prime modulus size (bits) */ 126*eabc0478Schristos #define ILEN 512 /* default identity modulus size (bits) */ 127abb0f93cSkardel #define MVMAX 100 /* max MV parameters */ 128abb0f93cSkardel 129abb0f93cSkardel /* 130abb0f93cSkardel * Strings used in X509v3 extension fields 131abb0f93cSkardel */ 132abb0f93cSkardel #define KEY_USAGE "digitalSignature,keyCertSign" 133abb0f93cSkardel #define BASIC_CONSTRAINTS "critical,CA:TRUE" 134abb0f93cSkardel #define EXT_KEY_PRIVATE "private" 135abb0f93cSkardel #define EXT_KEY_TRUST "trustRoot" 1362950cc38Schristos #endif /* AUTOKEY */ 137abb0f93cSkardel 138abb0f93cSkardel /* 139abb0f93cSkardel * Prototypes 140abb0f93cSkardel */ 141abb0f93cSkardel FILE *fheader (const char *, const char *, const char *); 142e19314b7Schristos int gen_md5 (const char *); 1432950cc38Schristos void followlink (char *, size_t); 1442950cc38Schristos #ifdef AUTOKEY 145e19314b7Schristos EVP_PKEY *gen_rsa (const char *); 146e19314b7Schristos EVP_PKEY *gen_dsa (const char *); 147e19314b7Schristos EVP_PKEY *gen_iffkey (const char *); 148e19314b7Schristos EVP_PKEY *gen_gqkey (const char *); 149e19314b7Schristos EVP_PKEY *gen_mvkey (const char *, EVP_PKEY **); 150abb0f93cSkardel void gen_mvserv (char *, EVP_PKEY **); 151e19314b7Schristos int x509 (EVP_PKEY *, const EVP_MD *, char *, const char *, 152abb0f93cSkardel char *); 153abb0f93cSkardel void cb (int, int, void *); 154e19314b7Schristos EVP_PKEY *genkey (const char *, const char *); 155abb0f93cSkardel EVP_PKEY *readkey (char *, char *, u_int *, EVP_PKEY **); 156abb0f93cSkardel void writekey (char *, char *, u_int *, EVP_PKEY **); 157abb0f93cSkardel u_long asn2ntp (ASN1_TIME *); 15803cfe0ffSchristos 15903cfe0ffSchristos static DSA* genDsaParams(int, char*); 16003cfe0ffSchristos static RSA* genRsaKeyPair(int, char*); 16103cfe0ffSchristos 1622950cc38Schristos #endif /* AUTOKEY */ 163abb0f93cSkardel 164abb0f93cSkardel /* 165abb0f93cSkardel * Program variables 166abb0f93cSkardel */ 167abb0f93cSkardel extern char *optarg; /* command line argument */ 168af12ab5eSchristos char const *progname; 1692950cc38Schristos u_int lifetime = DAYSPERYEAR; /* certificate lifetime (days) */ 170abb0f93cSkardel int nkeys; /* MV keys */ 171abb0f93cSkardel time_t epoch; /* Unix epoch (seconds) since 1970 */ 172abb0f93cSkardel u_int fstamp; /* NTP filestamp */ 1732950cc38Schristos char hostbuf[MAXHOSTNAME + 1]; 1742950cc38Schristos char *hostname = NULL; /* host, used in cert filenames */ 1752950cc38Schristos char *groupname = NULL; /* group name */ 1762950cc38Schristos char certnamebuf[2 * sizeof(hostbuf)]; 1772950cc38Schristos char *certname = NULL; /* certificate subject/issuer name */ 178abb0f93cSkardel char *passwd1 = NULL; /* input private key password */ 179abb0f93cSkardel char *passwd2 = NULL; /* output private key password */ 1802950cc38Schristos char filename[MAXFILENAME + 1]; /* file name */ 1812950cc38Schristos #ifdef AUTOKEY 1822950cc38Schristos u_int modulus = PLEN; /* prime modulus size (bits) */ 1832950cc38Schristos u_int modulus2 = ILEN; /* identity modulus size (bits) */ 184abb0f93cSkardel long d0, d1, d2, d3; /* callback counters */ 1852950cc38Schristos const EVP_CIPHER * cipher = NULL; 1862950cc38Schristos #endif /* AUTOKEY */ 187abb0f93cSkardel 188abb0f93cSkardel #ifdef SYS_WINNT 189abb0f93cSkardel BOOL init_randfile(); 190abb0f93cSkardel 191abb0f93cSkardel /* 1922950cc38Schristos * Don't try to follow symbolic links on Windows. Assume link == file. 193abb0f93cSkardel */ 194abb0f93cSkardel int 1952950cc38Schristos readlink( 1962950cc38Schristos char * link, 1972950cc38Schristos char * file, 1982950cc38Schristos int len 1992950cc38Schristos ) 200abb0f93cSkardel { 2018b8da087Schristos return (int)strlen(file); /* assume no overflow possible */ 202abb0f93cSkardel } 203abb0f93cSkardel 204abb0f93cSkardel /* 2052950cc38Schristos * Don't try to create symbolic links on Windows, that is supported on 2062950cc38Schristos * Vista and later only. Instead, if CreateHardLink is available (XP 2072950cc38Schristos * and later), hardlink the linkname to the original filename. On 2082950cc38Schristos * earlier systems, user must rename file to match expected link for 2092950cc38Schristos * ntpd to find it. To allow building a ntp-keygen.exe which loads on 2102950cc38Schristos * Windows pre-XP, runtime link to CreateHardLinkA(). 211abb0f93cSkardel */ 212abb0f93cSkardel int 2132950cc38Schristos symlink( 2142950cc38Schristos char * filename, 2152950cc38Schristos char* linkname 2162950cc38Schristos ) 2172950cc38Schristos { 2182950cc38Schristos typedef BOOL (WINAPI *PCREATEHARDLINKA)( 2192950cc38Schristos __in LPCSTR lpFileName, 2202950cc38Schristos __in LPCSTR lpExistingFileName, 2212950cc38Schristos __reserved LPSECURITY_ATTRIBUTES lpSA 2222950cc38Schristos ); 2232950cc38Schristos static PCREATEHARDLINKA pCreateHardLinkA; 2242950cc38Schristos static int tried; 2252950cc38Schristos HMODULE hDll; 2262950cc38Schristos FARPROC pfn; 2272950cc38Schristos int link_created; 2282950cc38Schristos int saved_errno; 2292950cc38Schristos 2302950cc38Schristos if (!tried) { 2312950cc38Schristos tried = TRUE; 2322950cc38Schristos hDll = LoadLibrary("kernel32"); 2332950cc38Schristos pfn = GetProcAddress(hDll, "CreateHardLinkA"); 2342950cc38Schristos pCreateHardLinkA = (PCREATEHARDLINKA)pfn; 235abb0f93cSkardel } 2362950cc38Schristos 2372950cc38Schristos if (NULL == pCreateHardLinkA) { 2382950cc38Schristos errno = ENOSYS; 2392950cc38Schristos return -1; 2402950cc38Schristos } 2412950cc38Schristos 2422950cc38Schristos link_created = (*pCreateHardLinkA)(linkname, filename, NULL); 2432950cc38Schristos 2442950cc38Schristos if (link_created) 2452950cc38Schristos return 0; 2462950cc38Schristos 2472950cc38Schristos saved_errno = GetLastError(); /* yes we play loose */ 2482950cc38Schristos mfprintf(stderr, "Create hard link %s to %s failed: %m\n", 2492950cc38Schristos linkname, filename); 2502950cc38Schristos errno = saved_errno; 2512950cc38Schristos return -1; 2522950cc38Schristos } 2532950cc38Schristos 254abb0f93cSkardel void 255abb0f93cSkardel InitWin32Sockets() { 256abb0f93cSkardel WORD wVersionRequested; 257abb0f93cSkardel WSADATA wsaData; 258abb0f93cSkardel wVersionRequested = MAKEWORD(2,0); 259abb0f93cSkardel if (WSAStartup(wVersionRequested, &wsaData)) 260abb0f93cSkardel { 261abb0f93cSkardel fprintf(stderr, "No useable winsock.dll\n"); 262abb0f93cSkardel exit(1); 263abb0f93cSkardel } 264abb0f93cSkardel } 265abb0f93cSkardel #endif /* SYS_WINNT */ 266abb0f93cSkardel 2672950cc38Schristos 2682950cc38Schristos /* 2692950cc38Schristos * followlink() - replace filename with its target if symlink. 2702950cc38Schristos * 271*eabc0478Schristos * readlink() does not null-terminate the result. 2722950cc38Schristos */ 2732950cc38Schristos void 2742950cc38Schristos followlink( 2752950cc38Schristos char * fname, 2762950cc38Schristos size_t bufsiz 2772950cc38Schristos ) 2782950cc38Schristos { 279*eabc0478Schristos ssize_t len; 280*eabc0478Schristos char * target; 2812950cc38Schristos 282*eabc0478Schristos REQUIRE(bufsiz > 0 && bufsiz <= SSIZE_MAX); 2832950cc38Schristos 284*eabc0478Schristos target = emalloc(bufsiz); 285*eabc0478Schristos len = readlink(fname, target, bufsiz); 2862950cc38Schristos if (len < 0) { 2872950cc38Schristos fname[0] = '\0'; 2882950cc38Schristos return; 2892950cc38Schristos } 290*eabc0478Schristos if ((size_t)len > bufsiz - 1) 291*eabc0478Schristos len = bufsiz - 1; 292*eabc0478Schristos memcpy(fname, target, len); 293*eabc0478Schristos fname[len] = '\0'; 294*eabc0478Schristos free(target); 2952950cc38Schristos } 2962950cc38Schristos 2972950cc38Schristos 298abb0f93cSkardel /* 299abb0f93cSkardel * Main program 300abb0f93cSkardel */ 301abb0f93cSkardel int 302abb0f93cSkardel main( 303abb0f93cSkardel int argc, /* command line options */ 304abb0f93cSkardel char **argv 305abb0f93cSkardel ) 306abb0f93cSkardel { 307abb0f93cSkardel struct timeval tv; /* initialization vector */ 308abb0f93cSkardel int md5key = 0; /* generate MD5 keys */ 3092950cc38Schristos int optct; /* option count */ 3102950cc38Schristos #ifdef AUTOKEY 311abb0f93cSkardel X509 *cert = NULL; /* X509 certificate */ 312abb0f93cSkardel EVP_PKEY *pkey_host = NULL; /* host key */ 313abb0f93cSkardel EVP_PKEY *pkey_sign = NULL; /* sign key */ 314abb0f93cSkardel EVP_PKEY *pkey_iffkey = NULL; /* IFF sever keys */ 315abb0f93cSkardel EVP_PKEY *pkey_gqkey = NULL; /* GQ server keys */ 316abb0f93cSkardel EVP_PKEY *pkey_mvkey = NULL; /* MV trusted agen keys */ 317abb0f93cSkardel EVP_PKEY *pkey_mvpar[MVMAX]; /* MV cleient keys */ 318abb0f93cSkardel int hostkey = 0; /* generate RSA keys */ 319abb0f93cSkardel int iffkey = 0; /* generate IFF keys */ 320abb0f93cSkardel int gqkey = 0; /* generate GQ keys */ 321abb0f93cSkardel int mvkey = 0; /* update MV keys */ 322abb0f93cSkardel int mvpar = 0; /* generate MV parameters */ 323abb0f93cSkardel char *sign = NULL; /* sign key */ 324abb0f93cSkardel EVP_PKEY *pkey = NULL; /* temp key */ 325abb0f93cSkardel const EVP_MD *ectx; /* EVP digest */ 326abb0f93cSkardel char pathbuf[MAXFILENAME + 1]; 327abb0f93cSkardel const char *scheme = NULL; /* digest/signature scheme */ 3282950cc38Schristos const char *ciphername = NULL; /* to encrypt priv. key */ 329e19314b7Schristos const char *exten = NULL; /* private extension */ 330abb0f93cSkardel char *grpkey = NULL; /* identity extension */ 331abb0f93cSkardel int nid; /* X509 digest/signature scheme */ 332abb0f93cSkardel FILE *fstr = NULL; /* file handle */ 333abb0f93cSkardel char groupbuf[MAXHOSTNAME + 1]; 3342950cc38Schristos u_int temp; 3352950cc38Schristos BIO * bp; 3362950cc38Schristos int i, cnt; 3372950cc38Schristos char * ptr; 3382950cc38Schristos #endif /* AUTOKEY */ 339ccc794f0Schristos #ifdef OPENSSL 340ccc794f0Schristos const char *sslvtext; 341ccc794f0Schristos int sslvmatch; 342ccc794f0Schristos #endif /* OPENSSL */ 343abb0f93cSkardel 344abb0f93cSkardel progname = argv[0]; 345abb0f93cSkardel 346abb0f93cSkardel #ifdef SYS_WINNT 347abb0f93cSkardel /* Initialize before OpenSSL checks */ 348abb0f93cSkardel InitWin32Sockets(); 349abb0f93cSkardel if (!init_randfile()) 350abb0f93cSkardel fprintf(stderr, "Unable to initialize .rnd file\n"); 351abb0f93cSkardel ssl_applink(); 352abb0f93cSkardel #endif 353abb0f93cSkardel 354abb0f93cSkardel #ifdef OPENSSL 355abb0f93cSkardel ssl_check_version(); 356abb0f93cSkardel #endif /* OPENSSL */ 357abb0f93cSkardel 358ea66d795Schristos ntp_crypto_srandom(); 359ea66d795Schristos 360abb0f93cSkardel /* 361abb0f93cSkardel * Process options, initialize host name and timestamp. 3622950cc38Schristos * gethostname() won't null-terminate if hostname is exactly the 3632950cc38Schristos * length provided for the buffer. 364abb0f93cSkardel */ 3652950cc38Schristos gethostname(hostbuf, sizeof(hostbuf) - 1); 3662950cc38Schristos hostbuf[COUNTOF(hostbuf) - 1] = '\0'; 367abb0f93cSkardel hostname = hostbuf; 3682950cc38Schristos groupname = hostbuf; 3692950cc38Schristos passwd1 = hostbuf; 3702950cc38Schristos passwd2 = NULL; 3712950cc38Schristos GETTIMEOFDAY(&tv, NULL); 372abb0f93cSkardel epoch = tv.tv_sec; 3732950cc38Schristos fstamp = (u_int)(epoch + JAN_1970); 374abb0f93cSkardel 3752950cc38Schristos optct = ntpOptionProcess(&ntp_keygenOptions, argc, argv); 376af12ab5eSchristos argc -= optct; // Just in case we care later. 377af12ab5eSchristos argv += optct; // Just in case we care later. 3783123f114Skardel 3793123f114Skardel #ifdef OPENSSL 380ccc794f0Schristos sslvtext = OpenSSL_version(OPENSSL_VERSION); 381ccc794f0Schristos sslvmatch = OpenSSL_version_num() == OPENSSL_VERSION_NUMBER; 382ccc794f0Schristos if (sslvmatch) 3833123f114Skardel fprintf(stderr, "Using OpenSSL version %s\n", 384ccc794f0Schristos sslvtext); 3853123f114Skardel else 3863123f114Skardel fprintf(stderr, "Built against OpenSSL %s, using version %s\n", 387ccc794f0Schristos OPENSSL_VERSION_TEXT, sslvtext); 3883123f114Skardel #endif /* OPENSSL */ 3893123f114Skardel 3902950cc38Schristos debug = OPT_VALUE_SET_DEBUG_LEVEL; 3912950cc38Schristos 392abb0f93cSkardel if (HAVE_OPT( MD5KEY )) 393abb0f93cSkardel md5key++; 3942950cc38Schristos #ifdef AUTOKEY 395ea66d795Schristos if (HAVE_OPT( PASSWORD )) 396ea66d795Schristos passwd1 = estrdup(OPT_ARG( PASSWORD )); 397abb0f93cSkardel 398ea66d795Schristos if (HAVE_OPT( EXPORT_PASSWD )) 399ea66d795Schristos passwd2 = estrdup(OPT_ARG( EXPORT_PASSWD )); 400abb0f93cSkardel 401abb0f93cSkardel if (HAVE_OPT( HOST_KEY )) 402abb0f93cSkardel hostkey++; 403abb0f93cSkardel 404abb0f93cSkardel if (HAVE_OPT( SIGN_KEY )) 4052950cc38Schristos sign = estrdup(OPT_ARG( SIGN_KEY )); 406abb0f93cSkardel 407abb0f93cSkardel if (HAVE_OPT( GQ_PARAMS )) 408abb0f93cSkardel gqkey++; 409abb0f93cSkardel 410abb0f93cSkardel if (HAVE_OPT( IFFKEY )) 411abb0f93cSkardel iffkey++; 412abb0f93cSkardel 413abb0f93cSkardel if (HAVE_OPT( MV_PARAMS )) { 414*eabc0478Schristos mvkey++; /* DLH are these two swapped? */ 415abb0f93cSkardel nkeys = OPT_VALUE_MV_PARAMS; 416abb0f93cSkardel } 417abb0f93cSkardel if (HAVE_OPT( MV_KEYS )) { 418*eabc0478Schristos mvpar++; /* not used! */ /* DLH are these two swapped? */ 419abb0f93cSkardel nkeys = OPT_VALUE_MV_KEYS; 420abb0f93cSkardel } 4212950cc38Schristos 4222950cc38Schristos if (HAVE_OPT( IMBITS )) 4232950cc38Schristos modulus2 = OPT_VALUE_IMBITS; 4242950cc38Schristos 425abb0f93cSkardel if (HAVE_OPT( MODULUS )) 426abb0f93cSkardel modulus = OPT_VALUE_MODULUS; 427abb0f93cSkardel 428abb0f93cSkardel if (HAVE_OPT( CERTIFICATE )) 429abb0f93cSkardel scheme = OPT_ARG( CERTIFICATE ); 430abb0f93cSkardel 4312950cc38Schristos if (HAVE_OPT( CIPHER )) 4322950cc38Schristos ciphername = OPT_ARG( CIPHER ); 433abb0f93cSkardel 4342950cc38Schristos if (HAVE_OPT( SUBJECT_NAME )) 4352950cc38Schristos hostname = estrdup(OPT_ARG( SUBJECT_NAME )); 4362950cc38Schristos 4372950cc38Schristos if (HAVE_OPT( IDENT )) 4382950cc38Schristos groupname = estrdup(OPT_ARG( IDENT )); 4392950cc38Schristos 4402950cc38Schristos if (HAVE_OPT( LIFETIME )) 4412950cc38Schristos lifetime = OPT_VALUE_LIFETIME; 442abb0f93cSkardel 443abb0f93cSkardel if (HAVE_OPT( PVT_CERT )) 444abb0f93cSkardel exten = EXT_KEY_PRIVATE; 445abb0f93cSkardel 446abb0f93cSkardel if (HAVE_OPT( TRUSTED_CERT )) 447abb0f93cSkardel exten = EXT_KEY_TRUST; 448abb0f93cSkardel 449abb0f93cSkardel /* 4502950cc38Schristos * Remove the group name from the hostname variable used 4512950cc38Schristos * in host and sign certificate file names. 4522950cc38Schristos */ 4532950cc38Schristos if (hostname != hostbuf) 4542950cc38Schristos ptr = strchr(hostname, '@'); 4552950cc38Schristos else 4562950cc38Schristos ptr = NULL; 4572950cc38Schristos if (ptr != NULL) { 4582950cc38Schristos *ptr = '\0'; 4592950cc38Schristos groupname = estrdup(ptr + 1); 4602950cc38Schristos /* -s @group is equivalent to -i group, host unch. */ 4612950cc38Schristos if (ptr == hostname) 4622950cc38Schristos hostname = hostbuf; 4632950cc38Schristos } 4642950cc38Schristos 4652950cc38Schristos /* 4662950cc38Schristos * Derive host certificate issuer/subject names from host name 4672950cc38Schristos * and optional group. If no groupname is provided, the issuer 4682950cc38Schristos * and subject is the hostname with no '@group', and the 4692950cc38Schristos * groupname variable is pointed to hostname for use in IFF, GQ, 4702950cc38Schristos * and MV parameters file names. 4712950cc38Schristos */ 4722950cc38Schristos if (groupname == hostbuf) { 4732950cc38Schristos certname = hostname; 4742950cc38Schristos } else { 4752950cc38Schristos snprintf(certnamebuf, sizeof(certnamebuf), "%s@%s", 4762950cc38Schristos hostname, groupname); 4772950cc38Schristos certname = certnamebuf; 4782950cc38Schristos } 4792950cc38Schristos 4802950cc38Schristos /* 481abb0f93cSkardel * Seed random number generator and grow weeds. 482abb0f93cSkardel */ 483ccc794f0Schristos #if OPENSSL_VERSION_NUMBER < 0x10100000L 484abb0f93cSkardel ERR_load_crypto_strings(); 485abb0f93cSkardel OpenSSL_add_all_algorithms(); 486ccc794f0Schristos #endif /* OPENSSL_VERSION_NUMBER */ 487abb0f93cSkardel if (!RAND_status()) { 4882950cc38Schristos if (RAND_file_name(pathbuf, sizeof(pathbuf)) == NULL) { 489abb0f93cSkardel fprintf(stderr, "RAND_file_name %s\n", 490abb0f93cSkardel ERR_error_string(ERR_get_error(), NULL)); 491abb0f93cSkardel exit (-1); 492abb0f93cSkardel } 493abb0f93cSkardel temp = RAND_load_file(pathbuf, -1); 494abb0f93cSkardel if (temp == 0) { 495abb0f93cSkardel fprintf(stderr, 496abb0f93cSkardel "RAND_load_file %s not found or empty\n", 497abb0f93cSkardel pathbuf); 498abb0f93cSkardel exit (-1); 499abb0f93cSkardel } 500abb0f93cSkardel fprintf(stderr, 501abb0f93cSkardel "Random seed file %s %u bytes\n", pathbuf, temp); 502abb0f93cSkardel RAND_add(&epoch, sizeof(epoch), 4.0); 503abb0f93cSkardel } 5042950cc38Schristos #endif /* AUTOKEY */ 505abb0f93cSkardel 506abb0f93cSkardel /* 5072950cc38Schristos * Create new unencrypted MD5 keys file if requested. If this 5082950cc38Schristos * option is selected, ignore all other options. 5092950cc38Schristos */ 5102950cc38Schristos if (md5key) { 5112950cc38Schristos gen_md5("md5"); 5122950cc38Schristos exit (0); 5132950cc38Schristos } 5142950cc38Schristos 5152950cc38Schristos #ifdef AUTOKEY 5162950cc38Schristos /* 517abb0f93cSkardel * Load previous certificate if available. 518abb0f93cSkardel */ 5192950cc38Schristos snprintf(filename, sizeof(filename), "ntpkey_cert_%s", hostname); 520abb0f93cSkardel if ((fstr = fopen(filename, "r")) != NULL) { 521abb0f93cSkardel cert = PEM_read_X509(fstr, NULL, NULL, NULL); 522abb0f93cSkardel fclose(fstr); 523abb0f93cSkardel } 524abb0f93cSkardel if (cert != NULL) { 525abb0f93cSkardel 526abb0f93cSkardel /* 527abb0f93cSkardel * Extract subject name. 528abb0f93cSkardel */ 529abb0f93cSkardel X509_NAME_oneline(X509_get_subject_name(cert), groupbuf, 530abb0f93cSkardel MAXFILENAME); 531abb0f93cSkardel 532abb0f93cSkardel /* 533abb0f93cSkardel * Extract digest/signature scheme. 534abb0f93cSkardel */ 535abb0f93cSkardel if (scheme == NULL) { 53603cfe0ffSchristos nid = X509_get_signature_nid(cert); 537abb0f93cSkardel scheme = OBJ_nid2sn(nid); 538abb0f93cSkardel } 539abb0f93cSkardel 540abb0f93cSkardel /* 541abb0f93cSkardel * If a key_usage extension field is present, determine 542abb0f93cSkardel * whether this is a trusted or private certificate. 543abb0f93cSkardel */ 544abb0f93cSkardel if (exten == NULL) { 545abb0f93cSkardel ptr = strstr(groupbuf, "CN="); 546abb0f93cSkardel cnt = X509_get_ext_count(cert); 547abb0f93cSkardel for (i = 0; i < cnt; i++) { 54803cfe0ffSchristos X509_EXTENSION *ext; 54903cfe0ffSchristos ASN1_OBJECT *obj; 55003cfe0ffSchristos 551abb0f93cSkardel ext = X509_get_ext(cert, i); 55203cfe0ffSchristos obj = X509_EXTENSION_get_object(ext); 55303cfe0ffSchristos 55403cfe0ffSchristos if (OBJ_obj2nid(obj) == 555abb0f93cSkardel NID_ext_key_usage) { 556abb0f93cSkardel bp = BIO_new(BIO_s_mem()); 557abb0f93cSkardel X509V3_EXT_print(bp, ext, 0, 0); 558abb0f93cSkardel BIO_gets(bp, pathbuf, 559abb0f93cSkardel MAXFILENAME); 560abb0f93cSkardel BIO_free(bp); 561abb0f93cSkardel if (strcmp(pathbuf, 562abb0f93cSkardel "Trust Root") == 0) 563abb0f93cSkardel exten = EXT_KEY_TRUST; 564abb0f93cSkardel else if (strcmp(pathbuf, 565abb0f93cSkardel "Private") == 0) 566abb0f93cSkardel exten = EXT_KEY_PRIVATE; 5672950cc38Schristos certname = estrdup(ptr + 3); 568abb0f93cSkardel } 569abb0f93cSkardel } 570abb0f93cSkardel } 571abb0f93cSkardel } 572abb0f93cSkardel if (scheme == NULL) 573abb0f93cSkardel scheme = "RSA-MD5"; 5742950cc38Schristos if (ciphername == NULL) 5752950cc38Schristos ciphername = "des-ede3-cbc"; 5762950cc38Schristos cipher = EVP_get_cipherbyname(ciphername); 5772950cc38Schristos if (cipher == NULL) { 5782950cc38Schristos fprintf(stderr, "Unknown cipher %s\n", ciphername); 5792950cc38Schristos exit(-1); 5802950cc38Schristos } 581abb0f93cSkardel fprintf(stderr, "Using host %s group %s\n", hostname, 582abb0f93cSkardel groupname); 583abb0f93cSkardel 584abb0f93cSkardel /* 585abb0f93cSkardel * Create a new encrypted RSA host key file if requested; 586abb0f93cSkardel * otherwise, look for an existing host key file. If not found, 587abb0f93cSkardel * create a new encrypted RSA host key file. If that fails, go 588abb0f93cSkardel * no further. 589abb0f93cSkardel */ 590abb0f93cSkardel if (hostkey) 591abb0f93cSkardel pkey_host = genkey("RSA", "host"); 592abb0f93cSkardel if (pkey_host == NULL) { 5932950cc38Schristos snprintf(filename, sizeof(filename), "ntpkey_host_%s", hostname); 594abb0f93cSkardel pkey_host = readkey(filename, passwd1, &fstamp, NULL); 595abb0f93cSkardel if (pkey_host != NULL) { 5962950cc38Schristos followlink(filename, sizeof(filename)); 597abb0f93cSkardel fprintf(stderr, "Using host key %s\n", 598abb0f93cSkardel filename); 599abb0f93cSkardel } else { 600abb0f93cSkardel pkey_host = genkey("RSA", "host"); 601abb0f93cSkardel } 602abb0f93cSkardel } 603abb0f93cSkardel if (pkey_host == NULL) { 604abb0f93cSkardel fprintf(stderr, "Generating host key fails\n"); 605abb0f93cSkardel exit(-1); 606abb0f93cSkardel } 607abb0f93cSkardel 608abb0f93cSkardel /* 609abb0f93cSkardel * Create new encrypted RSA or DSA sign keys file if requested; 610abb0f93cSkardel * otherwise, look for an existing sign key file. If not found, 611abb0f93cSkardel * use the host key instead. 612abb0f93cSkardel */ 613abb0f93cSkardel if (sign != NULL) 614abb0f93cSkardel pkey_sign = genkey(sign, "sign"); 615abb0f93cSkardel if (pkey_sign == NULL) { 6162950cc38Schristos snprintf(filename, sizeof(filename), "ntpkey_sign_%s", 6172950cc38Schristos hostname); 618abb0f93cSkardel pkey_sign = readkey(filename, passwd1, &fstamp, NULL); 619abb0f93cSkardel if (pkey_sign != NULL) { 6202950cc38Schristos followlink(filename, sizeof(filename)); 621abb0f93cSkardel fprintf(stderr, "Using sign key %s\n", 622abb0f93cSkardel filename); 6232950cc38Schristos } else { 624abb0f93cSkardel pkey_sign = pkey_host; 625abb0f93cSkardel fprintf(stderr, "Using host key as sign key\n"); 626abb0f93cSkardel } 627abb0f93cSkardel } 628abb0f93cSkardel 629abb0f93cSkardel /* 630abb0f93cSkardel * Create new encrypted GQ server keys file if requested; 631abb0f93cSkardel * otherwise, look for an exisiting file. If found, fetch the 632abb0f93cSkardel * public key for the certificate. 633abb0f93cSkardel */ 634abb0f93cSkardel if (gqkey) 635abb0f93cSkardel pkey_gqkey = gen_gqkey("gqkey"); 636abb0f93cSkardel if (pkey_gqkey == NULL) { 6372950cc38Schristos snprintf(filename, sizeof(filename), "ntpkey_gqkey_%s", 6382950cc38Schristos groupname); 639abb0f93cSkardel pkey_gqkey = readkey(filename, passwd1, &fstamp, NULL); 640abb0f93cSkardel if (pkey_gqkey != NULL) { 6412950cc38Schristos followlink(filename, sizeof(filename)); 642abb0f93cSkardel fprintf(stderr, "Using GQ parameters %s\n", 643abb0f93cSkardel filename); 644abb0f93cSkardel } 645abb0f93cSkardel } 64603cfe0ffSchristos if (pkey_gqkey != NULL) { 64703cfe0ffSchristos RSA *rsa; 64803cfe0ffSchristos const BIGNUM *q; 64903cfe0ffSchristos 650*eabc0478Schristos rsa = EVP_PKEY_get1_RSA(pkey_gqkey); 65103cfe0ffSchristos RSA_get0_factors(rsa, NULL, &q); 65203cfe0ffSchristos grpkey = BN_bn2hex(q); 653*eabc0478Schristos RSA_free(rsa); 65403cfe0ffSchristos } 655abb0f93cSkardel 656abb0f93cSkardel /* 657abb0f93cSkardel * Write the nonencrypted GQ client parameters to the stdout 658abb0f93cSkardel * stream. The parameter file is the server key file with the 659abb0f93cSkardel * private key obscured. 660abb0f93cSkardel */ 661abb0f93cSkardel if (pkey_gqkey != NULL && HAVE_OPT(ID_KEY)) { 662abb0f93cSkardel RSA *rsa; 663abb0f93cSkardel 6642950cc38Schristos snprintf(filename, sizeof(filename), 6652950cc38Schristos "ntpkey_gqpar_%s.%u", groupname, fstamp); 666abb0f93cSkardel fprintf(stderr, "Writing GQ parameters %s to stdout\n", 667abb0f93cSkardel filename); 668abb0f93cSkardel fprintf(stdout, "# %s\n# %s\n", filename, 669abb0f93cSkardel ctime(&epoch)); 670*eabc0478Schristos rsa = EVP_PKEY_get1_RSA(pkey_gqkey); 67103cfe0ffSchristos RSA_set0_factors(rsa, BN_dup(BN_value_one()), BN_dup(BN_value_one())); 672abb0f93cSkardel pkey = EVP_PKEY_new(); 673abb0f93cSkardel EVP_PKEY_assign_RSA(pkey, rsa); 6742950cc38Schristos PEM_write_PKCS8PrivateKey(stdout, pkey, NULL, NULL, 0, 6752950cc38Schristos NULL, NULL); 6762950cc38Schristos fflush(stdout); 677*eabc0478Schristos if (debug) { 678abb0f93cSkardel RSA_print_fp(stderr, rsa, 0); 679abb0f93cSkardel } 680*eabc0478Schristos EVP_PKEY_free(pkey); 681*eabc0478Schristos pkey = NULL; 682*eabc0478Schristos RSA_free(rsa); 683*eabc0478Schristos } 684abb0f93cSkardel 685abb0f93cSkardel /* 686abb0f93cSkardel * Write the encrypted GQ server keys to the stdout stream. 687abb0f93cSkardel */ 688abb0f93cSkardel if (pkey_gqkey != NULL && passwd2 != NULL) { 689abb0f93cSkardel RSA *rsa; 690abb0f93cSkardel 6912950cc38Schristos snprintf(filename, sizeof(filename), 6922950cc38Schristos "ntpkey_gqkey_%s.%u", groupname, fstamp); 693abb0f93cSkardel fprintf(stderr, "Writing GQ keys %s to stdout\n", 694abb0f93cSkardel filename); 695abb0f93cSkardel fprintf(stdout, "# %s\n# %s\n", filename, 696abb0f93cSkardel ctime(&epoch)); 697*eabc0478Schristos rsa = EVP_PKEY_get1_RSA(pkey_gqkey); 698abb0f93cSkardel pkey = EVP_PKEY_new(); 699abb0f93cSkardel EVP_PKEY_assign_RSA(pkey, rsa); 7002950cc38Schristos PEM_write_PKCS8PrivateKey(stdout, pkey, cipher, NULL, 0, 7012950cc38Schristos NULL, passwd2); 7022950cc38Schristos fflush(stdout); 703*eabc0478Schristos if (debug) { 704abb0f93cSkardel RSA_print_fp(stderr, rsa, 0); 705abb0f93cSkardel } 706*eabc0478Schristos EVP_PKEY_free(pkey); 707*eabc0478Schristos pkey = NULL; 708*eabc0478Schristos RSA_free(rsa); 709*eabc0478Schristos } 710abb0f93cSkardel 711abb0f93cSkardel /* 712abb0f93cSkardel * Create new encrypted IFF server keys file if requested; 713abb0f93cSkardel * otherwise, look for existing file. 714abb0f93cSkardel */ 715abb0f93cSkardel if (iffkey) 716abb0f93cSkardel pkey_iffkey = gen_iffkey("iffkey"); 717abb0f93cSkardel if (pkey_iffkey == NULL) { 7182950cc38Schristos snprintf(filename, sizeof(filename), "ntpkey_iffkey_%s", 7192950cc38Schristos groupname); 720abb0f93cSkardel pkey_iffkey = readkey(filename, passwd1, &fstamp, NULL); 721abb0f93cSkardel if (pkey_iffkey != NULL) { 7222950cc38Schristos followlink(filename, sizeof(filename)); 723abb0f93cSkardel fprintf(stderr, "Using IFF keys %s\n", 724abb0f93cSkardel filename); 725abb0f93cSkardel } 726abb0f93cSkardel } 727abb0f93cSkardel 728abb0f93cSkardel /* 729abb0f93cSkardel * Write the nonencrypted IFF client parameters to the stdout 730abb0f93cSkardel * stream. The parameter file is the server key file with the 731abb0f93cSkardel * private key obscured. 732abb0f93cSkardel */ 733abb0f93cSkardel if (pkey_iffkey != NULL && HAVE_OPT(ID_KEY)) { 734abb0f93cSkardel DSA *dsa; 735abb0f93cSkardel 7362950cc38Schristos snprintf(filename, sizeof(filename), 7372950cc38Schristos "ntpkey_iffpar_%s.%u", groupname, fstamp); 738abb0f93cSkardel fprintf(stderr, "Writing IFF parameters %s to stdout\n", 739abb0f93cSkardel filename); 740abb0f93cSkardel fprintf(stdout, "# %s\n# %s\n", filename, 741abb0f93cSkardel ctime(&epoch)); 742*eabc0478Schristos dsa = EVP_PKEY_get1_DSA(pkey_iffkey); 74303cfe0ffSchristos DSA_set0_key(dsa, NULL, BN_dup(BN_value_one())); 744abb0f93cSkardel pkey = EVP_PKEY_new(); 745abb0f93cSkardel EVP_PKEY_assign_DSA(pkey, dsa); 7462950cc38Schristos PEM_write_PKCS8PrivateKey(stdout, pkey, NULL, NULL, 0, 7472950cc38Schristos NULL, NULL); 7482950cc38Schristos fflush(stdout); 749*eabc0478Schristos if (debug) { 750abb0f93cSkardel DSA_print_fp(stderr, dsa, 0); 751abb0f93cSkardel } 752*eabc0478Schristos EVP_PKEY_free(pkey); 753*eabc0478Schristos pkey = NULL; 754*eabc0478Schristos DSA_free(dsa); 755*eabc0478Schristos } 756abb0f93cSkardel 757abb0f93cSkardel /* 758abb0f93cSkardel * Write the encrypted IFF server keys to the stdout stream. 759abb0f93cSkardel */ 760abb0f93cSkardel if (pkey_iffkey != NULL && passwd2 != NULL) { 761abb0f93cSkardel DSA *dsa; 762abb0f93cSkardel 7632950cc38Schristos snprintf(filename, sizeof(filename), 7642950cc38Schristos "ntpkey_iffkey_%s.%u", groupname, fstamp); 765abb0f93cSkardel fprintf(stderr, "Writing IFF keys %s to stdout\n", 766abb0f93cSkardel filename); 767abb0f93cSkardel fprintf(stdout, "# %s\n# %s\n", filename, 768abb0f93cSkardel ctime(&epoch)); 769*eabc0478Schristos dsa = EVP_PKEY_get1_DSA(pkey_iffkey); 770abb0f93cSkardel pkey = EVP_PKEY_new(); 771abb0f93cSkardel EVP_PKEY_assign_DSA(pkey, dsa); 7722950cc38Schristos PEM_write_PKCS8PrivateKey(stdout, pkey, cipher, NULL, 0, 7732950cc38Schristos NULL, passwd2); 7742950cc38Schristos fflush(stdout); 775*eabc0478Schristos if (debug) { 776abb0f93cSkardel DSA_print_fp(stderr, dsa, 0); 777abb0f93cSkardel } 778*eabc0478Schristos EVP_PKEY_free(pkey); 779*eabc0478Schristos pkey = NULL; 780*eabc0478Schristos DSA_free(dsa); 781*eabc0478Schristos } 782abb0f93cSkardel 783abb0f93cSkardel /* 784abb0f93cSkardel * Create new encrypted MV trusted-authority keys file if 785abb0f93cSkardel * requested; otherwise, look for existing keys file. 786abb0f93cSkardel */ 787abb0f93cSkardel if (mvkey) 788abb0f93cSkardel pkey_mvkey = gen_mvkey("mv", pkey_mvpar); 789abb0f93cSkardel if (pkey_mvkey == NULL) { 7902950cc38Schristos snprintf(filename, sizeof(filename), "ntpkey_mvta_%s", 7912950cc38Schristos groupname); 792abb0f93cSkardel pkey_mvkey = readkey(filename, passwd1, &fstamp, 793abb0f93cSkardel pkey_mvpar); 794abb0f93cSkardel if (pkey_mvkey != NULL) { 7952950cc38Schristos followlink(filename, sizeof(filename)); 796abb0f93cSkardel fprintf(stderr, "Using MV keys %s\n", 797abb0f93cSkardel filename); 798abb0f93cSkardel } 799abb0f93cSkardel } 800abb0f93cSkardel 801abb0f93cSkardel /* 802abb0f93cSkardel * Write the nonencrypted MV client parameters to the stdout 803abb0f93cSkardel * stream. For the moment, we always use the client parameters 804abb0f93cSkardel * associated with client key 1. 805abb0f93cSkardel */ 806abb0f93cSkardel if (pkey_mvkey != NULL && HAVE_OPT(ID_KEY)) { 8072950cc38Schristos snprintf(filename, sizeof(filename), 8082950cc38Schristos "ntpkey_mvpar_%s.%u", groupname, fstamp); 809abb0f93cSkardel fprintf(stderr, "Writing MV parameters %s to stdout\n", 810abb0f93cSkardel filename); 811abb0f93cSkardel fprintf(stdout, "# %s\n# %s\n", filename, 812abb0f93cSkardel ctime(&epoch)); 813abb0f93cSkardel pkey = pkey_mvpar[2]; 8142950cc38Schristos PEM_write_PKCS8PrivateKey(stdout, pkey, NULL, NULL, 0, 8152950cc38Schristos NULL, NULL); 8162950cc38Schristos fflush(stdout); 817*eabc0478Schristos if (debug) { 81803cfe0ffSchristos DSA_print_fp(stderr, EVP_PKEY_get0_DSA(pkey), 0); 819abb0f93cSkardel } 820*eabc0478Schristos } 821abb0f93cSkardel 822abb0f93cSkardel /* 823abb0f93cSkardel * Write the encrypted MV server keys to the stdout stream. 824abb0f93cSkardel */ 825abb0f93cSkardel if (pkey_mvkey != NULL && passwd2 != NULL) { 8262950cc38Schristos snprintf(filename, sizeof(filename), 8272950cc38Schristos "ntpkey_mvkey_%s.%u", groupname, fstamp); 828abb0f93cSkardel fprintf(stderr, "Writing MV keys %s to stdout\n", 829abb0f93cSkardel filename); 830abb0f93cSkardel fprintf(stdout, "# %s\n# %s\n", filename, 831abb0f93cSkardel ctime(&epoch)); 832abb0f93cSkardel pkey = pkey_mvpar[1]; 8332950cc38Schristos PEM_write_PKCS8PrivateKey(stdout, pkey, cipher, NULL, 0, 8342950cc38Schristos NULL, passwd2); 8352950cc38Schristos fflush(stdout); 836*eabc0478Schristos if (debug) { 83703cfe0ffSchristos DSA_print_fp(stderr, EVP_PKEY_get0_DSA(pkey), 0); 838abb0f93cSkardel } 839*eabc0478Schristos } 840abb0f93cSkardel 841abb0f93cSkardel /* 8422950cc38Schristos * Decode the digest/signature scheme and create the 8432950cc38Schristos * certificate. Do this every time we run the program. 844abb0f93cSkardel */ 845abb0f93cSkardel ectx = EVP_get_digestbyname(scheme); 846abb0f93cSkardel if (ectx == NULL) { 847abb0f93cSkardel fprintf(stderr, 848abb0f93cSkardel "Invalid digest/signature combination %s\n", 849abb0f93cSkardel scheme); 850abb0f93cSkardel exit (-1); 851abb0f93cSkardel } 8522950cc38Schristos x509(pkey_sign, ectx, grpkey, exten, certname); 8532950cc38Schristos #endif /* AUTOKEY */ 854abb0f93cSkardel exit(0); 855abb0f93cSkardel } 856abb0f93cSkardel 857abb0f93cSkardel 858abb0f93cSkardel /* 859abb0f93cSkardel * Generate semi-random MD5 keys compatible with NTPv3 and NTPv4. Also, 860abb0f93cSkardel * if OpenSSL is around, generate random SHA1 keys compatible with 861abb0f93cSkardel * symmetric key cryptography. 862abb0f93cSkardel */ 863abb0f93cSkardel int 864abb0f93cSkardel gen_md5( 865e19314b7Schristos const char *id /* file name id */ 866abb0f93cSkardel ) 867abb0f93cSkardel { 868abb0f93cSkardel u_char md5key[MD5SIZE + 1]; /* MD5 key */ 869abb0f93cSkardel FILE *str; 870abb0f93cSkardel int i, j; 871abb0f93cSkardel #ifdef OPENSSL 872abb0f93cSkardel u_char keystr[MD5SIZE]; 873abb0f93cSkardel u_char hexstr[2 * MD5SIZE + 1]; 874abb0f93cSkardel u_char hex[] = "0123456789abcdef"; 875abb0f93cSkardel #endif /* OPENSSL */ 876abb0f93cSkardel 877abb0f93cSkardel str = fheader("MD5key", id, groupname); 878abb0f93cSkardel for (i = 1; i <= MD5KEYS; i++) { 879abb0f93cSkardel for (j = 0; j < MD5SIZE; j++) { 8807476e6e4Schristos u_char temp; 881abb0f93cSkardel 882abb0f93cSkardel while (1) { 883ea66d795Schristos int rc; 884ea66d795Schristos 8857476e6e4Schristos rc = ntp_crypto_random_buf( 8867476e6e4Schristos &temp, sizeof(temp)); 887ea66d795Schristos if (-1 == rc) { 888ea66d795Schristos fprintf(stderr, "ntp_crypto_random_buf() failed.\n"); 889ea66d795Schristos exit (-1); 890ea66d795Schristos } 891abb0f93cSkardel if (temp == '#') 892abb0f93cSkardel continue; 893abb0f93cSkardel 894abb0f93cSkardel if (temp > 0x20 && temp < 0x7f) 895abb0f93cSkardel break; 896abb0f93cSkardel } 8977476e6e4Schristos md5key[j] = temp; 898abb0f93cSkardel } 899abb0f93cSkardel md5key[j] = '\0'; 900abb0f93cSkardel fprintf(str, "%2d MD5 %s # MD5 key\n", i, 901abb0f93cSkardel md5key); 902abb0f93cSkardel } 903abb0f93cSkardel #ifdef OPENSSL 904abb0f93cSkardel for (i = 1; i <= MD5KEYS; i++) { 905abb0f93cSkardel RAND_bytes(keystr, 20); 906abb0f93cSkardel for (j = 0; j < MD5SIZE; j++) { 907abb0f93cSkardel hexstr[2 * j] = hex[keystr[j] >> 4]; 908abb0f93cSkardel hexstr[2 * j + 1] = hex[keystr[j] & 0xf]; 909abb0f93cSkardel } 910abb0f93cSkardel hexstr[2 * MD5SIZE] = '\0'; 911abb0f93cSkardel fprintf(str, "%2d SHA1 %s # SHA1 key\n", i + MD5KEYS, 912abb0f93cSkardel hexstr); 913abb0f93cSkardel } 914abb0f93cSkardel #endif /* OPENSSL */ 915abb0f93cSkardel fclose(str); 916abb0f93cSkardel return (1); 917abb0f93cSkardel } 918abb0f93cSkardel 919abb0f93cSkardel 9202950cc38Schristos #ifdef AUTOKEY 921abb0f93cSkardel /* 922abb0f93cSkardel * readkey - load cryptographic parameters and keys 923abb0f93cSkardel * 924abb0f93cSkardel * This routine loads a PEM-encoded file of given name and password and 925abb0f93cSkardel * extracts the filestamp from the file name. It returns a pointer to 926abb0f93cSkardel * the first key if valid, NULL if not. 927abb0f93cSkardel */ 928abb0f93cSkardel EVP_PKEY * /* public/private key pair */ 929abb0f93cSkardel readkey( 930abb0f93cSkardel char *cp, /* file name */ 931abb0f93cSkardel char *passwd, /* password */ 932abb0f93cSkardel u_int *estamp, /* file stamp */ 933abb0f93cSkardel EVP_PKEY **evpars /* parameter list pointer */ 934abb0f93cSkardel ) 935abb0f93cSkardel { 936abb0f93cSkardel FILE *str; /* file handle */ 937abb0f93cSkardel EVP_PKEY *pkey = NULL; /* public/private key */ 938abb0f93cSkardel u_int gstamp; /* filestamp */ 939abb0f93cSkardel char linkname[MAXFILENAME]; /* filestamp buffer) */ 940abb0f93cSkardel EVP_PKEY *parkey; 941abb0f93cSkardel char *ptr; 942abb0f93cSkardel int i; 943abb0f93cSkardel 944abb0f93cSkardel /* 945abb0f93cSkardel * Open the key file. 946abb0f93cSkardel */ 947abb0f93cSkardel str = fopen(cp, "r"); 948abb0f93cSkardel if (str == NULL) 949abb0f93cSkardel return (NULL); 950abb0f93cSkardel 951abb0f93cSkardel /* 952abb0f93cSkardel * Read the filestamp, which is contained in the first line. 953abb0f93cSkardel */ 954abb0f93cSkardel if ((ptr = fgets(linkname, MAXFILENAME, str)) == NULL) { 955abb0f93cSkardel fprintf(stderr, "Empty key file %s\n", cp); 956abb0f93cSkardel fclose(str); 957abb0f93cSkardel return (NULL); 958abb0f93cSkardel } 959abb0f93cSkardel if ((ptr = strrchr(ptr, '.')) == NULL) { 960abb0f93cSkardel fprintf(stderr, "No filestamp found in %s\n", cp); 961abb0f93cSkardel fclose(str); 962abb0f93cSkardel return (NULL); 963abb0f93cSkardel } 964abb0f93cSkardel if (sscanf(++ptr, "%u", &gstamp) != 1) { 965abb0f93cSkardel fprintf(stderr, "Invalid filestamp found in %s\n", cp); 966abb0f93cSkardel fclose(str); 967abb0f93cSkardel return (NULL); 968abb0f93cSkardel } 969abb0f93cSkardel 970abb0f93cSkardel /* 971abb0f93cSkardel * Read and decrypt PEM-encoded private keys. The first one 972abb0f93cSkardel * found is returned. If others are expected, add them to the 973abb0f93cSkardel * parameter list. 974abb0f93cSkardel */ 975abb0f93cSkardel for (i = 0; i <= MVMAX - 1;) { 976abb0f93cSkardel parkey = PEM_read_PrivateKey(str, NULL, NULL, passwd); 977abb0f93cSkardel if (evpars != NULL) { 978abb0f93cSkardel evpars[i++] = parkey; 979abb0f93cSkardel evpars[i] = NULL; 980abb0f93cSkardel } 981abb0f93cSkardel if (parkey == NULL) 982abb0f93cSkardel break; 983abb0f93cSkardel 984abb0f93cSkardel if (pkey == NULL) 985abb0f93cSkardel pkey = parkey; 986abb0f93cSkardel if (debug) { 98703cfe0ffSchristos if (EVP_PKEY_base_id(parkey) == EVP_PKEY_DSA) 98803cfe0ffSchristos DSA_print_fp(stderr, EVP_PKEY_get0_DSA(parkey), 989abb0f93cSkardel 0); 99003cfe0ffSchristos else if (EVP_PKEY_base_id(parkey) == EVP_PKEY_RSA) 99103cfe0ffSchristos RSA_print_fp(stderr, EVP_PKEY_get0_RSA(parkey), 992abb0f93cSkardel 0); 993abb0f93cSkardel } 994abb0f93cSkardel } 995abb0f93cSkardel fclose(str); 996abb0f93cSkardel if (pkey == NULL) { 997abb0f93cSkardel fprintf(stderr, "Corrupt file %s or wrong key %s\n%s\n", 998abb0f93cSkardel cp, passwd, ERR_error_string(ERR_get_error(), 999abb0f93cSkardel NULL)); 1000abb0f93cSkardel exit (-1); 1001abb0f93cSkardel } 1002abb0f93cSkardel *estamp = gstamp; 1003abb0f93cSkardel return (pkey); 1004abb0f93cSkardel } 1005abb0f93cSkardel 1006abb0f93cSkardel 1007abb0f93cSkardel /* 1008abb0f93cSkardel * Generate RSA public/private key pair 1009abb0f93cSkardel */ 1010abb0f93cSkardel EVP_PKEY * /* public/private key pair */ 1011abb0f93cSkardel gen_rsa( 1012e19314b7Schristos const char *id /* file name id */ 1013abb0f93cSkardel ) 1014abb0f93cSkardel { 1015abb0f93cSkardel EVP_PKEY *pkey; /* private key */ 1016abb0f93cSkardel RSA *rsa; /* RSA parameters and key pair */ 1017abb0f93cSkardel FILE *str; 1018abb0f93cSkardel 1019abb0f93cSkardel fprintf(stderr, "Generating RSA keys (%d bits)...\n", modulus); 102003cfe0ffSchristos rsa = genRsaKeyPair(modulus, _UC("RSA")); 1021abb0f93cSkardel fprintf(stderr, "\n"); 1022abb0f93cSkardel if (rsa == NULL) { 1023abb0f93cSkardel fprintf(stderr, "RSA generate keys fails\n%s\n", 1024abb0f93cSkardel ERR_error_string(ERR_get_error(), NULL)); 1025abb0f93cSkardel return (NULL); 1026abb0f93cSkardel } 1027abb0f93cSkardel 1028abb0f93cSkardel /* 1029abb0f93cSkardel * For signature encryption it is not necessary that the RSA 1030abb0f93cSkardel * parameters be strictly groomed and once in a while the 1031abb0f93cSkardel * modulus turns out to be non-prime. Just for grins, we check 1032abb0f93cSkardel * the primality. 1033abb0f93cSkardel */ 1034abb0f93cSkardel if (!RSA_check_key(rsa)) { 1035abb0f93cSkardel fprintf(stderr, "Invalid RSA key\n%s\n", 1036abb0f93cSkardel ERR_error_string(ERR_get_error(), NULL)); 1037abb0f93cSkardel RSA_free(rsa); 1038abb0f93cSkardel return (NULL); 1039abb0f93cSkardel } 1040abb0f93cSkardel 1041abb0f93cSkardel /* 1042abb0f93cSkardel * Write the RSA parameters and keys as a RSA private key 1043abb0f93cSkardel * encoded in PEM. 1044abb0f93cSkardel */ 1045abb0f93cSkardel if (strcmp(id, "sign") == 0) 1046abb0f93cSkardel str = fheader("RSAsign", id, hostname); 1047abb0f93cSkardel else 1048abb0f93cSkardel str = fheader("RSAhost", id, hostname); 1049abb0f93cSkardel pkey = EVP_PKEY_new(); 1050abb0f93cSkardel EVP_PKEY_assign_RSA(pkey, rsa); 10512950cc38Schristos PEM_write_PKCS8PrivateKey(str, pkey, cipher, NULL, 0, NULL, 1052abb0f93cSkardel passwd1); 1053abb0f93cSkardel fclose(str); 1054abb0f93cSkardel if (debug) 1055abb0f93cSkardel RSA_print_fp(stderr, rsa, 0); 1056abb0f93cSkardel return (pkey); 1057abb0f93cSkardel } 1058abb0f93cSkardel 1059abb0f93cSkardel 1060abb0f93cSkardel /* 1061abb0f93cSkardel * Generate DSA public/private key pair 1062abb0f93cSkardel */ 1063abb0f93cSkardel EVP_PKEY * /* public/private key pair */ 1064abb0f93cSkardel gen_dsa( 1065e19314b7Schristos const char *id /* file name id */ 1066abb0f93cSkardel ) 1067abb0f93cSkardel { 1068abb0f93cSkardel EVP_PKEY *pkey; /* private key */ 1069abb0f93cSkardel DSA *dsa; /* DSA parameters */ 1070abb0f93cSkardel FILE *str; 1071abb0f93cSkardel 1072abb0f93cSkardel /* 1073abb0f93cSkardel * Generate DSA parameters. 1074abb0f93cSkardel */ 1075abb0f93cSkardel fprintf(stderr, 1076abb0f93cSkardel "Generating DSA parameters (%d bits)...\n", modulus); 107703cfe0ffSchristos dsa = genDsaParams(modulus, _UC("DSA")); 1078abb0f93cSkardel fprintf(stderr, "\n"); 1079abb0f93cSkardel if (dsa == NULL) { 1080abb0f93cSkardel fprintf(stderr, "DSA generate parameters fails\n%s\n", 1081abb0f93cSkardel ERR_error_string(ERR_get_error(), NULL)); 1082abb0f93cSkardel return (NULL); 1083abb0f93cSkardel } 1084abb0f93cSkardel 1085abb0f93cSkardel /* 1086abb0f93cSkardel * Generate DSA keys. 1087abb0f93cSkardel */ 1088abb0f93cSkardel fprintf(stderr, "Generating DSA keys (%d bits)...\n", modulus); 1089abb0f93cSkardel if (!DSA_generate_key(dsa)) { 1090abb0f93cSkardel fprintf(stderr, "DSA generate keys fails\n%s\n", 1091abb0f93cSkardel ERR_error_string(ERR_get_error(), NULL)); 1092abb0f93cSkardel DSA_free(dsa); 1093abb0f93cSkardel return (NULL); 1094abb0f93cSkardel } 1095abb0f93cSkardel 1096abb0f93cSkardel /* 1097abb0f93cSkardel * Write the DSA parameters and keys as a DSA private key 1098abb0f93cSkardel * encoded in PEM. 1099abb0f93cSkardel */ 1100abb0f93cSkardel str = fheader("DSAsign", id, hostname); 1101abb0f93cSkardel pkey = EVP_PKEY_new(); 1102abb0f93cSkardel EVP_PKEY_assign_DSA(pkey, dsa); 11032950cc38Schristos PEM_write_PKCS8PrivateKey(str, pkey, cipher, NULL, 0, NULL, 1104abb0f93cSkardel passwd1); 1105abb0f93cSkardel fclose(str); 1106abb0f93cSkardel if (debug) 1107abb0f93cSkardel DSA_print_fp(stderr, dsa, 0); 1108abb0f93cSkardel return (pkey); 1109abb0f93cSkardel } 1110abb0f93cSkardel 1111abb0f93cSkardel 1112abb0f93cSkardel /* 1113abb0f93cSkardel *********************************************************************** 1114abb0f93cSkardel * * 1115abb0f93cSkardel * The following routines implement the Schnorr (IFF) identity scheme * 1116abb0f93cSkardel * * 1117abb0f93cSkardel *********************************************************************** 1118abb0f93cSkardel * 1119abb0f93cSkardel * The Schnorr (IFF) identity scheme is intended for use when 1120abb0f93cSkardel * certificates are generated by some other trusted certificate 1121abb0f93cSkardel * authority and the certificate cannot be used to convey public 1122abb0f93cSkardel * parameters. There are two kinds of files: encrypted server files that 1123abb0f93cSkardel * contain private and public values and nonencrypted client files that 1124abb0f93cSkardel * contain only public values. New generations of server files must be 1125abb0f93cSkardel * securely transmitted to all servers of the group; client files can be 1126abb0f93cSkardel * distributed by any means. The scheme is self contained and 1127abb0f93cSkardel * independent of new generations of host keys, sign keys and 1128abb0f93cSkardel * certificates. 1129abb0f93cSkardel * 1130abb0f93cSkardel * The IFF values hide in a DSA cuckoo structure which uses the same 1131abb0f93cSkardel * parameters. The values are used by an identity scheme based on DSA 1132abb0f93cSkardel * cryptography and described in Stimson p. 285. The p is a 512-bit 1133abb0f93cSkardel * prime, g a generator of Zp* and q a 160-bit prime that divides p - 1 1134abb0f93cSkardel * and is a qth root of 1 mod p; that is, g^q = 1 mod p. The TA rolls a 1135abb0f93cSkardel * private random group key b (0 < b < q) and public key v = g^b, then 1136abb0f93cSkardel * sends (p, q, g, b) to the servers and (p, q, g, v) to the clients. 1137abb0f93cSkardel * Alice challenges Bob to confirm identity using the protocol described 1138abb0f93cSkardel * below. 1139abb0f93cSkardel * 1140abb0f93cSkardel * How it works 1141abb0f93cSkardel * 1142abb0f93cSkardel * The scheme goes like this. Both Alice and Bob have the public primes 1143abb0f93cSkardel * p, q and generator g. The TA gives private key b to Bob and public 1144abb0f93cSkardel * key v to Alice. 1145abb0f93cSkardel * 1146abb0f93cSkardel * Alice rolls new random challenge r (o < r < q) and sends to Bob in 1147abb0f93cSkardel * the IFF request message. Bob rolls new random k (0 < k < q), then 1148abb0f93cSkardel * computes y = k + b r mod q and x = g^k mod p and sends (y, hash(x)) 1149abb0f93cSkardel * to Alice in the response message. Besides making the response 1150abb0f93cSkardel * shorter, the hash makes it effectivey impossible for an intruder to 1151abb0f93cSkardel * solve for b by observing a number of these messages. 1152abb0f93cSkardel * 1153abb0f93cSkardel * Alice receives the response and computes g^y v^r mod p. After a bit 1154abb0f93cSkardel * of algebra, this simplifies to g^k. If the hash of this result 1155abb0f93cSkardel * matches hash(x), Alice knows that Bob has the group key b. The signed 1156abb0f93cSkardel * response binds this knowledge to Bob's private key and the public key 1157abb0f93cSkardel * previously received in his certificate. 1158abb0f93cSkardel */ 1159abb0f93cSkardel /* 1160abb0f93cSkardel * Generate Schnorr (IFF) keys. 1161abb0f93cSkardel */ 1162abb0f93cSkardel EVP_PKEY * /* DSA cuckoo nest */ 1163abb0f93cSkardel gen_iffkey( 1164e19314b7Schristos const char *id /* file name id */ 1165abb0f93cSkardel ) 1166abb0f93cSkardel { 1167abb0f93cSkardel EVP_PKEY *pkey; /* private key */ 1168abb0f93cSkardel DSA *dsa; /* DSA parameters */ 1169abb0f93cSkardel BN_CTX *ctx; /* BN working space */ 1170abb0f93cSkardel BIGNUM *b, *r, *k, *u, *v, *w; /* BN temp */ 1171abb0f93cSkardel FILE *str; 1172abb0f93cSkardel u_int temp; 117303cfe0ffSchristos const BIGNUM *p, *q, *g; 117403cfe0ffSchristos BIGNUM *pub_key, *priv_key; 1175abb0f93cSkardel 1176abb0f93cSkardel /* 1177abb0f93cSkardel * Generate DSA parameters for use as IFF parameters. 1178abb0f93cSkardel */ 1179abb0f93cSkardel fprintf(stderr, "Generating IFF keys (%d bits)...\n", 1180abb0f93cSkardel modulus2); 118103cfe0ffSchristos dsa = genDsaParams(modulus2, _UC("IFF")); 1182abb0f93cSkardel fprintf(stderr, "\n"); 1183abb0f93cSkardel if (dsa == NULL) { 1184abb0f93cSkardel fprintf(stderr, "DSA generate parameters fails\n%s\n", 1185abb0f93cSkardel ERR_error_string(ERR_get_error(), NULL)); 118603cfe0ffSchristos return (NULL); 1187abb0f93cSkardel } 118803cfe0ffSchristos DSA_get0_pqg(dsa, &p, &q, &g); 1189abb0f93cSkardel 1190abb0f93cSkardel /* 1191abb0f93cSkardel * Generate the private and public keys. The DSA parameters and 1192abb0f93cSkardel * private key are distributed to the servers, while all except 1193abb0f93cSkardel * the private key are distributed to the clients. 1194abb0f93cSkardel */ 1195abb0f93cSkardel b = BN_new(); r = BN_new(); k = BN_new(); 1196abb0f93cSkardel u = BN_new(); v = BN_new(); w = BN_new(); ctx = BN_CTX_new(); 119703cfe0ffSchristos BN_rand(b, BN_num_bits(q), -1, 0); /* a */ 119803cfe0ffSchristos BN_mod(b, b, q, ctx); 119903cfe0ffSchristos BN_sub(v, q, b); 120003cfe0ffSchristos BN_mod_exp(v, g, v, p, ctx); /* g^(q - b) mod p */ 120103cfe0ffSchristos BN_mod_exp(u, g, b, p, ctx); /* g^b mod p */ 120203cfe0ffSchristos BN_mod_mul(u, u, v, p, ctx); 1203abb0f93cSkardel temp = BN_is_one(u); 1204abb0f93cSkardel fprintf(stderr, 1205abb0f93cSkardel "Confirm g^(q - b) g^b = 1 mod p: %s\n", temp == 1 ? 1206abb0f93cSkardel "yes" : "no"); 1207abb0f93cSkardel if (!temp) { 1208abb0f93cSkardel BN_free(b); BN_free(r); BN_free(k); 1209abb0f93cSkardel BN_free(u); BN_free(v); BN_free(w); BN_CTX_free(ctx); 1210abb0f93cSkardel return (NULL); 1211abb0f93cSkardel } 121203cfe0ffSchristos pub_key = BN_dup(v); 121303cfe0ffSchristos priv_key = BN_dup(b); 121403cfe0ffSchristos DSA_set0_key(dsa, pub_key, priv_key); 1215abb0f93cSkardel 1216abb0f93cSkardel /* 1217abb0f93cSkardel * Here is a trial round of the protocol. First, Alice rolls 1218abb0f93cSkardel * random nonce r mod q and sends it to Bob. She needs only 1219abb0f93cSkardel * q from parameters. 1220abb0f93cSkardel */ 122103cfe0ffSchristos BN_rand(r, BN_num_bits(q), -1, 0); /* r */ 122203cfe0ffSchristos BN_mod(r, r, q, ctx); 1223abb0f93cSkardel 1224abb0f93cSkardel /* 1225abb0f93cSkardel * Bob rolls random nonce k mod q, computes y = k + b r mod q 1226abb0f93cSkardel * and x = g^k mod p, then sends (y, x) to Alice. He needs 1227abb0f93cSkardel * p, q and b from parameters and r from Alice. 1228abb0f93cSkardel */ 122903cfe0ffSchristos BN_rand(k, BN_num_bits(q), -1, 0); /* k, 0 < k < q */ 123003cfe0ffSchristos BN_mod(k, k, q, ctx); 123103cfe0ffSchristos BN_mod_mul(v, priv_key, r, q, ctx); /* b r mod q */ 1232abb0f93cSkardel BN_add(v, v, k); 123303cfe0ffSchristos BN_mod(v, v, q, ctx); /* y = k + b r mod q */ 123403cfe0ffSchristos BN_mod_exp(u, g, k, p, ctx); /* x = g^k mod p */ 1235abb0f93cSkardel 1236abb0f93cSkardel /* 1237abb0f93cSkardel * Alice verifies x = g^y v^r to confirm that Bob has group key 1238abb0f93cSkardel * b. She needs p, q, g from parameters, (y, x) from Bob and the 1239abb0f93cSkardel * original r. We omit the detail here thatt only the hash of y 1240abb0f93cSkardel * is sent. 1241abb0f93cSkardel */ 124203cfe0ffSchristos BN_mod_exp(v, g, v, p, ctx); /* g^y mod p */ 124303cfe0ffSchristos BN_mod_exp(w, pub_key, r, p, ctx); /* v^r */ 124403cfe0ffSchristos BN_mod_mul(v, w, v, p, ctx); /* product mod p */ 1245abb0f93cSkardel temp = BN_cmp(u, v); 1246abb0f93cSkardel fprintf(stderr, 1247abb0f93cSkardel "Confirm g^k = g^(k + b r) g^(q - b) r: %s\n", temp == 1248abb0f93cSkardel 0 ? "yes" : "no"); 1249abb0f93cSkardel BN_free(b); BN_free(r); BN_free(k); 1250abb0f93cSkardel BN_free(u); BN_free(v); BN_free(w); BN_CTX_free(ctx); 1251abb0f93cSkardel if (temp != 0) { 1252abb0f93cSkardel DSA_free(dsa); 1253abb0f93cSkardel return (NULL); 1254abb0f93cSkardel } 1255abb0f93cSkardel 1256abb0f93cSkardel /* 1257abb0f93cSkardel * Write the IFF keys as an encrypted DSA private key encoded in 1258abb0f93cSkardel * PEM. 1259abb0f93cSkardel * 1260abb0f93cSkardel * p modulus p 1261abb0f93cSkardel * q modulus q 1262abb0f93cSkardel * g generator g 1263abb0f93cSkardel * priv_key b 1264abb0f93cSkardel * public_key v 1265abb0f93cSkardel * kinv not used 1266abb0f93cSkardel * r not used 1267abb0f93cSkardel */ 1268abb0f93cSkardel str = fheader("IFFkey", id, groupname); 1269abb0f93cSkardel pkey = EVP_PKEY_new(); 1270abb0f93cSkardel EVP_PKEY_assign_DSA(pkey, dsa); 12712950cc38Schristos PEM_write_PKCS8PrivateKey(str, pkey, cipher, NULL, 0, NULL, 1272abb0f93cSkardel passwd1); 1273abb0f93cSkardel fclose(str); 1274abb0f93cSkardel if (debug) 1275abb0f93cSkardel DSA_print_fp(stderr, dsa, 0); 1276abb0f93cSkardel return (pkey); 1277abb0f93cSkardel } 1278abb0f93cSkardel 1279abb0f93cSkardel 1280abb0f93cSkardel /* 1281abb0f93cSkardel *********************************************************************** 1282abb0f93cSkardel * * 1283abb0f93cSkardel * The following routines implement the Guillou-Quisquater (GQ) * 1284abb0f93cSkardel * identity scheme * 1285abb0f93cSkardel * * 1286abb0f93cSkardel *********************************************************************** 1287abb0f93cSkardel * 1288abb0f93cSkardel * The Guillou-Quisquater (GQ) identity scheme is intended for use when 1289abb0f93cSkardel * the certificate can be used to convey public parameters. The scheme 1290abb0f93cSkardel * uses a X509v3 certificate extension field do convey the public key of 1291abb0f93cSkardel * a private key known only to servers. There are two kinds of files: 1292abb0f93cSkardel * encrypted server files that contain private and public values and 1293abb0f93cSkardel * nonencrypted client files that contain only public values. New 1294abb0f93cSkardel * generations of server files must be securely transmitted to all 1295abb0f93cSkardel * servers of the group; client files can be distributed by any means. 1296abb0f93cSkardel * The scheme is self contained and independent of new generations of 1297abb0f93cSkardel * host keys and sign keys. The scheme is self contained and independent 1298abb0f93cSkardel * of new generations of host keys and sign keys. 1299abb0f93cSkardel * 1300abb0f93cSkardel * The GQ parameters hide in a RSA cuckoo structure which uses the same 1301abb0f93cSkardel * parameters. The values are used by an identity scheme based on RSA 1302abb0f93cSkardel * cryptography and described in Stimson p. 300 (with errors). The 512- 1303abb0f93cSkardel * bit public modulus is n = p q, where p and q are secret large primes. 1304abb0f93cSkardel * The TA rolls private random group key b as RSA exponent. These values 1305abb0f93cSkardel * are known to all group members. 1306abb0f93cSkardel * 1307abb0f93cSkardel * When rolling new certificates, a server recomputes the private and 1308abb0f93cSkardel * public keys. The private key u is a random roll, while the public key 1309abb0f93cSkardel * is the inverse obscured by the group key v = (u^-1)^b. These values 1310abb0f93cSkardel * replace the private and public keys normally generated by the RSA 1311abb0f93cSkardel * scheme. Alice challenges Bob to confirm identity using the protocol 1312abb0f93cSkardel * described below. 1313abb0f93cSkardel * 1314abb0f93cSkardel * How it works 1315abb0f93cSkardel * 1316abb0f93cSkardel * The scheme goes like this. Both Alice and Bob have the same modulus n 1317abb0f93cSkardel * and some random b as the group key. These values are computed and 1318abb0f93cSkardel * distributed in advance via secret means, although only the group key 1319abb0f93cSkardel * b is truly secret. Each has a private random private key u and public 1320abb0f93cSkardel * key (u^-1)^b, although not necessarily the same ones. Bob and Alice 1321abb0f93cSkardel * can regenerate the key pair from time to time without affecting 1322abb0f93cSkardel * operations. The public key is conveyed on the certificate in an 1323abb0f93cSkardel * extension field; the private key is never revealed. 1324abb0f93cSkardel * 1325abb0f93cSkardel * Alice rolls new random challenge r and sends to Bob in the GQ 1326abb0f93cSkardel * request message. Bob rolls new random k, then computes y = k u^r mod 1327abb0f93cSkardel * n and x = k^b mod n and sends (y, hash(x)) to Alice in the response 1328abb0f93cSkardel * message. Besides making the response shorter, the hash makes it 1329abb0f93cSkardel * effectivey impossible for an intruder to solve for b by observing 1330abb0f93cSkardel * a number of these messages. 1331abb0f93cSkardel * 1332abb0f93cSkardel * Alice receives the response and computes y^b v^r mod n. After a bit 1333abb0f93cSkardel * of algebra, this simplifies to k^b. If the hash of this result 1334abb0f93cSkardel * matches hash(x), Alice knows that Bob has the group key b. The signed 1335abb0f93cSkardel * response binds this knowledge to Bob's private key and the public key 1336abb0f93cSkardel * previously received in his certificate. 1337abb0f93cSkardel */ 1338abb0f93cSkardel /* 1339abb0f93cSkardel * Generate Guillou-Quisquater (GQ) parameters file. 1340abb0f93cSkardel */ 1341abb0f93cSkardel EVP_PKEY * /* RSA cuckoo nest */ 1342abb0f93cSkardel gen_gqkey( 1343e19314b7Schristos const char *id /* file name id */ 1344abb0f93cSkardel ) 1345abb0f93cSkardel { 1346abb0f93cSkardel EVP_PKEY *pkey; /* private key */ 1347abb0f93cSkardel RSA *rsa; /* RSA parameters */ 1348abb0f93cSkardel BN_CTX *ctx; /* BN working space */ 1349abb0f93cSkardel BIGNUM *u, *v, *g, *k, *r, *y; /* BN temps */ 1350abb0f93cSkardel FILE *str; 1351abb0f93cSkardel u_int temp; 135203cfe0ffSchristos BIGNUM *b; 135303cfe0ffSchristos const BIGNUM *n; 1354abb0f93cSkardel 1355abb0f93cSkardel /* 1356abb0f93cSkardel * Generate RSA parameters for use as GQ parameters. 1357abb0f93cSkardel */ 1358abb0f93cSkardel fprintf(stderr, 1359abb0f93cSkardel "Generating GQ parameters (%d bits)...\n", 1360abb0f93cSkardel modulus2); 136103cfe0ffSchristos rsa = genRsaKeyPair(modulus2, _UC("GQ")); 1362abb0f93cSkardel fprintf(stderr, "\n"); 1363abb0f93cSkardel if (rsa == NULL) { 1364abb0f93cSkardel fprintf(stderr, "RSA generate keys fails\n%s\n", 1365abb0f93cSkardel ERR_error_string(ERR_get_error(), NULL)); 1366abb0f93cSkardel return (NULL); 1367abb0f93cSkardel } 136803cfe0ffSchristos RSA_get0_key(rsa, &n, NULL, NULL); 13692950cc38Schristos u = BN_new(); v = BN_new(); g = BN_new(); 13702950cc38Schristos k = BN_new(); r = BN_new(); y = BN_new(); 137103cfe0ffSchristos b = BN_new(); 1372abb0f93cSkardel 1373abb0f93cSkardel /* 1374abb0f93cSkardel * Generate the group key b, which is saved in the e member of 1375abb0f93cSkardel * the RSA structure. The group key is transmitted to each group 1376abb0f93cSkardel * member encrypted by the member private key. 1377abb0f93cSkardel */ 1378abb0f93cSkardel ctx = BN_CTX_new(); 137903cfe0ffSchristos BN_rand(b, BN_num_bits(n), -1, 0); /* b */ 138003cfe0ffSchristos BN_mod(b, b, n, ctx); 1381abb0f93cSkardel 1382abb0f93cSkardel /* 1383abb0f93cSkardel * When generating his certificate, Bob rolls random private key 1384abb0f93cSkardel * u, then computes inverse v = u^-1. 1385abb0f93cSkardel */ 138603cfe0ffSchristos BN_rand(u, BN_num_bits(n), -1, 0); /* u */ 138703cfe0ffSchristos BN_mod(u, u, n, ctx); 138803cfe0ffSchristos BN_mod_inverse(v, u, n, ctx); /* u^-1 mod n */ 138903cfe0ffSchristos BN_mod_mul(k, v, u, n, ctx); 1390abb0f93cSkardel 1391abb0f93cSkardel /* 1392abb0f93cSkardel * Bob computes public key v = (u^-1)^b, which is saved in an 1393abb0f93cSkardel * extension field on his certificate. We check that u^b v = 1394abb0f93cSkardel * 1 mod n. 1395abb0f93cSkardel */ 139603cfe0ffSchristos BN_mod_exp(v, v, b, n, ctx); 139703cfe0ffSchristos BN_mod_exp(g, u, b, n, ctx); /* u^b */ 139803cfe0ffSchristos BN_mod_mul(g, g, v, n, ctx); /* u^b (u^-1)^b */ 1399abb0f93cSkardel temp = BN_is_one(g); 1400abb0f93cSkardel fprintf(stderr, 1401abb0f93cSkardel "Confirm u^b (u^-1)^b = 1 mod n: %s\n", temp ? "yes" : 1402abb0f93cSkardel "no"); 1403abb0f93cSkardel if (!temp) { 1404abb0f93cSkardel BN_free(u); BN_free(v); 1405abb0f93cSkardel BN_free(g); BN_free(k); BN_free(r); BN_free(y); 1406abb0f93cSkardel BN_CTX_free(ctx); 1407abb0f93cSkardel RSA_free(rsa); 1408abb0f93cSkardel return (NULL); 1409abb0f93cSkardel } 141003cfe0ffSchristos /* setting 'u' and 'v' into a RSA object takes over ownership. 141103cfe0ffSchristos * Since we use these values again, we have to pass in dupes, 141203cfe0ffSchristos * or we'll corrupt the program! 141303cfe0ffSchristos */ 141403cfe0ffSchristos RSA_set0_factors(rsa, BN_dup(u), BN_dup(v)); 1415abb0f93cSkardel 1416abb0f93cSkardel /* 1417abb0f93cSkardel * Here is a trial run of the protocol. First, Alice rolls 1418abb0f93cSkardel * random nonce r mod n and sends it to Bob. She needs only n 1419abb0f93cSkardel * from parameters. 1420abb0f93cSkardel */ 142103cfe0ffSchristos BN_rand(r, BN_num_bits(n), -1, 0); /* r */ 142203cfe0ffSchristos BN_mod(r, r, n, ctx); 1423abb0f93cSkardel 1424abb0f93cSkardel /* 1425abb0f93cSkardel * Bob rolls random nonce k mod n, computes y = k u^r mod n and 1426abb0f93cSkardel * g = k^b mod n, then sends (y, g) to Alice. He needs n, u, b 1427abb0f93cSkardel * from parameters and r from Alice. 1428abb0f93cSkardel */ 142903cfe0ffSchristos BN_rand(k, BN_num_bits(n), -1, 0); /* k */ 143003cfe0ffSchristos BN_mod(k, k, n, ctx); 143103cfe0ffSchristos BN_mod_exp(y, u, r, n, ctx); /* u^r mod n */ 143203cfe0ffSchristos BN_mod_mul(y, k, y, n, ctx); /* y = k u^r mod n */ 143303cfe0ffSchristos BN_mod_exp(g, k, b, n, ctx); /* g = k^b mod n */ 1434abb0f93cSkardel 1435abb0f93cSkardel /* 1436abb0f93cSkardel * Alice verifies g = v^r y^b mod n to confirm that Bob has 1437abb0f93cSkardel * private key u. She needs n, g from parameters, public key v = 1438abb0f93cSkardel * (u^-1)^b from the certificate, (y, g) from Bob and the 1439abb0f93cSkardel * original r. We omit the detaul here that only the hash of g 1440abb0f93cSkardel * is sent. 1441abb0f93cSkardel */ 144203cfe0ffSchristos BN_mod_exp(v, v, r, n, ctx); /* v^r mod n */ 144303cfe0ffSchristos BN_mod_exp(y, y, b, n, ctx); /* y^b mod n */ 144403cfe0ffSchristos BN_mod_mul(y, v, y, n, ctx); /* v^r y^b mod n */ 1445abb0f93cSkardel temp = BN_cmp(y, g); 1446abb0f93cSkardel fprintf(stderr, "Confirm g^k = v^r y^b mod n: %s\n", temp == 0 ? 1447abb0f93cSkardel "yes" : "no"); 1448abb0f93cSkardel BN_CTX_free(ctx); BN_free(u); BN_free(v); 1449abb0f93cSkardel BN_free(g); BN_free(k); BN_free(r); BN_free(y); 1450abb0f93cSkardel if (temp != 0) { 1451abb0f93cSkardel RSA_free(rsa); 1452abb0f93cSkardel return (NULL); 1453abb0f93cSkardel } 1454abb0f93cSkardel 1455abb0f93cSkardel /* 1456abb0f93cSkardel * Write the GQ parameter file as an encrypted RSA private key 1457abb0f93cSkardel * encoded in PEM. 1458abb0f93cSkardel * 1459abb0f93cSkardel * n modulus n 1460abb0f93cSkardel * e group key b 1461abb0f93cSkardel * d not used 1462abb0f93cSkardel * p private key u 1463abb0f93cSkardel * q public key (u^-1)^b 1464abb0f93cSkardel * dmp1 not used 1465abb0f93cSkardel * dmq1 not used 1466abb0f93cSkardel * iqmp not used 1467abb0f93cSkardel */ 146803cfe0ffSchristos RSA_set0_key(rsa, NULL, b, BN_dup(BN_value_one())); 146903cfe0ffSchristos RSA_set0_crt_params(rsa, BN_dup(BN_value_one()), BN_dup(BN_value_one()), 147003cfe0ffSchristos BN_dup(BN_value_one())); 1471abb0f93cSkardel str = fheader("GQkey", id, groupname); 1472abb0f93cSkardel pkey = EVP_PKEY_new(); 1473abb0f93cSkardel EVP_PKEY_assign_RSA(pkey, rsa); 14742950cc38Schristos PEM_write_PKCS8PrivateKey(str, pkey, cipher, NULL, 0, NULL, 1475abb0f93cSkardel passwd1); 1476abb0f93cSkardel fclose(str); 1477abb0f93cSkardel if (debug) 1478abb0f93cSkardel RSA_print_fp(stderr, rsa, 0); 1479abb0f93cSkardel return (pkey); 1480abb0f93cSkardel } 1481abb0f93cSkardel 1482abb0f93cSkardel 1483abb0f93cSkardel /* 1484abb0f93cSkardel *********************************************************************** 1485abb0f93cSkardel * * 1486abb0f93cSkardel * The following routines implement the Mu-Varadharajan (MV) identity * 1487abb0f93cSkardel * scheme * 1488abb0f93cSkardel * * 1489abb0f93cSkardel *********************************************************************** 1490abb0f93cSkardel * 1491abb0f93cSkardel * The Mu-Varadharajan (MV) cryptosystem was originally intended when 1492abb0f93cSkardel * servers broadcast messages to clients, but clients never send 1493abb0f93cSkardel * messages to servers. There is one encryption key for the server and a 1494abb0f93cSkardel * separate decryption key for each client. It operated something like a 1495abb0f93cSkardel * pay-per-view satellite broadcasting system where the session key is 1496abb0f93cSkardel * encrypted by the broadcaster and the decryption keys are held in a 1497abb0f93cSkardel * tamperproof set-top box. 1498abb0f93cSkardel * 1499abb0f93cSkardel * The MV parameters and private encryption key hide in a DSA cuckoo 1500abb0f93cSkardel * structure which uses the same parameters, but generated in a 1501abb0f93cSkardel * different way. The values are used in an encryption scheme similar to 1502abb0f93cSkardel * El Gamal cryptography and a polynomial formed from the expansion of 1503abb0f93cSkardel * product terms (x - x[j]), as described in Mu, Y., and V. 1504abb0f93cSkardel * Varadharajan: Robust and Secure Broadcasting, Proc. Indocrypt 2001, 1505abb0f93cSkardel * 223-231. The paper has significant errors and serious omissions. 1506abb0f93cSkardel * 1507abb0f93cSkardel * Let q be the product of n distinct primes s1[j] (j = 1...n), where 1508abb0f93cSkardel * each s1[j] has m significant bits. Let p be a prime p = 2 * q + 1, so 1509abb0f93cSkardel * that q and each s1[j] divide p - 1 and p has M = n * m + 1 1510abb0f93cSkardel * significant bits. Let g be a generator of Zp; that is, gcd(g, p - 1) 1511abb0f93cSkardel * = 1 and g^q = 1 mod p. We do modular arithmetic over Zq and then 1512abb0f93cSkardel * project into Zp* as exponents of g. Sometimes we have to compute an 1513abb0f93cSkardel * inverse b^-1 of random b in Zq, but for that purpose we require 1514abb0f93cSkardel * gcd(b, q) = 1. We expect M to be in the 500-bit range and n 1515abb0f93cSkardel * relatively small, like 30. These are the parameters of the scheme and 1516abb0f93cSkardel * they are expensive to compute. 1517abb0f93cSkardel * 1518abb0f93cSkardel * We set up an instance of the scheme as follows. A set of random 1519abb0f93cSkardel * values x[j] mod q (j = 1...n), are generated as the zeros of a 1520abb0f93cSkardel * polynomial of order n. The product terms (x - x[j]) are expanded to 1521abb0f93cSkardel * form coefficients a[i] mod q (i = 0...n) in powers of x. These are 1522abb0f93cSkardel * used as exponents of the generator g mod p to generate the private 1523abb0f93cSkardel * encryption key A. The pair (gbar, ghat) of public server keys and the 1524abb0f93cSkardel * pairs (xbar[j], xhat[j]) (j = 1...n) of private client keys are used 1525abb0f93cSkardel * to construct the decryption keys. The devil is in the details. 1526abb0f93cSkardel * 1527abb0f93cSkardel * This routine generates a private server encryption file including the 1528abb0f93cSkardel * private encryption key E and partial decryption keys gbar and ghat. 1529abb0f93cSkardel * It then generates public client decryption files including the public 1530abb0f93cSkardel * keys xbar[j] and xhat[j] for each client j. The partial decryption 1531abb0f93cSkardel * files are used to compute the inverse of E. These values are suitably 1532abb0f93cSkardel * blinded so secrets are not revealed. 1533abb0f93cSkardel * 1534abb0f93cSkardel * The distinguishing characteristic of this scheme is the capability to 1535abb0f93cSkardel * revoke keys. Included in the calculation of E, gbar and ghat is the 1536abb0f93cSkardel * product s = prod(s1[j]) (j = 1...n) above. If the factor s1[j] is 1537abb0f93cSkardel * subsequently removed from the product and E, gbar and ghat 1538abb0f93cSkardel * recomputed, the jth client will no longer be able to compute E^-1 and 1539abb0f93cSkardel * thus unable to decrypt the messageblock. 1540abb0f93cSkardel * 1541abb0f93cSkardel * How it works 1542abb0f93cSkardel * 15432950cc38Schristos * The scheme goes like this. Bob has the server values (p, E, q, 15442950cc38Schristos * gbar, ghat) and Alice has the client values (p, xbar, xhat). 1545abb0f93cSkardel * 1546abb0f93cSkardel * Alice rolls new random nonce r mod p and sends to Bob in the MV 1547abb0f93cSkardel * request message. Bob rolls random nonce k mod q, encrypts y = r E^k 1548abb0f93cSkardel * mod p and sends (y, gbar^k, ghat^k) to Alice. 1549abb0f93cSkardel * 1550abb0f93cSkardel * Alice receives the response and computes the inverse (E^k)^-1 from 1551abb0f93cSkardel * the partial decryption keys gbar^k, ghat^k, xbar and xhat. She then 1552abb0f93cSkardel * decrypts y and verifies it matches the original r. The signed 1553abb0f93cSkardel * response binds this knowledge to Bob's private key and the public key 1554abb0f93cSkardel * previously received in his certificate. 1555abb0f93cSkardel */ 1556abb0f93cSkardel EVP_PKEY * /* DSA cuckoo nest */ 1557abb0f93cSkardel gen_mvkey( 1558e19314b7Schristos const char *id, /* file name id */ 1559abb0f93cSkardel EVP_PKEY **evpars /* parameter list pointer */ 1560abb0f93cSkardel ) 1561abb0f93cSkardel { 1562abb0f93cSkardel EVP_PKEY *pkey, *pkey1; /* private keys */ 1563abb0f93cSkardel DSA *dsa, *dsa2, *sdsa; /* DSA parameters */ 1564abb0f93cSkardel BN_CTX *ctx; /* BN working space */ 1565abb0f93cSkardel BIGNUM *a[MVMAX]; /* polynomial coefficient vector */ 156603cfe0ffSchristos BIGNUM *gs[MVMAX]; /* public key vector */ 1567abb0f93cSkardel BIGNUM *s1[MVMAX]; /* private enabling keys */ 1568abb0f93cSkardel BIGNUM *x[MVMAX]; /* polynomial zeros vector */ 1569abb0f93cSkardel BIGNUM *xbar[MVMAX], *xhat[MVMAX]; /* private keys vector */ 1570abb0f93cSkardel BIGNUM *b; /* group key */ 1571abb0f93cSkardel BIGNUM *b1; /* inverse group key */ 1572abb0f93cSkardel BIGNUM *s; /* enabling key */ 1573abb0f93cSkardel BIGNUM *biga; /* master encryption key */ 1574abb0f93cSkardel BIGNUM *bige; /* session encryption key */ 1575abb0f93cSkardel BIGNUM *gbar, *ghat; /* public key */ 1576abb0f93cSkardel BIGNUM *u, *v, *w; /* BN scratch */ 157703cfe0ffSchristos BIGNUM *p, *q, *g, *priv_key, *pub_key; 1578abb0f93cSkardel int i, j, n; 1579abb0f93cSkardel FILE *str; 1580abb0f93cSkardel u_int temp; 1581abb0f93cSkardel 1582abb0f93cSkardel /* 1583abb0f93cSkardel * Generate MV parameters. 1584abb0f93cSkardel * 1585abb0f93cSkardel * The object is to generate a multiplicative group Zp* modulo a 1586abb0f93cSkardel * prime p and a subset Zq mod q, where q is the product of n 1587abb0f93cSkardel * distinct primes s1[j] (j = 1...n) and q divides p - 1. We 1588abb0f93cSkardel * first generate n m-bit primes, where the product n m is in 1589abb0f93cSkardel * the order of 512 bits. One or more of these may have to be 1590abb0f93cSkardel * replaced later. As a practical matter, it is tough to find 1591abb0f93cSkardel * more than 31 distinct primes for 512 bits or 61 primes for 1592abb0f93cSkardel * 1024 bits. The latter can take several hundred iterations 1593abb0f93cSkardel * and several minutes on a Sun Blade 1000. 1594abb0f93cSkardel */ 1595abb0f93cSkardel n = nkeys; 1596abb0f93cSkardel fprintf(stderr, 1597abb0f93cSkardel "Generating MV parameters for %d keys (%d bits)...\n", n, 1598abb0f93cSkardel modulus2 / n); 1599abb0f93cSkardel ctx = BN_CTX_new(); u = BN_new(); v = BN_new(); w = BN_new(); 1600abb0f93cSkardel b = BN_new(); b1 = BN_new(); 1601abb0f93cSkardel dsa = DSA_new(); 160203cfe0ffSchristos p = BN_new(); q = BN_new(); g = BN_new(); 160303cfe0ffSchristos priv_key = BN_new(); pub_key = BN_new(); 1604abb0f93cSkardel temp = 0; 1605abb0f93cSkardel for (j = 1; j <= n; j++) { 1606abb0f93cSkardel s1[j] = BN_new(); 1607abb0f93cSkardel while (1) { 160803cfe0ffSchristos BN_generate_prime_ex(s1[j], modulus2 / n, 0, 1609abb0f93cSkardel NULL, NULL, NULL); 1610abb0f93cSkardel for (i = 1; i < j; i++) { 1611abb0f93cSkardel if (BN_cmp(s1[i], s1[j]) == 0) 1612abb0f93cSkardel break; 1613abb0f93cSkardel } 1614abb0f93cSkardel if (i == j) 1615abb0f93cSkardel break; 1616abb0f93cSkardel temp++; 1617abb0f93cSkardel } 1618abb0f93cSkardel } 1619abb0f93cSkardel fprintf(stderr, "Birthday keys regenerated %d\n", temp); 1620abb0f93cSkardel 1621abb0f93cSkardel /* 1622abb0f93cSkardel * Compute the modulus q as the product of the primes. Compute 1623abb0f93cSkardel * the modulus p as 2 * q + 1 and test p for primality. If p 1624abb0f93cSkardel * is composite, replace one of the primes with a new distinct 1625abb0f93cSkardel * one and try again. Note that q will hardly be a secret since 1626abb0f93cSkardel * we have to reveal p to servers, but not clients. However, 1627abb0f93cSkardel * factoring q to find the primes should be adequately hard, as 1628abb0f93cSkardel * this is the same problem considered hard in RSA. Question: is 1629abb0f93cSkardel * it as hard to find n small prime factors totalling n bits as 1630abb0f93cSkardel * it is to find two large prime factors totalling n bits? 1631abb0f93cSkardel * Remember, the bad guy doesn't know n. 1632abb0f93cSkardel */ 1633abb0f93cSkardel temp = 0; 1634abb0f93cSkardel while (1) { 163503cfe0ffSchristos BN_one(q); 1636abb0f93cSkardel for (j = 1; j <= n; j++) 163703cfe0ffSchristos BN_mul(q, q, s1[j], ctx); 163803cfe0ffSchristos BN_copy(p, q); 163903cfe0ffSchristos BN_add(p, p, p); 164003cfe0ffSchristos BN_add_word(p, 1); 164103cfe0ffSchristos if (BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) 1642abb0f93cSkardel break; 1643abb0f93cSkardel 1644abb0f93cSkardel temp++; 1645abb0f93cSkardel j = temp % n + 1; 1646abb0f93cSkardel while (1) { 164703cfe0ffSchristos BN_generate_prime_ex(u, modulus2 / n, 0, 164803cfe0ffSchristos NULL, NULL, NULL); 1649abb0f93cSkardel for (i = 1; i <= n; i++) { 1650abb0f93cSkardel if (BN_cmp(u, s1[i]) == 0) 1651abb0f93cSkardel break; 1652abb0f93cSkardel } 1653abb0f93cSkardel if (i > n) 1654abb0f93cSkardel break; 1655abb0f93cSkardel } 1656abb0f93cSkardel BN_copy(s1[j], u); 1657abb0f93cSkardel } 1658abb0f93cSkardel fprintf(stderr, "Defective keys regenerated %d\n", temp); 1659abb0f93cSkardel 1660abb0f93cSkardel /* 1661abb0f93cSkardel * Compute the generator g using a random roll such that 1662abb0f93cSkardel * gcd(g, p - 1) = 1 and g^q = 1. This is a generator of p, not 1663abb0f93cSkardel * q. This may take several iterations. 1664abb0f93cSkardel */ 166503cfe0ffSchristos BN_copy(v, p); 1666abb0f93cSkardel BN_sub_word(v, 1); 1667abb0f93cSkardel while (1) { 166803cfe0ffSchristos BN_rand(g, BN_num_bits(p) - 1, 0, 0); 166903cfe0ffSchristos BN_mod(g, g, p, ctx); 167003cfe0ffSchristos BN_gcd(u, g, v, ctx); 1671abb0f93cSkardel if (!BN_is_one(u)) 1672abb0f93cSkardel continue; 1673abb0f93cSkardel 167403cfe0ffSchristos BN_mod_exp(u, g, q, p, ctx); 1675abb0f93cSkardel if (BN_is_one(u)) 1676abb0f93cSkardel break; 1677abb0f93cSkardel } 1678abb0f93cSkardel 167903cfe0ffSchristos DSA_set0_pqg(dsa, p, q, g); 168003cfe0ffSchristos 1681abb0f93cSkardel /* 1682abb0f93cSkardel * Setup is now complete. Roll random polynomial roots x[j] 1683abb0f93cSkardel * (j = 1...n) for all j. While it may not be strictly 1684abb0f93cSkardel * necessary, Make sure each root has no factors in common with 1685abb0f93cSkardel * q. 1686abb0f93cSkardel */ 1687abb0f93cSkardel fprintf(stderr, 1688abb0f93cSkardel "Generating polynomial coefficients for %d roots (%d bits)\n", 168903cfe0ffSchristos n, BN_num_bits(q)); 1690abb0f93cSkardel for (j = 1; j <= n; j++) { 1691abb0f93cSkardel x[j] = BN_new(); 1692abb0f93cSkardel 1693abb0f93cSkardel while (1) { 169403cfe0ffSchristos BN_rand(x[j], BN_num_bits(q), 0, 0); 169503cfe0ffSchristos BN_mod(x[j], x[j], q, ctx); 169603cfe0ffSchristos BN_gcd(u, x[j], q, ctx); 1697abb0f93cSkardel if (BN_is_one(u)) 1698abb0f93cSkardel break; 1699abb0f93cSkardel } 1700abb0f93cSkardel } 1701abb0f93cSkardel 1702abb0f93cSkardel /* 1703abb0f93cSkardel * Generate polynomial coefficients a[i] (i = 0...n) from the 1704abb0f93cSkardel * expansion of root products (x - x[j]) mod q for all j. The 1705abb0f93cSkardel * method is a present from Charlie Boncelet. 1706abb0f93cSkardel */ 1707abb0f93cSkardel for (i = 0; i <= n; i++) { 1708abb0f93cSkardel a[i] = BN_new(); 1709abb0f93cSkardel BN_one(a[i]); 1710abb0f93cSkardel } 1711abb0f93cSkardel for (j = 1; j <= n; j++) { 1712abb0f93cSkardel BN_zero(w); 1713abb0f93cSkardel for (i = 0; i < j; i++) { 171403cfe0ffSchristos BN_copy(u, q); 171503cfe0ffSchristos BN_mod_mul(v, a[i], x[j], q, ctx); 1716abb0f93cSkardel BN_sub(u, u, v); 1717abb0f93cSkardel BN_add(u, u, w); 1718abb0f93cSkardel BN_copy(w, a[i]); 171903cfe0ffSchristos BN_mod(a[i], u, q, ctx); 1720abb0f93cSkardel } 1721abb0f93cSkardel } 1722abb0f93cSkardel 1723abb0f93cSkardel /* 172403cfe0ffSchristos * Generate gs[i] = g^a[i] mod p for all i and the generator g. 1725abb0f93cSkardel */ 1726abb0f93cSkardel for (i = 0; i <= n; i++) { 172703cfe0ffSchristos gs[i] = BN_new(); 172803cfe0ffSchristos BN_mod_exp(gs[i], g, a[i], p, ctx); 1729abb0f93cSkardel } 1730abb0f93cSkardel 1731abb0f93cSkardel /* 173203cfe0ffSchristos * Verify prod(gs[i]^(a[i] x[j]^i)) = 1 for all i, j. Note the 173303cfe0ffSchristos * a[i] x[j]^i exponent is computed mod q, but the gs[i] is 1734abb0f93cSkardel * computed mod p. also note the expression given in the paper 1735abb0f93cSkardel * is incorrect. 1736abb0f93cSkardel */ 1737abb0f93cSkardel temp = 1; 1738abb0f93cSkardel for (j = 1; j <= n; j++) { 1739abb0f93cSkardel BN_one(u); 1740abb0f93cSkardel for (i = 0; i <= n; i++) { 1741abb0f93cSkardel BN_set_word(v, i); 174203cfe0ffSchristos BN_mod_exp(v, x[j], v, q, ctx); 174303cfe0ffSchristos BN_mod_mul(v, v, a[i], q, ctx); 174403cfe0ffSchristos BN_mod_exp(v, g, v, p, ctx); 174503cfe0ffSchristos BN_mod_mul(u, u, v, p, ctx); 1746abb0f93cSkardel } 1747abb0f93cSkardel if (!BN_is_one(u)) 1748abb0f93cSkardel temp = 0; 1749abb0f93cSkardel } 1750abb0f93cSkardel fprintf(stderr, 175103cfe0ffSchristos "Confirm prod(gs[i]^(x[j]^i)) = 1 for all i, j: %s\n", temp ? 1752abb0f93cSkardel "yes" : "no"); 1753abb0f93cSkardel if (!temp) { 1754abb0f93cSkardel return (NULL); 1755abb0f93cSkardel } 1756abb0f93cSkardel 1757abb0f93cSkardel /* 1758abb0f93cSkardel * Make private encryption key A. Keep it around for awhile, 1759abb0f93cSkardel * since it is expensive to compute. 1760abb0f93cSkardel */ 1761abb0f93cSkardel biga = BN_new(); 1762abb0f93cSkardel 1763abb0f93cSkardel BN_one(biga); 1764abb0f93cSkardel for (j = 1; j <= n; j++) { 1765abb0f93cSkardel for (i = 0; i < n; i++) { 1766abb0f93cSkardel BN_set_word(v, i); 176703cfe0ffSchristos BN_mod_exp(v, x[j], v, q, ctx); 176803cfe0ffSchristos BN_mod_exp(v, gs[i], v, p, ctx); 176903cfe0ffSchristos BN_mod_mul(biga, biga, v, p, ctx); 1770abb0f93cSkardel } 1771abb0f93cSkardel } 1772abb0f93cSkardel 1773abb0f93cSkardel /* 1774abb0f93cSkardel * Roll private random group key b mod q (0 < b < q), where 1775abb0f93cSkardel * gcd(b, q) = 1 to guarantee b^-1 exists, then compute b^-1 1776abb0f93cSkardel * mod q. If b is changed, the client keys must be recomputed. 1777abb0f93cSkardel */ 1778abb0f93cSkardel while (1) { 177903cfe0ffSchristos BN_rand(b, BN_num_bits(q), 0, 0); 178003cfe0ffSchristos BN_mod(b, b, q, ctx); 178103cfe0ffSchristos BN_gcd(u, b, q, ctx); 1782abb0f93cSkardel if (BN_is_one(u)) 1783abb0f93cSkardel break; 1784abb0f93cSkardel } 178503cfe0ffSchristos BN_mod_inverse(b1, b, q, ctx); 1786abb0f93cSkardel 1787abb0f93cSkardel /* 1788abb0f93cSkardel * Make private client keys (xbar[j], xhat[j]) for all j. Note 1789abb0f93cSkardel * that the keys for the jth client do not s1[j] or the product 1790abb0f93cSkardel * s1[j]) (j = 1...n) which is q by construction. 1791abb0f93cSkardel * 1792abb0f93cSkardel * Compute the factor w such that w s1[j] = s1[j] for all j. The 1793abb0f93cSkardel * easy way to do this is to compute (q + s1[j]) / s1[j]. 1794abb0f93cSkardel * Exercise for the student: prove the remainder is always zero. 1795abb0f93cSkardel */ 1796abb0f93cSkardel for (j = 1; j <= n; j++) { 1797abb0f93cSkardel xbar[j] = BN_new(); xhat[j] = BN_new(); 1798abb0f93cSkardel 179903cfe0ffSchristos BN_add(w, q, s1[j]); 1800abb0f93cSkardel BN_div(w, u, w, s1[j], ctx); 1801abb0f93cSkardel BN_zero(xbar[j]); 1802abb0f93cSkardel BN_set_word(v, n); 1803abb0f93cSkardel for (i = 1; i <= n; i++) { 1804abb0f93cSkardel if (i == j) 1805abb0f93cSkardel continue; 18062950cc38Schristos 180703cfe0ffSchristos BN_mod_exp(u, x[i], v, q, ctx); 1808abb0f93cSkardel BN_add(xbar[j], xbar[j], u); 1809abb0f93cSkardel } 181003cfe0ffSchristos BN_mod_mul(xbar[j], xbar[j], b1, q, ctx); 181103cfe0ffSchristos BN_mod_exp(xhat[j], x[j], v, q, ctx); 181203cfe0ffSchristos BN_mod_mul(xhat[j], xhat[j], w, q, ctx); 1813abb0f93cSkardel } 1814abb0f93cSkardel 1815abb0f93cSkardel /* 1816abb0f93cSkardel * We revoke client j by dividing q by s1[j]. The quotient 1817abb0f93cSkardel * becomes the enabling key s. Note we always have to revoke 1818abb0f93cSkardel * one key; otherwise, the plaintext and cryptotext would be 1819abb0f93cSkardel * identical. For the present there are no provisions to revoke 1820abb0f93cSkardel * additional keys, so we sail on with only token revocations. 1821abb0f93cSkardel */ 1822abb0f93cSkardel s = BN_new(); 182303cfe0ffSchristos BN_copy(s, q); 1824abb0f93cSkardel BN_div(s, u, s, s1[n], ctx); 1825abb0f93cSkardel 1826abb0f93cSkardel /* 1827abb0f93cSkardel * For each combination of clients to be revoked, make private 1828abb0f93cSkardel * encryption key E = A^s and partial decryption keys gbar = g^s 1829abb0f93cSkardel * and ghat = g^(s b), all mod p. The servers use these keys to 1830abb0f93cSkardel * compute the session encryption key and partial decryption 1831abb0f93cSkardel * keys. These values must be regenerated if the enabling key is 1832abb0f93cSkardel * changed. 1833abb0f93cSkardel */ 1834abb0f93cSkardel bige = BN_new(); gbar = BN_new(); ghat = BN_new(); 183503cfe0ffSchristos BN_mod_exp(bige, biga, s, p, ctx); 183603cfe0ffSchristos BN_mod_exp(gbar, g, s, p, ctx); 183703cfe0ffSchristos BN_mod_mul(v, s, b, q, ctx); 183803cfe0ffSchristos BN_mod_exp(ghat, g, v, p, ctx); 1839abb0f93cSkardel 1840abb0f93cSkardel /* 1841abb0f93cSkardel * Notes: We produce the key media in three steps. The first 1842abb0f93cSkardel * step is to generate the system parameters p, q, g, b, A and 1843abb0f93cSkardel * the enabling keys s1[j]. Associated with each s1[j] are 1844abb0f93cSkardel * parameters xbar[j] and xhat[j]. All of these parameters are 1845abb0f93cSkardel * retained in a data structure protecteted by the trusted-agent 1846abb0f93cSkardel * password. The p, xbar[j] and xhat[j] paremeters are 1847abb0f93cSkardel * distributed to the j clients. When the client keys are to be 1848abb0f93cSkardel * activated, the enabled keys are multipied together to form 1849abb0f93cSkardel * the master enabling key s. This and the other parameters are 1850abb0f93cSkardel * used to compute the server encryption key E and the partial 1851abb0f93cSkardel * decryption keys gbar and ghat. 1852abb0f93cSkardel * 1853abb0f93cSkardel * In the identity exchange the client rolls random r and sends 1854abb0f93cSkardel * it to the server. The server rolls random k, which is used 1855abb0f93cSkardel * only once, then computes the session key E^k and partial 1856abb0f93cSkardel * decryption keys gbar^k and ghat^k. The server sends the 1857abb0f93cSkardel * encrypted r along with gbar^k and ghat^k to the client. The 1858abb0f93cSkardel * client completes the decryption and verifies it matches r. 1859abb0f93cSkardel */ 1860abb0f93cSkardel /* 1861abb0f93cSkardel * Write the MV trusted-agent parameters and keys as a DSA 1862abb0f93cSkardel * private key encoded in PEM. 1863abb0f93cSkardel * 1864abb0f93cSkardel * p modulus p 1865abb0f93cSkardel * q modulus q 1866abb0f93cSkardel * g generator g 1867abb0f93cSkardel * priv_key A mod p 1868abb0f93cSkardel * pub_key b mod q 1869abb0f93cSkardel * (remaining values are not used) 1870abb0f93cSkardel */ 1871abb0f93cSkardel i = 0; 1872abb0f93cSkardel str = fheader("MVta", "mvta", groupname); 1873abb0f93cSkardel fprintf(stderr, "Generating MV trusted-authority keys\n"); 187403cfe0ffSchristos BN_copy(priv_key, biga); 187503cfe0ffSchristos BN_copy(pub_key, b); 187603cfe0ffSchristos DSA_set0_key(dsa, pub_key, priv_key); 1877abb0f93cSkardel pkey = EVP_PKEY_new(); 1878abb0f93cSkardel EVP_PKEY_assign_DSA(pkey, dsa); 18792950cc38Schristos PEM_write_PKCS8PrivateKey(str, pkey, cipher, NULL, 0, NULL, 1880abb0f93cSkardel passwd1); 1881abb0f93cSkardel evpars[i++] = pkey; 1882abb0f93cSkardel if (debug) 1883abb0f93cSkardel DSA_print_fp(stderr, dsa, 0); 1884abb0f93cSkardel 1885abb0f93cSkardel /* 1886abb0f93cSkardel * Append the MV server parameters and keys as a DSA key encoded 1887abb0f93cSkardel * in PEM. 1888abb0f93cSkardel * 1889abb0f93cSkardel * p modulus p 1890abb0f93cSkardel * q modulus q (used only when generating k) 1891abb0f93cSkardel * g bige 1892abb0f93cSkardel * priv_key gbar 1893abb0f93cSkardel * pub_key ghat 1894abb0f93cSkardel * (remaining values are not used) 1895abb0f93cSkardel */ 1896abb0f93cSkardel fprintf(stderr, "Generating MV server keys\n"); 1897abb0f93cSkardel dsa2 = DSA_new(); 189803cfe0ffSchristos DSA_set0_pqg(dsa2, BN_dup(p), BN_dup(q), BN_dup(bige)); 189903cfe0ffSchristos DSA_set0_key(dsa2, BN_dup(ghat), BN_dup(gbar)); 1900abb0f93cSkardel pkey1 = EVP_PKEY_new(); 1901abb0f93cSkardel EVP_PKEY_assign_DSA(pkey1, dsa2); 19022950cc38Schristos PEM_write_PKCS8PrivateKey(str, pkey1, cipher, NULL, 0, NULL, 1903abb0f93cSkardel passwd1); 1904abb0f93cSkardel evpars[i++] = pkey1; 1905abb0f93cSkardel if (debug) 1906abb0f93cSkardel DSA_print_fp(stderr, dsa2, 0); 1907abb0f93cSkardel 1908abb0f93cSkardel /* 1909abb0f93cSkardel * Append the MV client parameters for each client j as DSA keys 1910abb0f93cSkardel * encoded in PEM. 1911abb0f93cSkardel * 1912abb0f93cSkardel * p modulus p 1913abb0f93cSkardel * priv_key xbar[j] mod q 1914abb0f93cSkardel * pub_key xhat[j] mod q 1915abb0f93cSkardel * (remaining values are not used) 1916abb0f93cSkardel */ 1917abb0f93cSkardel fprintf(stderr, "Generating %d MV client keys\n", n); 1918abb0f93cSkardel for (j = 1; j <= n; j++) { 1919abb0f93cSkardel sdsa = DSA_new(); 192003cfe0ffSchristos DSA_set0_pqg(sdsa, BN_dup(p), BN_dup(BN_value_one()), 192103cfe0ffSchristos BN_dup(BN_value_one())); 192203cfe0ffSchristos DSA_set0_key(sdsa, BN_dup(xhat[j]), BN_dup(xbar[j])); 1923abb0f93cSkardel pkey1 = EVP_PKEY_new(); 1924abb0f93cSkardel EVP_PKEY_set1_DSA(pkey1, sdsa); 19252950cc38Schristos PEM_write_PKCS8PrivateKey(str, pkey1, cipher, NULL, 0, 1926abb0f93cSkardel NULL, passwd1); 1927abb0f93cSkardel evpars[i++] = pkey1; 1928abb0f93cSkardel if (debug) 1929abb0f93cSkardel DSA_print_fp(stderr, sdsa, 0); 1930abb0f93cSkardel 1931abb0f93cSkardel /* 193203cfe0ffSchristos * The product (gbar^k)^xbar[j] (ghat^k)^xhat[j] and E 1933abb0f93cSkardel * are inverses of each other. We check that the product 1934abb0f93cSkardel * is one for each client except the ones that have been 1935abb0f93cSkardel * revoked. 1936abb0f93cSkardel */ 193703cfe0ffSchristos BN_mod_exp(v, gbar, xhat[j], p, ctx); 193803cfe0ffSchristos BN_mod_exp(u, ghat, xbar[j], p, ctx); 193903cfe0ffSchristos BN_mod_mul(u, u, v, p, ctx); 194003cfe0ffSchristos BN_mod_mul(u, u, bige, p, ctx); 1941abb0f93cSkardel if (!BN_is_one(u)) { 1942abb0f93cSkardel fprintf(stderr, "Revoke key %d\n", j); 1943abb0f93cSkardel continue; 1944abb0f93cSkardel } 1945abb0f93cSkardel } 1946abb0f93cSkardel evpars[i++] = NULL; 1947abb0f93cSkardel fclose(str); 1948abb0f93cSkardel 1949abb0f93cSkardel /* 1950abb0f93cSkardel * Free the countries. 1951abb0f93cSkardel */ 1952abb0f93cSkardel for (i = 0; i <= n; i++) { 195303cfe0ffSchristos BN_free(a[i]); BN_free(gs[i]); 1954abb0f93cSkardel } 1955abb0f93cSkardel for (j = 1; j <= n; j++) { 1956abb0f93cSkardel BN_free(x[j]); BN_free(xbar[j]); BN_free(xhat[j]); 1957abb0f93cSkardel BN_free(s1[j]); 1958abb0f93cSkardel } 1959abb0f93cSkardel return (pkey); 1960abb0f93cSkardel } 1961abb0f93cSkardel 1962abb0f93cSkardel 1963abb0f93cSkardel /* 1964abb0f93cSkardel * Generate X509v3 certificate. 1965abb0f93cSkardel * 1966abb0f93cSkardel * The certificate consists of the version number, serial number, 1967abb0f93cSkardel * validity interval, issuer name, subject name and public key. For a 1968abb0f93cSkardel * self-signed certificate, the issuer name is the same as the subject 1969abb0f93cSkardel * name and these items are signed using the subject private key. The 1970abb0f93cSkardel * validity interval extends from the current time to the same time one 1971abb0f93cSkardel * year hence. For NTP purposes, it is convenient to use the NTP seconds 1972abb0f93cSkardel * of the current time as the serial number. 1973abb0f93cSkardel */ 1974abb0f93cSkardel int 1975abb0f93cSkardel x509 ( 19762950cc38Schristos EVP_PKEY *pkey, /* signing key */ 19772950cc38Schristos const EVP_MD *md, /* signature/digest scheme */ 1978abb0f93cSkardel char *gqpub, /* identity extension (hex string) */ 1979e19314b7Schristos const char *exten, /* private cert extension */ 19802950cc38Schristos char *name /* subject/issuer name */ 1981abb0f93cSkardel ) 1982abb0f93cSkardel { 1983abb0f93cSkardel X509 *cert; /* X509 certificate */ 1984abb0f93cSkardel X509_NAME *subj; /* distinguished (common) name */ 1985abb0f93cSkardel X509_EXTENSION *ex; /* X509v3 extension */ 1986abb0f93cSkardel FILE *str; /* file handle */ 1987abb0f93cSkardel ASN1_INTEGER *serial; /* serial number */ 1988abb0f93cSkardel const char *id; /* digest/signature scheme name */ 1989abb0f93cSkardel char pathbuf[MAXFILENAME + 1]; 1990abb0f93cSkardel 1991abb0f93cSkardel /* 1992abb0f93cSkardel * Generate X509 self-signed certificate. 1993abb0f93cSkardel * 1994abb0f93cSkardel * Set the certificate serial to the NTP seconds for grins. Set 1995abb0f93cSkardel * the version to 3. Set the initial validity to the current 1996abb0f93cSkardel * time and the finalvalidity one year hence. 1997abb0f93cSkardel */ 199803cfe0ffSchristos id = OBJ_nid2sn(EVP_MD_pkey_type(md)); 1999abb0f93cSkardel fprintf(stderr, "Generating new certificate %s %s\n", name, id); 2000abb0f93cSkardel cert = X509_new(); 2001abb0f93cSkardel X509_set_version(cert, 2L); 2002abb0f93cSkardel serial = ASN1_INTEGER_new(); 2003abb0f93cSkardel ASN1_INTEGER_set(serial, (long)epoch + JAN_1970); 2004abb0f93cSkardel X509_set_serialNumber(cert, serial); 2005abb0f93cSkardel ASN1_INTEGER_free(serial); 2006ccc794f0Schristos X509_time_adj(X509_getm_notBefore(cert), 0L, &epoch); 2007ccc794f0Schristos X509_time_adj(X509_getm_notAfter(cert), lifetime * SECSPERDAY, &epoch); 2008abb0f93cSkardel subj = X509_get_subject_name(cert); 2009abb0f93cSkardel X509_NAME_add_entry_by_txt(subj, "commonName", MBSTRING_ASC, 20108b8da087Schristos (u_char *)name, -1, -1, 0); 2011abb0f93cSkardel subj = X509_get_issuer_name(cert); 2012abb0f93cSkardel X509_NAME_add_entry_by_txt(subj, "commonName", MBSTRING_ASC, 20138b8da087Schristos (u_char *)name, -1, -1, 0); 2014abb0f93cSkardel if (!X509_set_pubkey(cert, pkey)) { 20152950cc38Schristos fprintf(stderr, "Assign certificate signing key fails\n%s\n", 2016abb0f93cSkardel ERR_error_string(ERR_get_error(), NULL)); 2017abb0f93cSkardel X509_free(cert); 2018abb0f93cSkardel return (0); 2019abb0f93cSkardel } 2020abb0f93cSkardel 2021abb0f93cSkardel /* 2022abb0f93cSkardel * Add X509v3 extensions if present. These represent the minimum 2023abb0f93cSkardel * set defined in RFC3280 less the certificate_policy extension, 2024abb0f93cSkardel * which is seriously obfuscated in OpenSSL. 2025abb0f93cSkardel */ 2026abb0f93cSkardel /* 2027abb0f93cSkardel * The basic_constraints extension CA:TRUE allows servers to 2028abb0f93cSkardel * sign client certficitates. 2029abb0f93cSkardel */ 2030abb0f93cSkardel fprintf(stderr, "%s: %s\n", LN_basic_constraints, 2031abb0f93cSkardel BASIC_CONSTRAINTS); 2032abb0f93cSkardel ex = X509V3_EXT_conf_nid(NULL, NULL, NID_basic_constraints, 2033e19314b7Schristos _UC(BASIC_CONSTRAINTS)); 2034abb0f93cSkardel if (!X509_add_ext(cert, ex, -1)) { 2035abb0f93cSkardel fprintf(stderr, "Add extension field fails\n%s\n", 2036abb0f93cSkardel ERR_error_string(ERR_get_error(), NULL)); 2037abb0f93cSkardel return (0); 2038abb0f93cSkardel } 2039abb0f93cSkardel X509_EXTENSION_free(ex); 2040abb0f93cSkardel 2041abb0f93cSkardel /* 2042abb0f93cSkardel * The key_usage extension designates the purposes the key can 2043abb0f93cSkardel * be used for. 2044abb0f93cSkardel */ 2045abb0f93cSkardel fprintf(stderr, "%s: %s\n", LN_key_usage, KEY_USAGE); 2046e19314b7Schristos ex = X509V3_EXT_conf_nid(NULL, NULL, NID_key_usage, _UC(KEY_USAGE)); 2047abb0f93cSkardel if (!X509_add_ext(cert, ex, -1)) { 2048abb0f93cSkardel fprintf(stderr, "Add extension field fails\n%s\n", 2049abb0f93cSkardel ERR_error_string(ERR_get_error(), NULL)); 2050abb0f93cSkardel return (0); 2051abb0f93cSkardel } 2052abb0f93cSkardel X509_EXTENSION_free(ex); 2053abb0f93cSkardel /* 2054abb0f93cSkardel * The subject_key_identifier is used for the GQ public key. 2055abb0f93cSkardel * This should not be controversial. 2056abb0f93cSkardel */ 2057abb0f93cSkardel if (gqpub != NULL) { 2058abb0f93cSkardel fprintf(stderr, "%s\n", LN_subject_key_identifier); 2059abb0f93cSkardel ex = X509V3_EXT_conf_nid(NULL, NULL, 2060abb0f93cSkardel NID_subject_key_identifier, gqpub); 2061abb0f93cSkardel if (!X509_add_ext(cert, ex, -1)) { 2062abb0f93cSkardel fprintf(stderr, 2063abb0f93cSkardel "Add extension field fails\n%s\n", 2064abb0f93cSkardel ERR_error_string(ERR_get_error(), NULL)); 2065abb0f93cSkardel return (0); 2066abb0f93cSkardel } 2067abb0f93cSkardel X509_EXTENSION_free(ex); 2068abb0f93cSkardel } 2069abb0f93cSkardel 2070abb0f93cSkardel /* 2071abb0f93cSkardel * The extended key usage extension is used for special purpose 2072abb0f93cSkardel * here. The semantics probably do not conform to the designer's 2073abb0f93cSkardel * intent and will likely change in future. 2074abb0f93cSkardel * 2075abb0f93cSkardel * "trustRoot" designates a root authority 2076abb0f93cSkardel * "private" designates a private certificate 2077abb0f93cSkardel */ 2078abb0f93cSkardel if (exten != NULL) { 2079abb0f93cSkardel fprintf(stderr, "%s: %s\n", LN_ext_key_usage, exten); 2080abb0f93cSkardel ex = X509V3_EXT_conf_nid(NULL, NULL, 2081e19314b7Schristos NID_ext_key_usage, _UC(exten)); 2082abb0f93cSkardel if (!X509_add_ext(cert, ex, -1)) { 2083abb0f93cSkardel fprintf(stderr, 2084abb0f93cSkardel "Add extension field fails\n%s\n", 2085abb0f93cSkardel ERR_error_string(ERR_get_error(), NULL)); 2086abb0f93cSkardel return (0); 2087abb0f93cSkardel } 2088abb0f93cSkardel X509_EXTENSION_free(ex); 2089abb0f93cSkardel } 2090abb0f93cSkardel 2091abb0f93cSkardel /* 2092abb0f93cSkardel * Sign and verify. 2093abb0f93cSkardel */ 2094abb0f93cSkardel X509_sign(cert, pkey, md); 20953123f114Skardel if (X509_verify(cert, pkey) <= 0) { 2096abb0f93cSkardel fprintf(stderr, "Verify %s certificate fails\n%s\n", id, 2097abb0f93cSkardel ERR_error_string(ERR_get_error(), NULL)); 2098abb0f93cSkardel X509_free(cert); 2099abb0f93cSkardel return (0); 2100abb0f93cSkardel } 2101abb0f93cSkardel 2102abb0f93cSkardel /* 2103abb0f93cSkardel * Write the certificate encoded in PEM. 2104abb0f93cSkardel */ 21052950cc38Schristos snprintf(pathbuf, sizeof(pathbuf), "%scert", id); 2106abb0f93cSkardel str = fheader(pathbuf, "cert", hostname); 2107abb0f93cSkardel PEM_write_X509(str, cert); 2108abb0f93cSkardel fclose(str); 2109abb0f93cSkardel if (debug) 2110abb0f93cSkardel X509_print_fp(stderr, cert); 2111abb0f93cSkardel X509_free(cert); 2112abb0f93cSkardel return (1); 2113abb0f93cSkardel } 2114abb0f93cSkardel 2115abb0f93cSkardel #if 0 /* asn2ntp is used only with commercial certificates */ 2116abb0f93cSkardel /* 2117abb0f93cSkardel * asn2ntp - convert ASN1_TIME time structure to NTP time 2118abb0f93cSkardel */ 2119abb0f93cSkardel u_long 2120abb0f93cSkardel asn2ntp ( 2121abb0f93cSkardel ASN1_TIME *asn1time /* pointer to ASN1_TIME structure */ 2122abb0f93cSkardel ) 2123abb0f93cSkardel { 2124abb0f93cSkardel char *v; /* pointer to ASN1_TIME string */ 2125abb0f93cSkardel struct tm tm; /* time decode structure time */ 2126abb0f93cSkardel 2127abb0f93cSkardel /* 2128abb0f93cSkardel * Extract time string YYMMDDHHMMSSZ from ASN.1 time structure. 2129abb0f93cSkardel * Note that the YY, MM, DD fields start with one, the HH, MM, 2130abb0f93cSkardel * SS fiels start with zero and the Z character should be 'Z' 2131abb0f93cSkardel * for UTC. Also note that years less than 50 map to years 2132abb0f93cSkardel * greater than 100. Dontcha love ASN.1? 2133abb0f93cSkardel */ 2134abb0f93cSkardel if (asn1time->length > 13) 2135abb0f93cSkardel return (-1); 2136abb0f93cSkardel v = (char *)asn1time->data; 2137abb0f93cSkardel tm.tm_year = (v[0] - '0') * 10 + v[1] - '0'; 2138abb0f93cSkardel if (tm.tm_year < 50) 2139abb0f93cSkardel tm.tm_year += 100; 2140abb0f93cSkardel tm.tm_mon = (v[2] - '0') * 10 + v[3] - '0' - 1; 2141abb0f93cSkardel tm.tm_mday = (v[4] - '0') * 10 + v[5] - '0'; 2142abb0f93cSkardel tm.tm_hour = (v[6] - '0') * 10 + v[7] - '0'; 2143abb0f93cSkardel tm.tm_min = (v[8] - '0') * 10 + v[9] - '0'; 2144abb0f93cSkardel tm.tm_sec = (v[10] - '0') * 10 + v[11] - '0'; 2145abb0f93cSkardel tm.tm_wday = 0; 2146abb0f93cSkardel tm.tm_yday = 0; 2147abb0f93cSkardel tm.tm_isdst = 0; 2148abb0f93cSkardel return (mktime(&tm) + JAN_1970); 2149abb0f93cSkardel } 2150abb0f93cSkardel #endif 2151abb0f93cSkardel 2152abb0f93cSkardel /* 2153abb0f93cSkardel * Callback routine 2154abb0f93cSkardel */ 2155abb0f93cSkardel void 2156abb0f93cSkardel cb ( 2157abb0f93cSkardel int n1, /* arg 1 */ 2158abb0f93cSkardel int n2, /* arg 2 */ 2159abb0f93cSkardel void *chr /* arg 3 */ 2160abb0f93cSkardel ) 2161abb0f93cSkardel { 2162abb0f93cSkardel switch (n1) { 2163abb0f93cSkardel case 0: 2164abb0f93cSkardel d0++; 2165abb0f93cSkardel fprintf(stderr, "%s %d %d %lu\r", (char *)chr, n1, n2, 2166abb0f93cSkardel d0); 2167abb0f93cSkardel break; 2168abb0f93cSkardel case 1: 2169abb0f93cSkardel d1++; 2170abb0f93cSkardel fprintf(stderr, "%s\t\t%d %d %lu\r", (char *)chr, n1, 2171abb0f93cSkardel n2, d1); 2172abb0f93cSkardel break; 2173abb0f93cSkardel case 2: 2174abb0f93cSkardel d2++; 2175abb0f93cSkardel fprintf(stderr, "%s\t\t\t\t%d %d %lu\r", (char *)chr, 2176abb0f93cSkardel n1, n2, d2); 2177abb0f93cSkardel break; 2178abb0f93cSkardel case 3: 2179abb0f93cSkardel d3++; 2180abb0f93cSkardel fprintf(stderr, "%s\t\t\t\t\t\t%d %d %lu\r", 2181abb0f93cSkardel (char *)chr, n1, n2, d3); 2182abb0f93cSkardel break; 2183abb0f93cSkardel } 2184abb0f93cSkardel } 2185abb0f93cSkardel 2186abb0f93cSkardel 2187abb0f93cSkardel /* 2188abb0f93cSkardel * Generate key 2189abb0f93cSkardel */ 2190abb0f93cSkardel EVP_PKEY * /* public/private key pair */ 2191abb0f93cSkardel genkey( 2192e19314b7Schristos const char *type, /* key type (RSA or DSA) */ 2193e19314b7Schristos const char *id /* file name id */ 2194abb0f93cSkardel ) 2195abb0f93cSkardel { 2196abb0f93cSkardel if (type == NULL) 2197abb0f93cSkardel return (NULL); 2198abb0f93cSkardel if (strcmp(type, "RSA") == 0) 2199abb0f93cSkardel return (gen_rsa(id)); 2200abb0f93cSkardel 2201abb0f93cSkardel else if (strcmp(type, "DSA") == 0) 2202abb0f93cSkardel return (gen_dsa(id)); 2203abb0f93cSkardel 2204abb0f93cSkardel fprintf(stderr, "Invalid %s key type %s\n", id, type); 2205abb0f93cSkardel return (NULL); 2206abb0f93cSkardel } 220703cfe0ffSchristos 220803cfe0ffSchristos static RSA* 220903cfe0ffSchristos genRsaKeyPair( 221003cfe0ffSchristos int bits, 221103cfe0ffSchristos char * what 221203cfe0ffSchristos ) 221303cfe0ffSchristos { 221403cfe0ffSchristos RSA * rsa = RSA_new(); 221503cfe0ffSchristos BN_GENCB * gcb = BN_GENCB_new(); 221603cfe0ffSchristos BIGNUM * bne = BN_new(); 221703cfe0ffSchristos 221803cfe0ffSchristos if (gcb) 221903cfe0ffSchristos BN_GENCB_set_old(gcb, cb, what); 222003cfe0ffSchristos if (bne) 222103cfe0ffSchristos BN_set_word(bne, 65537); 222203cfe0ffSchristos if (!(rsa && gcb && bne && RSA_generate_key_ex( 222303cfe0ffSchristos rsa, bits, bne, gcb))) 222403cfe0ffSchristos { 222503cfe0ffSchristos RSA_free(rsa); 222603cfe0ffSchristos rsa = NULL; 222703cfe0ffSchristos } 222803cfe0ffSchristos BN_GENCB_free(gcb); 222903cfe0ffSchristos BN_free(bne); 223003cfe0ffSchristos return rsa; 223103cfe0ffSchristos } 223203cfe0ffSchristos 223303cfe0ffSchristos static DSA* 223403cfe0ffSchristos genDsaParams( 223503cfe0ffSchristos int bits, 223603cfe0ffSchristos char * what 223703cfe0ffSchristos ) 223803cfe0ffSchristos { 223903cfe0ffSchristos 224003cfe0ffSchristos DSA * dsa = DSA_new(); 224103cfe0ffSchristos BN_GENCB * gcb = BN_GENCB_new(); 224203cfe0ffSchristos u_char seed[20]; 224303cfe0ffSchristos 224403cfe0ffSchristos if (gcb) 224503cfe0ffSchristos BN_GENCB_set_old(gcb, cb, what); 224603cfe0ffSchristos RAND_bytes(seed, sizeof(seed)); 224703cfe0ffSchristos if (!(dsa && gcb && DSA_generate_parameters_ex( 224803cfe0ffSchristos dsa, bits, seed, sizeof(seed), NULL, NULL, gcb))) 224903cfe0ffSchristos { 225003cfe0ffSchristos DSA_free(dsa); 225103cfe0ffSchristos dsa = NULL; 225203cfe0ffSchristos } 225303cfe0ffSchristos BN_GENCB_free(gcb); 225403cfe0ffSchristos return dsa; 225503cfe0ffSchristos } 225603cfe0ffSchristos 22572950cc38Schristos #endif /* AUTOKEY */ 2258abb0f93cSkardel 2259abb0f93cSkardel 2260abb0f93cSkardel /* 2261abb0f93cSkardel * Generate file header and link 2262abb0f93cSkardel */ 2263abb0f93cSkardel FILE * 2264abb0f93cSkardel fheader ( 2265abb0f93cSkardel const char *file, /* file name id */ 2266abb0f93cSkardel const char *ulink, /* linkname */ 2267abb0f93cSkardel const char *owner /* owner name */ 2268abb0f93cSkardel ) 2269abb0f93cSkardel { 2270abb0f93cSkardel FILE *str; /* file handle */ 2271abb0f93cSkardel char linkname[MAXFILENAME]; /* link name */ 2272abb0f93cSkardel int temp; 22737476e6e4Schristos #ifdef HAVE_UMASK 22747476e6e4Schristos mode_t orig_umask; 22757476e6e4Schristos #endif 2276abb0f93cSkardel 22772950cc38Schristos snprintf(filename, sizeof(filename), "ntpkey_%s_%s.%u", file, 22782950cc38Schristos owner, fstamp); 22797476e6e4Schristos #ifdef HAVE_UMASK 22807476e6e4Schristos orig_umask = umask( S_IWGRP | S_IRWXO ); 22817476e6e4Schristos str = fopen(filename, "w"); 22827476e6e4Schristos (void) umask(orig_umask); 22837476e6e4Schristos #else 22847476e6e4Schristos str = fopen(filename, "w"); 22857476e6e4Schristos #endif 22867476e6e4Schristos if (str == NULL) { 2287abb0f93cSkardel perror("Write"); 2288abb0f93cSkardel exit (-1); 2289abb0f93cSkardel } 22907476e6e4Schristos if (strcmp(ulink, "md5") == 0) { 22917476e6e4Schristos strcpy(linkname,"ntp.keys"); 22927476e6e4Schristos } else { 22932950cc38Schristos snprintf(linkname, sizeof(linkname), "ntpkey_%s_%s", ulink, 22942950cc38Schristos hostname); 22957476e6e4Schristos } 22962950cc38Schristos (void)remove(linkname); /* The symlink() line below matters */ 2297abb0f93cSkardel temp = symlink(filename, linkname); 2298abb0f93cSkardel if (temp < 0) 2299abb0f93cSkardel perror(file); 2300abb0f93cSkardel fprintf(stderr, "Generating new %s file and link\n", ulink); 2301abb0f93cSkardel fprintf(stderr, "%s->%s\n", linkname, filename); 2302abb0f93cSkardel fprintf(str, "# %s\n# %s\n", filename, ctime(&epoch)); 2303abb0f93cSkardel return (str); 2304abb0f93cSkardel } 2305