ssldump/ssl/ssl_rec.c

457 lines
10 KiB
C
Raw Permalink Normal View History

2015-01-31 09:13:33 +00:00
/**
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
*/
#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;
2018-07-04 17:17:16 +00:00
Data *implicit_iv; /* for AEAD ciphers */
Data *write_key; /* for AEAD ciphers */
2015-01-31 09:13:33 +00:00
#ifdef OPENSSL
EVP_CIPHER_CTX *evp;
#endif
UINT4 seq;
};
2015-01-31 09:18:14 +00:00
char *digests[]={
2015-01-31 09:13:33 +00:00
"MD5",
"SHA1",
2015-01-31 09:18:14 +00:00
"SHA224",
"SHA256",
"SHA384",
"SHA512",
NULL
2015-01-31 09:13:33 +00:00
};
2015-01-31 09:18:14 +00:00
char *ciphers[]={
2015-01-31 09:13:33 +00:00
"DES",
2015-01-31 09:18:14 +00:00
"3DES",
2015-01-31 09:13:33 +00:00
"RC4",
"RC2",
2015-01-31 09:15:47 +00:00
"IDEA",
"AES128",
2015-01-31 09:18:14 +00:00
"AES256",
"CAMELLIA128",
"CAMELLIA256",
"SEED",
2018-07-03 01:56:07 +00:00
NULL,
"aes-128-gcm",
"aes-256-gcm"
2015-01-31 09:13:33 +00:00
};
static int tls_check_mac PROTO_LIST((ssl_rec_decoder *d,int ct,
2018-06-25 02:08:05 +00:00
int ver,UCHAR *data,UINT4 datalen,UCHAR *iv,UINT4 ivlen,UCHAR *mac));
2015-01-31 09:13:33 +00:00
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]);
2015-01-31 09:15:47 +00:00
if(!ciph)
ABORT(R_INTERNAL);
}
else {
ciph=EVP_enc_null();
2015-01-31 09:13:33 +00:00
}
if(!(dec=(ssl_rec_decoder *)calloc(1,sizeof(ssl_rec_decoder))))
2015-01-31 09:13:33 +00:00
ABORT(R_NO_MEMORY);
dec->cs=cs;
2018-07-04 17:17:16 +00:00
if((r=r_data_alloc(&dec->mac_key,cs->dig_len)))
2018-07-04 17:17:16 +00:00
ABORT(r);
if((r=r_data_alloc(&dec->implicit_iv,cs->block)))
2018-07-04 17:17:16 +00:00
ABORT(r);
memcpy(dec->implicit_iv->data,iv,cs->block);
if((r=r_data_create(&dec->write_key,sk,cs->eff_bits/8)))
2015-01-31 09:13:33 +00:00
ABORT(r);
2018-07-04 17:17:16 +00:00
/*
This is necessary for AEAD ciphers, because we must wait to fully initialize the cipher
in order to include the implicit IV
*/
2018-07-04 20:31:29 +00:00
if(IS_AEAD_CIPHER(cs)){
2018-07-04 17:17:16 +00:00
sk=NULL;
iv=NULL;
}
else
memcpy(dec->mac_key->data,mk,cs->dig_len);
if(!(dec->evp=EVP_CIPHER_CTX_new()))
2015-01-31 09:13:33 +00:00
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);
2018-07-04 17:17:16 +00:00
r_data_destroy(&d->implicit_iv);
r_data_destroy(&d->write_key);
2015-01-31 09:13:33 +00:00
#ifdef OPENSSL
if(d->evp){
EVP_CIPHER_CTX_free(d->evp);
2015-01-31 09:13:33 +00:00
}
free(*dp);
#endif
*dp=0;
return(0);
}
2018-06-25 02:08:05 +00:00
2018-07-04 17:17:16 +00:00
#define MSB(a) ((a>>8)&0xff)
#define LSB(a) (a&0xff)
2015-01-31 09:13:33 +00:00
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;
2018-07-04 17:17:16 +00:00
int r,encpadl,x;
2020-10-06 08:20:16 +00:00
UCHAR *mac,aead_tag[13],aead_nonce[12];
2015-01-31 09:13:33 +00:00
CRDUMP("Ciphertext",in,inl);
2018-07-04 20:31:29 +00:00
if(IS_AEAD_CIPHER(d->cs)){
2018-07-04 17:17:16 +00:00
memcpy(aead_nonce,d->implicit_iv->data,d->implicit_iv->len);
memcpy(aead_nonce+d->implicit_iv->len,in,12-d->implicit_iv->len);
in+=12-d->implicit_iv->len;
inl-=12-d->implicit_iv->len;
EVP_DecryptInit(d->evp,
NULL,
d->write_key->data,
aead_nonce);
2018-07-04 19:26:20 +00:00
/*
Then tag is always 16 bytes, as per:
https://tools.ietf.org/html/rfc5116#section-5.2
*/
2018-07-04 17:17:16 +00:00
EVP_CIPHER_CTX_ctrl(d->evp,EVP_CTRL_GCM_SET_TAG,16,in+(inl-16));
2018-07-04 19:26:20 +00:00
inl-=16;
2018-07-04 17:17:16 +00:00
fmt_seq(d->seq,aead_tag);
d->seq++;
aead_tag[8]=ct;
aead_tag[9]=MSB(version);
aead_tag[10]=LSB(version);
aead_tag[11]=MSB(inl);
aead_tag[12]=LSB(inl);
EVP_DecryptUpdate(d->evp,NULL,outl,aead_tag,13);
EVP_DecryptUpdate(d->evp,out,outl,in,inl);
if (!(x=EVP_DecryptFinal(d->evp,NULL,&x)))
ERETURN(SSL_BAD_MAC);
}
/*
Encrypt-then-MAC is not used with AEAD ciphers, as per:
https://tools.ietf.org/html/rfc7366#section-3
*/
else if(ssl->extensions->encrypt_then_mac==2){
2018-06-25 02:08:05 +00:00
*outl=inl;
2015-01-31 09:13:33 +00:00
2018-06-25 02:08:05 +00:00
/* First strip off the MAC */
*outl-=d->cs->dig_len;
mac=in+(*outl);
2015-01-31 09:13:33 +00:00
2018-06-25 02:08:05 +00:00
encpadl=*outl;
/* Now decrypt */
EVP_Cipher(d->evp,out,in,*outl);
CRDUMP("Plaintext",out,*outl);
/* And then strip off the padding*/
if(d->cs->block>1){
pad=out[*outl-1];
*outl-=(pad+1);
}
2015-01-31 09:18:14 +00:00
/* TLS 1.1 and beyond: remove explicit IV, only used with
* non-stream ciphers. */
if (ssl->version>=0x0302 && ssl->cs->block > 1) {
UINT4 blk = ssl->cs->block;
if (blk <= *outl) {
2018-06-25 02:08:05 +00:00
*outl-=blk;
memmove(out, out+blk, *outl);
2015-01-31 09:18:14 +00:00
}
else {
DBG((0,"Block size greater than Plaintext!"));
ERETURN(SSL_BAD_MAC);
}
2018-06-25 02:08:05 +00:00
if((r=tls_check_mac(d,ct,version,in+blk,encpadl,in,blk,mac)))
2018-06-25 02:08:05 +00:00
ERETURN(r);
2015-01-31 09:18:14 +00:00
}
2018-06-25 02:08:05 +00:00
else
if((r=tls_check_mac(d,ct,version,in,encpadl,NULL,0,mac)))
2018-06-25 02:08:05 +00:00
ERETURN(r);
2015-01-31 09:13:33 +00:00
}
2018-06-25 02:08:05 +00:00
else {
/* First decrypt*/
EVP_Cipher(d->evp,out,in,inl);
CRDUMP("Plaintext",out,inl);
*outl=inl;
2015-01-31 09:13:33 +00:00
2018-06-25 02:08:05 +00:00
/* 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)))
2018-06-25 02:08:05 +00:00
ERETURN(r);
}
else{
/* TLS 1.1 and beyond: remove explicit IV, only used with
* non-stream ciphers. */
if (ssl->version>=0x0302 && ssl->cs->block > 1) {
UINT4 blk = ssl->cs->block;
if (blk <= *outl) {
*outl-=blk;
memmove(out, out+blk, *outl);
}
else {
DBG((0,"Block size greater than Plaintext!"));
ERETURN(SSL_BAD_MAC);
}
}
if((r=tls_check_mac(d,ct,version,out,*outl,NULL,0,mac)))
2018-06-25 02:08:05 +00:00
ERETURN(r);
}
}
2015-01-31 09:13:33 +00:00
#endif
return(0);
}
#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);
}
2018-06-25 02:08:05 +00:00
static int tls_check_mac(d,ct,ver,data,datalen,iv,ivlen,mac)
2015-01-31 09:13:33 +00:00
ssl_rec_decoder *d;
int ct;
int ver;
UCHAR *data;
UINT4 datalen;
2018-06-25 02:08:05 +00:00
UCHAR *iv;
UINT4 ivlen;
2015-01-31 09:13:33 +00:00
UCHAR *mac;
{
HMAC_CTX *hm = HMAC_CTX_new();
if(!hm)
ERETURN(R_NO_MEMORY);
2015-01-31 09:13:33 +00:00
const EVP_MD *md;
UINT4 l;
2015-01-31 09:18:14 +00:00
UCHAR buf[128];
2015-01-31 09:13:33 +00:00
md=EVP_get_digestbyname(digests[d->cs->dig-0x40]);
HMAC_Init_ex(hm,d->mac_key->data,d->mac_key->len,md,NULL);
2015-01-31 09:13:33 +00:00
fmt_seq(d->seq,buf);
d->seq++;
HMAC_Update(hm,buf,8);
2015-01-31 09:13:33 +00:00
buf[0]=ct;
HMAC_Update(hm,buf,1);
2015-01-31 09:13:33 +00:00
buf[0]=MSB(ver);
buf[1]=LSB(ver);
HMAC_Update(hm,buf,2);
2015-01-31 09:13:33 +00:00
buf[0]=MSB(datalen);
buf[1]=LSB(datalen);
HMAC_Update(hm,buf,2);
2015-01-31 09:13:33 +00:00
2018-06-25 02:08:05 +00:00
/* for encrypt-then-mac with an explicit IV */
if(ivlen && iv){
HMAC_Update(hm,iv,ivlen);
HMAC_Update(hm,data,datalen-ivlen);
2018-06-25 02:08:05 +00:00
}
else
HMAC_Update(hm,data,datalen);
2015-01-31 09:13:33 +00:00
HMAC_Final(hm,buf,&l);
2015-01-31 09:13:33 +00:00
if(memcmp(mac,buf,l))
ERETURN(SSL_BAD_MAC);
HMAC_CTX_free(hm);
2015-01-31 09:13:33 +00:00
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 = EVP_MD_CTX_new();
2015-01-31 09:13:33 +00:00
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);
2015-01-31 09:13:33 +00:00
EVP_DigestUpdate(mc,d->mac_key->data,d->mac_key->len);
2015-01-31 09:13:33 +00:00
memset(buf,0x36,pad_ct);
EVP_DigestUpdate(mc,buf,pad_ct);
2015-01-31 09:13:33 +00:00
fmt_seq(d->seq,buf);
d->seq++;
EVP_DigestUpdate(mc,buf,8);
2015-01-31 09:13:33 +00:00
buf[0]=ct;
EVP_DigestUpdate(mc,buf,1);
2015-01-31 09:13:33 +00:00
buf[0]=MSB(datalen);
buf[1]=LSB(datalen);
EVP_DigestUpdate(mc,buf,2);
2015-01-31 09:13:33 +00:00
EVP_DigestUpdate(mc,data,datalen);
2015-01-31 09:13:33 +00:00
EVP_DigestFinal(mc,dgst,&l);
2015-01-31 09:13:33 +00:00
EVP_DigestInit(mc,md);
2015-01-31 09:13:33 +00:00
EVP_DigestUpdate(mc,d->mac_key->data,d->mac_key->len);
2015-01-31 09:13:33 +00:00
memset(buf,0x5c,pad_ct);
EVP_DigestUpdate(mc,buf,pad_ct);
2015-01-31 09:13:33 +00:00
EVP_DigestUpdate(mc,dgst,l);
2015-01-31 09:13:33 +00:00
EVP_DigestFinal(mc,dgst,&l);
2015-01-31 09:13:33 +00:00
if(memcmp(mac,dgst,l))
ERETURN(SSL_BAD_MAC);
EVP_MD_CTX_free(mc);
2015-01-31 09:13:33 +00:00
return(0);
}
#endif