From: Brian Inglis Date: Mon, 20 Feb 2017 05:21:29 +0000 (+0000) Subject: add AES-128 CMAC digest support as key type AES128CMAC length *MUST BE* 16 bytes X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d51377654cea778b7e205acb4fd0c1f5a970fc72;p=thirdparty%2Fntp.git add AES-128 CMAC digest support as key type AES128CMAC length *MUST BE* 16 bytes bk: 58aa7cd9a-QoqXcJfrwAi1FQiIW7iA --- diff --git a/html/authentic.html b/html/authentic.html index e529a6d18..06bb67bc7 100644 --- a/html/authentic.html +++ b/html/authentic.html @@ -46,14 +46,40 @@ required.

By default, the client sends non-authenticated packets and the server responds with non-authenticated packets. If the client sends authenticated packets, the server responds with authenticated packets if correct, or a crypto-NAK packet if not. In the case of unsolicited packets which might consume significant resources, such as broadcast or symmetric mode packets, authentication is required, unless overridden by a disable auth command. In the current climate of targeted broadcast or "letterbomb" attacks, defeating this requirement would be decidedly dangerous. In any case, the notrust flag, described on the Access Control Options page, can be used to disable access to all but correctly authenticated clients.

Symmetric Key Cryptography

The original NTPv3 specification (RFC-1305), as well as the current NTPv4 specification (RFC-5905), allows any one of possibly 65,534 message digest keys (excluding zero), each distinguished by a 32-bit key ID, to authenticate an association. The servers and clients involved must agree on the key ID, key type and key to authenticate NTP packets.

-

The message digest is a cryptographic hash computed by an algorithm such as MD5 or SHA. When authentication is specified, a message authentication code (MAC) is appended to the NTP packet header. The MAC consists of a 32-bit key identifier (key ID) followed by a 128- or 160-bit message digest. The algorithm computes the digest as the hash of a 128- or 160- bit message digest key concatenated with the NTP packet header fields with the exception of the MAC. On transmit, the message digest is computed and inserted in the MAC. On receive, the message digest is computed and compared with the MAC. The packet is accepted only if the two MACs are identical. If a discrepancy is found by the client, the client ignores the packet, but raises an alarm. If this happens at the server, the server returns a special message called a crypto-NAK. Since the crypto-NAK is protected by the loopback test, an intruder cannot disrupt the protocol by sending a bogus crypto-NAK.

+

The message digest is a cryptographic hash computed by an algorithm such as MD5, SHA, or AES-128 CMAC. When authentication is specified, a message authentication code (MAC) is appended to the NTP packet header. The MAC consists of a 32-bit key identifier (key ID) followed by a 128- or 160-bit message digest. The algorithm computes the digest as the hash of a 128- or 160- bit message digest key concatenated with the NTP packet header fields with the exception of the MAC. On transmit, the message digest is computed and inserted in the MAC. On receive, the message digest is computed and compared with the MAC. The packet is accepted only if the two MACs are identical. If a discrepancy is found by the client, the client ignores the packet, but raises an alarm. If this happens at the server, the server returns a special message called a crypto-NAK. Since the crypto-NAK is protected by the loopback test, an intruder cannot disrupt the protocol by sending a bogus crypto-NAK.

Keys and related information are specified in a keys file, which must be distributed and stored using secure means beyond the scope of the NTP protocol itself. Besides the keys used for ordinary NTP associations, additional keys can be used as passwords for the ntpq and ntpdc utility programs. Ordinarily, the ntp.keys file is generated by the ntp-keygen program, but it can be constructed and edited using an ordinary text editor.

Each line of the keys file consists of three or four fields: a key ID in the range 1 to 65,534, inclusive, a key type, a message digest key consisting of a printable ASCII string less than 40 characters or a 40-character hex digit string, and an optional comma-separated list of IPs that are allowed to serve time. If the OpenSSL library is installed, the key type can be any message digest algorithm supported by the library. If the OpenSSL library is not installed, the only permitted key type is MD5.

-
-

gif

-

Figure 1. Typical Symmetric Key File

-
-

Figure 1 shows a typical keys file used by the reference implementation when the OpenSSL library is installed. In this figure, for key IDs in he range 1-10, the key is interpreted as a printable ASCII string. For key IDs in the range 11-20, the key is a 40-character hex digit string. The key is truncated or zero-filled internally to either 128 or 160 bits, depending on the key type. The line can be edited later or new lines can be added to change any field. The key can be change to a password, such as 2late4Me for key ID 10. Note that two or more keys files can be combined in any order as long as the key IDs are distinct.

+ + +
+ Figure 1. Typical Symmetric Key File +
+
+# ntpkey_MD5key_bk.ntp.org.3595864945
+# Thu Dec 12 19:22:25 2013
+
+1  MD5 L";Nw<`.I<f4U0)247"i  # MD5 key
+2  MD5 &>l0%XXK9O'51VwV<xq~  # MD5 key
+3  MD5 lb4zLW~d^!K:]RsD'qb6  # MD5 key
+4  MD5 Yue:tL[+vR)M`n~bY,'?  # MD5 key
+5  MD5 B;fxlKgr/&4ZTbL6=RxA  # MD5 key
+6  MD5 4eYwa`o}3i@@V@..R9!l  # MD5 key
+7  MD5 `A.([h+;wTQ|xfi%Sn_!  # MD5 key
+8  MD5 45:V,r4]l6y^JH6"Sh?F  # MD5 key
+9  MD5 3-5vcn*6l29DS?Xdsg)*  # MD5 key
+10 MD5 2late4Me              # MD5 key
+11 SHA1 a27872d3030a9025b8446c751b4551a7629af65c  # SHA1 key
+12 SHA1 21bc3b4865dbb9e920902abdccb3e04ff97a5e74  # SHA1 key
+13 SHA1 2b7736fe24fef5ba85ae11594132ab5d6f6daba9  # SHA1 key
+14 SHA  a5332809c8878dd3a5b918819108a111509aeceb  # SHA  key
+15 MD2  2fe16c88c760ff2f16d4267e36c1aa6c926e6964  # MD2  key
+16 MD4  b2691811dc19cfc0e2f9bcacd74213f29812183d  # MD4  key
+17 MD5  e4d6735b8bdad58ec5ffcb087300a17f7fef1f7c  # MD5  key
+18 MDC2 a8d5e2315c025bf3a79174c87fbd10477de2eabc  # MDC2 key
+19 RIPEMD160 77ca332cafb30e3cafb174dcd5b80ded7ba9b3d2  # RIPEMD160 key
+20 AES128CMAC f92ff73eee86c1e7dc638d6489a04e4e555af878  # AES128CMAC key
+  
+

Figure 1 shows a typical keys file used by the reference implementation when the OpenSSL library is installed. In this figure, for key IDs in he range 1-10, the key is interpreted as a printable ASCII string. For key IDs in the range 11-20, the key is a 40-character hex digit string. The key is truncated or zero-filled internally to either 128 or 160 bits, depending on the key type. The line can be edited later or new lines can be added to change any field. The key can be changed to a password, such as 2late4Me for key ID 10. Note that two or more keys files can be combined in any order as long as the key IDs are distinct.

When ntpd is started, it reads the keys file specified by the keys command and installs the keys in the key cache. However, individual keys must be activated with the trustedkey configuration command before use. This allows, for instance, the installation of possibly several batches of keys and then activating a key remotely using ntpq or ntpdc. The requestkey command selects the key ID used as the password for the ntpdc utility, while the controlkey command selects the key ID used as the password for the ntpq utility.

Microsoft Windows Authentication

In addition to the above means, ntpd now supports Microsoft Windows MS-SNTP authentication using Active Directory services. This support was contributed by the Samba Team and is still in development. It is enabled using the mssntp flag of the restrict command described on the Access Control Options page. Note: Potential users should be aware that these services involve a TCP connection to another process that could potentially block, denying services to other users. Therefore, this flag should be used only for a dedicated server with no clients other than MS-SNTP.

diff --git a/html/keygen.html b/html/keygen.html index 191b7147b..bad97fb94 100644 --- a/html/keygen.html +++ b/html/keygen.html @@ -1,42 +1,42 @@ - - - -ntp-keygen - generate public and private keys - - - -

ntp-keygen - generate public and private keys

-

giffrom Alice's Adventures in Wonderland, Lewis Carroll

-

Alice holds the key.

-

Last update: - 10-Mar-2014 05:11 - UTC

-
-

Related Links

- -

Table of Contents

- -
-

Synopsis

-

ntp-keygen [ -deGHIMPT ] [ -b modulus ] [ -c [ RSA-MD2 | RSA-MD5 | RSA-SHA - | RSA-SHA1 | RSA-MDC2 | RSA-RIPEMD160 | DSA-SHA | DSA-SHA1 ] ] + + + + ntp-keygen - generate public and private keys + + + +

ntp-keygen - generate public and private keys

+

giffrom Alice's Adventures in Wonderland, Lewis Carroll

+

Alice holds the key.

+

Last update: + 10-Mar-2014 05:11 + UTC

+
+

Related Links

+ +

Table of Contents

+ +
+

Synopsis

+

ntp-keygen [ -deGHIMPT ] [ -b modulus ] [ -c [ RSA-MD2 | RSA-MD5 | RSA-SHA + | RSA-SHA1 | RSA-MDC2 | RSA-RIPEMD160 | DSA-SHA | DSA-SHA1 ] ] [ -C cipher ] [-i group ] [ -l days] [ -m modulus ] [ -p passwd1 ] [ -q passwd2 ] [ -S [ RSA | DSA ] ] [ -s host ] [ -V nkeys ]

Description

This program generates cryptographic data files used by the NTPv4 authentication and identity schemes. It can generate message digest keys used in symmetric key cryptography and, if the OpenSSL software library has been installed, it can generate host keys, sign keys, certificates, and identity keys and parameters used by the Autokey public key cryptography. The message digest keys file is generated in a format compatible with NTPv3. All other files are in PEM-encoded printable ASCII format so they can be embedded as MIME attachments in mail to other sites.

When used to generate message digest keys, the program produces a file containing - ten pseudo-random printable ASCII strings suitable for the MD5 message digest algorithm included in the distribution. If the OpenSSL library is installed, it produces an additional ten hex-encoded random bit strings suitable for the SHA1 and other message digest algorithms. The message digest keys file must be distributed and stored using secure means beyond the scope of NTP itself. Besides the keys used for ordinary NTP associations, additional keys can be defined as passwords for the ntpq and ntpdc utility programs.

+ ten pseudo-random printable ASCII strings suitable for the MD5 message digest algorithm included in the distribution. If the OpenSSL library is installed, it produces an additional ten hex-encoded random bit strings suitable for the SHA1, AES-128 CMAC, and other message digest algorithms. The message digest keys file must be distributed and stored using secure means beyond the scope of NTP itself. Besides the keys used for ordinary NTP associations, additional keys can be defined as passwords for the ntpq and ntpdc utility programs.

The remaining generated files are compatible with other OpenSSL applications and other Public Key Infrastructure (PKI) resources. Certificates generated by this program are compatible with extant industry practice, although some users might find the interpretation of X509v3 extension fields somewhat liberal. However, the identity keys are probably not compatible with anything other than Autokey.

Some files used by this program are encrypted using a private password. The -p option specifies the password for local encrypted files and the -q option the password for encrypted files sent to remote sites. If no password is specified, the host name returned by the Unix gethostname() function, normally the DNS name of the host, is used.

The pw option of the crypto configuration command specifies the read password for previously encrypted local files. This must match the local password used by this program. If not specified, the host name is used. Thus, if files are generated by this program without password, they can be read back by ntpd without password, but only on the same host.

@@ -46,7 +46,7 @@

To test and gain experience with Autokey concepts, log in as root and change to the keys directory, usually /usr/local/etc. When run for the first time, or if all files with names beginning ntpkey have been removed, use the ntp-keygen command without arguments to generate a default RSA host key and matching RSA-MD5 certificate with expiration date one year hence. If run again without options, the program uses the existing keys and parameters and generates only a new certificate with new expiration date one year hence.

Run the command on as many hosts as necessary. Designate one of them as the trusted host (TH) using ntp-keygen with the -T option and configure it to synchronize from reliable Internet servers. Then configure the other hosts to synchronize to the TH directly or indirectly. A certificate trail is created when Autokey asks the immediately ascendant host towards the TH to sign its certificate, which is then provided to the immediately descendant host on request. All group hosts should have acyclic certificate trails ending on the TH.

The host key is used to encrypt the cookie when required and so must be RSA type. By default, the host key is also the sign key used to encrypt signatures. A different sign key can be assigned using the -S option and this can be either RSA or DSA type. By default, the signature message digest type is MD5, but any combination of sign key type and message digest type supported by the OpenSSL library can be specified using the -c option.

-
The rules say cryptographic media should be generated with proventic filestamps, which means the host should already be synchronized before this program is run. This of course creates a chicken-and-egg problem when the host is started for the first time. Accordingly, the host time should be set by some other means, such as eyeball-and-wristwatch, at least so that the certificate lifetime is within the current year. After that and when the host is synchronized to a proventic source, the certificate should be re-generated.
+

The rules say cryptographic media should be generated with proventic filestamps, which means the host should already be synchronized before this program is run. This of course creates a chicken-and-egg problem when the host is started for the first time. Accordingly, the host time should be set by some other means, such as eyeball-and-wristwatch, at least so that the certificate lifetime is within the current year. After that and when the host is synchronized to a proventic source, the certificate should be re-generated.

Additional information on trusted groups and identity schemes is on the Autokey Public-Key Authentication page.

Command Line Options

@@ -100,16 +100,42 @@

All files begin with two nonencrypted lines. The first line contains the file name in the format ntpkey_key_host.fstamp. The second line contains the datestamp in conventional Unix date format. Lines beginning with # are ignored.

The remainder of the file contains cryptographic data encoded first using ASN.1 rules, then encrypted using the DES-CBC algorithm with given password and finally written in PEM-encoded printable ASCII text preceded and followed by MIME content identifier lines.

The format of the symmetric keys file, ordinarily named ntp.keys, is somewhat different than the other files in the interest of backward compatibility. Ordinarily, the file is generated by this program, but it can be constructed and edited using an ordinary text editor.

-
-

gif

-

Figure 1. Typical Symmetric Key File

-
+ + +
+ Figure 1. Typical Symmetric Key File +
+
+# ntpkey_MD5key_bk.ntp.org.3595864945
+# Thu Dec 12 19:22:25 2013
+
+1  MD5 L";Nw<`.I<f4U0)247"i  # MD5 key
+2  MD5 &>l0%XXK9O'51VwV<xq~  # MD5 key
+3  MD5 lb4zLW~d^!K:]RsD'qb6  # MD5 key
+4  MD5 Yue:tL[+vR)M`n~bY,'?  # MD5 key
+5  MD5 B;fxlKgr/&4ZTbL6=RxA  # MD5 key
+6  MD5 4eYwa`o}3i@@V@..R9!l  # MD5 key
+7  MD5 `A.([h+;wTQ|xfi%Sn_!  # MD5 key
+8  MD5 45:V,r4]l6y^JH6"Sh?F  # MD5 key
+9  MD5 3-5vcn*6l29DS?Xdsg)*  # MD5 key
+10 MD5 2late4Me              # MD5 key
+11 SHA1 a27872d3030a9025b8446c751b4551a7629af65c  # SHA1 key
+12 SHA1 21bc3b4865dbb9e920902abdccb3e04ff97a5e74  # SHA1 key
+13 SHA1 2b7736fe24fef5ba85ae11594132ab5d6f6daba9  # SHA1 key
+14 SHA  a5332809c8878dd3a5b918819108a111509aeceb  # SHA  key
+15 MD2  2fe16c88c760ff2f16d4267e36c1aa6c926e6964  # MD2  key
+16 MD4  b2691811dc19cfc0e2f9bcacd74213f29812183d  # MD4  key
+17 MD5  e4d6735b8bdad58ec5ffcb087300a17f7fef1f7c  # MD5  key
+18 MDC2 a8d5e2315c025bf3a79174c87fbd10477de2eabc  # MDC2 key
+19 RIPEMD160 77ca332cafb30e3cafb174dcd5b80ded7ba9b3d2  # RIPEMD160 key
+20 AES128CMAC f92ff73eee86c1e7dc638d6489a04e4e555af878  # AES128CMAC key
+  

Figure 1 shows a typical symmetric keys file used by the reference implementation. Each line of the file contains three fields, first an integer between 1 and 65534, inclusive, representing the key identifier used in the server and peer configuration commands. Next is the key type for the message digest algorithm, which in the absence of the OpenSSL library must be MD5 to designate the MD5 message digest algorithm. If the OpenSSL library is installed, the key type can be any message digest algorithm supported by that library. However, if compatibility with FIPS 140-2 is required, the key type must be either SHA or SHA1. The key type can be changed using an ASCII text editor.

An MD5 key consists of a printable ASCII string less than or equal to 16 characters and terminated by whitespace or a # character. An OpenSSL key consists of a hex-encoded ASCII string of 40 characters, which is truncated as necessary.

Note that the keys used by the ntpq and ntpdc programs are checked against passwords requested by the programs and entered by hand, so it is generally appropriate to specify these keys in human readable ASCII format.

The ntp-keygen program generates a MD5 symmetric keys file ntpkey_MD5key_hostname.filestamp. Since the file contains private shared keys, it should be visible only to root and distributed by secure means to other subnet hosts. The NTP daemon loads the file ntp.keys, so ntp-keygen installs a soft link from this name to the generated file. Subsequently, similar soft links must be installed by manual or automated means on the other subnet hosts. While this file is not used with the Autokey Version 2 protocol, it is needed to authenticate some remote configuration commands used by the ntpq and ntpdc utilities.

Bugs

-

It can take quite a while to generate some cryptographic values, from one to several minutes with modern architectures such as UltraSPARC and up to tens of minutes to an hour with older architectures such as SPARC IPC.

+

It can take quite a while to generate some cryptographic values.


diff --git a/html/ntpq.html b/html/ntpq.html index 1aa8df3fc..6fe4dfc09 100644 --- a/html/ntpq.html +++ b/html/ntpq.html @@ -71,7 +71,7 @@
keyid keyid
This command specifies the key number to be used to authenticate configuration requests. This must correspond to a key ID configured in ntp.conf for this purpose.
keytype
-
Specify the digest algorithm to use for authenticated requests, with default MD5. If the OpenSSL library is installed, digest can be be any message digest algorithm supported by the library. The current selections are: MD2, MD4, MD5, MDC2, RIPEMD160, SHA and SHA1.
+
Specify the digest algorithm to use for authenticated requests, with default MD5. If the OpenSSL library is installed, digest can be be any message digest algorithm supported by the library. The current selections are: MD2, MD4, MD5, MDC2, RIPEMD160, SHA, SHA1, and AES128CMAC.
ntpversion 1 | 2 | 3 | 4
Sets the NTP version number which ntpq claims in packets. Defaults to 2, Note that mode-6 control messages (and modes, for that matter) didn't exist in NTP version 1.
passwd
diff --git a/include/ntp_md5.h b/include/ntp_md5.h index 01b417a80..71332644f 100644 --- a/include/ntp_md5.h +++ b/include/ntp_md5.h @@ -7,6 +7,7 @@ #define NTP_MD5_H #ifdef OPENSSL +# include "openssl/cmac.h" # include "openssl/evp.h" # include "libssl_compat.h" #else /* !OPENSSL follows */ diff --git a/libntp/ssl_init.c b/libntp/ssl_init.c index bebf6e175..bf3f0c04c 100644 --- a/libntp/ssl_init.c +++ b/libntp/ssl_init.c @@ -13,12 +13,17 @@ #include #ifdef OPENSSL +#include "openssl/cmac.h" #include "openssl/crypto.h" #include "openssl/err.h" #include "openssl/evp.h" #include "openssl/opensslv.h" #include "libssl_compat.h" +#define MD5_LENGTH 16 +#define CMAC_LENGTH 16 +#define CMAC "AES128CMAC" + int ssl_init_done; #if OPENSSL_VERSION_NUMBER < 0x10100000L @@ -26,8 +31,9 @@ int ssl_init_done; static void atexit_ssl_cleanup(void) { - if (!ssl_init_done) + if (!ssl_init_done) { return; + } ssl_init_done = FALSE; EVP_cleanup(); @@ -103,46 +109,69 @@ keytype_from_text( /* * 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. + * recognized but matches our CMAC string use NID_cmac, or if + * it begins with 'M' use NID_md5 to be consistent with past + * behavior. */ INIT_SSL(); LIB_GETBUF(upcased); strlcpy(upcased, text, LIB_BUFLENGTH); - for (pch = upcased; '\0' != *pch; pch++) + + for (pch = upcased; '\0' != *pch; pch++) { *pch = (char)toupper((unsigned char)*pch); + } + key_type = OBJ_sn2nid(upcased); + + if (!key_type && !strncmp(CMAC, upcased, strlen(CMAC) + 1)) { + key_type = NID_cmac; + } #else key_type = 0; #endif - if (!key_type && 'm' == tolower((unsigned char)text[0])) + if (!key_type && 'm' == tolower((unsigned char)text[0])) { key_type = NID_md5; + } - if (!key_type) + if (!key_type) { return 0; + } if (NULL != pdigest_len) { #ifdef OPENSSL - EVP_MD_CTX *ctx; + EVP_MD * md; - 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) { + md = EVP_get_digestbynid(key_type); + digest_len = (md) ? EVP_MD_size(md) : 0; + + if (!md || digest_len <= 0) { + if (key_type == NID_cmac) { + digest_len = CMAC_LENGTH; + } else { fprintf(stderr, - "key type %s %u octet digests are too big, max %lu\n", - keytype_name(key_type), digest_len, - max_digest_len); + "key type %s is not supported by OpenSSL\n", + keytype_name(key_type)); msyslog(LOG_ERR, - "key type %s %u octet digests are too big, max %lu", - keytype_name(key_type), digest_len, - max_digest_len); + "key type %s is not supported by OpenSSL\n", + keytype_name(key_type)); return 0; + } + } + + 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; + digest_len = MD5_LENGTH; #endif *pdigest_len = digest_len; } @@ -167,8 +196,13 @@ keytype_name( #ifdef OPENSSL INIT_SSL(); name = OBJ_nid2sn(nid); - if (NULL == name) + + if (NID_cmac == nid) { + name = CMAC; + } else + if (NULL == name) { name = unknown_type; + } #else /* !OPENSSL follows */ if (NID_md5 == nid) name = "MD5"; @@ -203,3 +237,4 @@ getpass_keytype( return getpass(pass_prompt); } + diff --git a/ntpq/ntpq.c b/ntpq/ntpq.c index bfc5fa9cf..2efc7c74c 100644 --- a/ntpq/ntpq.c +++ b/ntpq/ntpq.c @@ -35,6 +35,8 @@ #include "openssl/objects.h" #include "openssl/err.h" #include "libssl_compat.h" + +#define CMAC "AES128CMAC" #endif #include @@ -459,6 +461,17 @@ ntpqmain( { u_int ihost; size_t icmd; +#ifdef OPENSSL +# ifdef HAVE_EVP_MD_DO_ALL_SORTED +# define K_PER_LINE 8 +# define K_NL_PFX_STR "\n " +# define K_DELIM_STR ", " + + int nl; + int append; + size_t len; +# endif +#endif #ifdef SYS_VXWORKS @@ -495,6 +508,52 @@ ntpqmain( #ifdef OPENSSL builtins[icmd].desc[0] = "digest-name"; + +# ifdef HAVE_EVP_MD_DO_ALL_SORTED + /* Append CMAC to SSL digests */ + + /* If list empty, we need to append CMAC and new line */ + append = nl = (!list || !*list); + + if (append) { + len = strlen(K_NL_PFX_STR) + strlen(CMAC); + list = (char *)erealloc(list, len + 1); + list[0] = '\0'; + } else { + /* Check if CMAC already in list */ + const char *cmac_sn; + char *cmac_p; + + cmac_sn = OBJ_nid2sn(NID_cmac); + cmac_p = strstr(list, cmac_sn); + + /* CMAC in list if found followed by null or "," */ + if (cmac_p) + cmac_p += strlen(cmac_sn); + + append = !(cmap_p && (!*cmap_p || ',' == *cmap_p)); + + if (append) { + char *last_nl; + + len = strlen(list) + strlen(CMAC); + /* Check if new entry will fit on last line */ + last_nl = strrchr(list, "\n"); + if (!last_nl) last_nl = list; + /* Do we need a new line? */ + nl = (len - (last_nl - list) + strlen(K_DELIM_STR) > 72); + len += (nl) ? strlen(K_NL_PFX_STR) : strlen(K_DELIM_STR); + list = (char *)erealloc(list, len + 1); + } + } + + /* Check if we need to append an entry */ + if (append) + sprintf(list + strlen(list), "%s%s", + ((nl) ? K_NL_PFX_STR : K_DELIM_STR), + CMAC); +# endif + my_easprintf(&msg, "set key type to use for authenticated requests, one of:%s", list); @@ -3575,9 +3634,8 @@ struct hstate { const char **seen; int idx; }; -#define K_PER_LINE 8 -#define K_NL_PFX_STR "\n " -#define K_DELIM_STR ", " + + static void list_md_fn(const EVP_MD *m, const char *from, const char *to, void *arg ) { size_t len, n; diff --git a/ports/winnt/scripts/mkver.bat b/ports/winnt/scripts/mkver.bat index 7b4482996..eac3f1065 100755 --- a/ports/winnt/scripts/mkver.bat +++ b/ports/winnt/scripts/mkver.bat @@ -1,4 +1,4 @@ -@echo off +@echo on GOTO PROG see notes/remarks directly below this header: @@ -142,6 +142,8 @@ REM **************************************************************************** IF NOT EXIST %TEMP%\TZ-%GENERATED_PROGRAM%.TMP GOTO NOTZINFO for /f "Tokens=1* Delims==" %%a in ('type %TEMP%\TZ-%GENERATED_PROGRAM%.TMP') do if %%a == "ActiveTimeBias" SET ACTIVEBIAS=%%b + REM Windows 10 - Home and possibly others + IF "%ACTIVEBIAS%" == "" for /f "Tokens=1* Delims==" %%a in ('type %TEMP%\TZ-%GENERATED_PROGRAM%.TMP') do if %%a == "Bias" SET ACTIVEBIAS=%%b for /f "Tokens=1* Delims=:" %%a in ('echo %ACTIVEBIAS%') do ( SET ACTIVEBIAS=%%b & SET PARTYP=%%a ) REM *** Clean up temporary file diff --git a/sntp/crypto.c b/sntp/crypto.c index e45b21360..b0b360c80 100644 --- a/sntp/crypto.c +++ b/sntp/crypto.c @@ -18,7 +18,6 @@ make_mac( { u_int len = mac_size; int key_type; - EVP_MD_CTX * ctx; if (cmp_key->key_len > 64) return 0; @@ -27,13 +26,80 @@ make_mac( INIT_SSL(); key_type = keytype_from_text(cmp_key->type, NULL); - - ctx = EVP_MD_CTX_new(); - EVP_DigestInit(ctx, EVP_get_digestbynid(key_type)); - EVP_DigestUpdate(ctx, (const u_char *)cmp_key->key_seq, (u_int)cmp_key->key_len); - EVP_DigestUpdate(ctx, pkt_data, (u_int)pkt_size); - EVP_DigestFinal(ctx, digest, &len); - EVP_MD_CTX_free(ctx); + +#ifdef OPENSSL + /* Check if CMAC key type specific code required */ + if (key_type = NID_cmac) { + CMAC_CTX * ctx; + + if (!(ctx = CMAC_CTX_new())) { + fprintf(stderr, "make_mac: CMAC %s CTX new failed.\n", CMAC); + msyslog(LOG_ERR, "make_mac: CMAC %s CTX new failed.", CMAC); + } else + if (!CMAC_Init(ctx, (const u_char *)cmp_key->key_seq, + (u_int)cmp_key->key_len, EVP_aes_128_cbc(), NULL)) { + fprintf(stderr, "make_mac: CMAC %s Init failed.\n", CMAC); + msyslog(LOG_ERR, "make_mac: CMAC %s Init failed.", CMAC); + } else + if (!CMAC_Update(ctx, pkt_data, (u_int)pkt_size)) { + fprintf(stderr, "make_mac: CMAC %s Update failed.\n", CMAC); + msyslog(LOG_ERR, "make_mac: CMAC %s Update failed.", CMAC); + } else + if (!CMAC_Final(ctx, digest, &len)) { + fprintf(stderr, "make_mac: CMAC %s Final failed.\n", CMAC); + msyslog(LOG_ERR, "make_mac: CMAC %s Final failed.", CMAC); + } + + CMAC_CTX_cleanup(ctx); + } else { /* generic MAC handling */ +#endif + EVP_MD_CTX * ctx; + +#ifdef OPENSSL + if (!(ctx = EVP_MD_CTX_new())) { + fprintf(stderr, "make_mac: MAC %s Digest CTX new failed.\n", + cmp_key->type); + msyslog(LOG_ERR, "make_mac: MAC %s Digest CTX new failed.", + cmp_key->type); + } else + if (!EVP_DigestInit(ctx, EVP_get_digestbynid(key_type))) { + fprintf(stderr, "make_mac: MAC %s Digest Init failed.\n", + cmp_key->type); + msyslog(LOG_ERR, "make_mac: MAC %s Digest Init failed.", + cmp_key->type); + } else + if (!EVP_DigestUpdate(ctx, (const u_char *)cmp_key->key_seq, + (u_int)cmp_key->key_len)) { + fprintf(stderr, "make_mac: MAC %s Digest Update key failed.\n", + cmp_key->type); + msyslog(LOG_ERR, "make_mac: MAC %s Digest Update key failed.", + cmp_key->type); + } else + if (!EVP_DigestUpdate(ctx, pkt_data, (u_int)pkt_size)) { + fprintf(stderr, "make_mac: MAC %s Digest Update data failed.\n", + cmp_key->type); + msyslog(LOG_ERR, "make_mac: MAC %s Digest Update data failed.", + cmp_key->type); + } else + if (!EVP_DigestFinal(ctx, digest, &len)) { + fprintf(stderr, "make_mac: MAC %s Digest Final failed.\n", + cmp_key->type); + msyslog(LOG_ERR, "make_mac: MAC %s Digest Final failed.", + cmp_key->type); + } +#else /* !OPENSSL */ + ctx = EVP_MD_CTX_new(); + EVP_DigestInit(ctx, EVP_get_digestbynid(key_type)); + EVP_DigestUpdate(ctx, (const u_char *)cmp_key->key_seq, + (u_int)cmp_key->key_len); + EVP_DigestUpdate(ctx, pkt_data, (u_int)pkt_size); + EVP_DigestFinal(ctx, digest, &len); +#endif + + EVP_MD_CTX_free(ctx); +#ifdef OPENSSL + } +#endif return (int)len; } diff --git a/sntp/tests/crypto.c b/sntp/tests/crypto.c index fb2dc6298..61323c505 100644 --- a/sntp/tests/crypto.c +++ b/sntp/tests/crypto.c @@ -5,14 +5,19 @@ #include "sntptest.h" #include "crypto.h" +#define CMAC "AES128CMAC" + #define MD5_LENGTH 16 #define SHA1_LENGTH 20 +#define CMAC_LENGTH 16 void test_MakeMd5Mac(void); void test_MakeSHA1Mac(void); +void test_MakeCMac(void); void test_VerifyCorrectMD5(void); void test_VerifySHA1(void); +void test_VerifyCMAC(void); void test_VerifyFailure(void); void test_PacketSizeNotMultipleOfFourBytes(void); @@ -72,6 +77,38 @@ test_MakeSHA1Mac(void) } +void +test_MakeCMac(void) +{ +#ifdef OPENSSL + + const char* PKT_DATA = "abcdefgh0123"; + const int PKT_LEN = strlen(PKT_DATA); + const char* EXPECTED_DIGEST = + "\xdd\x35\xd5\xf5\x14\x23\xd9\xd6" + "\x38\x5d\x29\x80\xfe\x51\xb9\x6b"; + char actual[CMAC_LENGTH]; + + struct key cmac; + cmac.next = NULL; + cmac.key_id = 30; + cmac.key_len = CMAC_LENGTH; + memcpy(&cmac.key_seq, "aes-128-cmac-seq", cmac.key_len); + memcpy(&cmac.type, CMAC, strlen(CMAC) + 1); + + TEST_ASSERT_EQUAL(CMAC_LENGTH, + make_mac(PKT_DATA, PKT_LEN, CMAC_LENGTH, &cmac, actual)); + + TEST_ASSERT_EQUAL_MEMORY(EXPECTED_DIGEST, actual, CMAC_LENGTH); + +#else + + TEST_IGNORE_MESSAGE("OpenSSL not found, skipping..."); + +#endif /* OPENSSL */ +} + + void test_VerifyCorrectMD5(void) { @@ -121,6 +158,36 @@ test_VerifySHA1(void) #endif /* OPENSSL */ } + +void +test_VerifyCMAC(void) +{ +#ifdef OPENSSL + + const char* PKT_DATA = + "sometestdata" /* Data */ + "\0\0\0\0" /* Key-ID (unused) */ + "\x4e\x0c\xf0\xe2\xc7\x8e\xbb\xbf" /* MAC */ + "\x79\xfc\x87\xc7\x8b\xb7\x4a\x0b"; + const int PKT_LEN = 12; + + struct key cmac; + cmac.next = NULL; + cmac.key_id = 0; + cmac.key_len = CMAC_LENGTH; + memcpy(&cmac.key_seq, "aes-128-cmac-key", cmac.key_len); + memcpy(&cmac.type, CMAC, strlen(CMAC) + 1); + + TEST_ASSERT_TRUE(auth_md5(PKT_DATA, PKT_LEN, CMAC_LENGTH, &cmac)); + +#else + + TEST_IGNORE_MESSAGE("OpenSSL not found, skipping..."); + +#endif /* OPENSSL */ +} + + void test_VerifyFailure(void) { diff --git a/sntp/tests/packetProcessing.c b/sntp/tests/packetProcessing.c index 660b5b6e2..ef03cb1f6 100644 --- a/sntp/tests/packetProcessing.c +++ b/sntp/tests/packetProcessing.c @@ -5,6 +5,8 @@ #include "ntp_stdlib.h" #include "unity.h" +#define CMAC "AES128CMAC" + const char * Version = "stub unit test Version string"; @@ -35,6 +37,7 @@ void test_AcceptNoSentPacketBroadcastMode(void); void test_CorrectUnauthenticatedPacket(void); void test_CorrectAuthenticatedPacketMD5(void); void test_CorrectAuthenticatedPacketSHA1(void); +void test_CorrectAuthenticatedPacketCMAC(void); /* [Bug 2998] There are some issues whith the definition of 'struct pkt' * when AUTOKEY is undefined -- the formal struct is too small to hold @@ -455,3 +458,26 @@ test_CorrectAuthenticatedPacketSHA1(void) process_pkt(&testpkt.p, &testsock, pkt_len, MODE_SERVER, &testspkt.p, "UnitTest")); } + + +void +test_CorrectAuthenticatedPacketCMAC(void) +{ + PrepareAuthenticationTest(30, 15, CMAC, "abcdefghijklmno"); + TEST_ASSERT_TRUE(ENABLED_OPT(AUTHENTICATION)); + + int pkt_len = LEN_PKT_NOMAC; + + /* Prepare the packet. */ + testpkt.p.exten[0] = htonl(20); + int mac_len = make_mac(&testpkt.p, pkt_len, + MAX_MAC_LEN, key_ptr, + &testpkt.p.exten[1]); + + pkt_len += 4 + mac_len; + + TEST_ASSERT_EQUAL(pkt_len, + process_pkt(&testpkt.p, &testsock, pkt_len, + MODE_SERVER, &testspkt.p, "UnitTest")); +} + diff --git a/tests/libntp/ssl_init.c b/tests/libntp/ssl_init.c index 435e5c912..9a59a9b42 100644 --- a/tests/libntp/ssl_init.c +++ b/tests/libntp/ssl_init.c @@ -6,6 +6,8 @@ # include "openssl/err.h" # include "openssl/rand.h" # include "openssl/evp.h" + +#define CMAC "AES128CMAC" #endif #include "unity.h" @@ -13,12 +15,15 @@ static const size_t TEST_MD5_DIGEST_LENGTH = 16; static const size_t TEST_SHA1_DIGEST_LENGTH = 20; +static const size_t TEST_CMAC_DIGEST_LENGTH = 16; void test_MD5KeyTypeWithoutDigestLength(void); void test_MD5KeyTypeWithDigestLength(void); void test_SHA1KeyTypeWithDigestLength(void); +void test_CMACKeyTypeWithDigestLength(void); void test_MD5KeyName(void); void test_SHA1KeyName(void); +void test_CMACKeyName(void); // keytype_from_text() @@ -52,6 +57,21 @@ test_SHA1KeyTypeWithDigestLength(void) { } +void +test_CMACKeyTypeWithDigestLength(void) { +#ifdef OPENSSL + size_t digestLength; + size_t expected = TEST_CMAC_DIGEST_LENGTH; + + TEST_ASSERT_EQUAL(NID_cmac, keytype_from_text(CMAC, &digestLength)); + TEST_ASSERT_EQUAL(expected, digestLength); + /* OPENSSL */ +#else + TEST_IGNORE_MESSAGE("Skipping because OPENSSL isn't defined"); +#endif +} + + // keytype_name() void test_MD5KeyName(void) { @@ -67,3 +87,14 @@ test_SHA1KeyName(void) { TEST_IGNORE_MESSAGE("Skipping because OPENSSL isn't defined"); #endif /* OPENSSL */ } + + +void +test_CMACKeyName(void) { +#ifdef OPENSSL + TEST_ASSERT_EQUAL_STRING(CMAC, keytype_name(NID_cmac)); +#else + TEST_IGNORE_MESSAGE("Skipping because OPENSSL isn't defined"); +#endif /* OPENSSL */ +} + diff --git a/util/ntp-keygen-opts.def b/util/ntp-keygen-opts.def index 3088cf528..5df2ddf3a 100644 --- a/util/ntp-keygen-opts.def +++ b/util/ntp-keygen-opts.def @@ -297,8 +297,8 @@ When used to generate message digest keys, the program produces a file containing ten pseudo-random printable ASCII strings suitable for the MD5 message digest algorithm included in the distribution. If the OpenSSL library is installed, it produces an additional ten -hex-encoded random bit strings suitable for the SHA1 and other message -digest algorithms. +hex-encoded random bit strings suitable for the SHA1, AES-128-CMAC, and +other message digest algorithms. The message digest keys file must be distributed and stored using secure means beyond the scope of NTP itself. Besides the keys used for ordinary NTP associations, additional keys @@ -1054,8 +1054,7 @@ The format of the symmetric keys file is somewhat different than the other files in the interest of backward compatibility. Since DES-CBC is deprecated in NTPv4, the only key format of interest is MD5 alphanumeric strings. -Following hte heard the keys are -entered one per line in the format +Following the header the keys are entered one per line in the format .D1 Ar keyno type key where .Ar keyno @@ -1134,10 +1133,7 @@ doc-section = { ds-type = 'BUGS'; ds-format = 'mdoc'; ds-text = <<- _END_MDOC_BUGS -It can take quite a while to generate some cryptographic values, -from one to several minutes with modern architectures -such as UltraSPARC and up to tens of minutes to an hour -with older architectures such as SPARC IPC. +It can take quite a while to generate some cryptographic values. .Pp Please report bugs to http://bugs.ntp.org . _END_MDOC_BUGS;