FreeBSD/contrib/ntp/ntpd/ntp_crypto.c
/*
 * crypto_key - load cryptographic parameters and keys
 *
 * This routine searches the key cache for matching name in the form
 * ntpkey_<key>_<name>, where <key> is one of host, sign, iff, gq, mv,
 * and <name> is the host/group name. If not found, it tries to load a
 * PEM-encoded file of the same name and extracts the filestamp from
 * the first line of the file name. It returns the key pointer if valid,
 * NULL if not.
 */
load cryptographic parameters and keys
This routine searches the key cache for matching name in the form
ntpkey__, where  is one of host, sign, iff, gq, mv,
and  is the host/group name. If not found, it tries to load a
PEM-encoded file of the same name and extracts the filestamp from
the first line of the file name. It returns the key pointer if valid,
NULL if not.
load cryptographic parameters and keys This routine searches the key cache for matching name in the form ntpkey__ where is one of host sign iff gq mv and is the host / group name . If not found it tries to load a PEM - encoded file of the same name and extracts the filestamp from the first line of the file name . It returns the key pointer if valid NULL if not .
static struct pkey_info *
crypto_key(
	char	*cp,		
	char	*passwd1,	
	sockaddr_u *addr 	
	)
{
	FILE	*str;		
	struct pkey_info *pkp;	
	EVP_PKEY *pkey = NULL;	
	tstamp_t fstamp;
	char	filename[MAXFILENAME]; 
	char	linkname[MAXFILENAME]; 
	char	statstr[NTP_MAXSTRLEN]; 
	char	*ptr;
	for (pkp = pkinfo; pkp != NULL; pkp = pkp->link) {
		if (strcmp(cp, pkp->name) == 0)
			return (pkp);
	}  
	if (*cp == '/')
		strlcpy(filename, cp, sizeof(filename));
	else
		snprintf(filename, sizeof(filename), "%s/%s", keysdir,
		    cp);
	str = fopen(filename, "r");
	if (str == NULL)
		return (NULL);
	if ((ptr = fgets(linkname, sizeof(linkname), str)) == NULL) {
		msyslog(LOG_ERR, "crypto_key: empty file %s",
		    filename);
		fclose(str);
		return (NULL);
	}
	if ((ptr = strrchr(ptr, '.')) == NULL) {
		msyslog(LOG_ERR, "crypto_key: no filestamp %s",
		    filename);
		fclose(str);
		return (NULL);
	}
	if (sscanf(++ptr, "%u", &fstamp) != 1) {
		msyslog(LOG_ERR, "crypto_key: invalid filestamp %s",
		    filename);
		fclose(str);
		return (NULL);
	}
	pkey = PEM_read_PrivateKey(str, NULL, NULL, passwd1);
	fclose(str);
	if (pkey == NULL) {
		msyslog(LOG_ERR, "crypto_key: %s",
		    ERR_error_string(ERR_get_error(), NULL));
		exit (-1);
	}
	pkp = emalloc(sizeof(struct pkey_info));
	pkp->link = pkinfo;
	pkinfo = pkp;
	pkp->pkey = pkey;
	pkp->name = estrdup(cp);
	pkp->fstamp = fstamp;
	if ((ptr = strrchr(linkname, '\n')) != NULL)
		*ptr = '\0'; 
	snprintf(statstr, sizeof(statstr), "%s mod %d", &linkname[2],
	    EVP_PKEY_size(pkey) * 8);
	record_crypto_stats(addr, statstr);
	DPRINTF(1, ("crypto_key: %s\n", statstr));
#ifdef DEBUG
	if (debug > 1) {
		if (EVP_PKEY_base_id(pkey) == EVP_PKEY_DSA)
			DSA_print_fp(stdout, EVP_PKEY_get0_DSA(pkey), 0);
		else if (EVP_PKEY_base_id(pkey) == EVP_PKEY_RSA)
			RSA_print_fp(stdout, EVP_PKEY_get0_RSA(pkey), 0);
	}
#endif
	return (pkp);
}
static struct pkey_info * crypto_key ( char * cp , char * passwd1 , sockaddr_u * addr ) { FILE * str ; struct pkey_info * pkp ; EVP_PKEY * pkey = NULL ; tstamp_t fstamp ; char filename [ MAXFILENAME ] ; char linkname [ MAXFILENAME ] ; char statstr [ NTP_MAXSTRLEN ] ; char * ptr ; for ( pkp = pkinfo ; pkp != NULL ; pkp = pkp -> link ) { if ( strcmp ( cp , pkp -> name ) == 0 ) return ( pkp ) ; } if ( * cp == ' ' ) strlcpy ( filename , cp , sizeof ( filename ) ) ; else snprintf ( filename , sizeof ( filename ) , " " , keysdir , cp ) ; str = fopen ( filename , " " ) ; if ( str == NULL ) return ( NULL ) ; if ( ( ptr = fgets ( linkname , sizeof ( linkname ) , str ) ) == NULL ) { msyslog ( LOG_ERR , " " , filename ) ; fclose ( str ) ; return ( NULL ) ; } if ( ( ptr = strrchr ( ptr , ' ' ) ) == NULL ) { msyslog ( LOG_ERR , " " , filename ) ; fclose ( str ) ; return ( NULL ) ; } if ( sscanf ( ++ ptr , " " , & fstamp ) != 1 ) { msyslog ( LOG_ERR , " " , filename ) ; fclose ( str ) ; return ( NULL ) ; } pkey = PEM_read_PrivateKey ( str , NULL , NULL , passwd1 ) ; fclose ( str ) ; if ( pkey == NULL ) { msyslog ( LOG_ERR , " " , ERR_error_string ( ERR_get_error ( ) , NULL ) ) ; exit ( -1 ) ; } pkp = emalloc ( sizeof ( struct pkey_info ) ) ; pkp -> link = pkinfo ; pkinfo = pkp ; pkp -> pkey = pkey ; pkp -> name = estrdup ( cp ) ; pkp -> fstamp = fstamp ; if ( ( ptr = strrchr ( linkname , ' \n ' ) ) != NULL ) * ptr = ' \0 ' ; snprintf ( statstr , sizeof ( statstr ) , " " , & linkname [ 2 ] , EVP_PKEY_size ( pkey ) * 8 ) ; record_crypto_stats ( addr , statstr ) ; DPRINTF ( 1 , ( " \n " , statstr ) ) ; #ifdef DEBUG if ( debug > 1 ) { if ( EVP_PKEY_base_id ( pkey ) == EVP_PKEY_DSA ) DSA_print_fp ( stdout , EVP_PKEY_get0_DSA ( pkey ) , 0 ) ; else if ( EVP_PKEY_base_id ( pkey ) == EVP_PKEY_RSA ) RSA_print_fp ( stdout , EVP_PKEY_get0_RSA ( pkey ) , 0 ) ; } #endif return ( pkp ) ; }
crypto_key - load cryptographic parameters and keys
 This routine searches the key cache for matching name in the form
 ntpkey_<key>_<name>, where <key> is one of host, sign, iff, gq, mv,
 and <name> is the host/group name.
crypto_key - load cryptographic parameters and keys This routine searches the key cache for matching name in the form ntpkey_<key > _<name > where <key > is one of host sign iff gq mv and <name > is the host / group name .
/* file name */ /* password */ /* IP address */ /* file handle */ /* generic key */ /* public/private key */ /* name of key file */ /* filestamp buffer) */ /* statistics for filegen */ /*
	 * Search the key cache for matching key and name.
	 */ /*
	 * Open the key file. If the first character of the file name is
	 * not '/', prepend the keys directory string. If something goes
	 * wrong, abandon ship.
	 */ /*
	 * Read the filestamp, which is contained in the first line.
	 */ /*
	 * Read and decrypt PEM-encoded private key. If it fails to
	 * decrypt, game over.
	 */ /*
	 * Make a new entry in the key cache.
	 */ /*
	 * Leave tracks in the cryptostats.
	 */