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);
124 SilcSKEStatus silc_ske_check_version(SilcSKE ske, unsigned char *version,
127 return SILC_SKE_STATUS_OK;
130 /* Performs key exchange protocol. This is used for both initiator
131 and responder key exchange. This may be called recursively. */
133 SILC_TASK_CALLBACK(silc_client_protocol_key_exchange)
135 SilcProtocol protocol = (SilcProtocol)context;
136 SilcClientKEInternalContext *ctx =
137 (SilcClientKEInternalContext *)protocol->context;
138 SilcClient client = (SilcClient)ctx->client;
139 SilcClientConnection conn = ctx->sock->user_data;
140 SilcSKEStatus status = 0;
142 SILC_LOG_DEBUG(("Start"));
144 if (protocol->state == SILC_PROTOCOL_STATE_UNKNOWN)
145 protocol->state = SILC_PROTOCOL_STATE_START;
147 switch(protocol->state) {
148 case SILC_PROTOCOL_STATE_START:
155 /* Allocate Key Exchange object */
156 ske = silc_ske_alloc();
158 ske->rng = client->rng;
160 if (ctx->responder == TRUE) {
162 SilcBuffer start_payload;
165 /* Start the key exchange by processing the received security
166 properties packet from initiator. */
167 status = silc_ske_responder_start(ske, ctx->rng, ctx->sock,
169 silc_client_protocol_ke_send_packet,
173 SilcSKEStartPayload *start_payload;
175 /* Assemble security properties. */
176 silc_ske_assemble_security_properties(ske, SILC_SKE_SP_FLAG_NONE,
180 /* Start the key exchange by sending our security properties
181 to the remote end. */
182 status = silc_ske_initiator_start(ske, ctx->rng, ctx->sock,
184 silc_client_protocol_ke_send_packet,
188 if (status != SILC_SKE_STATUS_OK) {
189 SILC_LOG_WARNING(("Error (type %d) during Key Exchange protocol",
191 SILC_LOG_DEBUG(("Error (type %d) during Key Exchange protocol",
194 protocol->state = SILC_PROTOCOL_STATE_ERROR;
195 protocol->execute(client->timeout_queue, 0, protocol, fd, 0, 0);
199 /* Advance the state of the protocol. */
208 if (ctx->responder == TRUE) {
211 silc_ske_responder_phase_1(ctx->ske,
212 ctx->ske->start_payload,
213 silc_server_protocol_ke_send_packet,
217 /* Call Phase-1 function. This processes the Key Exchange Start
218 paylaod reply we just got from the responder. The callback
219 function will receive the processed payload where we will
221 status = silc_ske_initiator_phase_1(ctx->ske, ctx->packet, NULL, NULL);
224 if (status != SILC_SKE_STATUS_OK) {
225 SILC_LOG_WARNING(("Error (type %d) during Key Exchange protocol",
227 SILC_LOG_DEBUG(("Error (type %d) during Key Exchange protocol",
230 protocol->state = SILC_PROTOCOL_STATE_ERROR;
231 protocol->execute(client->timeout_queue, 0, protocol, fd, 0, 0);
235 /* Advance the state of the protocol and call the next state. */
237 protocol->execute(client->timeout_queue, 0, protocol, fd, 0, 0);
245 if (ctx->responder == TRUE) {
248 silc_ske_responder_phase_2(ctx->ske,
249 ctx->ske->start_payload,
250 silc_server_protocol_ke_send_packet,
254 /* Call the Phase-2 function. This creates Diffie Hellman
255 key exchange parameters and sends our public part inside
256 Key Exhange 1 Payload to the responder. */
258 silc_ske_initiator_phase_2(ctx->ske,
260 silc_client_protocol_ke_send_packet,
264 if (status != SILC_SKE_STATUS_OK) {
265 SILC_LOG_WARNING(("Error (type %d) during Key Exchange protocol",
267 SILC_LOG_DEBUG(("Error (type %d) during Key Exchange protocol",
270 protocol->state = SILC_PROTOCOL_STATE_ERROR;
271 protocol->execute(client->timeout_queue, 0, protocol, fd, 0, 0);
275 /* Advance the state of the protocol. */
284 if (ctx->responder == TRUE) {
288 silc_ske_responder_phase_2(ctx->ske,
289 ctx->ske->start_payload,
290 silc_server_protocol_ke_send_packet,
294 /* Finish the protocol. This verifies the Key Exchange 2 payload
295 sent by responder. */
296 status = silc_ske_initiator_finish(ctx->ske, ctx->packet,
297 silc_client_protocol_ke_verify_key,
298 context, NULL, NULL);
301 if (status != SILC_SKE_STATUS_OK) {
303 if (status == SILC_SKE_STATUS_UNSUPPORTED_PUBLIC_KEY) {
304 client->ops->say(client, conn,
305 "Received unsupported server %s public key",
306 ctx->sock->hostname);
308 client->ops->say(client, conn,
309 "Error during key exchange protocol with server %s",
310 ctx->sock->hostname);
312 protocol->state = SILC_PROTOCOL_STATE_ERROR;
313 protocol->execute(client->timeout_queue, 0, protocol, fd, 0, 0);
317 /* Send Ok to the other end. We will end the protocol as server
318 sends Ok to us when we will take the new keys into use. */
319 silc_ske_end(ctx->ske, silc_client_protocol_ke_send_packet, context);
321 /* End the protocol on the next round */
322 protocol->state = SILC_PROTOCOL_STATE_END;
326 case SILC_PROTOCOL_STATE_END:
331 SilcSKEKeyMaterial *keymat;
333 /* Process the key material */
334 keymat = silc_calloc(1, sizeof(*keymat));
335 silc_ske_process_key_material(ctx->ske, 16, (16 * 8), 16, keymat);
337 /* Take the negotiated keys into use. */
338 silc_client_protocol_ke_set_keys(ctx->ske, ctx->sock, keymat,
339 ctx->ske->prop->cipher,
340 ctx->ske->prop->pkcs,
341 ctx->ske->prop->hash);
343 /* Protocol has ended, call the final callback */
344 if (protocol->final_callback)
345 protocol->execute_final(client->timeout_queue, 0, protocol, fd);
347 silc_protocol_free(protocol);
351 case SILC_PROTOCOL_STATE_ERROR:
353 * Error during protocol
356 /* Send abort notification */
357 silc_ske_abort(ctx->ske, ctx->ske->status,
358 silc_client_protocol_ke_send_packet,
361 /* On error the final callback is always called. */
362 if (protocol->final_callback)
363 protocol->execute_final(client->timeout_queue, 0, protocol, fd);
365 silc_protocol_free(protocol);
368 case SILC_PROTOCOL_STATE_FAILURE:
370 * Received failure from remote.
373 /* On error the final callback is always called. */
374 if (protocol->final_callback)
375 protocol->execute_final(client->timeout_queue, 0, protocol, fd);
377 silc_protocol_free(protocol);
379 case SILC_PROTOCOL_STATE_UNKNOWN:
385 * Connection Authentication protocol functions
388 SILC_TASK_CALLBACK(silc_client_protocol_connection_auth)
390 SilcProtocol protocol = (SilcProtocol)context;
391 SilcClientConnAuthInternalContext *ctx =
392 (SilcClientConnAuthInternalContext *)protocol->context;
393 SilcClient client = (SilcClient)ctx->client;
394 SilcClientConnection conn = ctx->sock->user_data;
396 SILC_LOG_DEBUG(("Start"));
398 if (protocol->state == SILC_PROTOCOL_STATE_UNKNOWN)
399 protocol->state = SILC_PROTOCOL_STATE_START;
401 switch(protocol->state) {
402 case SILC_PROTOCOL_STATE_START:
405 * Start protocol. We send authentication data to the server
406 * to be authenticated.
410 unsigned char *auth_data = NULL;
411 unsigned int auth_data_len = 0;
413 switch(ctx->auth_meth) {
414 case SILC_PROTOCOL_CONN_AUTH_NONE:
415 /* No authentication required */
418 case SILC_PROTOCOL_CONN_AUTH_PASSWORD:
419 /* Password authentication */
420 if (ctx->auth_data && ctx->auth_data_len) {
421 auth_data = ctx->auth_data;
422 auth_data_len = ctx->auth_data_len;
426 client->ops->say(client, conn,
427 "Password authentication required by server %s",
428 ctx->sock->hostname);
429 auth_data = client->ops->ask_passphrase(client, conn);
430 auth_data_len = strlen(auth_data);
433 case SILC_PROTOCOL_CONN_AUTH_PUBLIC_KEY:
438 payload_len = 4 + auth_data_len;
439 packet = silc_buffer_alloc(payload_len);
440 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
441 silc_buffer_format(packet,
442 SILC_STR_UI_SHORT(payload_len),
443 SILC_STR_UI_SHORT(SILC_SOCKET_TYPE_CLIENT),
444 SILC_STR_UI_XNSTRING(auth_data, auth_data_len),
447 /* Send the packet to server */
448 silc_client_packet_send(client, ctx->sock,
449 SILC_PACKET_CONNECTION_AUTH,
451 packet->data, packet->len, TRUE);
454 memset(auth_data, 0, auth_data_len);
455 silc_free(auth_data);
457 silc_buffer_free(packet);
459 /* Next state is end of protocol */
460 protocol->state = SILC_PROTOCOL_STATE_END;
464 case SILC_PROTOCOL_STATE_END:
467 * End protocol. Nothing special to be done here.
470 /* Protocol has ended, call the final callback */
471 if (protocol->final_callback)
472 protocol->execute_final(client->timeout_queue, 0, protocol, fd);
474 silc_protocol_free(protocol);
478 case SILC_PROTOCOL_STATE_ERROR:
481 * Error. Send notify to remote.
483 unsigned char error[4];
485 SILC_PUT32_MSB(SILC_CONN_AUTH_FAILED, error);
487 /* Error in protocol. Send FAILURE packet. Although I don't think
488 this could ever happen on client side. */
489 silc_client_packet_send(client, ctx->sock, SILC_PACKET_FAILURE,
490 NULL, 0, NULL, NULL, error, 4, TRUE);
492 /* On error the final callback is always called. */
493 if (protocol->final_callback)
494 protocol->execute_final(client->timeout_queue, 0, protocol, fd);
496 silc_protocol_free(protocol);
499 case SILC_PROTOCOL_STATE_FAILURE:
501 * Received failure from remote.
504 /* On error the final callback is always called. */
505 if (protocol->final_callback)
506 protocol->execute_final(client->timeout_queue, 0, protocol, fd);
508 silc_protocol_free(protocol);
511 case SILC_PROTOCOL_STATE_UNKNOWN:
516 /* Registers protocols used in client */
518 void silc_client_protocols_register(void)
520 silc_protocol_register(SILC_PROTOCOL_CLIENT_CONNECTION_AUTH,
521 silc_client_protocol_connection_auth);
522 silc_protocol_register(SILC_PROTOCOL_CLIENT_KEY_EXCHANGE,
523 silc_client_protocol_key_exchange);
526 /* Unregisters protocols */
528 void silc_client_protocols_unregister(void)
530 silc_protocol_unregister(SILC_PROTOCOL_CLIENT_CONNECTION_AUTH,
531 silc_client_protocol_connection_auth);
532 silc_protocol_unregister(SILC_PROTOCOL_CLIENT_KEY_EXCHANGE,
533 silc_client_protocol_key_exchange);