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 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
|
/*
* Copyright (C) 2000,2001 Nikos Mavroyanopoulos
*
* This file is part of GNUTLS.
*
* GNUTLS is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* GNUTLS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
#include "gnutls_int.h"
#include "gnutls_errors.h"
#include "gnutls_compress.h"
#include "gnutls_cipher.h"
#include "gnutls_algorithms.h"
#include "gnutls_hash_int.h"
#include "gnutls_cipher_int.h"
#include "debug.h"
#include "gnutls_random.h"
#include "gnutls_num.h"
#include "gnutls_datum.h"
#include "gnutls_kx.h"
#include "gnutls_record.h"
#include "gnutls_constate.h"
/* returns ciphertext which contains the headers too. This also
* calculates the size in the header field.
*/
int _gnutls_encrypt(GNUTLS_STATE state, const char* headers, int headers_size,
const char *data, size_t data_size,
uint8 ** ciphertext, ContentType type)
{
gnutls_datum plain = { (char*)data, data_size };
gnutls_datum comp, ciph;
int err;
if (data_size == 0)
return 0;
err = _gnutls_plaintext2TLSCompressed(state, &comp, plain);
if (err < 0) {
gnutls_assert();
return err;
}
err = _gnutls_compressed2TLSCiphertext(state, &ciph, comp, type, headers_size);
if (err < 0) {
gnutls_assert();
return err;
}
gnutls_free_datum(&comp);
/* copy the headers */
memcpy( ciph.data, headers, headers_size);
WRITEuint16( ciph.size - headers_size, &ciph.data[3]);
*ciphertext = ciph.data;
return ciph.size;
}
int _gnutls_decrypt(GNUTLS_STATE state, char *ciphertext,
size_t ciphertext_size, uint8 ** data,
ContentType type)
{
gnutls_datum gtxt;
gnutls_datum gcomp;
gnutls_datum gcipher;
int ret;
if (ciphertext_size == 0)
return 0;
gcipher.size = ciphertext_size;
gcipher.data = ciphertext;
ret = _gnutls_ciphertext2TLSCompressed(state, &gcomp, gcipher, type);
if (ret < 0) {
return ret;
}
ret = _gnutls_TLSCompressed2plaintext(state, >xt, gcomp);
if (ret < 0) {
gnutls_free_datum(&gcomp);
return ret;
}
gnutls_free_datum(&gcomp);
ret = gtxt.size;
*data = gtxt.data;
return ret;
}
/* This is the actual encryption
* (and also keeps some space for headers (RECORD_HEADER_SIZE) in the
* encrypted data)
*/
int _gnutls_compressed2TLSCiphertext(GNUTLS_STATE state,
gnutls_datum*
cipher,
gnutls_datum compressed, ContentType _type, int headers_size)
{
uint8 MAC[MAX_HASH_SIZE];
uint16 c_length;
uint8 *data;
uint8 pad;
uint8 rand;
uint64 seq_num;
int length,ret;
GNUTLS_MAC_HANDLE td;
uint8 type = _type;
uint8 major, minor;
int hash_size = _gnutls_mac_get_digest_size(state->security_parameters.write_mac_algorithm);
int blocksize =
_gnutls_cipher_get_block_size(state->security_parameters.
write_bulk_cipher_algorithm);
minor = _gnutls_version_get_minor(state->security_parameters.version);
major = _gnutls_version_get_major(state->security_parameters.version);
if ( state->security_parameters.version == GNUTLS_SSL3) { /* SSL 3.0 */
td =
gnutls_mac_init_ssl3(state->security_parameters.
write_mac_algorithm,
state->connection_state.
write_mac_secret.data,
state->connection_state.
write_mac_secret.size);
} else { /* TLS 1 */
td =
gnutls_hmac_init(state->security_parameters.
write_mac_algorithm,
state->connection_state.
write_mac_secret.data,
state->connection_state.
write_mac_secret.size);
}
if (td == GNUTLS_MAC_FAILED
&& state->security_parameters.write_mac_algorithm != GNUTLS_MAC_NULL) {
gnutls_assert();
return GNUTLS_E_UNKNOWN_MAC_ALGORITHM;
}
c_length = CONVuint16(compressed.size);
seq_num =
CONVuint64(&state->connection_state.write_sequence_number);
if (td != GNUTLS_MAC_FAILED) { /* actually when the algorithm in not the NULL one */
gnutls_hmac(td, UINT64DATA(seq_num), 8);
gnutls_hmac(td, &type, 1);
if ( state->security_parameters.version != GNUTLS_SSL3) { /* TLS 1.0 only */
gnutls_hmac(td, &major, 1);
gnutls_hmac(td, &minor, 1);
}
gnutls_hmac(td, &c_length, 2);
gnutls_hmac(td, compressed.data, compressed.size);
if ( state->security_parameters.version == GNUTLS_SSL3) { /* SSL 3.0 */
gnutls_mac_deinit_ssl3(td, MAC);
} else {
gnutls_hmac_deinit(td, MAC);
}
}
switch (_gnutls_cipher_is_block(state->security_parameters.write_bulk_cipher_algorithm)) {
case CIPHER_STREAM:
length =
compressed.size + hash_size;
data = gnutls_malloc(length+headers_size);
if (data==NULL) {
gnutls_assert();
return GNUTLS_E_MEMORY_ERROR;
}
break;
case CIPHER_BLOCK:
if (_gnutls_get_random(&rand, 1, GNUTLS_WEAK_RANDOM) < 0) {
gnutls_assert();
return GNUTLS_E_MEMORY_ERROR;
}
/* make rand a multiple of blocksize */
if ( state->security_parameters.version == GNUTLS_SSL3) {
rand = 0;
} else {
rand = (rand / blocksize) * blocksize;
/* added to avoid the case of pad calculated 0
* seen below for pad calculation.
*/
if (rand > blocksize) rand-=blocksize;
}
length =
compressed.size +
hash_size;
pad = (uint8) (blocksize - (length % blocksize)) + rand;
length += pad;
data = gnutls_malloc(length+headers_size);
if (data==NULL) {
gnutls_assert();
return GNUTLS_E_MEMORY_ERROR;
}
memset(&data[headers_size + length - pad], pad - 1, pad);
break;
default:
gnutls_assert();
return GNUTLS_E_UNKNOWN_CIPHER_TYPE;
}
memcpy(&data[headers_size], compressed.data, compressed.size);
memcpy(&data[compressed.size+headers_size], MAC, hash_size);
if ( (ret = gnutls_cipher_encrypt(state->connection_state.
write_cipher_state, &data[headers_size],
length)) < 0) {
gnutls_free( data);
return ret;
}
cipher->data = data;
cipher->size = length + headers_size;
return 0;
}
int _gnutls_ciphertext2TLSCompressed(GNUTLS_STATE state,
gnutls_datum *
compress,
gnutls_datum ciphertext, uint8 type)
{
uint8 MAC[MAX_HASH_SIZE];
uint16 c_length;
uint8 *data;
uint8 pad;
uint64 seq_num;
uint16 length;
GNUTLS_MAC_HANDLE td;
int blocksize, ret;
uint8 major, minor;
int hash_size = _gnutls_mac_get_digest_size(state->security_parameters.read_mac_algorithm);
minor = _gnutls_version_get_minor(state->security_parameters.version);
major = _gnutls_version_get_major(state->security_parameters.version);
blocksize = _gnutls_cipher_get_block_size(state->security_parameters.
read_bulk_cipher_algorithm);
if ( state->security_parameters.version == GNUTLS_SSL3) {
td =
gnutls_mac_init_ssl3(state->security_parameters.
read_mac_algorithm,
state->connection_state.
read_mac_secret.data,
state->connection_state.
read_mac_secret.size);
} else {
td =
gnutls_hmac_init(state->security_parameters.
read_mac_algorithm,
state->connection_state.
read_mac_secret.data,
state->connection_state.
read_mac_secret.size);
}
if (td == GNUTLS_MAC_FAILED
&& state->security_parameters.read_mac_algorithm != GNUTLS_MAC_NULL) {
gnutls_assert();
return GNUTLS_E_UNKNOWN_MAC_ALGORITHM;
}
switch (_gnutls_cipher_is_block(state->security_parameters.read_bulk_cipher_algorithm)) {
case CIPHER_STREAM:
if ( (ret = gnutls_cipher_decrypt(state->connection_state.
read_cipher_state, ciphertext.data,
ciphertext.size)) < 0) {
gnutls_assert();
return ret;
}
length =
ciphertext.size - hash_size;
break;
case CIPHER_BLOCK:
if ((ciphertext.size < blocksize)
|| (ciphertext.size % blocksize != 0)) {
gnutls_assert();
return GNUTLS_E_DECRYPTION_FAILED;
}
if ( (ret = gnutls_cipher_decrypt(state->connection_state.
read_cipher_state, ciphertext.data,
ciphertext.size)) < 0) {
gnutls_assert();
return ret;
}
pad = ciphertext.data[ciphertext.size - 1] + 1; /* pad */
length =
ciphertext.size - hash_size - pad;
if (pad >
ciphertext.size - hash_size) {
gnutls_assert();
return GNUTLS_E_RECEIVED_BAD_MESSAGE;
}
break;
default:
gnutls_assert();
return GNUTLS_E_UNKNOWN_CIPHER_TYPE;
}
data = gnutls_malloc(length);
if (data==NULL) {
gnutls_assert();
return GNUTLS_E_MEMORY_ERROR;
}
memcpy(data, ciphertext.data, length);
compress->data = data;
compress->size = length;
c_length = CONVuint16((uint16) compress->size);
seq_num = CONVuint64( &state->connection_state.read_sequence_number);
if (td != GNUTLS_MAC_FAILED) {
gnutls_hmac(td, UINT64DATA(seq_num), 8);
gnutls_hmac(td, &type, 1);
if ( state->security_parameters.version != GNUTLS_SSL3) { /* TLS 1.0 only */
gnutls_hmac(td, &major, 1);
gnutls_hmac(td, &minor, 1);
}
gnutls_hmac(td, &c_length, 2);
gnutls_hmac(td, data, compress->size);
if ( state->security_parameters.version == GNUTLS_SSL3) { /* SSL 3.0 */
gnutls_mac_deinit_ssl3(td, MAC);
} else {
gnutls_hmac_deinit(td, MAC);
}
}
/* HMAC was not the same. */
if (memcmp
(MAC, &ciphertext.data[compress->size], hash_size) != 0) {
gnutls_assert();
return GNUTLS_E_MAC_FAILED;
}
return 0;
}
|