2005-11-28 15:53:35 +00:00
|
|
|
/*
|
2006-07-07 08:49:06 +00:00
|
|
|
* Copyright (C) 2005-2006 Martin Willi
|
|
|
|
* Copyright (C) 2005 Jan Hutter
|
2005-11-28 15:53:35 +00:00
|
|
|
* Hochschule fuer Technik Rapperswil
|
|
|
|
*
|
|
|
|
* This program 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. See <http://www.fsf.org/copyleft/gpl.txt>.
|
|
|
|
*
|
|
|
|
* This program 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.
|
|
|
|
*/
|
2006-04-10 08:07:38 +00:00
|
|
|
|
2005-11-28 15:53:35 +00:00
|
|
|
#include <stddef.h>
|
2006-04-10 08:07:38 +00:00
|
|
|
#include <string.h>
|
2005-11-28 15:53:35 +00:00
|
|
|
|
|
|
|
#include "encryption_payload.h"
|
|
|
|
|
2005-12-02 12:38:55 +00:00
|
|
|
#include <daemon.h>
|
2005-11-28 15:53:35 +00:00
|
|
|
#include <encoding/payloads/encodings.h>
|
|
|
|
#include <utils/linked_list.h>
|
|
|
|
#include <encoding/generator.h>
|
|
|
|
#include <encoding/parser.h>
|
|
|
|
#include <utils/iterator.h>
|
2006-04-05 12:10:50 +00:00
|
|
|
#include <crypto/signers/signer.h>
|
2005-11-28 15:53:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
typedef struct private_encryption_payload_t private_encryption_payload_t;
|
|
|
|
|
|
|
|
/**
|
2005-11-28 18:24:10 +00:00
|
|
|
* Private data of an encryption_payload_t' Object.
|
2009-09-04 11:46:09 +00:00
|
|
|
*
|
2005-11-28 15:53:35 +00:00
|
|
|
*/
|
|
|
|
struct private_encryption_payload_t {
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2005-11-28 15:53:35 +00:00
|
|
|
/**
|
2005-11-28 18:24:10 +00:00
|
|
|
* Public encryption_payload_t interface.
|
2005-11-28 15:53:35 +00:00
|
|
|
*/
|
|
|
|
encryption_payload_t public;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2005-11-28 15:53:35 +00:00
|
|
|
/**
|
2009-09-04 11:46:09 +00:00
|
|
|
* There is no next payload for an encryption payload,
|
2005-11-28 15:53:35 +00:00
|
|
|
* since encryption payload MUST be the last one.
|
2009-09-04 11:46:09 +00:00
|
|
|
* next_payload means here the first payload of the
|
2005-11-28 15:53:35 +00:00
|
|
|
* contained, encrypted payload.
|
|
|
|
*/
|
|
|
|
u_int8_t next_payload;
|
|
|
|
|
|
|
|
/**
|
2005-11-28 18:24:10 +00:00
|
|
|
* Critical flag.
|
2005-11-28 15:53:35 +00:00
|
|
|
*/
|
|
|
|
bool critical;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2005-11-28 15:53:35 +00:00
|
|
|
/**
|
|
|
|
* Length of this payload
|
|
|
|
*/
|
|
|
|
u_int16_t payload_length;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2005-11-28 15:53:35 +00:00
|
|
|
/**
|
2005-11-28 18:24:10 +00:00
|
|
|
* Chunk containing the iv, data, padding,
|
|
|
|
* and (an eventually not calculated) signature.
|
2005-11-28 15:53:35 +00:00
|
|
|
*/
|
|
|
|
chunk_t encrypted;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2005-11-28 15:53:35 +00:00
|
|
|
/**
|
2005-11-28 18:24:10 +00:00
|
|
|
* Chunk containing the data in decrypted (unpadded) form.
|
2005-11-28 15:53:35 +00:00
|
|
|
*/
|
|
|
|
chunk_t decrypted;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2005-11-28 15:53:35 +00:00
|
|
|
/**
|
2005-11-28 18:24:10 +00:00
|
|
|
* Signer set by set_signer.
|
2005-11-28 15:53:35 +00:00
|
|
|
*/
|
|
|
|
signer_t *signer;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2005-11-29 15:58:09 +00:00
|
|
|
/**
|
|
|
|
* Crypter, supplied by encrypt/decrypt
|
|
|
|
*/
|
|
|
|
crypter_t *crypter;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2005-11-28 15:53:35 +00:00
|
|
|
/**
|
2005-11-28 18:24:10 +00:00
|
|
|
* Contained payloads of this encrpytion_payload.
|
2005-11-28 15:53:35 +00:00
|
|
|
*/
|
|
|
|
linked_list_t *payloads;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2005-11-28 18:24:10 +00:00
|
|
|
* Encoding rules to parse or generate a IKEv2-Encryption Payload.
|
2009-09-04 11:46:09 +00:00
|
|
|
*
|
|
|
|
* The defined offsets are the positions in a object of type
|
2005-11-28 15:53:35 +00:00
|
|
|
* private_encryption_payload_t.
|
|
|
|
*/
|
|
|
|
encoding_rule_t encryption_payload_encodings[] = {
|
|
|
|
/* 1 Byte next payload type, stored in the field next_payload */
|
2010-03-03 16:35:19 +00:00
|
|
|
{ U_INT_8, offsetof(private_encryption_payload_t, next_payload) },
|
2005-11-28 15:53:35 +00:00
|
|
|
/* the critical bit */
|
2010-03-03 16:35:19 +00:00
|
|
|
{ FLAG, offsetof(private_encryption_payload_t, critical) },
|
2005-11-28 15:53:35 +00:00
|
|
|
/* 7 Bit reserved bits, nowhere stored */
|
2010-03-03 16:35:19 +00:00
|
|
|
{ RESERVED_BIT, 0 },
|
|
|
|
{ RESERVED_BIT, 0 },
|
|
|
|
{ RESERVED_BIT, 0 },
|
|
|
|
{ RESERVED_BIT, 0 },
|
|
|
|
{ RESERVED_BIT, 0 },
|
|
|
|
{ RESERVED_BIT, 0 },
|
|
|
|
{ RESERVED_BIT, 0 },
|
2005-11-28 15:53:35 +00:00
|
|
|
/* Length of the whole encryption payload*/
|
2010-03-03 16:35:19 +00:00
|
|
|
{ PAYLOAD_LENGTH, offsetof(private_encryption_payload_t, payload_length) },
|
2005-11-28 15:53:35 +00:00
|
|
|
/* encrypted data, stored in a chunk. contains iv, data, padding */
|
2010-08-17 10:05:51 +00:00
|
|
|
{ ENCRYPTED_DATA, offsetof(private_encryption_payload_t, encrypted) },
|
2005-11-28 15:53:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
1 2 3
|
|
|
|
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
|
|
|
|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
! Next Payload !C! RESERVED ! Payload Length !
|
|
|
|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
! Initialization Vector !
|
|
|
|
! (length is block size for encryption algorithm) !
|
|
|
|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
! Encrypted IKE Payloads !
|
|
|
|
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
! ! Padding (0-255 octets) !
|
|
|
|
+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+
|
|
|
|
! ! Pad Length !
|
|
|
|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
~ Integrity Checksum Data ~
|
|
|
|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
*/
|
|
|
|
|
2010-08-17 10:05:51 +00:00
|
|
|
METHOD(payload_t, verify, status_t,
|
|
|
|
private_encryption_payload_t *this)
|
2005-11-28 15:53:35 +00:00
|
|
|
{
|
2005-11-30 09:11:48 +00:00
|
|
|
return SUCCESS;
|
2005-11-28 15:53:35 +00:00
|
|
|
}
|
|
|
|
|
2010-08-17 10:05:51 +00:00
|
|
|
METHOD(payload_t, get_encoding_rules, void,
|
|
|
|
private_encryption_payload_t *this, encoding_rule_t **rules,
|
|
|
|
size_t *count)
|
2005-11-28 15:53:35 +00:00
|
|
|
{
|
|
|
|
*rules = encryption_payload_encodings;
|
2010-08-17 10:05:51 +00:00
|
|
|
*count = countof(encryption_payload_encodings);
|
2005-11-28 15:53:35 +00:00
|
|
|
}
|
|
|
|
|
2010-08-17 10:05:51 +00:00
|
|
|
METHOD(payload_t, get_type, payload_type_t,
|
|
|
|
private_encryption_payload_t *this)
|
2005-11-28 15:53:35 +00:00
|
|
|
{
|
|
|
|
return ENCRYPTED;
|
|
|
|
}
|
|
|
|
|
2010-08-17 10:05:51 +00:00
|
|
|
METHOD(payload_t, get_next_type, payload_type_t,
|
|
|
|
private_encryption_payload_t *this)
|
2005-11-28 15:53:35 +00:00
|
|
|
{
|
2010-08-17 10:05:51 +00:00
|
|
|
return this->next_payload;
|
2005-11-28 15:53:35 +00:00
|
|
|
}
|
|
|
|
|
2010-08-17 10:05:51 +00:00
|
|
|
METHOD(payload_t, set_next_type, void,
|
|
|
|
private_encryption_payload_t *this, payload_type_t type)
|
2005-11-28 15:53:35 +00:00
|
|
|
{
|
2010-08-17 10:05:51 +00:00
|
|
|
/* the next payload is set during add */
|
2005-11-28 15:53:35 +00:00
|
|
|
}
|
|
|
|
|
2006-10-18 11:46:13 +00:00
|
|
|
/**
|
2010-08-17 10:05:51 +00:00
|
|
|
* Compute the lenght of the whole payload
|
2006-10-18 11:46:13 +00:00
|
|
|
*/
|
|
|
|
static void compute_length(private_encryption_payload_t *this)
|
|
|
|
{
|
2010-08-17 10:05:51 +00:00
|
|
|
enumerator_t *enumerator;
|
|
|
|
payload_t *payload;
|
2006-10-18 11:46:13 +00:00
|
|
|
size_t block_size, length = 0;
|
|
|
|
|
2010-08-17 10:05:51 +00:00
|
|
|
enumerator = this->payloads->create_enumerator(this->payloads);
|
|
|
|
while (enumerator->enumerate(enumerator, &payload))
|
2006-10-18 11:46:13 +00:00
|
|
|
{
|
2010-08-17 10:05:51 +00:00
|
|
|
length += payload->get_length(payload);
|
2006-10-18 11:46:13 +00:00
|
|
|
}
|
2010-08-17 10:05:51 +00:00
|
|
|
enumerator->destroy(enumerator);
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2006-10-18 11:46:13 +00:00
|
|
|
if (this->crypter && this->signer)
|
|
|
|
{
|
|
|
|
/* append one byte for padding length */
|
|
|
|
length++;
|
|
|
|
/* append padding */
|
|
|
|
block_size = this->crypter->get_block_size(this->crypter);
|
|
|
|
length += block_size - length % block_size;
|
|
|
|
/* add iv */
|
2010-08-13 10:20:25 +00:00
|
|
|
length += this->crypter->get_iv_size(this->crypter);
|
2006-10-18 11:46:13 +00:00
|
|
|
/* add signature */
|
|
|
|
length += this->signer->get_block_size(this->signer);
|
|
|
|
}
|
|
|
|
length += ENCRYPTION_PAYLOAD_HEADER_LENGTH;
|
|
|
|
this->payload_length = length;
|
|
|
|
}
|
|
|
|
|
2010-08-17 10:05:51 +00:00
|
|
|
METHOD(payload_t, get_length, size_t,
|
|
|
|
private_encryption_payload_t *this)
|
2005-11-28 15:53:35 +00:00
|
|
|
{
|
2006-10-18 11:46:13 +00:00
|
|
|
compute_length(this);
|
2005-11-28 15:53:35 +00:00
|
|
|
return this->payload_length;
|
|
|
|
}
|
|
|
|
|
2010-08-17 10:05:51 +00:00
|
|
|
METHOD(encryption_payload_t, create_payload_iterator, iterator_t*,
|
|
|
|
private_encryption_payload_t *this, bool forward)
|
2005-11-28 15:53:35 +00:00
|
|
|
{
|
2010-08-17 10:05:51 +00:00
|
|
|
return this->payloads->create_iterator(this->payloads, forward);
|
2005-11-28 15:53:35 +00:00
|
|
|
}
|
|
|
|
|
2010-08-17 10:05:51 +00:00
|
|
|
METHOD(encryption_payload_t, add_payload, void,
|
|
|
|
private_encryption_payload_t *this, payload_t *payload)
|
2005-11-28 15:53:35 +00:00
|
|
|
{
|
|
|
|
payload_t *last_payload;
|
2010-08-17 10:05:51 +00:00
|
|
|
|
2005-11-28 18:24:10 +00:00
|
|
|
if (this->payloads->get_count(this->payloads) > 0)
|
2005-11-28 15:53:35 +00:00
|
|
|
{
|
2010-08-17 10:05:51 +00:00
|
|
|
this->payloads->get_last(this->payloads, (void **)&last_payload);
|
2005-11-29 15:58:09 +00:00
|
|
|
last_payload->set_next_type(last_payload, payload->get_type(payload));
|
2005-11-28 15:53:35 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-11-29 15:58:09 +00:00
|
|
|
this->next_payload = payload->get_type(payload);
|
2005-11-28 15:53:35 +00:00
|
|
|
}
|
|
|
|
payload->set_next_type(payload, NO_PAYLOAD);
|
2010-08-17 10:05:51 +00:00
|
|
|
this->payloads->insert_last(this->payloads, payload);
|
2006-10-18 11:46:13 +00:00
|
|
|
compute_length(this);
|
2005-11-28 15:53:35 +00:00
|
|
|
}
|
|
|
|
|
2010-08-17 10:05:51 +00:00
|
|
|
METHOD(encryption_payload_t, remove_first_payload, status_t,
|
|
|
|
private_encryption_payload_t *this, payload_t **payload)
|
2005-11-30 10:23:15 +00:00
|
|
|
{
|
|
|
|
return this->payloads->remove_first(this->payloads, (void**)payload);
|
|
|
|
}
|
|
|
|
|
2010-08-17 10:05:51 +00:00
|
|
|
METHOD(encryption_payload_t, get_payload_count, size_t,
|
|
|
|
private_encryption_payload_t *this)
|
2005-11-30 10:23:15 +00:00
|
|
|
{
|
|
|
|
return this->payloads->get_count(this->payloads);
|
|
|
|
}
|
|
|
|
|
2006-10-18 11:46:13 +00:00
|
|
|
/**
|
|
|
|
* Generate payload before encryption.
|
|
|
|
*/
|
|
|
|
static void generate(private_encryption_payload_t *this)
|
|
|
|
{
|
2010-08-17 10:05:51 +00:00
|
|
|
payload_t *current, *next;
|
2006-10-18 11:46:13 +00:00
|
|
|
generator_t *generator;
|
2010-08-17 10:05:51 +00:00
|
|
|
enumerator_t *enumerator;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2006-10-18 11:46:13 +00:00
|
|
|
compute_length(this);
|
2010-08-17 10:05:51 +00:00
|
|
|
chunk_free(&this->decrypted);
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2010-08-17 10:05:51 +00:00
|
|
|
enumerator = this->payloads->create_enumerator(this->payloads);
|
|
|
|
if (enumerator->enumerate(enumerator, ¤t))
|
2006-10-18 11:46:13 +00:00
|
|
|
{
|
2010-08-17 10:05:51 +00:00
|
|
|
this->next_payload = current->get_type(current);
|
|
|
|
|
|
|
|
generator = generator_create();
|
|
|
|
while (enumerator->enumerate(enumerator, &next))
|
|
|
|
{
|
|
|
|
current->set_next_type(current, next->get_type(next));
|
|
|
|
generator->generate_payload(generator, current);
|
|
|
|
current = next;
|
|
|
|
}
|
|
|
|
enumerator->destroy(enumerator);
|
|
|
|
current->set_next_type(current, NO_PAYLOAD);
|
|
|
|
generator->generate_payload(generator, current);
|
|
|
|
|
|
|
|
generator->write_to_chunk(generator, &this->decrypted);
|
|
|
|
generator->destroy(generator);
|
|
|
|
DBG2(DBG_ENC, "generated content in encryption payload");
|
2006-10-18 11:46:13 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-10-26 09:46:56 +00:00
|
|
|
DBG2(DBG_ENC, "generating contained payloads, but none available");
|
2006-10-18 11:46:13 +00:00
|
|
|
}
|
2010-08-17 10:05:51 +00:00
|
|
|
enumerator->destroy(enumerator);
|
2006-10-18 11:46:13 +00:00
|
|
|
}
|
2005-11-30 10:23:15 +00:00
|
|
|
|
2010-08-17 10:05:51 +00:00
|
|
|
METHOD(encryption_payload_t, encrypt, status_t,
|
|
|
|
private_encryption_payload_t *this)
|
2005-11-28 15:53:35 +00:00
|
|
|
{
|
2005-11-29 15:58:09 +00:00
|
|
|
chunk_t iv, padding, to_crypt, result;
|
2008-04-15 05:56:35 +00:00
|
|
|
rng_t *rng;
|
2005-11-29 15:58:09 +00:00
|
|
|
size_t block_size;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2005-11-29 15:58:09 +00:00
|
|
|
if (this->signer == NULL || this->crypter == NULL)
|
2005-11-28 15:53:35 +00:00
|
|
|
{
|
2006-10-26 09:46:56 +00:00
|
|
|
DBG1(DBG_ENC, "could not encrypt, signer/crypter not set");
|
2005-11-28 15:53:35 +00:00
|
|
|
return INVALID_STATE;
|
|
|
|
}
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2005-11-28 15:53:35 +00:00
|
|
|
/* for random data in iv and padding */
|
2008-04-15 05:56:35 +00:00
|
|
|
rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK);
|
|
|
|
if (!rng)
|
|
|
|
{
|
|
|
|
DBG1(DBG_ENC, "could not encrypt, no RNG found");
|
|
|
|
return FAILED;
|
|
|
|
}
|
2005-11-28 15:53:35 +00:00
|
|
|
/* build payload chunk */
|
2006-10-18 11:46:13 +00:00
|
|
|
generate(this);
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2006-10-26 09:46:56 +00:00
|
|
|
DBG2(DBG_ENC, "encrypting payloads");
|
|
|
|
DBG3(DBG_ENC, "data to encrypt %B", &this->decrypted);
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2005-11-28 15:53:35 +00:00
|
|
|
/* build padding */
|
2005-11-29 15:58:09 +00:00
|
|
|
block_size = this->crypter->get_block_size(this->crypter);
|
|
|
|
padding.len = block_size - ((this->decrypted.len + 1) % block_size);
|
2008-04-15 05:56:35 +00:00
|
|
|
rng->allocate_bytes(rng, padding.len, &padding);
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2005-11-28 15:53:35 +00:00
|
|
|
/* concatenate payload data, padding, padding len */
|
2005-11-29 15:58:09 +00:00
|
|
|
to_crypt.len = this->decrypted.len + padding.len + 1;
|
2006-04-10 08:07:38 +00:00
|
|
|
to_crypt.ptr = malloc(to_crypt.len);
|
2005-11-28 15:53:35 +00:00
|
|
|
|
2005-11-29 15:58:09 +00:00
|
|
|
memcpy(to_crypt.ptr, this->decrypted.ptr, this->decrypted.len);
|
|
|
|
memcpy(to_crypt.ptr + this->decrypted.len, padding.ptr, padding.len);
|
|
|
|
*(to_crypt.ptr + to_crypt.len - 1) = padding.len;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2005-11-28 15:53:35 +00:00
|
|
|
/* build iv */
|
2010-08-13 10:20:25 +00:00
|
|
|
iv.len = this->crypter->get_iv_size(this->crypter);
|
2008-04-15 05:56:35 +00:00
|
|
|
rng->allocate_bytes(rng, iv.len, &iv);
|
|
|
|
rng->destroy(rng);
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2006-10-26 09:46:56 +00:00
|
|
|
DBG3(DBG_ENC, "data before encryption with padding %B", &to_crypt);
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2005-11-29 15:58:09 +00:00
|
|
|
/* encrypt to_crypt chunk */
|
2006-04-10 08:07:38 +00:00
|
|
|
free(this->encrypted.ptr);
|
2008-04-22 07:14:24 +00:00
|
|
|
this->crypter->encrypt(this->crypter, to_crypt, iv, &result);
|
2006-04-10 08:07:38 +00:00
|
|
|
free(padding.ptr);
|
|
|
|
free(to_crypt.ptr);
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2006-10-26 09:46:56 +00:00
|
|
|
DBG3(DBG_ENC, "data after encryption %B", &result);
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2005-11-29 15:58:09 +00:00
|
|
|
/* build encrypted result with iv and signature */
|
|
|
|
this->encrypted.len = iv.len + result.len + this->signer->get_block_size(this->signer);
|
2006-04-10 08:07:38 +00:00
|
|
|
free(this->encrypted.ptr);
|
|
|
|
this->encrypted.ptr = malloc(this->encrypted.len);
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2005-11-29 15:58:09 +00:00
|
|
|
/* fill in result, signature is left out */
|
|
|
|
memcpy(this->encrypted.ptr, iv.ptr, iv.len);
|
|
|
|
memcpy(this->encrypted.ptr + iv.len, result.ptr, result.len);
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2006-04-10 08:07:38 +00:00
|
|
|
free(result.ptr);
|
|
|
|
free(iv.ptr);
|
2006-10-26 09:46:56 +00:00
|
|
|
DBG3(DBG_ENC, "data after encryption with IV and (invalid) signature %B",
|
2006-10-18 11:46:13 +00:00
|
|
|
&this->encrypted);
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2005-11-28 15:53:35 +00:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
2006-10-18 11:46:13 +00:00
|
|
|
/**
|
|
|
|
* Parse the payloads after decryption.
|
|
|
|
*/
|
|
|
|
static status_t parse(private_encryption_payload_t *this)
|
|
|
|
{
|
|
|
|
parser_t *parser;
|
|
|
|
status_t status;
|
2010-08-17 10:05:51 +00:00
|
|
|
payload_type_t type;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2006-10-18 11:46:13 +00:00
|
|
|
parser = parser_create(this->decrypted);
|
2010-08-17 10:05:51 +00:00
|
|
|
type = this->next_payload;
|
|
|
|
while (type != NO_PAYLOAD)
|
2006-10-18 11:46:13 +00:00
|
|
|
{
|
2010-08-17 10:05:51 +00:00
|
|
|
payload_t *payload;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2010-08-17 10:05:51 +00:00
|
|
|
status = parser->parse_payload(parser, type, &payload);
|
2006-10-18 11:46:13 +00:00
|
|
|
if (status != SUCCESS)
|
|
|
|
{
|
|
|
|
parser->destroy(parser);
|
|
|
|
return PARSE_ERROR;
|
|
|
|
}
|
2010-08-17 10:05:51 +00:00
|
|
|
status = payload->verify(payload);
|
2006-10-18 11:46:13 +00:00
|
|
|
if (status != SUCCESS)
|
|
|
|
{
|
2006-10-26 09:46:56 +00:00
|
|
|
DBG1(DBG_ENC, "%N verification failed",
|
2010-08-17 10:05:51 +00:00
|
|
|
payload_type_names, payload->get_type(payload));
|
|
|
|
payload->destroy(payload);
|
2006-10-18 11:46:13 +00:00
|
|
|
parser->destroy(parser);
|
|
|
|
return VERIFY_ERROR;
|
|
|
|
}
|
2010-08-17 10:05:51 +00:00
|
|
|
type = payload->get_next_type(payload);
|
|
|
|
this->payloads->insert_last(this->payloads, payload);
|
2006-10-18 11:46:13 +00:00
|
|
|
}
|
|
|
|
parser->destroy(parser);
|
2010-08-17 10:05:51 +00:00
|
|
|
DBG2(DBG_ENC, "parsed content of encryption payload");
|
2006-10-18 11:46:13 +00:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
2010-08-17 10:05:51 +00:00
|
|
|
METHOD(encryption_payload_t, decrypt, status_t,
|
|
|
|
private_encryption_payload_t *this)
|
2005-11-28 15:53:35 +00:00
|
|
|
{
|
|
|
|
chunk_t iv, concatenated;
|
|
|
|
u_int8_t padding_length;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2006-10-26 09:46:56 +00:00
|
|
|
DBG2(DBG_ENC, "decrypting encryption payload");
|
|
|
|
DBG3(DBG_ENC, "data before decryption with IV and (invalid) signature %B",
|
2006-10-18 11:46:13 +00:00
|
|
|
&this->encrypted);
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2005-11-29 15:58:09 +00:00
|
|
|
if (this->signer == NULL || this->crypter == NULL)
|
2005-11-28 15:53:35 +00:00
|
|
|
{
|
2006-10-26 09:46:56 +00:00
|
|
|
DBG1(DBG_ENC, "could not decrypt, no crypter/signer set");
|
2005-11-28 15:53:35 +00:00
|
|
|
return INVALID_STATE;
|
|
|
|
}
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2005-11-28 15:53:35 +00:00
|
|
|
/* get IV */
|
2010-08-13 10:20:25 +00:00
|
|
|
iv.len = this->crypter->get_iv_size(this->crypter);
|
|
|
|
if (iv.len > this->encrypted.len)
|
|
|
|
{
|
|
|
|
DBG1(DBG_ENC, "could not decrypt, input too short");
|
|
|
|
return FAILED;
|
|
|
|
}
|
2005-11-28 15:53:35 +00:00
|
|
|
iv.ptr = this->encrypted.ptr;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2010-08-13 10:20:25 +00:00
|
|
|
/* point concatenated to data + padding + padding_length */
|
2005-11-28 15:53:35 +00:00
|
|
|
concatenated.ptr = this->encrypted.ptr + iv.len;
|
2008-04-22 07:14:24 +00:00
|
|
|
concatenated.len = this->encrypted.len - iv.len -
|
|
|
|
this->signer->get_block_size(this->signer);
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-04-22 07:14:24 +00:00
|
|
|
/* concatenated must be a multiple of block_size of crypter */
|
2010-08-13 10:20:25 +00:00
|
|
|
if (concatenated.len < iv.len ||
|
|
|
|
concatenated.len % this->crypter->get_block_size(this->crypter))
|
2005-11-28 15:53:35 +00:00
|
|
|
{
|
2006-10-26 09:46:56 +00:00
|
|
|
DBG1(DBG_ENC, "could not decrypt, invalid input");
|
2005-11-28 15:53:35 +00:00
|
|
|
return FAILED;
|
|
|
|
}
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2005-11-28 15:53:35 +00:00
|
|
|
/* free previus data, if any */
|
2006-04-10 08:07:38 +00:00
|
|
|
free(this->decrypted.ptr);
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2006-10-26 09:46:56 +00:00
|
|
|
DBG3(DBG_ENC, "data before decryption %B", &concatenated);
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2008-04-22 07:14:24 +00:00
|
|
|
this->crypter->decrypt(this->crypter, concatenated, iv, &this->decrypted);
|
|
|
|
|
2006-10-26 09:46:56 +00:00
|
|
|
DBG3(DBG_ENC, "data after decryption with padding %B", &this->decrypted);
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2005-11-28 15:53:35 +00:00
|
|
|
/* get padding length, sits just bevore signature */
|
|
|
|
padding_length = *(this->decrypted.ptr + this->decrypted.len - 1);
|
2009-09-04 11:46:09 +00:00
|
|
|
/* add one byte to the padding length, since the padding_length field is
|
2008-04-22 07:14:24 +00:00
|
|
|
* not included */
|
2005-11-29 15:58:09 +00:00
|
|
|
padding_length++;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2005-11-28 15:53:35 +00:00
|
|
|
/* check size again */
|
2010-03-03 16:35:19 +00:00
|
|
|
if (padding_length > concatenated.len || padding_length > this->decrypted.len)
|
2005-11-28 15:53:35 +00:00
|
|
|
{
|
2006-10-26 09:46:56 +00:00
|
|
|
DBG1(DBG_ENC, "decryption failed, invalid padding length found. Invalid key?");
|
2005-11-28 15:53:35 +00:00
|
|
|
/* decryption failed :-/ */
|
|
|
|
return FAILED;
|
|
|
|
}
|
2010-03-03 16:35:19 +00:00
|
|
|
this->decrypted.len -= padding_length;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2005-11-28 15:53:35 +00:00
|
|
|
/* free padding */
|
2006-04-10 08:07:38 +00:00
|
|
|
this->decrypted.ptr = realloc(this->decrypted.ptr, this->decrypted.len);
|
2006-10-26 09:46:56 +00:00
|
|
|
DBG3(DBG_ENC, "data after decryption without padding %B", &this->decrypted);
|
|
|
|
DBG2(DBG_ENC, "decryption successful, trying to parse content");
|
2006-10-18 11:46:13 +00:00
|
|
|
return parse(this);
|
2005-11-28 15:53:35 +00:00
|
|
|
}
|
|
|
|
|
2010-08-17 10:05:51 +00:00
|
|
|
METHOD(encryption_payload_t, set_transforms, void,
|
|
|
|
private_encryption_payload_t *this, crypter_t* crypter, signer_t* signer)
|
2005-11-28 15:53:35 +00:00
|
|
|
{
|
|
|
|
this->signer = signer;
|
2005-11-29 15:58:09 +00:00
|
|
|
this->crypter = crypter;
|
2005-11-28 15:53:35 +00:00
|
|
|
}
|
|
|
|
|
2010-08-17 10:05:51 +00:00
|
|
|
METHOD(encryption_payload_t, build_signature, status_t,
|
|
|
|
private_encryption_payload_t *this, chunk_t data)
|
2005-11-28 15:53:35 +00:00
|
|
|
{
|
|
|
|
chunk_t data_without_sig = data;
|
|
|
|
chunk_t sig;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2005-11-28 15:53:35 +00:00
|
|
|
if (this->signer == NULL)
|
|
|
|
{
|
2006-10-26 09:46:56 +00:00
|
|
|
DBG1(DBG_ENC, "unable to build signature, no signer set");
|
2005-11-28 15:53:35 +00:00
|
|
|
return INVALID_STATE;
|
|
|
|
}
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2005-11-28 15:53:35 +00:00
|
|
|
sig.len = this->signer->get_block_size(this->signer);
|
|
|
|
data_without_sig.len -= sig.len;
|
|
|
|
sig.ptr = data.ptr + data_without_sig.len;
|
2006-10-26 09:46:56 +00:00
|
|
|
DBG2(DBG_ENC, "building signature");
|
2005-11-28 15:53:35 +00:00
|
|
|
this->signer->get_signature(this->signer, data_without_sig, sig.ptr);
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
2010-08-17 10:05:51 +00:00
|
|
|
METHOD(encryption_payload_t, verify_signature, status_t,
|
|
|
|
private_encryption_payload_t *this, chunk_t data)
|
2005-11-28 15:53:35 +00:00
|
|
|
{
|
|
|
|
chunk_t sig, data_without_sig;
|
|
|
|
bool valid;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2005-11-28 15:53:35 +00:00
|
|
|
if (this->signer == NULL)
|
|
|
|
{
|
2006-10-26 09:46:56 +00:00
|
|
|
DBG1(DBG_ENC, "unable to verify signature, no signer set");
|
2005-11-28 15:53:35 +00:00
|
|
|
return INVALID_STATE;
|
|
|
|
}
|
|
|
|
/* find signature in data chunk */
|
|
|
|
sig.len = this->signer->get_block_size(this->signer);
|
|
|
|
if (data.len <= sig.len)
|
|
|
|
{
|
2006-10-26 09:46:56 +00:00
|
|
|
DBG1(DBG_ENC, "unable to verify signature, invalid input");
|
2005-11-28 15:53:35 +00:00
|
|
|
return FAILED;
|
|
|
|
}
|
|
|
|
sig.ptr = data.ptr + data.len - sig.len;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2005-11-28 15:53:35 +00:00
|
|
|
/* verify it */
|
|
|
|
data_without_sig.len = data.len - sig.len;
|
|
|
|
data_without_sig.ptr = data.ptr;
|
2005-12-06 16:04:39 +00:00
|
|
|
valid = this->signer->verify_signature(this->signer, data_without_sig, sig);
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2005-11-28 15:53:35 +00:00
|
|
|
if (!valid)
|
|
|
|
{
|
2006-10-26 09:46:56 +00:00
|
|
|
DBG1(DBG_ENC, "signature verification failed");
|
2005-11-28 15:53:35 +00:00
|
|
|
return FAILED;
|
|
|
|
}
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2006-10-26 09:46:56 +00:00
|
|
|
DBG2(DBG_ENC, "signature verification successful");
|
2005-11-28 15:53:35 +00:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
2010-08-17 10:05:51 +00:00
|
|
|
METHOD2(payload_t, encryption_payload_t, destroy, void,
|
|
|
|
private_encryption_payload_t *this)
|
2005-12-02 12:38:55 +00:00
|
|
|
{
|
2006-10-24 08:46:17 +00:00
|
|
|
this->payloads->destroy_offset(this->payloads, offsetof(payload_t, destroy));
|
2006-04-10 08:07:38 +00:00
|
|
|
free(this->encrypted.ptr);
|
|
|
|
free(this->decrypted.ptr);
|
|
|
|
free(this);
|
2005-12-02 12:38:55 +00:00
|
|
|
}
|
|
|
|
|
2005-11-28 15:53:35 +00:00
|
|
|
/*
|
|
|
|
* Described in header
|
|
|
|
*/
|
|
|
|
encryption_payload_t *encryption_payload_create()
|
|
|
|
{
|
2010-08-17 10:05:51 +00:00
|
|
|
private_encryption_payload_t *this;
|
|
|
|
|
|
|
|
INIT(this,
|
|
|
|
.public = {
|
|
|
|
.payload_interface = {
|
|
|
|
.verify = _verify,
|
|
|
|
.get_encoding_rules = _get_encoding_rules,
|
|
|
|
.get_length = _get_length,
|
|
|
|
.get_next_type = _get_next_type,
|
|
|
|
.set_next_type = _set_next_type,
|
|
|
|
.get_type = _get_type,
|
|
|
|
.destroy = _destroy,
|
|
|
|
},
|
|
|
|
.create_payload_iterator = _create_payload_iterator,
|
|
|
|
.add_payload = _add_payload,
|
|
|
|
.remove_first_payload = _remove_first_payload,
|
|
|
|
.get_payload_count = _get_payload_count,
|
|
|
|
.encrypt = _encrypt,
|
|
|
|
.decrypt = _decrypt,
|
|
|
|
.set_transforms = _set_transforms,
|
|
|
|
.build_signature = _build_signature,
|
|
|
|
.verify_signature = _verify_signature,
|
|
|
|
.destroy = _destroy,
|
|
|
|
},
|
|
|
|
.next_payload = NO_PAYLOAD,
|
|
|
|
.payload_length = ENCRYPTION_PAYLOAD_HEADER_LENGTH,
|
|
|
|
.payloads = linked_list_create(),
|
|
|
|
);
|
|
|
|
|
|
|
|
return &this->public;
|
2005-11-28 15:53:35 +00:00
|
|
|
}
|