Diff

Differences From Artifact [5fabc46d13]:

To Artifact [a767cecdef]:


100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
extern struct ssh_hostkey ssh_dss;
struct ssh_hostkey *hostkey_algs[] = { &ssh_dss };

extern struct ssh_mac ssh_sha1;

SHA_State exhash;

static void nullmac_sesskey(unsigned char *key, int len) { }
static void nullmac_generate(unsigned char *blk, int len, unsigned long seq) { }
static int nullmac_verify(unsigned char *blk, int len, unsigned long seq) { return 1; }
struct ssh_mac ssh_mac_none = {
    nullmac_sesskey, nullmac_generate, nullmac_verify, "none", 0
};
struct ssh_mac *macs[] = { &ssh_sha1, &ssh_mac_none };

struct ssh_compress ssh_comp_none = {
    "none"
};
struct ssh_compress *compressions[] = { &ssh_comp_none };







|



|







100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
extern struct ssh_hostkey ssh_dss;
struct ssh_hostkey *hostkey_algs[] = { &ssh_dss };

extern struct ssh_mac ssh_sha1;

SHA_State exhash;

static void nullmac_key(unsigned char *key) { }
static void nullmac_generate(unsigned char *blk, int len, unsigned long seq) { }
static int nullmac_verify(unsigned char *blk, int len, unsigned long seq) { return 1; }
struct ssh_mac ssh_mac_none = {
    nullmac_key, nullmac_key, nullmac_generate, nullmac_verify, "none", 0
};
struct ssh_mac *macs[] = { &ssh_sha1, &ssh_mac_none };

struct ssh_compress ssh_comp_none = {
    "none"
};
struct ssh_compress *compressions[] = { &ssh_comp_none };
342
343
344
345
346
347
348




349
350




351
352
353
354
355
356
357
        (*datalen)--;
    }
#ifdef FWHACK
    if (!memcmp(pktin.data, "Remo", 4)) {/* "Remo"te server has closed ... */
        /* FIXME */
    }
#endif




    if (sccipher)
        sccipher->decrypt(pktin.data, cipherblk);





    /*
     * Now get the length and padding figures.
     */
    len = GET_32BIT(pktin.data);
    pad = pktin.data[4];








>
>
>
>


>
>
>
>







342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
        (*datalen)--;
    }
#ifdef FWHACK
    if (!memcmp(pktin.data, "Remo", 4)) {/* "Remo"te server has closed ... */
        /* FIXME */
    }
#endif
    debug(("Got initblk:"));
    for (i = 0; i < cipherblk; i++)
        debug(("  %02x", (unsigned char)pktin.data[i]));
    debug(("\r\n"));
    if (sccipher)
        sccipher->decrypt(pktin.data, cipherblk);
    debug(("Decrypted initblk:"));
    for (i = 0; i < cipherblk; i++)
        debug(("  %02x", (unsigned char)pktin.data[i]));
    debug(("\r\n"));

    /*
     * Now get the length and padding figures.
     */
    len = GET_32BIT(pktin.data);
    pad = pktin.data[4];

388
389
390
391
392
393
394





395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
	pktin.data[i] = *(*data)++;
        (*datalen)--;
    }
    /* Decrypt everything _except_ the MAC. */
    if (sccipher)
        sccipher->decrypt(pktin.data + cipherblk, packetlen - cipherblk);






    /*
     * Check the MAC.
     */
    if (scmac && !scmac->verify(pktin.data, incoming_sequence++, len+4))
	fatalbox("Incorrect MAC received on packet");

    pktin.savedpos = 6;
    pktin.type = pktin.data[5];
#if 0
    debug(("Got packet len=%d pad=%d\r\n", len, pad));
    for (i = 0; i < payload; i++)
        debug(("  %02x", (unsigned char)pktin.data[i]));
    debug(("\r\n"));
#endif

    /*
     * FIXME: handle IGNORE and DEBUG messages.
     */

    crFinish(0);
}







>
>
>
>
>








<
<
<
<
<
<







396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415






416
417
418
419
420
421
422
	pktin.data[i] = *(*data)++;
        (*datalen)--;
    }
    /* Decrypt everything _except_ the MAC. */
    if (sccipher)
        sccipher->decrypt(pktin.data + cipherblk, packetlen - cipherblk);

    debug(("Got packet len=%d pad=%d\r\n", len, pad));
    for (i = 0; i < packetlen; i++)
        debug(("  %02x", (unsigned char)pktin.data[i]));
    debug(("\r\n"));

    /*
     * Check the MAC.
     */
    if (scmac && !scmac->verify(pktin.data, incoming_sequence++, len+4))
	fatalbox("Incorrect MAC received on packet");

    pktin.savedpos = 6;
    pktin.type = pktin.data[5];







    /*
     * FIXME: handle IGNORE and DEBUG messages.
     */

    crFinish(0);
}
1305
1306
1307
1308
1309
1310
1311




















1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329

1330
1331
1332
1333
1334
1335
1336
        while (haylen > 0 && *haystack != ',')
            haylen--, haystack++;
        if (haylen == 0)
            return 0;
        haylen--, haystack++;          /* skip over comma itself */
    }
}





















/*
 * Handle the SSH2 key exchange phase.
 */
static int do_ssh2_kex(unsigned char *in, int inlen, int ispkt)
{
    static int i, len;
    static char *str;
    static Bignum e, f, K;
    static struct ssh_cipher *cscipher_tobe = NULL;
    static struct ssh_cipher *sccipher_tobe = NULL;
    static struct ssh_mac *csmac_tobe = NULL;
    static struct ssh_mac *scmac_tobe = NULL;
    static struct ssh_compress *cscomp_tobe = NULL;
    static struct ssh_compress *sccomp_tobe = NULL;
    static char *hostkeydata, *sigdata;
    static int hostkeylen, siglen;
    static unsigned char exchange_hash[20];


    crBegin;

    /*
     * Construct and send our key exchange packet.
     */
    ssh2_pkt_init(SSH2_MSG_KEXINIT);







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>


















>







1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
        while (haylen > 0 && *haystack != ',')
            haylen--, haystack++;
        if (haylen == 0)
            return 0;
        haylen--, haystack++;          /* skip over comma itself */
    }
}

/*
 * SSH2 key creation method.
 */
void ssh2_mkkey(Bignum K, char *H, char chr, char *keyspace) {
    SHA_State s;
    /* First 20 bytes. */
    SHA_Init(&s);
    sha_mpint(&s, K);
    SHA_Bytes(&s, H, 20);
    SHA_Bytes(&s, &chr, 1);
    SHA_Bytes(&s, H, 20);
    SHA_Final(&s, keyspace);
    /* Next 20 bytes. */
    SHA_Init(&s);
    sha_mpint(&s, K);
    SHA_Bytes(&s, H, 20);
    SHA_Bytes(&s, keyspace, 20);
    SHA_Final(&s, keyspace+20);
}

/*
 * Handle the SSH2 key exchange phase.
 */
static int do_ssh2_kex(unsigned char *in, int inlen, int ispkt)
{
    static int i, len;
    static char *str;
    static Bignum e, f, K;
    static struct ssh_cipher *cscipher_tobe = NULL;
    static struct ssh_cipher *sccipher_tobe = NULL;
    static struct ssh_mac *csmac_tobe = NULL;
    static struct ssh_mac *scmac_tobe = NULL;
    static struct ssh_compress *cscomp_tobe = NULL;
    static struct ssh_compress *sccomp_tobe = NULL;
    static char *hostkeydata, *sigdata;
    static int hostkeylen, siglen;
    static unsigned char exchange_hash[20];
    static unsigned char keyspace[40];

    crBegin;

    /*
     * Construct and send our key exchange packet.
     */
    ssh2_pkt_init(SSH2_MSG_KEXINIT);
1515
1516
1517
1518
1519
1520
1521
1522


1523
1524
































1525
1526
1527
1528
1529
1530
1531

    debug(("Exchange hash is:\r\n"));
    for (i = 0; i < 20; i++)
        debug((" %02x", exchange_hash[i]));
    debug(("\r\n"));

    /*
     * FIXME: verify hostkeydata and sigdata.


     */
    
































    crWaitUntil(0);

    crFinish(1);
}

static void ssh2_protocol(unsigned char *in, int inlen, int ispkt) {
    crBegin;







|
>
>

|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593

    debug(("Exchange hash is:\r\n"));
    for (i = 0; i < 20; i++)
        debug((" %02x", exchange_hash[i]));
    debug(("\r\n"));

    /*
     * FIXME: verify host key. This bit will be moderately
     * unpleasant, because of having to rewrite it to work
     * alongside the old scheme.
     */

    /*
     * FIXME: verify signature of exchange hash.
     */

    /*
     * Send SSH2_MSG_NEWKEYS. Expect it from server.
     */
    ssh2_pkt_init(SSH2_MSG_NEWKEYS);
    ssh2_pkt_send();
    crWaitUntil(ispkt);
    if (pktin.type != SSH2_MSG_NEWKEYS)
        fatalbox("expected new-keys packet from server");

    /*
     * Create and initialise session keys.
     */
    cscipher = cscipher_tobe;
    sccipher = sccipher_tobe;
    csmac = csmac_tobe;
    scmac = scmac_tobe;
    cscomp = cscomp_tobe;
    sccomp = sccomp_tobe;
    /*
     * Set IVs after keys.
     */
    ssh2_mkkey(K, exchange_hash, 'C', keyspace); cscipher->setcskey(keyspace);
    ssh2_mkkey(K, exchange_hash, 'D', keyspace); cscipher->setsckey(keyspace);
    ssh2_mkkey(K, exchange_hash, 'A', keyspace); cscipher->setcsiv(keyspace);
    ssh2_mkkey(K, exchange_hash, 'B', keyspace); sccipher->setsciv(keyspace);
    ssh2_mkkey(K, exchange_hash, 'E', keyspace); csmac->setcskey(keyspace);
    ssh2_mkkey(K, exchange_hash, 'F', keyspace); scmac->setsckey(keyspace);

    crWaitUntil(0);

    crFinish(1);
}

static void ssh2_protocol(unsigned char *in, int inlen, int ispkt) {
    crBegin;