moved typedefs to beginning of files to solve some include problems

splitted authenticator to have a separate implementation for each auth_method_t
using va_copy to clone va_lists, should fix proplems on AMD64
some other cleanups
This commit is contained in:
Martin Willi
2006-10-30 14:07:05 +00:00
parent 09cb5472bc
commit 382b481795
119 changed files with 1196 additions and 1120 deletions
@@ -0,0 +1,53 @@
/**
* @file authenticator.c
*
* @brief Generic constructor for authenticators.
*
*/
/*
* Copyright (C) 2006 Martin Willi
* 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.
*/
#include <string.h>
#include "authenticator.h"
#include <sa/authenticators/rsa_authenticator.h>
#include <sa/authenticators/psk_authenticator.h>
ENUM_BEGIN(auth_method_names, AUTH_RSA, AUTH_DSS,
"RSA signature",
"pre-shared key",
"DSS signature");
ENUM_NEXT(auth_method_names, AUTH_EAP, AUTH_EAP, AUTH_DSS,
"EAP");
ENUM_END(auth_method_names, AUTH_EAP);
/*
* Described in header.
*/
authenticator_t *authenticator_create(ike_sa_t *ike_sa, auth_method_t auth_method)
{
switch (auth_method)
{
case AUTH_RSA:
return (authenticator_t*)rsa_authenticator_create(ike_sa);
case AUTH_PSK:
return (authenticator_t*)psk_authenticator_create(ike_sa);
default:
return NULL;
}
}
@@ -0,0 +1,139 @@
/**
* @file authenticator.h
*
* @brief Interface of authenticator_t.
*
*/
/*
* Copyright (C) 2005-2006 Martin Willi
* Copyright (C) 2005 Jan Hutter
* 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.
*/
#ifndef AUTHENTICATOR_H_
#define AUTHENTICATOR_H_
typedef enum auth_method_t auth_method_t;
typedef struct authenticator_t authenticator_t;
#include <types.h>
#include <sa/ike_sa.h>
#include <encoding/payloads/auth_payload.h>
/**
* Method to use for authentication.
*
* @ingroup authenticator
*/
enum auth_method_t {
/**
* Computed as specified in section 2.15 of RFC using
* an RSA private key over a PKCS#1 padded hash.
*/
AUTH_RSA = 1,
/**
* Computed as specified in section 2.15 of RFC using the
* shared key associated with the identity in the ID payload
* and the negotiated prf function
*/
AUTH_PSK = 2,
/**
* Computed as specified in section 2.15 of RFC using a
* DSS private key over a SHA-1 hash.
*/
AUTH_DSS = 3,
/**
* EAP authentication. This value is never negotiated and therefore
* a value from private use.
*/
AUTH_EAP = 201,
};
/**
* enum names for auth_method_t.
*
* @ingroup authenticator
*/
extern enum_name_t *auth_method_names;
/**
* @brief Authenticator interface implemented by the various authenticators.
*
* Currently the following two AUTH methods are supported:
* - shared key message integrity code (AUTH_PSK)
* - RSA digital signature (AUTH_RSA)
*
* @b Constructors:
* - authenticator_create()
*
* @ingroup authenticator
*/
struct authenticator_t {
/**
* @brief Verify a received authentication payload.
*
* @param this calling object
* @param ike_sa_init binary representation of received ike_sa_init
* @param my_nonce the sent nonce
* @param auth_payload authentication payload to verify
*
* @return
* - SUCCESS,
* - FAILED if verification failed
* - INVALID_ARG if auth_method does not match
* - NOT_FOUND if credentials not found
*/
status_t (*verify) (authenticator_t *this, chunk_t ike_sa_init,
chunk_t my_nonce, auth_payload_t *auth_payload);
/**
* @brief Build an authentication payload to send to the other peer.
*
* @param this calling object
* @param ike_sa_init binary representation of sent ike_sa_init
* @param other_nonce the received nonce
* @param[out] auth_payload the resulting authentication payload
*
* @return
* - SUCCESS,
* - NOT_FOUND if the data for AUTH method could not be found
*/
status_t (*build) (authenticator_t *this, chunk_t ike_sa_init,
chunk_t other_nonce, auth_payload_t **auth_payload);
/**
* @brief Destroys a authenticator_t object.
*
* @param this calling object
*/
void (*destroy) (authenticator_t *this);
};
/**
* @brief Creates an authenticator for the specified auth method.
*
* @param ike_sa associated ike_sa
* @param auth_method authentication method to use for build()/verify()
*
* @return authenticator_t object
*
* @ingroup sa
*/
authenticator_t *authenticator_create(ike_sa_t *ike_sa, auth_method_t auth_method);
#endif /* AUTHENTICATOR_H_ */
@@ -0,0 +1,193 @@
/**
* @file authenticator.c
*
* @brief Implementation of authenticator_t.
*
*/
/*
* Copyright (C) 2005-2006 Martin Willi
* Copyright (C) 2005 Jan Hutter
* 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.
*/
#include <string.h>
#include "psk_authenticator.h"
#include <config/policies/policy.h>
#include <daemon.h>
/**
* Key pad for the AUTH method SHARED_KEY_MESSAGE_INTEGRITY_CODE.
*/
#define IKEV2_KEY_PAD "Key Pad for IKEv2"
#define IKEV2_KEY_PAD_LENGTH 17
typedef struct private_psk_authenticator_t private_psk_authenticator_t;
/**
* Private data of an psk_authenticator_t object.
*/
struct private_psk_authenticator_t {
/**
* Public authenticator_t interface.
*/
psk_authenticator_t public;
/**
* Assigned IKE_SA
*/
ike_sa_t *ike_sa;
};
/**
* Function implemented in rsa_authenticator.c
*/
extern chunk_t build_tbs_octets(private_psk_authenticator_t *this, chunk_t ike_sa_init,
chunk_t nonce, identification_t *id, prf_t *prf);
/**
* Creates the AUTH data using auth method SHARED_KEY_MESSAGE_INTEGRITY_CODE.
*/
static chunk_t build_shared_key_signature(private_psk_authenticator_t *this,
chunk_t ike_sa_init,
chunk_t nonce,
chunk_t secret,
identification_t *id,
prf_t *prf)
{
chunk_t key_pad, key, auth_data, octets;
octets = build_tbs_octets(this, ike_sa_init, nonce, id, prf);
/* AUTH = prf(prf(Shared Secret,"Key Pad for IKEv2"), <msg octets>) */
key_pad.ptr = IKEV2_KEY_PAD;
key_pad.len = IKEV2_KEY_PAD_LENGTH;
prf->set_key(prf, secret);
prf->allocate_bytes(prf, key_pad, &key);
prf->set_key(prf, key);
prf->allocate_bytes(prf, octets, &auth_data);
DBG3(DBG_IKE, "octets = message + nonce + prf(Sk_px, IDx') %B", &octets);
DBG3(DBG_IKE, "secret %B", &secret);
DBG3(DBG_IKE, "keypad %B", &key_pad);
DBG3(DBG_IKE, "prf(secret, keypad) %B", &key);
DBG3(DBG_IKE, "AUTH = prf(prf(secret, keypad), octets) %B", &auth_data);
chunk_free(&octets);
chunk_free(&key);
return auth_data;
}
/**
* Implementation of authenticator_t.verify.
*/
static status_t verify(private_psk_authenticator_t *this, chunk_t ike_sa_init,
chunk_t my_nonce, auth_payload_t *auth_payload)
{
status_t status;
chunk_t auth_data, recv_auth_data, shared_key;
identification_t *my_id, *other_id;
my_id = this->ike_sa->get_my_id(this->ike_sa);
other_id = this->ike_sa->get_other_id(this->ike_sa);
status = charon->credentials->get_shared_key(charon->credentials, my_id,
other_id, &shared_key);
if (status != SUCCESS)
{
DBG1(DBG_IKE, "no shared key found for '%D' - '%D'", my_id, other_id);
return status;
}
auth_data = build_shared_key_signature(this, ike_sa_init, my_nonce,
shared_key, other_id,
this->ike_sa->get_auth_verify(this->ike_sa));
chunk_free(&shared_key);
recv_auth_data = auth_payload->get_data(auth_payload);
if (auth_data.len != recv_auth_data.len ||
!memeq(auth_data.ptr, recv_auth_data.ptr, auth_data.len))
{
DBG1(DBG_IKE, "PSK MAC verification failed");
chunk_free(&auth_data);
return FAILED;
}
chunk_free(&auth_data);
DBG1(DBG_IKE, "authentication of '%D' with %N successful",
other_id, auth_method_names, AUTH_PSK);
return SUCCESS;
}
/**
* Implementation of authenticator_t.build.
*/
static status_t build(private_psk_authenticator_t *this, chunk_t ike_sa_init,
chunk_t other_nonce, auth_payload_t **auth_payload)
{
chunk_t shared_key;
chunk_t auth_data;
status_t status;
identification_t *my_id, *other_id;
my_id = this->ike_sa->get_my_id(this->ike_sa);
other_id = this->ike_sa->get_other_id(this->ike_sa);
DBG1(DBG_IKE, "authentication of '%D' (myself) with %N",
my_id, auth_method_names, AUTH_PSK);
status = charon->credentials->get_shared_key(charon->credentials, my_id,
other_id, &shared_key);
if (status != SUCCESS)
{
DBG1(DBG_IKE, "no shared key found for '%D' - '%D'", my_id, other_id);
return status;
}
auth_data = build_shared_key_signature(this, ike_sa_init,
other_nonce, shared_key, my_id,
this->ike_sa->get_auth_build(this->ike_sa));
DBG2(DBG_IKE, "successfully created shared key MAC");
chunk_free(&shared_key);
*auth_payload = auth_payload_create();
(*auth_payload)->set_auth_method(*auth_payload, AUTH_PSK);
(*auth_payload)->set_data(*auth_payload, auth_data);
chunk_free(&auth_data);
return SUCCESS;
}
/**
* Implementation of authenticator_t.destroy.
*/
static void destroy(private_psk_authenticator_t *this)
{
free(this);
}
/*
* Described in header.
*/
psk_authenticator_t *psk_authenticator_create(ike_sa_t *ike_sa)
{
private_psk_authenticator_t *this = malloc_thing(private_psk_authenticator_t);
/* public functions */
this->public.authenticator_interface.verify = (status_t(*)(authenticator_t*,chunk_t,chunk_t,auth_payload_t*))verify;
this->public.authenticator_interface.build = (status_t(*)(authenticator_t*,chunk_t,chunk_t,auth_payload_t**))build;
this->public.authenticator_interface.destroy = (void(*)(authenticator_t*))destroy;
/* private data */
this->ike_sa = ike_sa;
return &this->public;
}
@@ -0,0 +1,57 @@
/**
* @file psk_authenticator.h
*
* @brief Interface of psk_authenticator_t.
*
*/
/*
* Copyright (C) 2006 Martin Willi
* 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.
*/
#ifndef PSK_AUTHENTICATOR_H_
#define PSK_AUTHENTICATOR_H_
typedef struct psk_authenticator_t psk_authenticator_t;
#include <sa/authenticators/authenticator.h>
/**
* @brief Implementation of the authenticator_t interface using AUTH_PSK.
*
* @b Constructors:
* - psk_authenticator_create()
* - authenticator_create() using auth_method AUTH_PSK
*
* @ingroup authenticator
*/
struct psk_authenticator_t {
/**
* Implemented authenticator_t interface.
*/
authenticator_t authenticator_interface;
};
/**
* @brief Creates an authenticator for AUTH_PSK.
*
* @param ike_sa associated ike_sa
* @return psk_authenticator_t object
*
* @ingroup authenticator
*/
psk_authenticator_t *psk_authenticator_create(ike_sa_t *ike_sa);
#endif /* PSK_AUTHENTICATOR_H_ */
@@ -0,0 +1,194 @@
/**
* @file authenticator.c
*
* @brief Implementation of authenticator_t.
*
*/
/*
* Copyright (C) 2005-2006 Martin Willi
* Copyright (C) 2005 Jan Hutter
* 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.
*/
#include <string.h>
#include "rsa_authenticator.h"
#include <config/policies/policy.h>
#include <daemon.h>
typedef struct private_rsa_authenticator_t private_rsa_authenticator_t;
/**
* Private data of an rsa_authenticator_t object.
*/
struct private_rsa_authenticator_t {
/**
* Public authenticator_t interface.
*/
rsa_authenticator_t public;
/**
* Assigned IKE_SA
*/
ike_sa_t *ike_sa;
};
/**
* Builds the octets to be signed as described in section 2.15 of RFC 4306
*/
chunk_t build_tbs_octets(private_rsa_authenticator_t *this, chunk_t ike_sa_init,
chunk_t nonce, identification_t *id, prf_t *prf)
{
u_int8_t id_header_buf[] = {0x00, 0x00, 0x00, 0x00};
chunk_t id_header = chunk_from_buf(id_header_buf);
chunk_t id_with_header, id_prfd, id_encoding;
id_header_buf[0] = id->get_type(id);
id_encoding = id->get_encoding(id);
id_with_header = chunk_cat("cc", id_header, id_encoding);
prf->allocate_bytes(prf, id_with_header, &id_prfd);
chunk_free(&id_with_header);
return chunk_cat("ccm", ike_sa_init, nonce, id_prfd);
}
/**
* Implementation of authenticator_t.verify.
*/
static status_t verify(private_rsa_authenticator_t *this, chunk_t ike_sa_init,
chunk_t my_nonce, auth_payload_t *auth_payload)
{
status_t status;
chunk_t auth_data, octets;
rsa_public_key_t *public_key;
identification_t *other_id;
other_id = this->ike_sa->get_other_id(this->ike_sa);
if (auth_payload->get_auth_method(auth_payload) != AUTH_RSA)
{
return INVALID_ARG;
}
auth_data = auth_payload->get_data(auth_payload);
public_key = charon->credentials->get_trusted_public_key(charon->credentials,
other_id);
if (public_key == NULL)
{
DBG1(DBG_IKE, "no RSA public key found for '%D'", other_id);
return NOT_FOUND;
}
octets = build_tbs_octets(this, ike_sa_init, my_nonce, other_id,
this->ike_sa->get_auth_verify(this->ike_sa));
status = public_key->verify_emsa_pkcs1_signature(public_key, octets, auth_data);
chunk_free(&octets);
if (status != SUCCESS)
{
DBG1(DBG_IKE, "RSA signature verification failed");
return status;
}
DBG1(DBG_IKE, "authentication of '%D' with %N successful",
other_id, auth_method_names, AUTH_RSA);
return SUCCESS;
}
/**
* Implementation of authenticator_t.build.
*/
static status_t build(private_rsa_authenticator_t *this, chunk_t ike_sa_init,
chunk_t other_nonce, auth_payload_t **auth_payload)
{
chunk_t chunk;
chunk_t octets;
chunk_t auth_data;
status_t status;
rsa_public_key_t *my_pubkey;
rsa_private_key_t *my_key;
identification_t *my_id;
my_id = this->ike_sa->get_my_id(this->ike_sa);
DBG1(DBG_IKE, "authentication of '%D' with %N (myself)",
my_id, auth_method_names, AUTH_RSA);
DBG2(DBG_IKE, "looking for RSA public key belonging to '%D'", my_id);
my_pubkey = charon->credentials->get_rsa_public_key(charon->credentials, my_id);
if (my_pubkey == NULL)
{
DBG1(DBG_IKE, "no RSA public key found for '%D'", my_id);
return NOT_FOUND;
}
DBG2(DBG_IKE, "matching RSA public key found");
chunk = my_pubkey->get_keyid(my_pubkey);
DBG2(DBG_IKE, "looking for RSA private key with keyid %#B", &chunk);
my_key = charon->credentials->get_rsa_private_key(charon->credentials, my_pubkey);
if (my_key == NULL)
{
DBG1(DBG_IKE, "no RSA private key found with for %D with keyid %#B",
my_id, &chunk);
return NOT_FOUND;
}
DBG2(DBG_IKE, "matching RSA private key found");
octets = build_tbs_octets(this, ike_sa_init, other_nonce, my_id,
this->ike_sa->get_auth_build(this->ike_sa));
status = my_key->build_emsa_pkcs1_signature(my_key, HASH_SHA1, octets, &auth_data);
chunk_free(&octets);
if (status != SUCCESS)
{
my_key->destroy(my_key);
DBG1(DBG_IKE, "build signature of SHA1 hash failed");
return status;
}
DBG2(DBG_IKE, "successfully signed with RSA private key");
*auth_payload = auth_payload_create();
(*auth_payload)->set_auth_method(*auth_payload, AUTH_RSA);
(*auth_payload)->set_data(*auth_payload, auth_data);
my_key->destroy(my_key);
chunk_free(&auth_data);
return SUCCESS;
}
/**
* Implementation of authenticator_t.destroy.
*/
static void destroy(private_rsa_authenticator_t *this)
{
free(this);
}
/*
* Described in header.
*/
rsa_authenticator_t *rsa_authenticator_create(ike_sa_t *ike_sa)
{
private_rsa_authenticator_t *this = malloc_thing(private_rsa_authenticator_t);
/* public functions */
this->public.authenticator_interface.verify = (status_t(*)(authenticator_t*,chunk_t,chunk_t,auth_payload_t*))verify;
this->public.authenticator_interface.build = (status_t(*)(authenticator_t*,chunk_t,chunk_t,auth_payload_t**))build;
this->public.authenticator_interface.destroy = (void(*)(authenticator_t*))destroy;
/* private data */
this->ike_sa = ike_sa;
return &this->public;
}
@@ -0,0 +1,57 @@
/**
* @file rsa_authenticator.h
*
* @brief Interface of rsa_authenticator_t.
*
*/
/*
* Copyright (C) 2006 Martin Willi
* 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.
*/
#ifndef RSA_AUTHENTICATOR_H_
#define RSA_AUTHENTICATOR_H_
typedef struct rsa_authenticator_t rsa_authenticator_t;
#include <sa/authenticators/authenticator.h>
/**
* @brief Implementation of the authenticator_t interface using AUTH_RSA.
*
* @b Constructors:
* - rsa_authenticator_create()
* - authenticator_create() using auth_method AUTH_RSA
*
* @ingroup authenticator
*/
struct rsa_authenticator_t {
/**
* Implemented authenticator_t interface.
*/
authenticator_t authenticator_interface;
};
/**
* @brief Creates an authenticator for AUTH_RSA.
*
* @param ike_sa associated ike_sa
* @return rsa_authenticator_t object
*
* @ingroup authenticator
*/
rsa_authenticator_t *rsa_authenticator_create(ike_sa_t *ike_sa);
#endif /* RSA_AUTHENTICATOR_H_ */