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