5 Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
7 Copyright (C) 1997 - 2000 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
21 * Client side of the protocols.
25 #include "clientlibincludes.h"
27 SILC_TASK_CALLBACK(silc_client_protocol_connection_auth);
28 SILC_TASK_CALLBACK(silc_client_protocol_key_exchange);
30 extern char *silc_version_string;
33 * Key Exhange protocol functions
36 /* Function that is called when SKE protocol sends packets to network. */
38 static void silc_client_protocol_ke_send_packet(SilcSKE ske,
43 SilcProtocol protocol = (SilcProtocol)context;
44 SilcClientKEInternalContext *ctx =
45 (SilcClientKEInternalContext *)protocol->context;
46 SilcClient client = (SilcClient)ctx->client;
48 /* Send the packet immediately */
49 silc_client_packet_send(client, ske->sock, type, NULL, 0, NULL, NULL,
50 packet->data, packet->len, TRUE);
54 /* Callback that is called when we have received KE2 payload from
55 responder. We try to verify the public key now. */
58 silc_client_protocol_ke_verify_key(SilcSKE ske,
59 unsigned char *pk_data,
61 SilcSKEPKType pk_type,
64 SilcProtocol protocol = (SilcProtocol)context;
65 SilcClientKEInternalContext *ctx =
66 (SilcClientKEInternalContext *)protocol->context;
67 SilcClient client = (SilcClient)ctx->client;
69 SILC_LOG_DEBUG(("Start"));
71 /* Verify server key from user. */
72 if (!client->ops->verify_server_key(client, ctx->sock->user_data,
73 pk_data, pk_len, pk_type))
74 return SILC_SKE_STATUS_UNSUPPORTED_PUBLIC_KEY;
76 return SILC_SKE_STATUS_OK;
79 /* Sets the negotiated key material into use for particular connection. */
81 static void silc_client_protocol_ke_set_keys(SilcSKE ske,
82 SilcSocketConnection sock,
83 SilcSKEKeyMaterial *keymat,
88 SilcClientConnection conn = (SilcClientConnection)sock->user_data;
91 SILC_LOG_DEBUG(("Setting new keys into use"));
93 /* Allocate cipher to be used in the communication */
94 silc_cipher_alloc(cipher->cipher->name, &conn->send_key);
95 silc_cipher_alloc(cipher->cipher->name, &conn->receive_key);
97 conn->send_key->cipher->set_key(conn->send_key->context,
100 conn->send_key->set_iv(conn->send_key, keymat->send_iv);
101 conn->receive_key->cipher->set_key(conn->receive_key->context,
102 keymat->receive_enc_key,
103 keymat->enc_key_len);
104 conn->receive_key->set_iv(conn->receive_key, keymat->receive_iv);
106 /* Allocate PKCS to be used */
108 /* XXX Do we ever need to allocate PKCS for the connection??
109 If yes, we need to change KE protocol to get the initiators
111 silc_pkcs_alloc(pkcs->pkcs->name, &conn->public_Key);
112 silc_pkcs_set_public_key(conn->public_key, ske->ke2_payload->pk_data,
113 ske->ke2_payload->pk_len);
116 /* Save HMAC key to be used in the communication. */
117 silc_hash_alloc(hash->hash->name, &nhash);
118 silc_hmac_alloc(nhash, &conn->hmac);
119 silc_hmac_set_key(conn->hmac, keymat->hmac_key, keymat->hmac_key_len);
122 /* Performs key exchange protocol. This is used for both initiator
123 and responder key exchange. This may be called recursively. */
125 SILC_TASK_CALLBACK(silc_client_protocol_key_exchange)
127 SilcProtocol protocol = (SilcProtocol)context;
128 SilcClientKEInternalContext *ctx =
129 (SilcClientKEInternalContext *)protocol->context;
130 SilcClient client = (SilcClient)ctx->client;
131 SilcClientConnection conn = ctx->sock->user_data;
132 SilcSKEStatus status = 0;
134 SILC_LOG_DEBUG(("Start"));
136 if (protocol->state == SILC_PROTOCOL_STATE_UNKNOWN)
137 protocol->state = SILC_PROTOCOL_STATE_START;
139 switch(protocol->state) {
140 case SILC_PROTOCOL_STATE_START:
147 /* Allocate Key Exchange object */
148 ske = silc_ske_alloc();
151 if (ctx->responder == TRUE) {
153 SilcBuffer start_payload;
156 /* Start the key exchange by processing the received security
157 properties packet from initiator. */
158 status = silc_ske_responder_start(ske, ctx->rng, ctx->sock,
160 silc_client_protocol_ke_send_packet,
164 SilcSKEStartPayload *start_payload;
166 /* Assemble security properties. */
167 silc_ske_assemble_security_properties(ske, silc_version_string,
170 /* Start the key exchange by sending our security properties
171 to the remote end. */
172 status = silc_ske_initiator_start(ske, ctx->rng, ctx->sock,
174 silc_client_protocol_ke_send_packet,
178 if (status != SILC_SKE_STATUS_OK) {
179 SILC_LOG_WARNING(("Error (type %d) during Key Exchange protocol",
181 SILC_LOG_DEBUG(("Error (type %d) during Key Exchange protocol",
184 protocol->state = SILC_PROTOCOL_STATE_ERROR;
185 protocol->execute(client->timeout_queue, 0, protocol, fd, 0, 0);
189 /* Advance the state of the protocol. */
198 if (ctx->responder == TRUE) {
201 silc_ske_responder_phase_1(ctx->ske,
202 ctx->ske->start_payload,
203 silc_server_protocol_ke_send_packet,
207 /* Call Phase-1 function. This processes the Key Exchange Start
208 paylaod reply we just got from the responder. The callback
209 function will receive the processed payload where we will
211 status = silc_ske_initiator_phase_1(ctx->ske, ctx->packet, NULL, NULL);
214 if (status != SILC_SKE_STATUS_OK) {
215 SILC_LOG_WARNING(("Error (type %d) during Key Exchange protocol",
217 SILC_LOG_DEBUG(("Error (type %d) during Key Exchange protocol",
220 protocol->state = SILC_PROTOCOL_STATE_ERROR;
221 protocol->execute(client->timeout_queue, 0, protocol, fd, 0, 0);
225 /* Advance the state of the protocol and call the next state. */
227 protocol->execute(client->timeout_queue, 0, protocol, fd, 0, 0);
235 if (ctx->responder == TRUE) {
238 silc_ske_responder_phase_2(ctx->ske,
239 ctx->ske->start_payload,
240 silc_server_protocol_ke_send_packet,
244 /* Call the Phase-2 function. This creates Diffie Hellman
245 key exchange parameters and sends our public part inside
246 Key Exhange 1 Payload to the responder. */
248 silc_ske_initiator_phase_2(ctx->ske,
250 silc_client_protocol_ke_send_packet,
254 if (status != SILC_SKE_STATUS_OK) {
255 SILC_LOG_WARNING(("Error (type %d) during Key Exchange protocol",
257 SILC_LOG_DEBUG(("Error (type %d) during Key Exchange protocol",
260 protocol->state = SILC_PROTOCOL_STATE_ERROR;
261 protocol->execute(client->timeout_queue, 0, protocol, fd, 0, 0);
265 /* Advance the state of the protocol. */
274 if (ctx->responder == TRUE) {
278 silc_ske_responder_phase_2(ctx->ske,
279 ctx->ske->start_payload,
280 silc_server_protocol_ke_send_packet,
284 /* Finish the protocol. This verifies the Key Exchange 2 payload
285 sent by responder. */
286 status = silc_ske_initiator_finish(ctx->ske, ctx->packet,
287 silc_client_protocol_ke_verify_key,
288 context, NULL, NULL);
291 if (status != SILC_SKE_STATUS_OK) {
293 if (status == SILC_SKE_STATUS_UNSUPPORTED_PUBLIC_KEY) {
294 client->ops->say(client, conn,
295 "Received unsupported server %s public key",
296 ctx->sock->hostname);
298 client->ops->say(client, conn,
299 "Error during key exchange protocol with server %s",
300 ctx->sock->hostname);
302 protocol->state = SILC_PROTOCOL_STATE_ERROR;
303 protocol->execute(client->timeout_queue, 0, protocol, fd, 0, 0);
307 /* Send Ok to the other end. We will end the protocol as server
308 sends Ok to us when we will take the new keys into use. */
309 silc_ske_end(ctx->ske, silc_client_protocol_ke_send_packet, context);
311 /* End the protocol on the next round */
312 protocol->state = SILC_PROTOCOL_STATE_END;
315 case SILC_PROTOCOL_STATE_END:
320 SilcSKEKeyMaterial *keymat;
322 /* Process the key material */
323 keymat = silc_calloc(1, sizeof(*keymat));
324 silc_ske_process_key_material(ctx->ske, 16, (16 * 8), 16, keymat);
326 /* Take the negotiated keys into use. */
327 silc_client_protocol_ke_set_keys(ctx->ske, ctx->sock, keymat,
328 ctx->ske->prop->cipher,
329 ctx->ske->prop->pkcs,
330 ctx->ske->prop->hash);
332 /* Protocol has ended, call the final callback */
333 if (protocol->final_callback)
334 protocol->execute_final(client->timeout_queue, 0, protocol, fd);
336 silc_protocol_free(protocol);
339 case SILC_PROTOCOL_STATE_ERROR:
341 /* On error the final callback is always called. */
342 if (protocol->final_callback)
343 protocol->execute_final(client->timeout_queue, 0, protocol, fd);
345 silc_protocol_free(protocol);
347 case SILC_PROTOCOL_STATE_UNKNOWN:
353 * Connection Authentication protocol functions
356 SILC_TASK_CALLBACK(silc_client_protocol_connection_auth)
358 SilcProtocol protocol = (SilcProtocol)context;
359 SilcClientConnAuthInternalContext *ctx =
360 (SilcClientConnAuthInternalContext *)protocol->context;
361 SilcClient client = (SilcClient)ctx->client;
362 SilcClientConnection conn = ctx->sock->user_data;
364 SILC_LOG_DEBUG(("Start"));
366 if (protocol->state == SILC_PROTOCOL_STATE_UNKNOWN)
367 protocol->state = SILC_PROTOCOL_STATE_START;
369 switch(protocol->state) {
370 case SILC_PROTOCOL_STATE_START:
373 * Start protocol. We send authentication data to the server
374 * to be authenticated.
378 unsigned char *auth_data = NULL;
379 unsigned int auth_data_len = 0;
381 switch(ctx->auth_meth) {
382 case SILC_PROTOCOL_CONN_AUTH_NONE:
383 /* No authentication required */
386 case SILC_PROTOCOL_CONN_AUTH_PASSWORD:
387 /* Password authentication */
388 if (ctx->auth_data && ctx->auth_data_len) {
389 auth_data = ctx->auth_data;
390 auth_data_len = ctx->auth_data_len;
394 client->ops->say(client, conn,
395 "Password authentication required by server %s",
396 ctx->sock->hostname);
397 auth_data = client->ops->ask_passphrase(client, conn);
398 auth_data_len = strlen(auth_data);
401 case SILC_PROTOCOL_CONN_AUTH_PUBLIC_KEY:
406 payload_len = 4 + auth_data_len;
407 packet = silc_buffer_alloc(payload_len);
408 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
409 silc_buffer_format(packet,
410 SILC_STR_UI_SHORT(payload_len),
411 SILC_STR_UI_SHORT(SILC_SOCKET_TYPE_CLIENT),
412 SILC_STR_UI_XNSTRING(auth_data, auth_data_len),
415 /* Send the packet to server */
416 silc_client_packet_send(client, ctx->sock,
417 SILC_PACKET_CONNECTION_AUTH,
419 packet->data, packet->len, TRUE);
422 memset(auth_data, 0, auth_data_len);
423 silc_free(auth_data);
425 silc_buffer_free(packet);
427 /* Next state is end of protocol */
428 protocol->state = SILC_PROTOCOL_STATE_END;
432 case SILC_PROTOCOL_STATE_END:
435 * End protocol. Nothing special to be done here.
438 /* Protocol has ended, call the final callback */
439 if (protocol->final_callback)
440 protocol->execute_final(client->timeout_queue, 0, protocol, fd);
442 silc_protocol_free(protocol);
446 case SILC_PROTOCOL_STATE_ERROR:
452 /* Error in protocol. Send FAILURE packet. Although I don't think
453 this could ever happen on client side. */
454 silc_client_packet_send(client, ctx->sock, SILC_PACKET_FAILURE,
455 NULL, 0, NULL, NULL, NULL, 0, TRUE);
457 /* On error the final callback is always called. */
458 if (protocol->final_callback)
459 protocol->execute_final(client->timeout_queue, 0, protocol, fd);
461 silc_protocol_free(protocol);
464 case SILC_PROTOCOL_STATE_UNKNOWN:
469 /* Registers protocols used in client */
471 void silc_client_protocols_register(void)
473 silc_protocol_register(SILC_PROTOCOL_CLIENT_CONNECTION_AUTH,
474 silc_client_protocol_connection_auth);
475 silc_protocol_register(SILC_PROTOCOL_CLIENT_KEY_EXCHANGE,
476 silc_client_protocol_key_exchange);
479 /* Unregisters protocols */
481 void silc_client_protocols_unregister(void)
483 silc_protocol_unregister(SILC_PROTOCOL_CLIENT_CONNECTION_AUTH,
484 silc_client_protocol_connection_auth);
485 silc_protocol_unregister(SILC_PROTOCOL_CLIENT_KEY_EXCHANGE,
486 silc_client_protocol_key_exchange);