authentication failure is handled in ike_auth.c
[strongswan.git] / src / charon / sa / authenticators / rsa_authenticator.c
1 /**
2 * @file rsa_authenticator.c
3 *
4 * @brief Implementation of rsa_authenticator_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005-2006 Martin Willi
10 * Copyright (C) 2005 Jan Hutter
11 * Hochschule fuer Technik Rapperswil
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
17 *
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * for more details.
22 */
23
24 #include <string.h>
25
26 #include "rsa_authenticator.h"
27
28 #include <daemon.h>
29
30
31 typedef struct private_rsa_authenticator_t private_rsa_authenticator_t;
32
33 /**
34 * Private data of an rsa_authenticator_t object.
35 */
36 struct private_rsa_authenticator_t {
37
38 /**
39 * Public authenticator_t interface.
40 */
41 rsa_authenticator_t public;
42
43 /**
44 * Assigned IKE_SA
45 */
46 ike_sa_t *ike_sa;
47 };
48
49 /**
50 * Function implemented in psk_authenticator.c
51 */
52 extern chunk_t build_tbs_octets(chunk_t ike_sa_init, chunk_t nonce,
53 identification_t *id, prf_t *prf);
54
55 /**
56 * Implementation of authenticator_t.verify.
57 */
58 static status_t verify(private_rsa_authenticator_t *this, chunk_t ike_sa_init,
59 chunk_t my_nonce, auth_payload_t *auth_payload)
60 {
61 status_t status;
62 chunk_t auth_data, octets;
63 identification_t *other_id;
64 ca_info_t *issuer;
65 prf_t *prf;
66
67 other_id = this->ike_sa->get_other_id(this->ike_sa);
68
69 if (auth_payload->get_auth_method(auth_payload) != AUTH_RSA)
70 {
71 return INVALID_ARG;
72 }
73 auth_data = auth_payload->get_data(auth_payload);
74 prf = this->ike_sa->get_prf(this->ike_sa);
75 prf->set_key(prf, this->ike_sa->get_skp_verify(this->ike_sa));
76 octets = build_tbs_octets(ike_sa_init, my_nonce, other_id, prf);
77 status = charon->credentials->verify_signature(charon->credentials,
78 octets, auth_data, other_id, &issuer);
79 chunk_free(&octets);
80
81 if (status == SUCCESS)
82 {
83 DBG1(DBG_IKE, "authentication of '%D' with %N successful",
84 other_id, auth_method_names, AUTH_RSA);
85 }
86 return status;
87 }
88
89 /**
90 * Implementation of authenticator_t.build.
91 */
92 static status_t build(private_rsa_authenticator_t *this, chunk_t ike_sa_init,
93 chunk_t other_nonce, auth_payload_t **auth_payload)
94 {
95 chunk_t chunk;
96 chunk_t octets;
97 chunk_t auth_data;
98 status_t status;
99 rsa_public_key_t *my_pubkey;
100 rsa_private_key_t *my_key;
101 identification_t *my_id;
102 prf_t *prf;
103
104 my_id = this->ike_sa->get_my_id(this->ike_sa);
105 DBG1(DBG_IKE, "authentication of '%D' (myself) with %N",
106 my_id, auth_method_names, AUTH_RSA);
107 DBG2(DBG_IKE, "looking for RSA public key belonging to '%D'", my_id);
108
109 my_pubkey = charon->credentials->get_rsa_public_key(charon->credentials, my_id);
110 if (my_pubkey == NULL)
111 {
112 DBG1(DBG_IKE, "no RSA public key found for '%D'", my_id);
113 return NOT_FOUND;
114 }
115 DBG2(DBG_IKE, "matching RSA public key found");
116 chunk = my_pubkey->get_keyid(my_pubkey);
117 DBG2(DBG_IKE, "looking for RSA private key with keyid %#B", &chunk);
118 my_key = charon->credentials->get_rsa_private_key(charon->credentials, my_pubkey);
119 if (my_key == NULL)
120 {
121 DBG1(DBG_IKE, "no RSA private key found with for %D with keyid %#B",
122 my_id, &chunk);
123 return NOT_FOUND;
124 }
125 DBG2(DBG_IKE, "matching RSA private key found");
126
127 prf = this->ike_sa->get_prf(this->ike_sa);
128 prf->set_key(prf, this->ike_sa->get_skp_build(this->ike_sa));
129 octets = build_tbs_octets(ike_sa_init, other_nonce, my_id, prf);
130 status = my_key->build_emsa_pkcs1_signature(my_key, HASH_SHA1, octets, &auth_data);
131 chunk_free(&octets);
132
133 if (status != SUCCESS)
134 {
135 my_key->destroy(my_key);
136 DBG1(DBG_IKE, "build signature of SHA1 hash failed");
137 return status;
138 }
139 DBG2(DBG_IKE, "successfully signed with RSA private key");
140
141 *auth_payload = auth_payload_create();
142 (*auth_payload)->set_auth_method(*auth_payload, AUTH_RSA);
143 (*auth_payload)->set_data(*auth_payload, auth_data);
144
145 my_key->destroy(my_key);
146 chunk_free(&auth_data);
147 return SUCCESS;
148 }
149
150 /**
151 * Implementation of authenticator_t.destroy.
152 */
153 static void destroy(private_rsa_authenticator_t *this)
154 {
155 free(this);
156 }
157
158 /*
159 * Described in header.
160 */
161 rsa_authenticator_t *rsa_authenticator_create(ike_sa_t *ike_sa)
162 {
163 private_rsa_authenticator_t *this = malloc_thing(private_rsa_authenticator_t);
164
165 /* public functions */
166 this->public.authenticator_interface.verify = (status_t(*)(authenticator_t*,chunk_t,chunk_t,auth_payload_t*))verify;
167 this->public.authenticator_interface.build = (status_t(*)(authenticator_t*,chunk_t,chunk_t,auth_payload_t**))build;
168 this->public.authenticator_interface.destroy = (void(*)(authenticator_t*))destroy;
169
170 /* private data */
171 this->ike_sa = ike_sa;
172
173 return &this->public;
174 }