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 /* Checks the version string of the server. */
124 SilcSKEStatus silc_ske_check_version(SilcSKE ske, unsigned char *version,
127 SilcSocketConnection conn = (SilcSocketConnection)ske->sock->user_data;
128 SilcClient client = (SilcClient)ske->user_data;
129 SilcSKEStatus status = SILC_SKE_STATUS_OK;
131 /* Check for initial version string */
132 if (!strstr(version, "SILC-1.0-"))
133 status = SILC_SKE_STATUS_BAD_VERSION;
135 /* Check software version */
137 if (len < strlen(silc_version_string))
138 status = SILC_SKE_STATUS_BAD_VERSION;
140 /* XXX for now there is no other tests due to the abnormal version
141 string that is used */
143 if (status != SILC_SKE_STATUS_OK)
144 client->ops->say(client, conn,
145 "We don't support server version `%s'", version);
150 /* Performs key exchange protocol. This is used for both initiator
151 and responder key exchange. This may be called recursively. */
153 SILC_TASK_CALLBACK(silc_client_protocol_key_exchange)
155 SilcProtocol protocol = (SilcProtocol)context;
156 SilcClientKEInternalContext *ctx =
157 (SilcClientKEInternalContext *)protocol->context;
158 SilcClient client = (SilcClient)ctx->client;
159 SilcClientConnection conn = ctx->sock->user_data;
160 SilcSKEStatus status = 0;
162 SILC_LOG_DEBUG(("Start"));
164 if (protocol->state == SILC_PROTOCOL_STATE_UNKNOWN)
165 protocol->state = SILC_PROTOCOL_STATE_START;
167 switch(protocol->state) {
168 case SILC_PROTOCOL_STATE_START:
175 /* Allocate Key Exchange object */
176 ske = silc_ske_alloc();
178 ske->rng = client->rng;
179 ske->user_data = (void *)client;
181 if (ctx->responder == TRUE) {
183 SilcBuffer start_payload;
186 /* Start the key exchange by processing the received security
187 properties packet from initiator. */
188 status = silc_ske_responder_start(ske, ctx->rng, ctx->sock,
190 silc_client_protocol_ke_send_packet,
194 SilcSKEStartPayload *start_payload;
196 /* Assemble security properties. */
197 silc_ske_assemble_security_properties(ske, SILC_SKE_SP_FLAG_NONE,
201 /* Start the key exchange by sending our security properties
202 to the remote end. */
203 status = silc_ske_initiator_start(ske, ctx->rng, ctx->sock,
205 silc_client_protocol_ke_send_packet,
209 if (status != SILC_SKE_STATUS_OK) {
210 SILC_LOG_WARNING(("Error (type %d) during Key Exchange protocol",
212 SILC_LOG_DEBUG(("Error (type %d) during Key Exchange protocol",
215 protocol->state = SILC_PROTOCOL_STATE_ERROR;
216 protocol->execute(client->timeout_queue, 0, protocol, fd, 0, 0);
220 /* Advance the state of the protocol. */
229 if (ctx->responder == TRUE) {
232 silc_ske_responder_phase_1(ctx->ske,
233 ctx->ske->start_payload,
234 silc_server_protocol_ke_send_packet,
238 /* Call Phase-1 function. This processes the Key Exchange Start
239 paylaod reply we just got from the responder. The callback
240 function will receive the processed payload where we will
242 status = silc_ske_initiator_phase_1(ctx->ske, ctx->packet->buffer,
246 if (status != SILC_SKE_STATUS_OK) {
247 SILC_LOG_WARNING(("Error (type %d) during Key Exchange protocol",
249 SILC_LOG_DEBUG(("Error (type %d) during Key Exchange protocol",
252 protocol->state = SILC_PROTOCOL_STATE_ERROR;
253 protocol->execute(client->timeout_queue, 0, protocol, fd, 0, 0);
257 /* Advance the state of the protocol and call the next state. */
259 protocol->execute(client->timeout_queue, 0, protocol, fd, 0, 0);
267 if (ctx->responder == TRUE) {
270 silc_ske_responder_phase_2(ctx->ske,
271 ctx->ske->start_payload,
272 silc_server_protocol_ke_send_packet,
276 /* Call the Phase-2 function. This creates Diffie Hellman
277 key exchange parameters and sends our public part inside
278 Key Exhange 1 Payload to the responder. */
280 silc_ske_initiator_phase_2(ctx->ske,
282 silc_client_protocol_ke_send_packet,
286 if (status != SILC_SKE_STATUS_OK) {
287 SILC_LOG_WARNING(("Error (type %d) during Key Exchange protocol",
289 SILC_LOG_DEBUG(("Error (type %d) during Key Exchange protocol",
292 protocol->state = SILC_PROTOCOL_STATE_ERROR;
293 protocol->execute(client->timeout_queue, 0, protocol, fd, 0, 0);
297 /* Advance the state of the protocol. */
306 if (ctx->responder == TRUE) {
310 silc_ske_responder_phase_2(ctx->ske,
311 ctx->ske->start_payload,
312 silc_server_protocol_ke_send_packet,
316 /* Finish the protocol. This verifies the Key Exchange 2 payload
317 sent by responder. */
318 status = silc_ske_initiator_finish(ctx->ske, ctx->packet->buffer,
319 silc_client_protocol_ke_verify_key,
320 context, NULL, NULL);
323 if (status != SILC_SKE_STATUS_OK) {
325 if (status == SILC_SKE_STATUS_UNSUPPORTED_PUBLIC_KEY) {
326 client->ops->say(client, conn,
327 "Received unsupported server %s public key",
328 ctx->sock->hostname);
330 client->ops->say(client, conn,
331 "Error during key exchange protocol with server %s",
332 ctx->sock->hostname);
334 protocol->state = SILC_PROTOCOL_STATE_ERROR;
335 protocol->execute(client->timeout_queue, 0, protocol, fd, 0, 0);
339 /* Send Ok to the other end. We will end the protocol as server
340 sends Ok to us when we will take the new keys into use. */
341 silc_ske_end(ctx->ske, silc_client_protocol_ke_send_packet, context);
343 /* End the protocol on the next round */
344 protocol->state = SILC_PROTOCOL_STATE_END;
348 case SILC_PROTOCOL_STATE_END:
353 SilcSKEKeyMaterial *keymat;
355 /* Process the key material */
356 keymat = silc_calloc(1, sizeof(*keymat));
357 silc_ske_process_key_material(ctx->ske, 16, (16 * 8), 16, keymat);
359 /* Take the negotiated keys into use. */
360 silc_client_protocol_ke_set_keys(ctx->ske, ctx->sock, keymat,
361 ctx->ske->prop->cipher,
362 ctx->ske->prop->pkcs,
363 ctx->ske->prop->hash);
365 /* Protocol has ended, call the final callback */
366 if (protocol->final_callback)
367 protocol->execute_final(client->timeout_queue, 0, protocol, fd);
369 silc_protocol_free(protocol);
373 case SILC_PROTOCOL_STATE_ERROR:
375 * Error during protocol
378 /* Send abort notification */
379 silc_ske_abort(ctx->ske, ctx->ske->status,
380 silc_client_protocol_ke_send_packet,
383 /* On error the final callback is always called. */
384 if (protocol->final_callback)
385 protocol->execute_final(client->timeout_queue, 0, protocol, fd);
387 silc_protocol_free(protocol);
390 case SILC_PROTOCOL_STATE_FAILURE:
392 * Received failure from remote.
395 /* On error the final callback is always called. */
396 if (protocol->final_callback)
397 protocol->execute_final(client->timeout_queue, 0, protocol, fd);
399 silc_protocol_free(protocol);
401 case SILC_PROTOCOL_STATE_UNKNOWN:
407 * Connection Authentication protocol functions
410 SILC_TASK_CALLBACK(silc_client_protocol_connection_auth)
412 SilcProtocol protocol = (SilcProtocol)context;
413 SilcClientConnAuthInternalContext *ctx =
414 (SilcClientConnAuthInternalContext *)protocol->context;
415 SilcClient client = (SilcClient)ctx->client;
416 SilcClientConnection conn = ctx->sock->user_data;
418 SILC_LOG_DEBUG(("Start"));
420 if (protocol->state == SILC_PROTOCOL_STATE_UNKNOWN)
421 protocol->state = SILC_PROTOCOL_STATE_START;
423 switch(protocol->state) {
424 case SILC_PROTOCOL_STATE_START:
427 * Start protocol. We send authentication data to the server
428 * to be authenticated.
432 unsigned char *auth_data = NULL;
433 unsigned int auth_data_len = 0;
435 switch(ctx->auth_meth) {
437 /* No authentication required */
440 case SILC_AUTH_PASSWORD:
441 /* Password authentication */
442 if (ctx->auth_data && ctx->auth_data_len) {
443 auth_data = ctx->auth_data;
444 auth_data_len = ctx->auth_data_len;
448 client->ops->say(client, conn,
449 "Password authentication required by server %s",
450 ctx->sock->hostname);
451 auth_data = client->ops->ask_passphrase(client, conn);
452 auth_data_len = strlen(auth_data);
455 case SILC_AUTH_PUBLIC_KEY:
460 payload_len = 4 + auth_data_len;
461 packet = silc_buffer_alloc(payload_len);
462 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
463 silc_buffer_format(packet,
464 SILC_STR_UI_SHORT(payload_len),
465 SILC_STR_UI_SHORT(SILC_SOCKET_TYPE_CLIENT),
466 SILC_STR_UI_XNSTRING(auth_data, auth_data_len),
469 /* Send the packet to server */
470 silc_client_packet_send(client, ctx->sock,
471 SILC_PACKET_CONNECTION_AUTH,
473 packet->data, packet->len, TRUE);
476 memset(auth_data, 0, auth_data_len);
477 silc_free(auth_data);
479 silc_buffer_free(packet);
481 /* Next state is end of protocol */
482 protocol->state = SILC_PROTOCOL_STATE_END;
486 case SILC_PROTOCOL_STATE_END:
489 * End protocol. Nothing special to be done here.
492 /* Protocol has ended, call the final callback */
493 if (protocol->final_callback)
494 protocol->execute_final(client->timeout_queue, 0, protocol, fd);
496 silc_protocol_free(protocol);
500 case SILC_PROTOCOL_STATE_ERROR:
503 * Error. Send notify to remote.
505 unsigned char error[4];
507 SILC_PUT32_MSB(SILC_AUTH_FAILED, error);
509 /* Error in protocol. Send FAILURE packet. Although I don't think
510 this could ever happen on client side. */
511 silc_client_packet_send(client, ctx->sock, SILC_PACKET_FAILURE,
512 NULL, 0, NULL, NULL, error, 4, TRUE);
514 /* On error the final callback is always called. */
515 if (protocol->final_callback)
516 protocol->execute_final(client->timeout_queue, 0, protocol, fd);
518 silc_protocol_free(protocol);
521 case SILC_PROTOCOL_STATE_FAILURE:
523 * Received failure from remote.
526 /* On error the final callback is always called. */
527 if (protocol->final_callback)
528 protocol->execute_final(client->timeout_queue, 0, protocol, fd);
530 silc_protocol_free(protocol);
533 case SILC_PROTOCOL_STATE_UNKNOWN:
538 /* Registers protocols used in client */
540 void silc_client_protocols_register(void)
542 silc_protocol_register(SILC_PROTOCOL_CLIENT_CONNECTION_AUTH,
543 silc_client_protocol_connection_auth);
544 silc_protocol_register(SILC_PROTOCOL_CLIENT_KEY_EXCHANGE,
545 silc_client_protocol_key_exchange);
548 /* Unregisters protocols */
550 void silc_client_protocols_unregister(void)
552 silc_protocol_unregister(SILC_PROTOCOL_CLIENT_CONNECTION_AUTH,
553 silc_client_protocol_connection_auth);
554 silc_protocol_unregister(SILC_PROTOCOL_CLIENT_KEY_EXCHANGE,
555 silc_client_protocol_key_exchange);