File: ssl_rec.c

package info (click to toggle)
ssldump 0.9b3-6
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,196 kB
  • ctags: 1,379
  • sloc: ansic: 6,151; sh: 1,508; yacc: 183; makefile: 90; lex: 67
file content (319 lines) | stat: -rw-r--r-- 7,152 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/**
   ssl_rec.c


   Copyright (C) 1999-2000 RTFM, Inc.
   All Rights Reserved

   This package is a SSLv3/TLS protocol analyzer written by Eric Rescorla
   <ekr@rtfm.com> and licensed by RTFM, Inc.

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions
   are met:
   1. Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
   2. Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
   3. All advertising materials mentioning features or use of this software
      must display the following acknowledgement:
   
      This product includes software developed by Eric Rescorla for
      RTFM, Inc.

   4. Neither the name of RTFM, Inc. nor the name of Eric Rescorla may be
      used to endorse or promote products derived from this
      software without specific prior written permission.

   THIS SOFTWARE IS PROVIDED BY ERIC RESCORLA AND RTFM, INC. ``AS IS'' AND
   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY SUCH DAMAGE.

   $Id: ssl_rec.c,v 1.3 2000/11/03 06:38:06 ekr Exp $


   ekr@rtfm.com  Wed Aug 18 15:46:57 1999
 */


static char *RCSSTRING="$Id: ssl_rec.c,v 1.3 2000/11/03 06:38:06 ekr Exp $";

#include "network.h"
#include "ssl_h.h"
#include "sslprint.h"
#include "ssl.enums.h"
#ifdef OPENSSL
#include <openssl/ssl.h>
#include <openssl/hmac.h>
#include <openssl/evp.h>
#endif
#include "ssldecode.h"
#include "ssl_rec.h"

struct ssl_rec_decoder_ {
     SSL_CipherSuite *cs;
     Data *mac_key;
#ifdef OPENSSL     
     EVP_CIPHER_CTX *evp;
#endif     
     UINT4 seq;
};


static char *digests[]={
     "MD5",
     "SHA1"
};

static char *ciphers[]={
     "DES",
     "DES3",
     "RC4",
     "RC2",
     "IDEA",
     "AES128",
     "AES256"
};


static int tls_check_mac PROTO_LIST((ssl_rec_decoder *d,int ct,
  int ver,UCHAR *data,UINT4 datalen,UCHAR *mac));
static int fmt_seq PROTO_LIST((UINT4 num,UCHAR *buf));

int ssl_create_rec_decoder(dp,cs,mk,sk,iv)
  ssl_rec_decoder **dp;
  SSL_CipherSuite *cs;
  UCHAR *mk;
  UCHAR *sk;
  UCHAR *iv;
  {
    int r,_status;
    ssl_rec_decoder *dec=0;
#ifdef OPENSSL
    const EVP_CIPHER *ciph=0;

    /* Find the SSLeay cipher */
    if(cs->enc!=ENC_NULL){
      ciph=(EVP_CIPHER *)EVP_get_cipherbyname(ciphers[cs->enc-0x30]);
      if(!ciph)
        ABORT(R_INTERNAL);
    }
    else {
      ciph=EVP_enc_null();
    }

    if(!(dec=(ssl_rec_decoder *)calloc(sizeof(ssl_rec_decoder),1)))
      ABORT(R_NO_MEMORY);

    dec->cs=cs;
    if(r=r_data_create(&dec->mac_key,mk,cs->dig_len))
      ABORT(r);
    if(!(dec->evp=(EVP_CIPHER_CTX *)malloc(sizeof(EVP_CIPHER_CTX))))
      ABORT(R_NO_MEMORY);
    EVP_CIPHER_CTX_init(dec->evp);
    EVP_CipherInit(dec->evp,ciph,sk,iv,0);
#endif
    
    *dp=dec;
    _status=0;
  abort:
    if(_status){
      ssl_destroy_rec_decoder(&dec);
    }
    return(_status);
  }

int ssl_destroy_rec_decoder(dp)
  ssl_rec_decoder **dp;
  {
    ssl_rec_decoder *d;
    
    if(!dp || !*dp)
      return(0);
    d=*dp;

    r_data_destroy(&d->mac_key);
#ifdef OPENSSL    
    if(d->evp){
      EVP_CIPHER_CTX_cleanup(d->evp);
      free(d->evp);
    }
    free(*dp);
#endif    

    *dp=0;
    return(0);
  }
    
int ssl_decode_rec_data(ssl,d,ct,version,in,inl,out,outl)
  ssl_obj *ssl;
  ssl_rec_decoder *d;
  int ct;
  int version;
  UCHAR *in;
  int inl;
  UCHAR *out;
  int *outl;
  {
#ifdef OPENSSL
    int pad;
    int r;
    UCHAR *mac;
    
    CRDUMP("Ciphertext",in,inl);
    /* First decrypt*/
    EVP_Cipher(d->evp,out,in,inl);

    CRDUMP("Plaintext",out,inl);    
    *outl=inl;
    
    /* Now strip off the padding*/
    if(d->cs->block>1){
      pad=out[inl-1];
      *outl-=(pad+1);
    }

    /* And the MAC */
    *outl-=d->cs->dig_len;
    mac=out+(*outl);
    CRDUMP("Record data",out,*outl);

    /* Now check the MAC */
    if(ssl->version==0x300){
      if(r=ssl3_check_mac(d,ct,version,out,*outl,mac))
        ERETURN(r);
    }
    else{
      if(r=tls_check_mac(d,ct,version,out,*outl,mac))
        ERETURN(r);
    }
    
#endif    
    return(0);
  }
    

#define MSB(a) ((a>>8)&0xff)
#define LSB(a) (a&0xff)
#ifdef OPENSSL

/* This should go to 2^128, but we're never really going to see
   more than 2^64, so we cheat*/
static int fmt_seq(num,buf)
  UINT4 num;
  UCHAR *buf;
  {
    UINT4 netnum;
    
    memset(buf,0,8);
    netnum=htonl(num);
    memcpy(buf+4,&netnum,4);

    return(0);
  }
  
static int tls_check_mac(d,ct,ver,data,datalen,mac)
  ssl_rec_decoder *d;
  int ct;
  int ver;
  UCHAR *data;
  UINT4 datalen;
  UCHAR *mac;
  {
    HMAC_CTX hm;
    const EVP_MD *md;
    UINT4 l;
    UCHAR buf[20];
    
    md=EVP_get_digestbyname(digests[d->cs->dig-0x40]);
    HMAC_Init(&hm,d->mac_key->data,d->mac_key->len,md);

    fmt_seq(d->seq,buf);
    d->seq++;
    HMAC_Update(&hm,buf,8);
    buf[0]=ct;
    HMAC_Update(&hm,buf,1);

    buf[0]=MSB(ver);
    buf[1]=LSB(ver);
    HMAC_Update(&hm,buf,2);

    buf[0]=MSB(datalen);
    buf[1]=LSB(datalen);
    HMAC_Update(&hm,buf,2);

    HMAC_Update(&hm,data,datalen);
    
    HMAC_Final(&hm,buf,&l);
    if(memcmp(mac,buf,l))
      ERETURN(SSL_BAD_MAC);

    HMAC_cleanup(&hm);
    return(0);
  }

int ssl3_check_mac(d,ct,ver,data,datalen,mac)
  ssl_rec_decoder *d;
  int ct;
  int ver;
  UCHAR *data;
  UINT4 datalen;
  UCHAR *mac;
  {
    EVP_MD_CTX mc;
    const EVP_MD *md;
    UINT4 l;
    UCHAR buf[64],dgst[20];
    int pad_ct;

    pad_ct=(d->cs->dig==DIG_SHA)?40:48;
    
    md=EVP_get_digestbyname(digests[d->cs->dig-0x40]);
    EVP_DigestInit(&mc,md);

    EVP_DigestUpdate(&mc,d->mac_key->data,d->mac_key->len);

    memset(buf,0x36,pad_ct);
    EVP_DigestUpdate(&mc,buf,pad_ct);

    fmt_seq(d->seq,buf);
    d->seq++;
    EVP_DigestUpdate(&mc,buf,8);

    buf[0]=ct;
    EVP_DigestUpdate(&mc,buf,1);
    
    buf[0]=MSB(datalen);
    buf[1]=LSB(datalen);
    EVP_DigestUpdate(&mc,buf,2);    

    EVP_DigestUpdate(&mc,data,datalen);

    EVP_DigestFinal(&mc,dgst,&l);
    
    EVP_DigestInit(&mc,md);

    EVP_DigestUpdate(&mc,d->mac_key->data,d->mac_key->len);
    
    memset(buf,0x5c,pad_ct);
    EVP_DigestUpdate(&mc,buf,pad_ct);

    EVP_DigestUpdate(&mc,dgst,l);

    EVP_DigestFinal(&mc,dgst,&l);

    if(memcmp(mac,dgst,l))
      ERETURN(SSL_BAD_MAC);

    return(0);
  }
    
#endif