]> git.ipfire.org Git - thirdparty/ntp.git/commitdiff
new crypto signature cleanup
authorHarlan Stenn <stenn@ntp.org>
Wed, 11 Nov 2009 08:08:32 +0000 (03:08 -0500)
committerHarlan Stenn <stenn@ntp.org>
Wed, 11 Nov 2009 08:08:32 +0000 (03:08 -0500)
bk: 4afa7100ksceNnQ7QJuUutQWG873eA

ChangeLog
html/authopt.html
html/keygen.html
libntp/authkeys.c
libntp/authreadkeys.c
util/ntp-keygen.c

index cc781463015c22e1c7232da1395d520e14147d1e..d7fd572a4702383a6bea63c9238a241cc329cbb8 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,4 @@
+* new crypto signature cleanup.
 (4.2.5p242-RC) 2009/11/10 Released by Harlan Stenn <stenn@ntp.org>
 * [Bug 1363] CID 92 clarify fallthrough case in clk_trimtsip.c
 * [Bug 1366] ioctl(TIOCSCTTY, 0) fails on NetBSD *[0-2].* > 3.99.7.
index 288628d9c2395601519ccb113adf86fdfba29a80..2909193add4e59fcc138e3de828b657bcbd467a2 100644 (file)
@@ -23,7 +23,7 @@
 <p>Our resident cryptographer; now you see him, now you don't.</p>
 
 <p>Last update: 
-                       <!-- #BeginDate format:En2m -->07-Nov-2009  20:25<!-- #EndDate -->
+                       <!-- #BeginDate format:En2m -->11-Nov-2009  5:48<!-- #EndDate -->
 UTC</p>
 <br clear="left">
 
@@ -73,8 +73,8 @@ UTC</p>
        conformance to FIPS 140-2 is required, only a limited subset of these algorithms
        is available.</p>
 
-<p>NTPv4 includes the NTPv3 scheme, properly called symmetric key cryptography,
-       and in addition a new scheme based on public key cryptography and called
+<p>NTPv4 includes the NTPv3 scheme
+       and optionally a new scheme based on public key cryptography and called
        Autokey. Public key cryptography is generally considered more secure than
        symmetric key cryptography, since the security is based on private and public
        values which are generated by each participant and where the private value
@@ -83,7 +83,15 @@ UTC</p>
        or the <a href="keygen.html"><tt>ntp-keygen</tt></a> utility
        program in the NTP software distribution.</p>
 
-<p>While the algorithms for symmetric key cryptography are included in the NTPv4 software distribution, Autokey cryptography requires the OpenSSL software library to be installed before building the NTP distribution. This library is available from <a href="http://www.openssl.org">http://www.openssl.org</a> and can be installed using the procedures outlined in the <a href="build.html">Building and Installing the Distribution</a> page. Once installed, the configure and build process automatically detects the library and links the library routines required.</p>
+<p>While the algorithms for MD5 symmetric key cryptography are included in the
+       NTPv4 software distribution, modern algorithms for symmetric key and public
+       key cryptograpny   requires the OpenSSL software library
+       to be installed before building the NTP distribution. This library is available
+       from <a href="http://www.openssl.org">http://www.openssl.org</a> and
+       can be installed using the procedures outlined in the <a href="build.html">Building
+       and Installing the Distribution</a> page. Once installed, the configure and
+       build process automatically detects the library and links the library routines
+       required.</p>
 
 <p>Note that according to US law, NTP binaries including OpenSSL library components,
        including the OpenSSL library itself, cannot be exported outside the
@@ -112,7 +120,8 @@ UTC</p>
        program generates pseudo-random keys, one key for each line. Each line consists
        of three fields, the key identifier as a decimal number from 1 to 65534 inclusive,
        a key type chosen from the  keywords of the <tt>digest</tt> option of the <tt>crypto</tt> command,
-       and a 16-character printable ASCII string as the key itself.</p>
+       and a 20-character printable ASCII string or a 40-character hex string as
+       the key itself.</p>
 
 <p>When <tt>ntpd</tt> is first started, it reads the key file specified by the <tt>keys</tt> command and installs the keys in the key cache. However, individual keys must be activated with the <tt>trustedkey</tt> configuration command before use. This allows, for instance, the installation of possibly several batches of keys and then activating a key remotely using <tt>ntpdc</tt>. The <tt>requestkey</tt> command selects the key ID used as the password for the <tt>ntpdc</tt> utility, while the <tt>controlkey</tt> command selects the key ID used as the password for the <tt>ntpq</tt> utility.</p>
 <p>By default, the message digest algorithm is MD5 selected by the key type
index 3d72220fe6a415ca32a643593cb6b9b7a728837e..04508a4d0a186cb0f6caebd78cbc67fdff4e230b 100644 (file)
@@ -17,7 +17,7 @@
 <p>Alice holds the key.</p>
 
 <p>Last update: 
-       <!-- #BeginDate format:En2m -->04-Nov-2009  21:40<!-- #EndDate -->
+       <!-- #BeginDate format:En2m -->11-Nov-2009  6:01<!-- #EndDate -->
 </p>
 <br clear="left">
 
 
 <h4 id="descrip">Description</h4>
 
-<p>This program generates cryptographic data files used by the NTPv4 authentication and identity schemes. It generates MD5 keys used in symmetric key cryptography and, if the OpenSSL software library has been installed, it generates host keys, sign keys, certificates and identity keys used in the Autokey public key cryptography. The symmetric 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.</p>
+<p>This program generates cryptographic data files used by the NTPv4 authentication
+       and identity schemes. It generates MD5 message digest keys used in symmetric
+       key cryptography and, if the OpenSSL software library has been installed,
+       it generates message digest keys for other algorithms, as well as host keys,
+       sign keys, certificates and identity keys used in the Autokey public key
+       cryptography. The symmetric 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.</p>
 
 <p>When used to generate symmetric keys, the program produces a file containing
-       16 pseudo-random keys of 128 bits in length. If this is the only need, run <tt>ntp-keygen</tt> with
+       ten pseudo-random printable ASCII strings, as well as ten random hex strings,
+       both of `60 bits in length. 
+       If this is the only need, run <tt>ntp-keygen</tt> with
        the <tt>-M</tt> option
        and disregard the remainder of this page. The file can be edited later with
        purpose-chosen passwords for the <tt>ntpq</tt> and <tt>ntpdc</tt> programs.
index 9c32e09f36a43fa2e7e9e1bc3e9af05304dcfdaa..b9b76d9e6bcfc5aa81de4bbf9c53c80690f236a2 100644 (file)
@@ -390,8 +390,17 @@ MD5auth_setkey(
        sk->next = key_hash[KEYHASH(keyno)];
        key_hash[KEYHASH(keyno)] = sk;
 #ifdef DEBUG
-       if (debug)
-               printf("auth_setkey: key %d type %d\n", sk->keyid, sk->type);
+       if (debug > 1) {
+               char    hex[] = "0123456789abcdef";
+               int     j;
+
+               printf("auth_setkey: key %d type %d len %d ", sk->keyid,
+                   sk->type, sk->keylen);
+               for (j = 0; j < sk->keylen; j++)
+                               printf("%c%c", hex[key[j] >> 4],
+                                   hex[key[j] & 0xf]);
+               printf("\n");
+       }       
 #endif
        authnumkeys++;
 }
index 38f25b2b681539f4548383d0fb80b1bd57d5fdc1..b329159e56f47d9b9332e4cf81a3bfe5b4267034 100644 (file)
@@ -22,7 +22,7 @@ static char *nexttok (char **);
  */
 static char *
 nexttok(
-       char **str
+       char    **str
        )
 {
        register char *cp;
@@ -69,12 +69,15 @@ authreadkeys(
        const char *file
        )
 {
-       FILE *fp;
-       char *line;
-       char *token, *keystr;
-       keyid_t keyno;
-       int keytype;
-       char buf[512];          /* lots of room for line */
+       FILE    *fp;
+       char    *line;
+       char    *token;
+       keyid_t keyno;
+       int     keytype;
+       char    buf[512];               /* lots of room for line */
+       u_char  keystr[20];
+       int     len;
+       int     j;
 
        /*
         * Open file.  Complain and return if it can't be opened.
@@ -134,7 +137,7 @@ authreadkeys(
                 * by OpenSSL. The key type is the NID used by the message
                 * digest algorithm. Ther are a number of inconsistencies in
                 * the OpenSSL database. We attempt to discover them here
-                * and prevent use of inconsistent data.
+                * and prevent use of inconsistent data later.
                 */
                if (strcmp(token, "M") == 0 || strcmp(token, "m") == 0)
                        token  = "MD5";
@@ -162,10 +165,12 @@ authreadkeys(
                }
                keytype = KEY_TYPE_MD5;
 #endif /* OPENSSL */
-               keystr = token;
 
                /*
-                * Finally, get key and insert it
+                * Finally, get key and insert it. If it is longer than 20
+                * characters, it is a binary string encoded in hex;
+                * otherwise, it is a text string of printable ASCII
+                * characters.
                 */
                token = nexttok(&line);
                if (token == NULL) {
@@ -173,8 +178,29 @@ authreadkeys(
                            "authreadkeys: no key for key %d", keyno);
                        continue;
                }
-               MD5auth_setkey(keyno, keytype, (u_char *)token,
-                   strlen(token));
+               len = strlen(token);
+               if (len <= 20) {
+                       MD5auth_setkey(keyno, keytype, (u_char *)token, len);
+               } else {
+                       char    hex[] = "0123456789abcdef";
+                       int     temp;
+                       char    *ptr;
+
+                       for (j = 0; j < len; j++) {
+                               ptr = strchr(hex, tolower(token[j]));
+                               if (ptr == NULL) {
+                                       msyslog(LOG_ERR,
+                                           "authreadkeys: invalid hex digit for key %d", keyno);
+                                       continue;
+                               }
+                               temp = ptr - hex;
+                               if (j & 1)
+                                       keystr[j / 2] |= temp;
+                               else
+                                       keystr[j / 2] = temp << 4;
+                       }
+                       MD5auth_setkey(keyno, keytype, keystr, len / 2);
+               }
        }
        fclose(fp);
        return (1);
index 61eba4d5f685d7462cad3cc2ccde2cc6c88747b8..da12b21251e4f244580bfe6476b16b50ecfacf27 100644 (file)
 /*
  * Cryptodefines
  */
-#define        MD5KEYS         16      /* number of MD5 keys generated */
+#define        MD5KEYS         10      /* number of keys generated of each type */
+#define        MD5SIZE         20      /* maximum key size */
 #define        JAN_1970        2208988800UL /* NTP seconds */
 #define YEAR           ((long)60*60*24*365) /* one year in seconds */
 #define MAXFILENAME    256     /* max file name length */
@@ -690,7 +691,9 @@ main(
 
 
 /*
- * Generate semi-random MD5 keys compatible with NTPv3 and NTPv4
+ * Generate semi-random MD5 keys compatible with NTPv3 and NTPv4. Also,
+ * if OpenSSL is around, generate random SHA1 keys compatible with
+ * symmetric key cryptography.
  */
 int
 gen_md5(
@@ -700,11 +703,16 @@ gen_md5(
        u_char  md5key[16+1];   /* MD5 key */
        FILE    *str;
        int     i, j;
+#ifdef OPENSSL
+       u_char  keystr[MD5SIZE];
+       u_char  hexstr[2 * MD5SIZE + 1];
+       u_char  hex[] = "0123456789abcdef";
+#endif /* OPENSSL */
 
        str = fheader("MD5key", id, groupname);
        ntp_srandom((u_long)epoch);
        for (i = 1; i <= MD5KEYS; i++) {
-               for (j = 0; j < 16; j++) {
+               for (j = 0; j < MD5SIZE; j++) {
                        int temp;
 
                        while (1) {
@@ -718,9 +726,21 @@ gen_md5(
                        md5key[j] = (u_char)temp;
                }
                md5key[j] = '\0';
-               fprintf(str, "%2d MD5 %16s      # MD5 key\n", i,
+               fprintf(str, "%2d MD5 %s  # MD5 key\n", i,
                    md5key);
        }
+#ifdef OPENSSL
+       for (i = 1; i <= MD5KEYS; i++) {
+               RAND_bytes(keystr, 20);
+               for (j = 0; j < MD5SIZE; j++) {
+                       hexstr[2 * j] = hex[keystr[j] >> 4];
+                       hexstr[2 * j + 1] = hex[keystr[j] & 0xf];
+               }
+               hexstr[2 * MD5SIZE] = '\0';
+               fprintf(str, "%2d SHA1 %s  # SHA1 key\n", i + MD5KEYS,
+                   hexstr);
+       }
+#endif /* OPENSSL */
        fclose(str);
        return (1);
 }