~ubuntu-branches/ubuntu/trusty/libssh/trusty

« back to all changes in this revision

Viewing changes to libssh/kex.c

  • Committer: Bazaar Package Importer
  • Author(s): Laurent Bigonville
  • Date: 2009-12-12 14:29:12 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20091212142912-ha5g2iibt6nfnjq8
Tags: 0.4.0-1
* New upstream release.
  - Bump soname
  - Adjust .symbols file
* Readd static library in -dev package
* Let dh_lintian install override file
* debian/README.Debian: Update file
* debian/rules: Add list-missing rule

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
 * MA 02111-1307, USA.
22
22
 */
23
23
 
 
24
#include "config.h"
 
25
 
24
26
#include <string.h>
25
27
#include <stdlib.h>
26
28
#include <stdio.h>
29
31
#include <arpa/inet.h>
30
32
#endif
31
33
 
32
 
#include "config.h"
33
34
#include "libssh/priv.h"
34
35
#include "libssh/ssh2.h"
35
36
#include "libssh/ssh1.h"
 
37
#include "libssh/buffer.h"
 
38
#include "libssh/packet.h"
 
39
#include "libssh/session.h"
 
40
#include "libssh/wrapper.h"
 
41
#include "libssh/keys.h"
 
42
#include "libssh/dh.h"
36
43
 
37
44
#ifdef HAVE_LIBGCRYPT
38
45
#define BLOWFISH "blowfish-cbc,"
232
239
    return NULL;
233
240
}
234
241
 
235
 
int ssh_get_kex(SSH_SESSION *session, int server_kex) {
236
 
  STRING *str = NULL;
 
242
int ssh_get_kex(ssh_session session, int server_kex) {
 
243
  ssh_string str = NULL;
237
244
  char *strings[10];
238
245
  int i;
239
246
 
311
318
  return -1;
312
319
}
313
320
 
314
 
void ssh_list_kex(struct ssh_session *session, KEX *kex) {
 
321
void ssh_list_kex(ssh_session session, KEX *kex) {
315
322
  int i = 0;
316
323
 
317
324
#ifdef DEBUG_CRYPTO
328
335
/* it must be aware of the server kex message */
329
336
/* it can fail if option is null, not any user specified kex method matches the server one, if not any default kex matches */
330
337
 
331
 
int set_kex(SSH_SESSION *session){
 
338
int set_kex(ssh_session session){
332
339
    KEX *server = &session->server_kex;
333
340
    KEX *client=&session->client_kex;
334
 
    SSH_OPTIONS *options=session->options;
335
341
    int i;
336
342
    const char *wanted;
337
343
    enter_function();
338
 
    /* the client might ask for a specific cookie to be sent. useful for server debugging */
339
 
    if(options->wanted_cookie)
340
 
        memcpy(client->cookie,options->wanted_cookie,16);
341
 
    else
342
 
        ssh_get_random(client->cookie,16,0);
 
344
    ssh_get_random(client->cookie,16,0);
343
345
    client->methods=malloc(10 * sizeof(char **));
344
346
    if (client->methods == NULL) {
345
347
      ssh_set_error(session, SSH_FATAL, "No space left");
348
350
    }
349
351
    memset(client->methods,0,10*sizeof(char **));
350
352
    for (i=0;i<10;i++){
351
 
        if(!(wanted=options->wanted_methods[i]))
 
353
        if(!(wanted=session->wanted_methods[i]))
352
354
            wanted=default_methods[i];
353
355
        client->methods[i]=ssh_find_matching(server->methods[i],wanted);
354
356
        if(!client->methods[i] && i < SSH_LANG_C_S){
371
373
}
372
374
 
373
375
/* this function only sends the predefined set of kex methods */
374
 
int ssh_send_kex(SSH_SESSION *session, int server_kex) {
 
376
int ssh_send_kex(ssh_session session, int server_kex) {
375
377
  KEX *kex = (server_kex ? &session->server_kex : &session->client_kex);
376
 
  STRING *str = NULL;
 
378
  ssh_string str = NULL;
377
379
  int i;
378
380
 
379
381
  enter_function();
444
446
 
445
447
/* makes a STRING contating 3 strings : ssh-rsa1,e and n */
446
448
/* this is a public key in openssh's format */
447
 
static STRING *make_rsa1_string(STRING *e, STRING *n){
448
 
  BUFFER *buffer = NULL;
449
 
  STRING *rsa = NULL;
450
 
  STRING *ret = NULL;
 
449
static ssh_string make_rsa1_string(ssh_string e, ssh_string n){
 
450
  ssh_buffer buffer = NULL;
 
451
  ssh_string rsa = NULL;
 
452
  ssh_string ret = NULL;
451
453
 
452
454
  buffer = buffer_new();
453
455
  rsa = string_from_char("ssh-rsa1");
475
477
  return ret;
476
478
}
477
479
 
478
 
static int build_session_id1(SSH_SESSION *session, STRING *servern,
479
 
    STRING *hostn) {
 
480
static int build_session_id1(ssh_session session, ssh_string servern,
 
481
    ssh_string hostn) {
480
482
  MD5CTX md5 = NULL;
481
483
 
482
484
  md5 = md5_init();
488
490
  ssh_print_hexa("host modulus",hostn->string,string_len(hostn));
489
491
  ssh_print_hexa("server modulus",servern->string,string_len(servern));
490
492
#endif
491
 
  md5_update(md5,hostn->string,string_len(hostn));
492
 
  md5_update(md5,servern->string,string_len(servern));
 
493
  md5_update(md5,string_data(hostn),string_len(hostn));
 
494
  md5_update(md5,string_data(servern),string_len(servern));
493
495
  md5_update(md5,session->server_kex.cookie,8);
494
496
  md5_final(session->next_crypto->session_id,md5);
495
497
#ifdef DEBUG_CRYPTO
500
502
}
501
503
 
502
504
/* returns 1 if the modulus of k1 is < than the one of k2 */
503
 
static int modulus_smaller(PUBLIC_KEY *k1, PUBLIC_KEY *k2){
 
505
static int modulus_smaller(ssh_public_key k1, ssh_public_key k2){
504
506
    bignum n1;
505
507
    bignum n2;
506
508
    int res;
529
531
}
530
532
 
531
533
#define ABS(A) ( (A)<0 ? -(A):(A) )
532
 
static STRING *encrypt_session_key(SSH_SESSION *session, PUBLIC_KEY *srvkey,
533
 
    PUBLIC_KEY *hostkey, int slen, int hlen) {
 
534
static ssh_string encrypt_session_key(ssh_session session, ssh_public_key srvkey,
 
535
    ssh_public_key hostkey, int slen, int hlen) {
534
536
  unsigned char buffer[32] = {0};
535
537
  int i;
536
 
  STRING *data1 = NULL;
537
 
  STRING *data2 = NULL;
 
538
  ssh_string data1 = NULL;
 
539
  ssh_string data2 = NULL;
538
540
 
539
541
  /* first, generate a session key */
540
542
  ssh_get_random(session->next_crypto->encryptkey, 32, 1);
606
608
 *    32-bit int   supported_authentications_mask
607
609
 */
608
610
 
609
 
int ssh_get_kex1(SSH_SESSION *session) {
610
 
  STRING *server_exp = NULL;
611
 
  STRING *server_mod = NULL;
612
 
  STRING *host_exp = NULL;
613
 
  STRING *host_mod = NULL;
614
 
  STRING *serverkey = NULL;
615
 
  STRING *hostkey = NULL;
616
 
  STRING *enc_session = NULL;
617
 
  PUBLIC_KEY *srv = NULL;
618
 
  PUBLIC_KEY *host = NULL;
619
 
  u32 server_bits;
620
 
  u32 host_bits;
621
 
  u32 protocol_flags;
622
 
  u32 supported_ciphers_mask;
623
 
  u32 supported_authentications_mask;
624
 
  u16 bits;
 
611
int ssh_get_kex1(ssh_session session) {
 
612
  ssh_string server_exp = NULL;
 
613
  ssh_string server_mod = NULL;
 
614
  ssh_string host_exp = NULL;
 
615
  ssh_string host_mod = NULL;
 
616
  ssh_string serverkey = NULL;
 
617
  ssh_string hostkey = NULL;
 
618
  ssh_string enc_session = NULL;
 
619
  ssh_public_key srv = NULL;
 
620
  ssh_public_key host = NULL;
 
621
  uint32_t server_bits;
 
622
  uint32_t host_bits;
 
623
  uint32_t protocol_flags;
 
624
  uint32_t supported_ciphers_mask;
 
625
  uint32_t supported_authentications_mask;
 
626
  uint16_t bits;
625
627
  int rc = -1;
626
628
  int ko;
627
629
 
661
663
  buffer_get_u32(session->in_buffer, &supported_ciphers_mask);
662
664
  ko = buffer_get_u32(session->in_buffer, &supported_authentications_mask);
663
665
 
664
 
  if ((ko != sizeof(u32)) || !host_mod || !host_exp
 
666
  if ((ko != sizeof(uint32_t)) || !host_mod || !host_exp
665
667
      || !server_mod || !server_exp) {
666
668
    ssh_log(session, SSH_LOG_RARE, "Invalid SSH_SMSG_PUBLIC_KEY packet");
667
669
    ssh_set_error(session, SSH_FATAL, "Invalid SSH_SMSG_PUBLIC_KEY packet");
738
740
      bits, string_len(enc_session));
739
741
  bits = htons(bits);
740
742
  /* the encrypted mpint */
741
 
  if (buffer_add_data(session->out_buffer, &bits, sizeof(u16)) < 0) {
 
743
  if (buffer_add_data(session->out_buffer, &bits, sizeof(uint16_t)) < 0) {
742
744
    goto error;
743
745
  }
744
 
  if (buffer_add_data(session->out_buffer, enc_session->string,
 
746
  if (buffer_add_data(session->out_buffer, string_data(enc_session),
745
747
        string_len(enc_session)) < 0) {
746
748
    goto error;
747
749
  }