LCOV - code coverage report
Current view: top level - openssh-6.6p1 - kexgexs.c (source / functions) Hit Total Coverage
Test: lcov_coverage_final.info Lines: 68 84 81.0 %
Date: 2014-08-01 Functions: 1 1 100.0 %
Branches: 17 35 48.6 %

           Branch data     Line data    Source code
       1                 :            : /* $OpenBSD: kexgexs.c,v 1.19 2014/02/02 03:44:31 djm Exp $ */
       2                 :            : /*
       3                 :            :  * Copyright (c) 2000 Niels Provos.  All rights reserved.
       4                 :            :  * Copyright (c) 2001 Markus Friedl.  All rights reserved.
       5                 :            :  *
       6                 :            :  * Redistribution and use in source and binary forms, with or without
       7                 :            :  * modification, are permitted provided that the following conditions
       8                 :            :  * are met:
       9                 :            :  * 1. Redistributions of source code must retain the above copyright
      10                 :            :  *    notice, this list of conditions and the following disclaimer.
      11                 :            :  * 2. Redistributions in binary form must reproduce the above copyright
      12                 :            :  *    notice, this list of conditions and the following disclaimer in the
      13                 :            :  *    documentation and/or other materials provided with the distribution.
      14                 :            :  *
      15                 :            :  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
      16                 :            :  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
      17                 :            :  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
      18                 :            :  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
      19                 :            :  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
      20                 :            :  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
      21                 :            :  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
      22                 :            :  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
      23                 :            :  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
      24                 :            :  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
      25                 :            :  */
      26                 :            : 
      27                 :            : #include "includes.h"
      28                 :            : 
      29                 :            : #include <sys/param.h>
      30                 :            : 
      31                 :            : #include <stdarg.h>
      32                 :            : #include <stdio.h>
      33                 :            : #include <string.h>
      34                 :            : #include <signal.h>
      35                 :            : 
      36                 :            : #include <openssl/dh.h>
      37                 :            : 
      38                 :            : #include "xmalloc.h"
      39                 :            : #include "buffer.h"
      40                 :            : #include "key.h"
      41                 :            : #include "cipher.h"
      42                 :            : #include "kex.h"
      43                 :            : #include "log.h"
      44                 :            : #include "packet.h"
      45                 :            : #include "dh.h"
      46                 :            : #include "ssh2.h"
      47                 :            : #include "compat.h"
      48                 :            : #ifdef GSSAPI
      49                 :            : #include "ssh-gss.h"
      50                 :            : #endif
      51                 :            : #include "monitor_wrap.h"
      52                 :            : 
      53                 :            : void
      54                 :         16 : kexgex_server(Kex *kex)
      55                 :            : {
      56                 :         16 :         BIGNUM *shared_secret = NULL, *dh_client_pub = NULL;
      57                 :            :         Key *server_host_public, *server_host_private;
      58                 :            :         DH *dh;
      59                 :         16 :         u_char *kbuf, *hash, *signature = NULL, *server_host_key_blob = NULL;
      60                 :            :         u_int sbloblen, klen, slen, hashlen;
      61                 :         16 :         int omin = -1, min = -1, omax = -1, max = -1, onbits = -1, nbits = -1;
      62                 :            :         int type, kout;
      63                 :            : 
      64 [ +  - ][ -  + ]:         16 :         if (kex->load_host_public_key == NULL ||
      65                 :         16 :             kex->load_host_private_key == NULL)
      66                 :          0 :                 fatal("Cannot load hostkey");
      67                 :         16 :         server_host_public = kex->load_host_public_key(kex->hostkey_type);
      68         [ -  + ]:         16 :         if (server_host_public == NULL)
      69                 :          0 :                 fatal("Unsupported hostkey type %d", kex->hostkey_type);
      70                 :         16 :         server_host_private = kex->load_host_private_key(kex->hostkey_type);
      71                 :            : 
      72                 :         16 :         type = packet_read();
      73      [ +  -  - ]:         16 :         switch (type) {
      74                 :            :         case SSH2_MSG_KEX_DH_GEX_REQUEST:
      75                 :         16 :                 debug("SSH2_MSG_KEX_DH_GEX_REQUEST received");
      76                 :         16 :                 omin = min = packet_get_int();
      77                 :         16 :                 onbits = nbits = packet_get_int();
      78                 :         16 :                 omax = max = packet_get_int();
      79                 :         16 :                 min = MAX(DH_GRP_MIN, min);
      80                 :         16 :                 max = MIN(DH_GRP_MAX, max);
      81                 :         16 :                 nbits = MAX(DH_GRP_MIN, nbits);
      82                 :         16 :                 nbits = MIN(DH_GRP_MAX, nbits);
      83                 :         16 :                 break;
      84                 :            :         case SSH2_MSG_KEX_DH_GEX_REQUEST_OLD:
      85                 :          0 :                 debug("SSH2_MSG_KEX_DH_GEX_REQUEST_OLD received");
      86                 :          0 :                 onbits = nbits = packet_get_int();
      87                 :            :                 /* unused for old GEX */
      88                 :          0 :                 omin = min = DH_GRP_MIN;
      89                 :          0 :                 omax = max = DH_GRP_MAX;
      90                 :          0 :                 break;
      91                 :            :         default:
      92                 :          0 :                 fatal("protocol error during kex, no DH_GEX_REQUEST: %d", type);
      93                 :            :         }
      94         [ -  + ]:         16 :         packet_check_eom();
      95                 :            : 
      96 [ +  - ][ -  + ]:         16 :         if (omax < omin || onbits < omin || omax < onbits)
      97                 :          0 :                 fatal("DH_GEX_REQUEST, bad parameters: %d !< %d !< %d",
      98                 :            :                     omin, onbits, omax);
      99                 :            : 
     100                 :            :         /* Contact privileged parent */
     101         [ +  - ]:         16 :         dh = PRIVSEP(choose_dh(min, nbits, max));
     102         [ -  + ]:         16 :         if (dh == NULL)
     103                 :          0 :                 packet_disconnect("Protocol error: no matching DH grp found");
     104                 :            : 
     105                 :         16 :         debug("SSH2_MSG_KEX_DH_GEX_GROUP sent");
     106                 :         16 :         packet_start(SSH2_MSG_KEX_DH_GEX_GROUP);
     107                 :         16 :         packet_put_bignum2(dh->p);
     108                 :         16 :         packet_put_bignum2(dh->g);
     109                 :         16 :         packet_send();
     110                 :            : 
     111                 :            :         /* flush */
     112                 :         16 :         packet_write_wait();
     113                 :            : 
     114                 :            :         /* Compute our exchange value in parallel with the client */
     115                 :         16 :         dh_gen_key(dh, kex->we_need * 8);
     116                 :            : 
     117                 :         16 :         debug("expecting SSH2_MSG_KEX_DH_GEX_INIT");
     118                 :         16 :         packet_read_expect(SSH2_MSG_KEX_DH_GEX_INIT);
     119                 :            : 
     120                 :            :         /* key, cert */
     121         [ -  + ]:         16 :         if ((dh_client_pub = BN_new()) == NULL)
     122                 :          0 :                 fatal("dh_client_pub == NULL");
     123                 :         16 :         packet_get_bignum2(dh_client_pub);
     124         [ -  + ]:         16 :         packet_check_eom();
     125                 :            : 
     126                 :            : #ifdef DEBUG_KEXDH
     127                 :            :         fprintf(stderr, "dh_client_pub= ");
     128                 :            :         BN_print_fp(stderr, dh_client_pub);
     129                 :            :         fprintf(stderr, "\n");
     130                 :            :         debug("bits %d", BN_num_bits(dh_client_pub));
     131                 :            : #endif
     132                 :            : 
     133                 :            : #ifdef DEBUG_KEXDH
     134                 :            :         DHparams_print_fp(stderr, dh);
     135                 :            :         fprintf(stderr, "pub= ");
     136                 :            :         BN_print_fp(stderr, dh->pub_key);
     137                 :            :         fprintf(stderr, "\n");
     138                 :            : #endif
     139         [ -  + ]:         16 :         if (!dh_pub_is_valid(dh, dh_client_pub))
     140                 :          0 :                 packet_disconnect("bad client public DH value");
     141                 :            : 
     142                 :         16 :         klen = DH_size(dh);
     143                 :         16 :         kbuf = xmalloc(klen);
     144         [ -  + ]:         16 :         if ((kout = DH_compute_key(kbuf, dh_client_pub, dh)) < 0)
     145                 :          0 :                 fatal("DH_compute_key: failed");
     146                 :            : #ifdef DEBUG_KEXDH
     147                 :            :         dump_digest("shared secret", kbuf, kout);
     148                 :            : #endif
     149         [ -  + ]:         16 :         if ((shared_secret = BN_new()) == NULL)
     150                 :          0 :                 fatal("kexgex_server: BN_new failed");
     151         [ -  + ]:         16 :         if (BN_bin2bn(kbuf, kout, shared_secret) == NULL)
     152                 :          0 :                 fatal("kexgex_server: BN_bin2bn failed");
     153                 :         16 :         explicit_bzero(kbuf, klen);
     154                 :         16 :         free(kbuf);
     155                 :            : 
     156                 :         16 :         key_to_blob(server_host_public, &server_host_key_blob, &sbloblen);
     157                 :            : 
     158         [ -  + ]:         16 :         if (type == SSH2_MSG_KEX_DH_GEX_REQUEST_OLD)
     159                 :          0 :                 omin = min = omax = max = -1;
     160                 :            : 
     161                 :            :         /* calc H */
     162                 :         16 :         kexgex_hash(
     163                 :            :             kex->hash_alg,
     164                 :            :             kex->client_version_string,
     165                 :            :             kex->server_version_string,
     166                 :         16 :             buffer_ptr(&kex->peer), buffer_len(&kex->peer),
     167                 :         16 :             buffer_ptr(&kex->my), buffer_len(&kex->my),
     168                 :            :             server_host_key_blob, sbloblen,
     169                 :            :             omin, onbits, omax,
     170                 :            :             dh->p, dh->g,
     171                 :            :             dh_client_pub,
     172                 :            :             dh->pub_key,
     173                 :            :             shared_secret,
     174                 :            :             &hash, &hashlen
     175                 :            :         );
     176                 :         16 :         BN_clear_free(dh_client_pub);
     177                 :            : 
     178                 :            :         /* save session id := H */
     179         [ +  - ]:         16 :         if (kex->session_id == NULL) {
     180                 :         16 :                 kex->session_id_len = hashlen;
     181                 :         16 :                 kex->session_id = xmalloc(kex->session_id_len);
     182                 :         16 :                 memcpy(kex->session_id, hash, kex->session_id_len);
     183                 :            :         }
     184                 :            : 
     185                 :            :         /* sign H */
     186                 :         16 :         kex->sign(server_host_private, server_host_public, &signature, &slen,
     187                 :            :             hash, hashlen);
     188                 :            : 
     189                 :            :         /* destroy_sensitive_data(); */
     190                 :            : 
     191                 :            :         /* send server hostkey, DH pubkey 'f' and singed H */
     192                 :         16 :         debug("SSH2_MSG_KEX_DH_GEX_REPLY sent");
     193                 :         16 :         packet_start(SSH2_MSG_KEX_DH_GEX_REPLY);
     194                 :         16 :         packet_put_string(server_host_key_blob, sbloblen);
     195                 :         16 :         packet_put_bignum2(dh->pub_key);     /* f */
     196                 :         16 :         packet_put_string(signature, slen);
     197                 :         16 :         packet_send();
     198                 :            : 
     199                 :         16 :         free(signature);
     200                 :         16 :         free(server_host_key_blob);
     201                 :            :         /* have keys, free DH */
     202                 :         16 :         DH_free(dh);
     203                 :            : 
     204                 :         16 :         kex_derive_keys_bn(kex, hash, hashlen, shared_secret);
     205                 :         16 :         BN_clear_free(shared_secret);
     206                 :            : 
     207                 :         16 :         kex_finish(kex);
     208                 :         16 : }

Generated by: LCOV version 1.9