FreeBSD/contrib/ntp/libntp/ssl_init.c
/*
 * keytype_from_text	returns OpenSSL NID for digest by name, and
 *			optionally the associated digest length.
 *
 * Used by ntpd authreadkeys(), ntpq and ntpdc keytype()
 */
keytype_from_text    returns OpenSSL NID for digest by name, and
optionally the associated digest length.

Used by ntpd authreadkeys(), ntpq and ntpdc keytype()
keytype_from_text returns OpenSSL NID for digest by name and optionally the associated digest length . Used by ntpd authreadkeys () ntpq and ntpdc keytype ()
int
keytype_from_text(
	const char *text,
	size_t *pdigest_len
	)
{
	int		key_type;
	u_int		digest_len;
#ifdef OPENSSL
	const u_long	max_digest_len = MAX_MAC_LEN - sizeof(keyid_t);
	u_char		digest[EVP_MAX_MD_SIZE];
	char *		upcased;
	char *		pch;
	INIT_SSL();
	LIB_GETBUF(upcased);
	strlcpy(upcased, text, LIB_BUFLENGTH);
	for (pch = upcased; '\0' != *pch; pch++)
		*pch = (char)toupper((unsigned char)*pch);
	key_type = OBJ_sn2nid(upcased);
#else
	key_type = 0;
#endif
	if (!key_type && 'm' == tolower((unsigned char)text[0]))
		key_type = NID_md5;
	if (!key_type)
		return 0;
	if (NULL != pdigest_len) {
#ifdef OPENSSL
		EVP_MD_CTX	*ctx;
		ctx = EVP_MD_CTX_new();
		EVP_DigestInit(ctx, EVP_get_digestbynid(key_type));
		EVP_DigestFinal(ctx, digest, &digest_len);
		EVP_MD_CTX_free(ctx);
		if (digest_len > max_digest_len) {
			fprintf(stderr,
				"key type %s %u octet digests are too big, max %lu\n",
				keytype_name(key_type), digest_len,
				max_digest_len);
			msyslog(LOG_ERR,
				"key type %s %u octet digests are too big, max %lu",
				keytype_name(key_type), digest_len,
				max_digest_len);
			return 0;
		}
#else
		digest_len = 16;
#endif
		*pdigest_len = digest_len;
	}
	return key_type;
}
int keytype_from_text ( const char * text , size_t * pdigest_len ) { int key_type ; u_int digest_len ; #ifdef OPENSSL const u_long max_digest_len = MAX_MAC_LEN - sizeof ( keyid_t ) ; u_char digest [ EVP_MAX_MD_SIZE ] ; char * upcased ; char * pch ; INIT_SSL ( ) ; LIB_GETBUF ( upcased ) ; strlcpy ( upcased , text , LIB_BUFLENGTH ) ; for ( pch = upcased ; ' \0 ' != * pch ; pch ++ ) * pch = ( char ) toupper ( ( unsigned char ) * pch ) ; key_type = OBJ_sn2nid ( upcased ) ; #else key_type = 0 ; #endif if ( ! key_type && ' ' == tolower ( ( unsigned char ) text [ 0 ] ) ) key_type = NID_md5 ; if ( ! key_type ) return 0 ; if ( NULL != pdigest_len ) { #ifdef OPENSSL EVP_MD_CTX * ctx ; ctx = EVP_MD_CTX_new ( ) ; EVP_DigestInit ( ctx , EVP_get_digestbynid ( key_type ) ) ; EVP_DigestFinal ( ctx , digest , & digest_len ) ; EVP_MD_CTX_free ( ctx ) ; if ( digest_len > max_digest_len ) { fprintf ( stderr , " \n " , keytype_name ( key_type ) , digest_len , max_digest_len ) ; msyslog ( LOG_ERR , " " , keytype_name ( key_type ) , digest_len , max_digest_len ) ; return 0 ; } #else digest_len = 16 ; #endif * pdigest_len = digest_len ; } return key_type ; }
keytype_from_text    returns OpenSSL NID for digest by name, and
                      optionally the associated digest length.
keytype_from_text returns OpenSSL NID for digest by name and optionally the associated digest length .
/*
	 * OpenSSL digest short names are capitalized, so uppercase the
	 * digest name before passing to OBJ_sn2nid().  If it is not
	 * recognized but begins with 'M' use NID_md5 to be consistent
	 * with past behavior.
	 */