Home | History | Annotate | Line # | Download | only in dist
signature.c revision 1.2.20.1
      1  1.2.20.1       snj /*
      2       1.1  christos  * Redistribution and use in source and binary forms, with or without
      3       1.1  christos  * modification, are permitted provided that: (1) source code
      4       1.1  christos  * distributions retain the above copyright notice and this paragraph
      5       1.1  christos  * in its entirety, and (2) distributions including binary code include
      6       1.1  christos  * the above copyright notice and this paragraph in its entirety in
      7       1.1  christos  * the documentation or other materials provided with the distribution.
      8       1.1  christos  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND
      9       1.1  christos  * WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT
     10       1.1  christos  * LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
     11       1.1  christos  * FOR A PARTICULAR PURPOSE.
     12       1.1  christos  *
     13       1.1  christos  * Functions for signature and digest verification.
     14  1.2.20.1       snj  *
     15       1.1  christos  * Original code by Hannes Gredler (hannes (at) juniper.net)
     16       1.1  christos  */
     17       1.1  christos 
     18       1.2  christos #include <sys/cdefs.h>
     19       1.1  christos #ifndef lint
     20  1.2.20.1       snj __RCSID("$NetBSD: signature.c,v 1.2.20.1 2017/02/19 07:36:20 snj Exp $");
     21       1.1  christos #endif
     22       1.1  christos 
     23       1.1  christos #ifdef HAVE_CONFIG_H
     24       1.1  christos #include "config.h"
     25       1.1  christos #endif
     26       1.1  christos 
     27  1.2.20.1       snj #include <netdissect-stdinc.h>
     28       1.1  christos 
     29       1.1  christos #include <string.h>
     30  1.2.20.1       snj #include <stdlib.h>
     31       1.1  christos 
     32  1.2.20.1       snj #include "netdissect.h"
     33       1.1  christos #include "signature.h"
     34       1.1  christos 
     35       1.1  christos #ifdef HAVE_LIBCRYPTO
     36       1.1  christos #include <openssl/md5.h>
     37       1.1  christos #endif
     38       1.1  christos 
     39       1.1  christos const struct tok signature_check_values[] = {
     40       1.1  christos     { SIGNATURE_VALID, "valid"},
     41       1.1  christos     { SIGNATURE_INVALID, "invalid"},
     42  1.2.20.1       snj     { CANT_ALLOCATE_COPY, "can't allocate memory"},
     43       1.1  christos     { CANT_CHECK_SIGNATURE, "unchecked"},
     44       1.1  christos     { 0, NULL }
     45       1.1  christos };
     46       1.1  christos 
     47       1.1  christos 
     48       1.1  christos #ifdef HAVE_LIBCRYPTO
     49       1.1  christos /*
     50       1.1  christos  * Compute a HMAC MD5 sum.
     51       1.1  christos  * Taken from rfc2104, Appendix.
     52       1.1  christos  */
     53  1.2.20.1       snj USES_APPLE_DEPRECATED_API
     54       1.1  christos static void
     55  1.2.20.1       snj signature_compute_hmac_md5(const uint8_t *text, int text_len, unsigned char *key,
     56  1.2.20.1       snj                            unsigned int key_len, uint8_t *digest)
     57       1.1  christos {
     58       1.1  christos     MD5_CTX context;
     59       1.1  christos     unsigned char k_ipad[65];    /* inner padding - key XORd with ipad */
     60       1.1  christos     unsigned char k_opad[65];    /* outer padding - key XORd with opad */
     61       1.1  christos     unsigned char tk[16];
     62       1.1  christos     int i;
     63       1.1  christos 
     64       1.1  christos     /* if key is longer than 64 bytes reset it to key=MD5(key) */
     65       1.1  christos     if (key_len > 64) {
     66       1.1  christos 
     67       1.1  christos         MD5_CTX tctx;
     68       1.1  christos 
     69       1.1  christos         MD5_Init(&tctx);
     70       1.1  christos         MD5_Update(&tctx, key, key_len);
     71       1.1  christos         MD5_Final(tk, &tctx);
     72       1.1  christos 
     73       1.1  christos         key = tk;
     74       1.1  christos         key_len = 16;
     75       1.1  christos     }
     76       1.1  christos 
     77       1.1  christos     /*
     78       1.1  christos      * the HMAC_MD5 transform looks like:
     79       1.1  christos      *
     80       1.1  christos      * MD5(K XOR opad, MD5(K XOR ipad, text))
     81       1.1  christos      *
     82       1.1  christos      * where K is an n byte key
     83       1.1  christos      * ipad is the byte 0x36 repeated 64 times
     84       1.1  christos      * opad is the byte 0x5c repeated 64 times
     85       1.1  christos      * and text is the data being protected
     86       1.1  christos      */
     87       1.1  christos 
     88       1.1  christos     /* start out by storing key in pads */
     89       1.1  christos     memset(k_ipad, 0, sizeof k_ipad);
     90       1.1  christos     memset(k_opad, 0, sizeof k_opad);
     91       1.1  christos     memcpy(k_ipad, key, key_len);
     92       1.1  christos     memcpy(k_opad, key, key_len);
     93       1.1  christos 
     94       1.1  christos     /* XOR key with ipad and opad values */
     95       1.1  christos     for (i=0; i<64; i++) {
     96       1.1  christos         k_ipad[i] ^= 0x36;
     97       1.1  christos         k_opad[i] ^= 0x5c;
     98       1.1  christos     }
     99       1.1  christos 
    100       1.1  christos     /*
    101       1.1  christos      * perform inner MD5
    102       1.1  christos      */
    103       1.1  christos     MD5_Init(&context);                   /* init context for 1st pass */
    104       1.1  christos     MD5_Update(&context, k_ipad, 64);     /* start with inner pad */
    105       1.1  christos     MD5_Update(&context, text, text_len); /* then text of datagram */
    106       1.1  christos     MD5_Final(digest, &context);          /* finish up 1st pass */
    107       1.1  christos 
    108       1.1  christos     /*
    109       1.1  christos      * perform outer MD5
    110       1.1  christos      */
    111       1.1  christos     MD5_Init(&context);                   /* init context for 2nd pass */
    112       1.1  christos     MD5_Update(&context, k_opad, 64);     /* start with outer pad */
    113       1.1  christos     MD5_Update(&context, digest, 16);     /* then results of 1st hash */
    114       1.1  christos     MD5_Final(digest, &context);          /* finish up 2nd pass */
    115       1.1  christos }
    116  1.2.20.1       snj USES_APPLE_RST
    117       1.1  christos 
    118       1.1  christos /*
    119       1.1  christos  * Verify a cryptographic signature of the packet.
    120       1.1  christos  * Currently only MD5 is supported.
    121       1.1  christos  */
    122       1.1  christos int
    123  1.2.20.1       snj signature_verify(netdissect_options *ndo, const u_char *pptr, u_int plen,
    124  1.2.20.1       snj                  const u_char *sig_ptr, void (*clear_rtn)(void *),
    125  1.2.20.1       snj                  const void *clear_arg)
    126       1.1  christos {
    127  1.2.20.1       snj     uint8_t *packet_copy, *sig_copy;
    128  1.2.20.1       snj     uint8_t sig[16];
    129       1.1  christos     unsigned int i;
    130       1.1  christos 
    131  1.2.20.1       snj     if (!ndo->ndo_sigsecret) {
    132  1.2.20.1       snj         return (CANT_CHECK_SIGNATURE);
    133  1.2.20.1       snj     }
    134  1.2.20.1       snj 
    135       1.1  christos     /*
    136  1.2.20.1       snj      * Do we have all the packet data to be checked?
    137       1.1  christos      */
    138  1.2.20.1       snj     if (!ND_TTEST2(pptr, plen)) {
    139  1.2.20.1       snj         /* No. */
    140  1.2.20.1       snj         return (CANT_CHECK_SIGNATURE);
    141  1.2.20.1       snj     }
    142       1.1  christos 
    143  1.2.20.1       snj     /*
    144  1.2.20.1       snj      * Do we have the entire signature to check?
    145  1.2.20.1       snj      */
    146  1.2.20.1       snj     if (!ND_TTEST2(sig_ptr, sizeof(sig))) {
    147  1.2.20.1       snj         /* No. */
    148  1.2.20.1       snj         return (CANT_CHECK_SIGNATURE);
    149  1.2.20.1       snj     }
    150  1.2.20.1       snj     if (sig_ptr + sizeof(sig) > pptr + plen) {
    151  1.2.20.1       snj         /* No. */
    152       1.1  christos         return (CANT_CHECK_SIGNATURE);
    153       1.1  christos     }
    154       1.1  christos 
    155  1.2.20.1       snj     /*
    156  1.2.20.1       snj      * Make a copy of the packet, so we don't overwrite the original.
    157  1.2.20.1       snj      */
    158  1.2.20.1       snj     packet_copy = malloc(plen);
    159  1.2.20.1       snj     if (packet_copy == NULL) {
    160  1.2.20.1       snj         return (CANT_ALLOCATE_COPY);
    161  1.2.20.1       snj     }
    162       1.1  christos 
    163  1.2.20.1       snj     memcpy(packet_copy, pptr, plen);
    164       1.1  christos 
    165  1.2.20.1       snj     /*
    166  1.2.20.1       snj      * Clear the signature in the copy.
    167  1.2.20.1       snj      */
    168  1.2.20.1       snj     sig_copy = packet_copy + (sig_ptr - pptr);
    169  1.2.20.1       snj     memset(sig_copy, 0, sizeof(sig));
    170  1.2.20.1       snj 
    171  1.2.20.1       snj     /*
    172  1.2.20.1       snj      * Clear anything else that needs to be cleared in the copy.
    173  1.2.20.1       snj      * Our caller is assumed to have vetted the clear_arg pointer.
    174  1.2.20.1       snj      */
    175  1.2.20.1       snj     (*clear_rtn)((void *)(packet_copy + ((const uint8_t *)clear_arg - pptr)));
    176       1.1  christos 
    177  1.2.20.1       snj     /*
    178  1.2.20.1       snj      * Compute the signature.
    179  1.2.20.1       snj      */
    180  1.2.20.1       snj     signature_compute_hmac_md5(packet_copy, plen,
    181  1.2.20.1       snj                                (unsigned char *)ndo->ndo_sigsecret,
    182  1.2.20.1       snj                                strlen(ndo->ndo_sigsecret), sig);
    183  1.2.20.1       snj 
    184  1.2.20.1       snj     /*
    185  1.2.20.1       snj      * Free the copy.
    186  1.2.20.1       snj      */
    187  1.2.20.1       snj     free(packet_copy);
    188  1.2.20.1       snj 
    189  1.2.20.1       snj     /*
    190  1.2.20.1       snj      * Does the computed signature match the signature in the packet?
    191  1.2.20.1       snj      */
    192  1.2.20.1       snj     if (memcmp(sig_ptr, sig, sizeof(sig)) == 0) {
    193  1.2.20.1       snj         /* Yes. */
    194  1.2.20.1       snj         return (SIGNATURE_VALID);
    195  1.2.20.1       snj     } else {
    196  1.2.20.1       snj         /* No - print the computed signature. */
    197       1.1  christos         for (i = 0; i < sizeof(sig); ++i) {
    198  1.2.20.1       snj             ND_PRINT((ndo, "%02x", sig[i]));
    199       1.1  christos         }
    200       1.1  christos 
    201       1.1  christos         return (SIGNATURE_INVALID);
    202       1.1  christos     }
    203       1.1  christos }
    204  1.2.20.1       snj #else
    205  1.2.20.1       snj int
    206  1.2.20.1       snj signature_verify(netdissect_options *ndo _U_, const u_char *pptr _U_,
    207  1.2.20.1       snj                  u_int plen _U_, const u_char *sig_ptr _U_,
    208  1.2.20.1       snj                  void (*clear_rtn)(void *) _U_, const void *clear_arg _U_)
    209  1.2.20.1       snj {
    210  1.2.20.1       snj     return (CANT_CHECK_SIGNATURE);
    211  1.2.20.1       snj }
    212       1.1  christos #endif
    213       1.1  christos 
    214       1.1  christos /*
    215       1.1  christos  * Local Variables:
    216       1.1  christos  * c-style: whitesmith
    217       1.1  christos  * c-basic-offset: 4
    218       1.1  christos  * End:
    219       1.1  christos  */
    220