[BACK]Return to monitor_wrap.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Annotation of src/usr.bin/ssh/monitor_wrap.c, Revision 1.71

1.71    ! djm         1: /* $OpenBSD: monitor_wrap.c,v 1.70 2010/08/31 11:54:45 djm Exp $ */
1.1       provos      2: /*
                      3:  * Copyright 2002 Niels Provos <provos@citi.umich.edu>
                      4:  * Copyright 2002 Markus Friedl <markus@openbsd.org>
                      5:  * All rights reserved.
                      6:  *
                      7:  * Redistribution and use in source and binary forms, with or without
                      8:  * modification, are permitted provided that the following conditions
                      9:  * are met:
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. Redistributions in binary form must reproduce the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer in the
                     14:  *    documentation and/or other materials provided with the distribution.
                     15:  *
                     16:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     17:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     18:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     19:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     20:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     21:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     22:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     23:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     24:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     25:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     26:  */
                     27:
1.46      stevesk    28: #include <sys/types.h>
1.51      dtucker    29: #include <sys/uio.h>
1.61      djm        30: #include <sys/queue.h>
1.46      stevesk    31:
1.47      stevesk    32: #include <errno.h>
1.46      stevesk    33: #include <pwd.h>
1.50      deraadt    34: #include <signal.h>
1.49      stevesk    35: #include <stdio.h>
1.48      stevesk    36: #include <string.h>
1.51      dtucker    37: #include <unistd.h>
1.68      dtucker    38:
                     39: #include <openssl/bn.h>
                     40: #include <openssl/dh.h>
                     41: #include <openssl/evp.h>
1.1       provos     42:
1.50      deraadt    43: #include "xmalloc.h"
1.1       provos     44: #include "ssh.h"
                     45: #include "dh.h"
1.50      deraadt    46: #include "buffer.h"
                     47: #include "key.h"
                     48: #include "cipher.h"
1.1       provos     49: #include "kex.h"
1.50      deraadt    50: #include "hostfile.h"
1.1       provos     51: #include "auth.h"
1.22      markus     52: #include "auth-options.h"
1.1       provos     53: #include "packet.h"
                     54: #include "mac.h"
                     55: #include "log.h"
1.54      miod       56: #include <zlib.h>
1.1       provos     57: #include "monitor.h"
1.50      deraadt    58: #ifdef GSSAPI
                     59: #include "ssh-gss.h"
                     60: #endif
1.1       provos     61: #include "monitor_wrap.h"
                     62: #include "atomicio.h"
                     63: #include "monitor_fdpass.h"
1.45      djm        64: #include "misc.h"
1.65      djm        65: #include "schnorr.h"
1.64      djm        66: #include "jpake.h"
1.70      djm        67: #include "uuencode.h"
1.1       provos     68:
                     69: #include "channels.h"
                     70: #include "session.h"
1.55      dtucker    71: #include "servconf.h"
1.67      andreas    72: #include "roaming.h"
1.29      markus     73:
1.1       provos     74: /* Imports */
                     75: extern int compat20;
                     76: extern z_stream incoming_stream;
                     77: extern z_stream outgoing_stream;
1.7       mouring    78: extern struct monitor *pmonitor;
1.39      dtucker    79: extern Buffer loginmsg;
1.55      dtucker    80: extern ServerOptions options;
1.1       provos     81:
1.32      markus     82: int
                     83: mm_is_monitor(void)
                     84: {
                     85:        /*
                     86:         * m_pid is only set in the privileged part, and
                     87:         * points to the unprivileged child.
                     88:         */
1.34      markus     89:        return (pmonitor && pmonitor->m_pid > 0);
1.32      markus     90: }
                     91:
1.1       provos     92: void
1.36      avsm       93: mm_request_send(int sock, enum monitor_reqtype type, Buffer *m)
1.1       provos     94: {
1.13      deraadt    95:        u_int mlen = buffer_len(m);
1.1       provos     96:        u_char buf[5];
                     97:
1.8       markus     98:        debug3("%s entering: type %d", __func__, type);
1.1       provos     99:
1.45      djm       100:        put_u32(buf, mlen + 1);
1.10      deraadt   101:        buf[4] = (u_char) type;         /* 1st byte of payload is mesg-type */
1.36      avsm      102:        if (atomicio(vwrite, sock, buf, sizeof(buf)) != sizeof(buf))
1.40      avsm      103:                fatal("%s: write: %s", __func__, strerror(errno));
1.36      avsm      104:        if (atomicio(vwrite, sock, buffer_ptr(m), mlen) != mlen)
1.40      avsm      105:                fatal("%s: write: %s", __func__, strerror(errno));
1.1       provos    106: }
                    107:
                    108: void
1.36      avsm      109: mm_request_receive(int sock, Buffer *m)
1.1       provos    110: {
                    111:        u_char buf[4];
1.13      deraadt   112:        u_int msg_len;
1.1       provos    113:
1.8       markus    114:        debug3("%s entering", __func__);
1.1       provos    115:
1.40      avsm      116:        if (atomicio(read, sock, buf, sizeof(buf)) != sizeof(buf)) {
                    117:                if (errno == EPIPE)
1.32      markus    118:                        cleanup_exit(255);
1.40      avsm      119:                fatal("%s: read: %s", __func__, strerror(errno));
1.1       provos    120:        }
1.45      djm       121:        msg_len = get_u32(buf);
1.1       provos    122:        if (msg_len > 256 * 1024)
1.8       markus    123:                fatal("%s: read: bad msg_len %d", __func__, msg_len);
1.1       provos    124:        buffer_clear(m);
                    125:        buffer_append_space(m, msg_len);
1.40      avsm      126:        if (atomicio(read, sock, buffer_ptr(m), msg_len) != msg_len)
                    127:                fatal("%s: read: %s", __func__, strerror(errno));
1.1       provos    128: }
                    129:
                    130: void
1.36      avsm      131: mm_request_receive_expect(int sock, enum monitor_reqtype type, Buffer *m)
1.1       provos    132: {
                    133:        u_char rtype;
                    134:
1.8       markus    135:        debug3("%s entering: type %d", __func__, type);
1.1       provos    136:
1.36      avsm      137:        mm_request_receive(sock, m);
1.1       provos    138:        rtype = buffer_get_char(m);
                    139:        if (rtype != type)
1.8       markus    140:                fatal("%s: read: rtype %d != type %d", __func__,
1.1       provos    141:                    rtype, type);
                    142: }
                    143:
                    144: DH *
                    145: mm_choose_dh(int min, int nbits, int max)
                    146: {
                    147:        BIGNUM *p, *g;
                    148:        int success = 0;
                    149:        Buffer m;
                    150:
                    151:        buffer_init(&m);
                    152:        buffer_put_int(&m, min);
                    153:        buffer_put_int(&m, nbits);
                    154:        buffer_put_int(&m, max);
                    155:
1.7       mouring   156:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_MODULI, &m);
1.1       provos    157:
1.8       markus    158:        debug3("%s: waiting for MONITOR_ANS_MODULI", __func__);
1.7       mouring   159:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_MODULI, &m);
1.1       provos    160:
                    161:        success = buffer_get_char(&m);
                    162:        if (success == 0)
1.8       markus    163:                fatal("%s: MONITOR_ANS_MODULI failed", __func__);
1.1       provos    164:
                    165:        if ((p = BN_new()) == NULL)
1.8       markus    166:                fatal("%s: BN_new failed", __func__);
1.3       markus    167:        if ((g = BN_new()) == NULL)
1.8       markus    168:                fatal("%s: BN_new failed", __func__);
1.1       provos    169:        buffer_get_bignum2(&m, p);
                    170:        buffer_get_bignum2(&m, g);
                    171:
1.8       markus    172:        debug3("%s: remaining %d", __func__, buffer_len(&m));
1.1       provos    173:        buffer_free(&m);
                    174:
                    175:        return (dh_new_group(g, p));
                    176: }
                    177:
                    178: int
                    179: mm_key_sign(Key *key, u_char **sigp, u_int *lenp, u_char *data, u_int datalen)
                    180: {
1.7       mouring   181:        Kex *kex = *pmonitor->m_pkex;
1.1       provos    182:        Buffer m;
                    183:
1.8       markus    184:        debug3("%s entering", __func__);
1.1       provos    185:
                    186:        buffer_init(&m);
                    187:        buffer_put_int(&m, kex->host_key_index(key));
                    188:        buffer_put_string(&m, data, datalen);
                    189:
1.7       mouring   190:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SIGN, &m);
1.1       provos    191:
1.8       markus    192:        debug3("%s: waiting for MONITOR_ANS_SIGN", __func__);
1.7       mouring   193:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SIGN, &m);
1.1       provos    194:        *sigp  = buffer_get_string(&m, lenp);
                    195:        buffer_free(&m);
                    196:
                    197:        return (0);
                    198: }
                    199:
                    200: struct passwd *
1.37      dtucker   201: mm_getpwnamallow(const char *username)
1.1       provos    202: {
                    203:        Buffer m;
                    204:        struct passwd *pw;
1.55      dtucker   205:        u_int len;
                    206:        ServerOptions *newopts;
1.1       provos    207:
1.8       markus    208:        debug3("%s entering", __func__);
1.1       provos    209:
                    210:        buffer_init(&m);
1.37      dtucker   211:        buffer_put_cstring(&m, username);
1.1       provos    212:
1.7       mouring   213:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PWNAM, &m);
1.1       provos    214:
1.8       markus    215:        debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__);
1.7       mouring   216:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, &m);
1.1       provos    217:
                    218:        if (buffer_get_char(&m) == 0) {
1.60      dtucker   219:                pw = NULL;
                    220:                goto out;
1.1       provos    221:        }
1.55      dtucker   222:        pw = buffer_get_string(&m, &len);
                    223:        if (len != sizeof(struct passwd))
1.8       markus    224:                fatal("%s: struct passwd size mismatch", __func__);
1.1       provos    225:        pw->pw_name = buffer_get_string(&m, NULL);
                    226:        pw->pw_passwd = buffer_get_string(&m, NULL);
                    227:        pw->pw_gecos = buffer_get_string(&m, NULL);
                    228:        pw->pw_class = buffer_get_string(&m, NULL);
                    229:        pw->pw_dir = buffer_get_string(&m, NULL);
                    230:        pw->pw_shell = buffer_get_string(&m, NULL);
1.55      dtucker   231:
1.60      dtucker   232: out:
1.55      dtucker   233:        /* copy options block as a Match directive may have changed some */
                    234:        newopts = buffer_get_string(&m, &len);
                    235:        if (len != sizeof(*newopts))
                    236:                fatal("%s: option block size mismatch", __func__);
1.71    ! djm       237:
        !           238: #define M_CP_STROPT(x) do { \
        !           239:                if (newopts->x != NULL) \
        !           240:                        newopts->x = buffer_get_string(&m, NULL); \
        !           241:        } while (0)
        !           242:        /* See comment in servconf.h */
        !           243:        COPY_MATCH_STRING_OPTS();
        !           244: #undef M_CP_STROPT
        !           245:
1.55      dtucker   246:        copy_set_server_options(&options, newopts, 1);
                    247:        xfree(newopts);
                    248:
1.1       provos    249:        buffer_free(&m);
                    250:
                    251:        return (pw);
1.6       djm       252: }
                    253:
1.33      markus    254: char *
                    255: mm_auth2_read_banner(void)
1.6       djm       256: {
                    257:        Buffer m;
                    258:        char *banner;
                    259:
1.8       markus    260:        debug3("%s entering", __func__);
1.6       djm       261:
                    262:        buffer_init(&m);
1.7       mouring   263:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, &m);
1.6       djm       264:        buffer_clear(&m);
                    265:
1.33      markus    266:        mm_request_receive_expect(pmonitor->m_recvfd,
                    267:            MONITOR_ANS_AUTH2_READ_BANNER, &m);
1.6       djm       268:        banner = buffer_get_string(&m, NULL);
                    269:        buffer_free(&m);
1.10      deraadt   270:
1.33      markus    271:        /* treat empty banner as missing banner */
                    272:        if (strlen(banner) == 0) {
                    273:                xfree(banner);
                    274:                banner = NULL;
                    275:        }
1.6       djm       276:        return (banner);
1.1       provos    277: }
                    278:
                    279: /* Inform the privileged process about service and style */
                    280:
                    281: void
                    282: mm_inform_authserv(char *service, char *style)
                    283: {
                    284:        Buffer m;
                    285:
1.8       markus    286:        debug3("%s entering", __func__);
1.1       provos    287:
                    288:        buffer_init(&m);
                    289:        buffer_put_cstring(&m, service);
                    290:        buffer_put_cstring(&m, style ? style : "");
                    291:
1.7       mouring   292:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, &m);
1.1       provos    293:
                    294:        buffer_free(&m);
                    295: }
                    296:
                    297: /* Do the password authentication */
                    298: int
                    299: mm_auth_password(Authctxt *authctxt, char *password)
                    300: {
                    301:        Buffer m;
                    302:        int authenticated = 0;
                    303:
1.8       markus    304:        debug3("%s entering", __func__);
1.1       provos    305:
                    306:        buffer_init(&m);
                    307:        buffer_put_cstring(&m, password);
1.7       mouring   308:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, &m);
1.1       provos    309:
1.8       markus    310:        debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__);
1.7       mouring   311:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUTHPASSWORD, &m);
1.1       provos    312:
                    313:        authenticated = buffer_get_int(&m);
                    314:
                    315:        buffer_free(&m);
                    316:
1.3       markus    317:        debug3("%s: user %sauthenticated",
1.8       markus    318:            __func__, authenticated ? "" : "not ");
1.1       provos    319:        return (authenticated);
                    320: }
                    321:
                    322: int
                    323: mm_user_key_allowed(struct passwd *pw, Key *key)
                    324: {
                    325:        return (mm_key_allowed(MM_USERKEY, NULL, NULL, key));
                    326: }
                    327:
                    328: int
                    329: mm_hostbased_key_allowed(struct passwd *pw, char *user, char *host,
                    330:     Key *key)
                    331: {
                    332:        return (mm_key_allowed(MM_HOSTKEY, user, host, key));
                    333: }
                    334:
                    335: int
                    336: mm_auth_rhosts_rsa_key_allowed(struct passwd *pw, char *user,
                    337:     char *host, Key *key)
                    338: {
                    339:        int ret;
                    340:
                    341:        key->type = KEY_RSA; /* XXX hack for key_to_blob */
                    342:        ret = mm_key_allowed(MM_RSAHOSTKEY, user, host, key);
                    343:        key->type = KEY_RSA1;
                    344:        return (ret);
                    345: }
                    346:
                    347: int
                    348: mm_key_allowed(enum mm_keytype type, char *user, char *host, Key *key)
                    349: {
                    350:        Buffer m;
                    351:        u_char *blob;
                    352:        u_int len;
1.22      markus    353:        int allowed = 0, have_forced = 0;
1.1       provos    354:
1.8       markus    355:        debug3("%s entering", __func__);
1.1       provos    356:
                    357:        /* Convert the key to a blob and the pass it over */
                    358:        if (!key_to_blob(key, &blob, &len))
                    359:                return (0);
                    360:
                    361:        buffer_init(&m);
                    362:        buffer_put_int(&m, type);
                    363:        buffer_put_cstring(&m, user ? user : "");
                    364:        buffer_put_cstring(&m, host ? host : "");
                    365:        buffer_put_string(&m, blob, len);
                    366:        xfree(blob);
                    367:
1.7       mouring   368:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, &m);
1.1       provos    369:
1.8       markus    370:        debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__);
1.7       mouring   371:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYALLOWED, &m);
1.1       provos    372:
                    373:        allowed = buffer_get_int(&m);
                    374:
1.22      markus    375:        /* fake forced command */
                    376:        auth_clear_options();
                    377:        have_forced = buffer_get_int(&m);
                    378:        forced_command = have_forced ? xstrdup("true") : NULL;
                    379:
1.1       provos    380:        buffer_free(&m);
                    381:
                    382:        return (allowed);
                    383: }
                    384:
1.3       markus    385: /*
1.1       provos    386:  * This key verify needs to send the key type along, because the
                    387:  * privileged parent makes the decision if the key is allowed
                    388:  * for authentication.
                    389:  */
                    390:
                    391: int
                    392: mm_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
                    393: {
                    394:        Buffer m;
                    395:        u_char *blob;
                    396:        u_int len;
                    397:        int verified = 0;
                    398:
1.8       markus    399:        debug3("%s entering", __func__);
1.1       provos    400:
                    401:        /* Convert the key to a blob and the pass it over */
                    402:        if (!key_to_blob(key, &blob, &len))
                    403:                return (0);
                    404:
                    405:        buffer_init(&m);
                    406:        buffer_put_string(&m, blob, len);
                    407:        buffer_put_string(&m, sig, siglen);
                    408:        buffer_put_string(&m, data, datalen);
                    409:        xfree(blob);
                    410:
1.7       mouring   411:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, &m);
1.1       provos    412:
1.8       markus    413:        debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__);
1.7       mouring   414:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYVERIFY, &m);
1.1       provos    415:
                    416:        verified = buffer_get_int(&m);
                    417:
                    418:        buffer_free(&m);
                    419:
                    420:        return (verified);
                    421: }
                    422:
                    423: /* Export key state after authentication */
                    424: Newkeys *
                    425: mm_newkeys_from_blob(u_char *blob, int blen)
                    426: {
                    427:        Buffer b;
                    428:        u_int len;
                    429:        Newkeys *newkey = NULL;
                    430:        Enc *enc;
                    431:        Mac *mac;
                    432:        Comp *comp;
                    433:
1.8       markus    434:        debug3("%s: %p(%d)", __func__, blob, blen);
1.1       provos    435: #ifdef DEBUG_PK
                    436:        dump_base64(stderr, blob, blen);
                    437: #endif
                    438:        buffer_init(&b);
                    439:        buffer_append(&b, blob, blen);
                    440:
                    441:        newkey = xmalloc(sizeof(*newkey));
                    442:        enc = &newkey->enc;
                    443:        mac = &newkey->mac;
                    444:        comp = &newkey->comp;
                    445:
                    446:        /* Enc structure */
                    447:        enc->name = buffer_get_string(&b, NULL);
                    448:        buffer_get(&b, &enc->cipher, sizeof(enc->cipher));
                    449:        enc->enabled = buffer_get_int(&b);
                    450:        enc->block_size = buffer_get_int(&b);
                    451:        enc->key = buffer_get_string(&b, &enc->key_len);
                    452:        enc->iv = buffer_get_string(&b, &len);
                    453:        if (len != enc->block_size)
1.12      deraadt   454:                fatal("%s: bad ivlen: expected %u != %u", __func__,
1.1       provos    455:                    enc->block_size, len);
                    456:
                    457:        if (enc->name == NULL || cipher_by_name(enc->name) != enc->cipher)
1.8       markus    458:                fatal("%s: bad cipher name %s or pointer %p", __func__,
1.1       provos    459:                    enc->name, enc->cipher);
                    460:
                    461:        /* Mac structure */
                    462:        mac->name = buffer_get_string(&b, NULL);
1.56      djm       463:        if (mac->name == NULL || mac_setup(mac, mac->name) == -1)
1.57      pvalchev  464:                fatal("%s: can not setup mac %s", __func__, mac->name);
1.1       provos    465:        mac->enabled = buffer_get_int(&b);
                    466:        mac->key = buffer_get_string(&b, &len);
                    467:        if (len > mac->key_len)
1.12      deraadt   468:                fatal("%s: bad mac key length: %u > %d", __func__, len,
1.1       provos    469:                    mac->key_len);
                    470:        mac->key_len = len;
                    471:
                    472:        /* Comp structure */
                    473:        comp->type = buffer_get_int(&b);
                    474:        comp->enabled = buffer_get_int(&b);
                    475:        comp->name = buffer_get_string(&b, NULL);
                    476:
                    477:        len = buffer_len(&b);
                    478:        if (len != 0)
1.12      deraadt   479:                error("newkeys_from_blob: remaining bytes in blob %u", len);
1.1       provos    480:        buffer_free(&b);
                    481:        return (newkey);
                    482: }
                    483:
                    484: int
                    485: mm_newkeys_to_blob(int mode, u_char **blobp, u_int *lenp)
                    486: {
                    487:        Buffer b;
                    488:        int len;
                    489:        Enc *enc;
                    490:        Mac *mac;
                    491:        Comp *comp;
1.66      andreas   492:        Newkeys *newkey = (Newkeys *)packet_get_newkeys(mode);
1.1       provos    493:
1.8       markus    494:        debug3("%s: converting %p", __func__, newkey);
1.1       provos    495:
                    496:        if (newkey == NULL) {
1.8       markus    497:                error("%s: newkey == NULL", __func__);
1.1       provos    498:                return 0;
                    499:        }
                    500:        enc = &newkey->enc;
                    501:        mac = &newkey->mac;
                    502:        comp = &newkey->comp;
                    503:
                    504:        buffer_init(&b);
                    505:        /* Enc structure */
                    506:        buffer_put_cstring(&b, enc->name);
                    507:        /* The cipher struct is constant and shared, you export pointer */
                    508:        buffer_append(&b, &enc->cipher, sizeof(enc->cipher));
                    509:        buffer_put_int(&b, enc->enabled);
                    510:        buffer_put_int(&b, enc->block_size);
                    511:        buffer_put_string(&b, enc->key, enc->key_len);
                    512:        packet_get_keyiv(mode, enc->iv, enc->block_size);
                    513:        buffer_put_string(&b, enc->iv, enc->block_size);
                    514:
                    515:        /* Mac structure */
                    516:        buffer_put_cstring(&b, mac->name);
                    517:        buffer_put_int(&b, mac->enabled);
                    518:        buffer_put_string(&b, mac->key, mac->key_len);
                    519:
                    520:        /* Comp structure */
                    521:        buffer_put_int(&b, comp->type);
                    522:        buffer_put_int(&b, comp->enabled);
                    523:        buffer_put_cstring(&b, comp->name);
                    524:
                    525:        len = buffer_len(&b);
1.16      markus    526:        if (lenp != NULL)
                    527:                *lenp = len;
                    528:        if (blobp != NULL) {
                    529:                *blobp = xmalloc(len);
                    530:                memcpy(*blobp, buffer_ptr(&b), len);
                    531:        }
1.1       provos    532:        memset(buffer_ptr(&b), 0, len);
                    533:        buffer_free(&b);
                    534:        return len;
                    535: }
                    536:
1.2       markus    537: static void
1.1       provos    538: mm_send_kex(Buffer *m, Kex *kex)
                    539: {
                    540:        buffer_put_string(m, kex->session_id, kex->session_id_len);
                    541:        buffer_put_int(m, kex->we_need);
                    542:        buffer_put_int(m, kex->hostkey_type);
                    543:        buffer_put_int(m, kex->kex_type);
                    544:        buffer_put_string(m, buffer_ptr(&kex->my), buffer_len(&kex->my));
                    545:        buffer_put_string(m, buffer_ptr(&kex->peer), buffer_len(&kex->peer));
                    546:        buffer_put_int(m, kex->flags);
                    547:        buffer_put_cstring(m, kex->client_version_string);
                    548:        buffer_put_cstring(m, kex->server_version_string);
                    549: }
                    550:
                    551: void
1.36      avsm      552: mm_send_keystate(struct monitor *monitor)
1.1       provos    553: {
1.66      andreas   554:        Buffer m, *input, *output;
1.1       provos    555:        u_char *blob, *p;
                    556:        u_int bloblen, plen;
1.25      markus    557:        u_int32_t seqnr, packets;
1.63      markus    558:        u_int64_t blocks, bytes;
1.1       provos    559:
                    560:        buffer_init(&m);
                    561:
                    562:        if (!compat20) {
                    563:                u_char iv[24];
1.11      markus    564:                u_char *key;
                    565:                u_int ivlen, keylen;
1.1       provos    566:
                    567:                buffer_put_int(&m, packet_get_protocol_flags());
                    568:
                    569:                buffer_put_int(&m, packet_get_ssh1_cipher());
                    570:
1.11      markus    571:                debug3("%s: Sending ssh1 KEY+IV", __func__);
                    572:                keylen = packet_get_encryption_key(NULL);
                    573:                key = xmalloc(keylen+1);        /* add 1 if keylen == 0 */
                    574:                keylen = packet_get_encryption_key(key);
                    575:                buffer_put_string(&m, key, keylen);
                    576:                memset(key, 0, keylen);
                    577:                xfree(key);
                    578:
1.1       provos    579:                ivlen = packet_get_keyiv_len(MODE_OUT);
                    580:                packet_get_keyiv(MODE_OUT, iv, ivlen);
                    581:                buffer_put_string(&m, iv, ivlen);
                    582:                ivlen = packet_get_keyiv_len(MODE_OUT);
                    583:                packet_get_keyiv(MODE_IN, iv, ivlen);
                    584:                buffer_put_string(&m, iv, ivlen);
                    585:                goto skip;
                    586:        } else {
                    587:                /* Kex for rekeying */
1.36      avsm      588:                mm_send_kex(&m, *monitor->m_pkex);
1.1       provos    589:        }
                    590:
                    591:        debug3("%s: Sending new keys: %p %p",
1.66      andreas   592:            __func__, packet_get_newkeys(MODE_OUT),
                    593:            packet_get_newkeys(MODE_IN));
1.1       provos    594:
                    595:        /* Keys from Kex */
                    596:        if (!mm_newkeys_to_blob(MODE_OUT, &blob, &bloblen))
1.8       markus    597:                fatal("%s: conversion of newkeys failed", __func__);
1.1       provos    598:
                    599:        buffer_put_string(&m, blob, bloblen);
                    600:        xfree(blob);
                    601:
                    602:        if (!mm_newkeys_to_blob(MODE_IN, &blob, &bloblen))
1.8       markus    603:                fatal("%s: conversion of newkeys failed", __func__);
1.1       provos    604:
                    605:        buffer_put_string(&m, blob, bloblen);
                    606:        xfree(blob);
                    607:
1.63      markus    608:        packet_get_state(MODE_OUT, &seqnr, &blocks, &packets, &bytes);
1.25      markus    609:        buffer_put_int(&m, seqnr);
                    610:        buffer_put_int64(&m, blocks);
                    611:        buffer_put_int(&m, packets);
1.63      markus    612:        buffer_put_int64(&m, bytes);
                    613:        packet_get_state(MODE_IN, &seqnr, &blocks, &packets, &bytes);
1.25      markus    614:        buffer_put_int(&m, seqnr);
                    615:        buffer_put_int64(&m, blocks);
                    616:        buffer_put_int(&m, packets);
1.63      markus    617:        buffer_put_int64(&m, bytes);
1.1       provos    618:
1.8       markus    619:        debug3("%s: New keys have been sent", __func__);
1.1       provos    620:  skip:
                    621:        /* More key context */
                    622:        plen = packet_get_keycontext(MODE_OUT, NULL);
                    623:        p = xmalloc(plen+1);
                    624:        packet_get_keycontext(MODE_OUT, p);
                    625:        buffer_put_string(&m, p, plen);
                    626:        xfree(p);
                    627:
                    628:        plen = packet_get_keycontext(MODE_IN, NULL);
                    629:        p = xmalloc(plen+1);
                    630:        packet_get_keycontext(MODE_IN, p);
                    631:        buffer_put_string(&m, p, plen);
                    632:        xfree(p);
                    633:
                    634:        /* Compression state */
1.8       markus    635:        debug3("%s: Sending compression state", __func__);
1.1       provos    636:        buffer_put_string(&m, &outgoing_stream, sizeof(outgoing_stream));
                    637:        buffer_put_string(&m, &incoming_stream, sizeof(incoming_stream));
                    638:
                    639:        /* Network I/O buffers */
1.66      andreas   640:        input = (Buffer *)packet_get_input();
                    641:        output = (Buffer *)packet_get_output();
                    642:        buffer_put_string(&m, buffer_ptr(input), buffer_len(input));
                    643:        buffer_put_string(&m, buffer_ptr(output), buffer_len(output));
1.67      andreas   644:
                    645:        /* Roaming */
                    646:        if (compat20) {
                    647:                buffer_put_int64(&m, get_sent_bytes());
                    648:                buffer_put_int64(&m, get_recv_bytes());
                    649:        }
1.1       provos    650:
1.36      avsm      651:        mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, &m);
1.8       markus    652:        debug3("%s: Finished sending state", __func__);
1.1       provos    653:
                    654:        buffer_free(&m);
                    655: }
                    656:
                    657: int
1.42      deraadt   658: mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
1.1       provos    659: {
                    660:        Buffer m;
1.39      dtucker   661:        char *p, *msg;
1.62      djm       662:        int success = 0, tmp1 = -1, tmp2 = -1;
                    663:
                    664:        /* Kludge: ensure there are fds free to receive the pty/tty */
                    665:        if ((tmp1 = dup(pmonitor->m_recvfd)) == -1 ||
                    666:            (tmp2 = dup(pmonitor->m_recvfd)) == -1) {
                    667:                error("%s: cannot allocate fds for pty", __func__);
                    668:                if (tmp1 > 0)
                    669:                        close(tmp1);
                    670:                if (tmp2 > 0)
                    671:                        close(tmp2);
                    672:                return 0;
                    673:        }
                    674:        close(tmp1);
                    675:        close(tmp2);
1.1       provos    676:
                    677:        buffer_init(&m);
1.7       mouring   678:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, &m);
1.1       provos    679:
1.8       markus    680:        debug3("%s: waiting for MONITOR_ANS_PTY", __func__);
1.7       mouring   681:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, &m);
1.1       provos    682:
                    683:        success = buffer_get_int(&m);
                    684:        if (success == 0) {
1.8       markus    685:                debug3("%s: pty alloc failed", __func__);
1.1       provos    686:                buffer_free(&m);
                    687:                return (0);
                    688:        }
                    689:        p = buffer_get_string(&m, NULL);
1.39      dtucker   690:        msg = buffer_get_string(&m, NULL);
1.1       provos    691:        buffer_free(&m);
                    692:
                    693:        strlcpy(namebuf, p, namebuflen); /* Possible truncation */
                    694:        xfree(p);
1.39      dtucker   695:
                    696:        buffer_append(&loginmsg, msg, strlen(msg));
                    697:        xfree(msg);
1.1       provos    698:
1.58      djm       699:        if ((*ptyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1 ||
                    700:            (*ttyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1)
                    701:                fatal("%s: receive fds failed", __func__);
1.1       provos    702:
                    703:        /* Success */
                    704:        return (1);
                    705: }
                    706:
                    707: void
1.32      markus    708: mm_session_pty_cleanup2(Session *s)
1.1       provos    709: {
                    710:        Buffer m;
                    711:
                    712:        if (s->ttyfd == -1)
                    713:                return;
                    714:        buffer_init(&m);
                    715:        buffer_put_cstring(&m, s->tty);
1.7       mouring   716:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m);
1.1       provos    717:        buffer_free(&m);
1.3       markus    718:
1.1       provos    719:        /* closed dup'ed master */
1.62      djm       720:        if (s->ptymaster != -1 && close(s->ptymaster) < 0)
                    721:                error("close(s->ptymaster/%d): %s",
                    722:                    s->ptymaster, strerror(errno));
1.1       provos    723:
                    724:        /* unlink pty from session */
                    725:        s->ttyfd = -1;
                    726: }
                    727:
                    728: /* Request process termination */
                    729:
                    730: void
                    731: mm_terminate(void)
                    732: {
                    733:        Buffer m;
                    734:
                    735:        buffer_init(&m);
1.7       mouring   736:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, &m);
1.1       provos    737:        buffer_free(&m);
                    738: }
                    739:
                    740: int
                    741: mm_ssh1_session_key(BIGNUM *num)
                    742: {
                    743:        int rsafail;
                    744:        Buffer m;
                    745:
                    746:        buffer_init(&m);
                    747:        buffer_put_bignum2(&m, num);
1.7       mouring   748:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSKEY, &m);
1.1       provos    749:
1.7       mouring   750:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SESSKEY, &m);
1.1       provos    751:
                    752:        rsafail = buffer_get_int(&m);
                    753:        buffer_get_bignum2(&m, num);
                    754:
                    755:        buffer_free(&m);
                    756:
                    757:        return (rsafail);
                    758: }
                    759:
1.2       markus    760: static void
1.1       provos    761: mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
                    762:     char ***prompts, u_int **echo_on)
                    763: {
1.10      deraadt   764:        *name = xstrdup("");
                    765:        *infotxt = xstrdup("");
1.1       provos    766:        *numprompts = 1;
1.43      djm       767:        *prompts = xcalloc(*numprompts, sizeof(char *));
                    768:        *echo_on = xcalloc(*numprompts, sizeof(u_int));
1.1       provos    769:        (*echo_on)[0] = 0;
                    770: }
                    771:
                    772: int
                    773: mm_bsdauth_query(void *ctx, char **name, char **infotxt,
                    774:    u_int *numprompts, char ***prompts, u_int **echo_on)
                    775: {
                    776:        Buffer m;
1.21      markus    777:        u_int success;
1.1       provos    778:        char *challenge;
                    779:
1.8       markus    780:        debug3("%s: entering", __func__);
1.1       provos    781:
                    782:        buffer_init(&m);
1.7       mouring   783:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m);
1.1       provos    784:
1.7       mouring   785:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY,
1.1       provos    786:            &m);
1.21      markus    787:        success = buffer_get_int(&m);
                    788:        if (success == 0) {
1.8       markus    789:                debug3("%s: no challenge", __func__);
1.1       provos    790:                buffer_free(&m);
                    791:                return (-1);
                    792:        }
                    793:
                    794:        /* Get the challenge, and format the response */
                    795:        challenge  = buffer_get_string(&m, NULL);
                    796:        buffer_free(&m);
                    797:
                    798:        mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
                    799:        (*prompts)[0] = challenge;
                    800:
1.8       markus    801:        debug3("%s: received challenge: %s", __func__, challenge);
1.1       provos    802:
                    803:        return (0);
                    804: }
                    805:
                    806: int
                    807: mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
                    808: {
                    809:        Buffer m;
                    810:        int authok;
                    811:
1.8       markus    812:        debug3("%s: entering", __func__);
1.1       provos    813:        if (numresponses != 1)
                    814:                return (-1);
                    815:
                    816:        buffer_init(&m);
                    817:        buffer_put_cstring(&m, responses[0]);
1.7       mouring   818:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m);
1.1       provos    819:
1.7       mouring   820:        mm_request_receive_expect(pmonitor->m_recvfd,
1.1       provos    821:            MONITOR_ANS_BSDAUTHRESPOND, &m);
                    822:
                    823:        authok = buffer_get_int(&m);
                    824:        buffer_free(&m);
                    825:
                    826:        return ((authok == 0) ? -1 : 0);
                    827: }
                    828:
                    829:
                    830: void
                    831: mm_ssh1_session_id(u_char session_id[16])
                    832: {
                    833:        Buffer m;
                    834:        int i;
                    835:
1.8       markus    836:        debug3("%s entering", __func__);
1.1       provos    837:
                    838:        buffer_init(&m);
                    839:        for (i = 0; i < 16; i++)
                    840:                buffer_put_char(&m, session_id[i]);
                    841:
1.7       mouring   842:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSID, &m);
1.1       provos    843:        buffer_free(&m);
                    844: }
                    845:
                    846: int
                    847: mm_auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey)
                    848: {
                    849:        Buffer m;
                    850:        Key *key;
                    851:        u_char *blob;
                    852:        u_int blen;
1.22      markus    853:        int allowed = 0, have_forced = 0;
1.1       provos    854:
1.8       markus    855:        debug3("%s entering", __func__);
1.1       provos    856:
                    857:        buffer_init(&m);
                    858:        buffer_put_bignum2(&m, client_n);
                    859:
1.7       mouring   860:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSAKEYALLOWED, &m);
                    861:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSAKEYALLOWED, &m);
1.1       provos    862:
                    863:        allowed = buffer_get_int(&m);
1.22      markus    864:
                    865:        /* fake forced command */
                    866:        auth_clear_options();
                    867:        have_forced = buffer_get_int(&m);
                    868:        forced_command = have_forced ? xstrdup("true") : NULL;
1.1       provos    869:
                    870:        if (allowed && rkey != NULL) {
                    871:                blob = buffer_get_string(&m, &blen);
                    872:                if ((key = key_from_blob(blob, blen)) == NULL)
1.8       markus    873:                        fatal("%s: key_from_blob failed", __func__);
1.1       provos    874:                *rkey = key;
                    875:                xfree(blob);
                    876:        }
                    877:        buffer_free(&m);
                    878:
                    879:        return (allowed);
                    880: }
                    881:
                    882: BIGNUM *
                    883: mm_auth_rsa_generate_challenge(Key *key)
                    884: {
                    885:        Buffer m;
                    886:        BIGNUM *challenge;
                    887:        u_char *blob;
                    888:        u_int blen;
                    889:
1.8       markus    890:        debug3("%s entering", __func__);
1.1       provos    891:
                    892:        if ((challenge = BN_new()) == NULL)
1.8       markus    893:                fatal("%s: BN_new failed", __func__);
1.1       provos    894:
                    895:        key->type = KEY_RSA;    /* XXX cheat for key_to_blob */
                    896:        if (key_to_blob(key, &blob, &blen) == 0)
1.8       markus    897:                fatal("%s: key_to_blob failed", __func__);
1.1       provos    898:        key->type = KEY_RSA1;
                    899:
                    900:        buffer_init(&m);
                    901:        buffer_put_string(&m, blob, blen);
                    902:        xfree(blob);
                    903:
1.7       mouring   904:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSACHALLENGE, &m);
                    905:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSACHALLENGE, &m);
1.1       provos    906:
                    907:        buffer_get_bignum2(&m, challenge);
                    908:        buffer_free(&m);
                    909:
                    910:        return (challenge);
                    911: }
                    912:
                    913: int
                    914: mm_auth_rsa_verify_response(Key *key, BIGNUM *p, u_char response[16])
                    915: {
                    916:        Buffer m;
                    917:        u_char *blob;
                    918:        u_int blen;
                    919:        int success = 0;
                    920:
1.8       markus    921:        debug3("%s entering", __func__);
1.1       provos    922:
                    923:        key->type = KEY_RSA;    /* XXX cheat for key_to_blob */
                    924:        if (key_to_blob(key, &blob, &blen) == 0)
1.8       markus    925:                fatal("%s: key_to_blob failed", __func__);
1.1       provos    926:        key->type = KEY_RSA1;
                    927:
                    928:        buffer_init(&m);
                    929:        buffer_put_string(&m, blob, blen);
                    930:        buffer_put_string(&m, response, 16);
                    931:        xfree(blob);
                    932:
1.7       mouring   933:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSARESPONSE, &m);
                    934:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSARESPONSE, &m);
1.1       provos    935:
                    936:        success = buffer_get_int(&m);
                    937:        buffer_free(&m);
                    938:
                    939:        return (success);
                    940: }
1.29      markus    941:
                    942: #ifdef GSSAPI
                    943: OM_uint32
1.36      avsm      944: mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid)
1.29      markus    945: {
                    946:        Buffer m;
                    947:        OM_uint32 major;
                    948:
                    949:        /* Client doesn't get to see the context */
                    950:        *ctx = NULL;
                    951:
                    952:        buffer_init(&m);
1.36      avsm      953:        buffer_put_string(&m, goid->elements, goid->length);
1.29      markus    954:
                    955:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, &m);
                    956:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, &m);
                    957:
                    958:        major = buffer_get_int(&m);
                    959:
                    960:        buffer_free(&m);
                    961:        return (major);
                    962: }
                    963:
                    964: OM_uint32
                    965: mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in,
                    966:     gss_buffer_desc *out, OM_uint32 *flags)
                    967: {
                    968:        Buffer m;
                    969:        OM_uint32 major;
1.30      deraadt   970:        u_int len;
1.29      markus    971:
                    972:        buffer_init(&m);
                    973:        buffer_put_string(&m, in->value, in->length);
                    974:
                    975:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, &m);
                    976:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, &m);
                    977:
                    978:        major = buffer_get_int(&m);
1.30      deraadt   979:        out->value = buffer_get_string(&m, &len);
                    980:        out->length = len;
1.29      markus    981:        if (flags)
                    982:                *flags = buffer_get_int(&m);
                    983:
                    984:        buffer_free(&m);
                    985:
                    986:        return (major);
1.35      markus    987: }
                    988:
                    989: OM_uint32
                    990: mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
                    991: {
                    992:        Buffer m;
                    993:        OM_uint32 major;
                    994:
                    995:        buffer_init(&m);
                    996:        buffer_put_string(&m, gssbuf->value, gssbuf->length);
                    997:        buffer_put_string(&m, gssmic->value, gssmic->length);
                    998:
                    999:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, &m);
                   1000:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSCHECKMIC,
                   1001:            &m);
                   1002:
                   1003:        major = buffer_get_int(&m);
                   1004:        buffer_free(&m);
                   1005:        return(major);
1.29      markus   1006: }
                   1007:
                   1008: int
                   1009: mm_ssh_gssapi_userok(char *user)
                   1010: {
                   1011:        Buffer m;
                   1012:        int authenticated = 0;
                   1013:
                   1014:        buffer_init(&m);
                   1015:
                   1016:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, &m);
                   1017:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSUSEROK,
                   1018:                                  &m);
                   1019:
                   1020:        authenticated = buffer_get_int(&m);
                   1021:
                   1022:        buffer_free(&m);
                   1023:        debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
                   1024:        return (authenticated);
                   1025: }
                   1026: #endif /* GSSAPI */
1.64      djm      1027:
                   1028: #ifdef JPAKE
                   1029: void
                   1030: mm_auth2_jpake_get_pwdata(Authctxt *authctxt, BIGNUM **s,
                   1031:     char **hash_scheme, char **salt)
                   1032: {
                   1033:        Buffer m;
                   1034:
                   1035:        debug3("%s entering", __func__);
                   1036:
                   1037:        buffer_init(&m);
                   1038:        mm_request_send(pmonitor->m_recvfd,
                   1039:            MONITOR_REQ_JPAKE_GET_PWDATA, &m);
                   1040:
                   1041:        debug3("%s: waiting for MONITOR_ANS_JPAKE_GET_PWDATA", __func__);
                   1042:        mm_request_receive_expect(pmonitor->m_recvfd,
                   1043:            MONITOR_ANS_JPAKE_GET_PWDATA, &m);
                   1044:
                   1045:        *hash_scheme = buffer_get_string(&m, NULL);
                   1046:        *salt = buffer_get_string(&m, NULL);
                   1047:
                   1048:        buffer_free(&m);
                   1049: }
                   1050:
                   1051: void
1.65      djm      1052: mm_jpake_step1(struct modp_group *grp,
1.64      djm      1053:     u_char **id, u_int *id_len,
                   1054:     BIGNUM **priv1, BIGNUM **priv2, BIGNUM **g_priv1, BIGNUM **g_priv2,
                   1055:     u_char **priv1_proof, u_int *priv1_proof_len,
                   1056:     u_char **priv2_proof, u_int *priv2_proof_len)
                   1057: {
                   1058:        Buffer m;
                   1059:
                   1060:        debug3("%s entering", __func__);
                   1061:
                   1062:        buffer_init(&m);
                   1063:        mm_request_send(pmonitor->m_recvfd,
                   1064:            MONITOR_REQ_JPAKE_STEP1, &m);
                   1065:
                   1066:        debug3("%s: waiting for MONITOR_ANS_JPAKE_STEP1", __func__);
                   1067:        mm_request_receive_expect(pmonitor->m_recvfd,
                   1068:            MONITOR_ANS_JPAKE_STEP1, &m);
                   1069:
                   1070:        if ((*priv1 = BN_new()) == NULL ||
                   1071:            (*priv2 = BN_new()) == NULL ||
                   1072:            (*g_priv1 = BN_new()) == NULL ||
                   1073:            (*g_priv2 = BN_new()) == NULL)
                   1074:                fatal("%s: BN_new", __func__);
                   1075:
                   1076:        *id = buffer_get_string(&m, id_len);
                   1077:        /* priv1 and priv2 are, well, private */
                   1078:        buffer_get_bignum2(&m, *g_priv1);
                   1079:        buffer_get_bignum2(&m, *g_priv2);
                   1080:        *priv1_proof = buffer_get_string(&m, priv1_proof_len);
                   1081:        *priv2_proof = buffer_get_string(&m, priv2_proof_len);
                   1082:
                   1083:        buffer_free(&m);
                   1084: }
                   1085:
                   1086: void
1.65      djm      1087: mm_jpake_step2(struct modp_group *grp, BIGNUM *s,
1.64      djm      1088:     BIGNUM *mypub1, BIGNUM *theirpub1, BIGNUM *theirpub2, BIGNUM *mypriv2,
                   1089:     const u_char *theirid, u_int theirid_len,
                   1090:     const u_char *myid, u_int myid_len,
                   1091:     const u_char *theirpub1_proof, u_int theirpub1_proof_len,
                   1092:     const u_char *theirpub2_proof, u_int theirpub2_proof_len,
                   1093:     BIGNUM **newpub,
                   1094:     u_char **newpub_exponent_proof, u_int *newpub_exponent_proof_len)
                   1095: {
                   1096:        Buffer m;
                   1097:
                   1098:        debug3("%s entering", __func__);
                   1099:
                   1100:        buffer_init(&m);
                   1101:        /* monitor already has all bignums except theirpub1, theirpub2 */
                   1102:        buffer_put_bignum2(&m, theirpub1);
                   1103:        buffer_put_bignum2(&m, theirpub2);
                   1104:        /* monitor already knows our id */
                   1105:        buffer_put_string(&m, theirid, theirid_len);
                   1106:        buffer_put_string(&m, theirpub1_proof, theirpub1_proof_len);
                   1107:        buffer_put_string(&m, theirpub2_proof, theirpub2_proof_len);
                   1108:
                   1109:        mm_request_send(pmonitor->m_recvfd,
                   1110:            MONITOR_REQ_JPAKE_STEP2, &m);
                   1111:
                   1112:        debug3("%s: waiting for MONITOR_ANS_JPAKE_STEP2", __func__);
                   1113:        mm_request_receive_expect(pmonitor->m_recvfd,
                   1114:            MONITOR_ANS_JPAKE_STEP2, &m);
                   1115:
                   1116:        if ((*newpub = BN_new()) == NULL)
                   1117:                fatal("%s: BN_new", __func__);
                   1118:
                   1119:        buffer_get_bignum2(&m, *newpub);
                   1120:        *newpub_exponent_proof = buffer_get_string(&m,
                   1121:            newpub_exponent_proof_len);
                   1122:
                   1123:        buffer_free(&m);
                   1124: }
                   1125:
                   1126: void
1.65      djm      1127: mm_jpake_key_confirm(struct modp_group *grp, BIGNUM *s, BIGNUM *step2_val,
1.64      djm      1128:     BIGNUM *mypriv2, BIGNUM *mypub1, BIGNUM *mypub2,
                   1129:     BIGNUM *theirpub1, BIGNUM *theirpub2,
                   1130:     const u_char *my_id, u_int my_id_len,
                   1131:     const u_char *their_id, u_int their_id_len,
                   1132:     const u_char *sess_id, u_int sess_id_len,
                   1133:     const u_char *theirpriv2_s_proof, u_int theirpriv2_s_proof_len,
                   1134:     BIGNUM **k,
                   1135:     u_char **confirm_hash, u_int *confirm_hash_len)
                   1136: {
                   1137:        Buffer m;
                   1138:
                   1139:        debug3("%s entering", __func__);
                   1140:
                   1141:        buffer_init(&m);
                   1142:        /* monitor already has all bignums except step2_val */
                   1143:        buffer_put_bignum2(&m, step2_val);
                   1144:        /* monitor already knows all the ids */
                   1145:        buffer_put_string(&m, theirpriv2_s_proof, theirpriv2_s_proof_len);
                   1146:
                   1147:        mm_request_send(pmonitor->m_recvfd,
                   1148:            MONITOR_REQ_JPAKE_KEY_CONFIRM, &m);
                   1149:
                   1150:        debug3("%s: waiting for MONITOR_ANS_JPAKE_KEY_CONFIRM", __func__);
                   1151:        mm_request_receive_expect(pmonitor->m_recvfd,
                   1152:            MONITOR_ANS_JPAKE_KEY_CONFIRM, &m);
                   1153:
                   1154:        /* 'k' is sensitive and stays in the monitor */
                   1155:        *confirm_hash = buffer_get_string(&m, confirm_hash_len);
                   1156:
                   1157:        buffer_free(&m);
                   1158: }
                   1159:
                   1160: int
                   1161: mm_jpake_check_confirm(const BIGNUM *k,
                   1162:     const u_char *peer_id, u_int peer_id_len,
                   1163:     const u_char *sess_id, u_int sess_id_len,
                   1164:     const u_char *peer_confirm_hash, u_int peer_confirm_hash_len)
                   1165: {
                   1166:        Buffer m;
                   1167:        int success = 0;
                   1168:
                   1169:        debug3("%s entering", __func__);
                   1170:
                   1171:        buffer_init(&m);
                   1172:        /* k is dummy in slave, ignored */
                   1173:        /* monitor knows all the ids */
                   1174:        buffer_put_string(&m, peer_confirm_hash, peer_confirm_hash_len);
                   1175:        mm_request_send(pmonitor->m_recvfd,
                   1176:            MONITOR_REQ_JPAKE_CHECK_CONFIRM, &m);
                   1177:
                   1178:        debug3("%s: waiting for MONITOR_ANS_JPAKE_CHECK_CONFIRM", __func__);
                   1179:        mm_request_receive_expect(pmonitor->m_recvfd,
                   1180:            MONITOR_ANS_JPAKE_CHECK_CONFIRM, &m);
                   1181:
                   1182:        success = buffer_get_int(&m);
                   1183:        buffer_free(&m);
                   1184:
                   1185:        debug3("%s: success = %d", __func__, success);
                   1186:        return success;
                   1187: }
                   1188: #endif /* JPAKE */