Implemented IKEv1 PSK HASH payload processing in separated authenticator
[strongswan.git] / src / libcharon / sa / authenticators / psk_v1_authenticator.c
1 /*
2 * Copyright (C) 2011 Martin Willi
3 * Copyright (C) 2011 revosec AG
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "psk_v1_authenticator.h"
17
18 #include <daemon.h>
19 #include <sa/keymat_v1.h>
20 #include <encoding/payloads/hash_payload.h>
21
22 typedef struct private_psk_v1_authenticator_t private_psk_v1_authenticator_t;
23
24 /**
25 * Private data of an psk_v1_authenticator_t object.
26 */
27 struct private_psk_v1_authenticator_t {
28
29 /**
30 * Public authenticator_t interface.
31 */
32 psk_v1_authenticator_t public;
33
34 /**
35 * Assigned IKE_SA
36 */
37 ike_sa_t *ike_sa;
38
39 /**
40 * TRUE if we are initiator
41 */
42 bool initiator;
43
44 /**
45 * DH key exchange
46 */
47 diffie_hellman_t *dh;
48
49 /**
50 * Others DH public value
51 */
52 chunk_t dh_value;
53
54 /**
55 * Encoded SA payload, without fixed header
56 */
57 chunk_t sa_payload;
58 };
59
60 METHOD(authenticator_t, build, status_t,
61 private_psk_v1_authenticator_t *this, message_t *message)
62 {
63 hash_payload_t *hash_payload;
64 keymat_v1_t *keymat;
65 chunk_t hash, dh;
66
67 this->dh->get_my_public_value(this->dh, &dh);
68 keymat = (keymat_v1_t*)this->ike_sa->get_keymat(this->ike_sa);
69 hash = keymat->get_hash(keymat, this->initiator, dh, this->dh_value,
70 this->ike_sa->get_id(this->ike_sa), this->sa_payload,
71 this->ike_sa->get_my_id(this->ike_sa));
72 free(dh.ptr);
73
74 hash_payload = hash_payload_create(HASH_V1);
75 hash_payload->set_hash(hash_payload, hash);
76 message->add_payload(message, &hash_payload->payload_interface);
77 free(hash.ptr);
78
79 return SUCCESS;
80 }
81
82 METHOD(authenticator_t, process, status_t,
83 private_psk_v1_authenticator_t *this, message_t *message)
84 {
85 hash_payload_t *hash_payload;
86 keymat_v1_t *keymat;
87 chunk_t hash, dh;
88
89 hash_payload = (hash_payload_t*)message->get_payload(message, HASH_V1);
90 if (!hash_payload)
91 {
92 DBG1(DBG_IKE, "HASH payload missing in message");
93 return FAILED;
94 }
95
96 this->dh->get_my_public_value(this->dh, &dh);
97 keymat = (keymat_v1_t*)this->ike_sa->get_keymat(this->ike_sa);
98 hash = keymat->get_hash(keymat, !this->initiator, this->dh_value, dh,
99 this->ike_sa->get_id(this->ike_sa), this->sa_payload,
100 this->ike_sa->get_other_id(this->ike_sa));
101 free(dh.ptr);
102
103 if (chunk_equals(hash, hash_payload->get_hash(hash_payload)))
104 {
105 free(hash.ptr);
106 return SUCCESS;
107 }
108 free(hash.ptr);
109 DBG1(DBG_IKE, "calculated HASH does not match HASH payload");
110 return FAILED;
111 }
112
113 METHOD(authenticator_t, destroy, void,
114 private_psk_v1_authenticator_t *this)
115 {
116 free(this);
117 }
118
119 /*
120 * Described in header.
121 */
122 psk_v1_authenticator_t *psk_v1_authenticator_create(ike_sa_t *ike_sa,
123 bool initiator, diffie_hellman_t *dh,
124 chunk_t dh_value, chunk_t sa_payload)
125 {
126 private_psk_v1_authenticator_t *this;
127
128 INIT(this,
129 .public = {
130 .authenticator = {
131 .build = _build,
132 .process = _process,
133 .is_mutual = (void*)return_false,
134 .destroy = _destroy,
135 },
136 },
137 .ike_sa = ike_sa,
138 .initiator = initiator,
139 .dh = dh,
140 .dh_value = dh_value,
141 .sa_payload = sa_payload,
142 );
143
144 return &this->public;
145 }