[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.72

1.72    ! djm         1: /* $OpenBSD: monitor_wrap.c,v 1.71 2011/05/20 03:25: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.72    ! djm       205:        u_int len, i;
1.55      dtucker   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)
1.72    ! djm       242: #define M_CP_STRARRAYOPT(x, nx) do { \
        !           243:                for (i = 0; i < newopts->nx; i++) \
        !           244:                        newopts->x[i] = buffer_get_string(&m, NULL); \
        !           245:        } while (0)
1.71      djm       246:        /* See comment in servconf.h */
                    247:        COPY_MATCH_STRING_OPTS();
                    248: #undef M_CP_STROPT
1.72    ! djm       249: #undef M_CP_STRARRAYOPT
1.71      djm       250:
1.55      dtucker   251:        copy_set_server_options(&options, newopts, 1);
                    252:        xfree(newopts);
                    253:
1.1       provos    254:        buffer_free(&m);
                    255:
                    256:        return (pw);
1.6       djm       257: }
                    258:
1.33      markus    259: char *
                    260: mm_auth2_read_banner(void)
1.6       djm       261: {
                    262:        Buffer m;
                    263:        char *banner;
                    264:
1.8       markus    265:        debug3("%s entering", __func__);
1.6       djm       266:
                    267:        buffer_init(&m);
1.7       mouring   268:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, &m);
1.6       djm       269:        buffer_clear(&m);
                    270:
1.33      markus    271:        mm_request_receive_expect(pmonitor->m_recvfd,
                    272:            MONITOR_ANS_AUTH2_READ_BANNER, &m);
1.6       djm       273:        banner = buffer_get_string(&m, NULL);
                    274:        buffer_free(&m);
1.10      deraadt   275:
1.33      markus    276:        /* treat empty banner as missing banner */
                    277:        if (strlen(banner) == 0) {
                    278:                xfree(banner);
                    279:                banner = NULL;
                    280:        }
1.6       djm       281:        return (banner);
1.1       provos    282: }
                    283:
                    284: /* Inform the privileged process about service and style */
                    285:
                    286: void
                    287: mm_inform_authserv(char *service, char *style)
                    288: {
                    289:        Buffer m;
                    290:
1.8       markus    291:        debug3("%s entering", __func__);
1.1       provos    292:
                    293:        buffer_init(&m);
                    294:        buffer_put_cstring(&m, service);
                    295:        buffer_put_cstring(&m, style ? style : "");
                    296:
1.7       mouring   297:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, &m);
1.1       provos    298:
                    299:        buffer_free(&m);
                    300: }
                    301:
                    302: /* Do the password authentication */
                    303: int
                    304: mm_auth_password(Authctxt *authctxt, char *password)
                    305: {
                    306:        Buffer m;
                    307:        int authenticated = 0;
                    308:
1.8       markus    309:        debug3("%s entering", __func__);
1.1       provos    310:
                    311:        buffer_init(&m);
                    312:        buffer_put_cstring(&m, password);
1.7       mouring   313:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, &m);
1.1       provos    314:
1.8       markus    315:        debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__);
1.7       mouring   316:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUTHPASSWORD, &m);
1.1       provos    317:
                    318:        authenticated = buffer_get_int(&m);
                    319:
                    320:        buffer_free(&m);
                    321:
1.3       markus    322:        debug3("%s: user %sauthenticated",
1.8       markus    323:            __func__, authenticated ? "" : "not ");
1.1       provos    324:        return (authenticated);
                    325: }
                    326:
                    327: int
                    328: mm_user_key_allowed(struct passwd *pw, Key *key)
                    329: {
                    330:        return (mm_key_allowed(MM_USERKEY, NULL, NULL, key));
                    331: }
                    332:
                    333: int
                    334: mm_hostbased_key_allowed(struct passwd *pw, char *user, char *host,
                    335:     Key *key)
                    336: {
                    337:        return (mm_key_allowed(MM_HOSTKEY, user, host, key));
                    338: }
                    339:
                    340: int
                    341: mm_auth_rhosts_rsa_key_allowed(struct passwd *pw, char *user,
                    342:     char *host, Key *key)
                    343: {
                    344:        int ret;
                    345:
                    346:        key->type = KEY_RSA; /* XXX hack for key_to_blob */
                    347:        ret = mm_key_allowed(MM_RSAHOSTKEY, user, host, key);
                    348:        key->type = KEY_RSA1;
                    349:        return (ret);
                    350: }
                    351:
                    352: int
                    353: mm_key_allowed(enum mm_keytype type, char *user, char *host, Key *key)
                    354: {
                    355:        Buffer m;
                    356:        u_char *blob;
                    357:        u_int len;
1.22      markus    358:        int allowed = 0, have_forced = 0;
1.1       provos    359:
1.8       markus    360:        debug3("%s entering", __func__);
1.1       provos    361:
                    362:        /* Convert the key to a blob and the pass it over */
                    363:        if (!key_to_blob(key, &blob, &len))
                    364:                return (0);
                    365:
                    366:        buffer_init(&m);
                    367:        buffer_put_int(&m, type);
                    368:        buffer_put_cstring(&m, user ? user : "");
                    369:        buffer_put_cstring(&m, host ? host : "");
                    370:        buffer_put_string(&m, blob, len);
                    371:        xfree(blob);
                    372:
1.7       mouring   373:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, &m);
1.1       provos    374:
1.8       markus    375:        debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__);
1.7       mouring   376:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYALLOWED, &m);
1.1       provos    377:
                    378:        allowed = buffer_get_int(&m);
                    379:
1.22      markus    380:        /* fake forced command */
                    381:        auth_clear_options();
                    382:        have_forced = buffer_get_int(&m);
                    383:        forced_command = have_forced ? xstrdup("true") : NULL;
                    384:
1.1       provos    385:        buffer_free(&m);
                    386:
                    387:        return (allowed);
                    388: }
                    389:
1.3       markus    390: /*
1.1       provos    391:  * This key verify needs to send the key type along, because the
                    392:  * privileged parent makes the decision if the key is allowed
                    393:  * for authentication.
                    394:  */
                    395:
                    396: int
                    397: mm_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
                    398: {
                    399:        Buffer m;
                    400:        u_char *blob;
                    401:        u_int len;
                    402:        int verified = 0;
                    403:
1.8       markus    404:        debug3("%s entering", __func__);
1.1       provos    405:
                    406:        /* Convert the key to a blob and the pass it over */
                    407:        if (!key_to_blob(key, &blob, &len))
                    408:                return (0);
                    409:
                    410:        buffer_init(&m);
                    411:        buffer_put_string(&m, blob, len);
                    412:        buffer_put_string(&m, sig, siglen);
                    413:        buffer_put_string(&m, data, datalen);
                    414:        xfree(blob);
                    415:
1.7       mouring   416:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, &m);
1.1       provos    417:
1.8       markus    418:        debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__);
1.7       mouring   419:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYVERIFY, &m);
1.1       provos    420:
                    421:        verified = buffer_get_int(&m);
                    422:
                    423:        buffer_free(&m);
                    424:
                    425:        return (verified);
                    426: }
                    427:
                    428: /* Export key state after authentication */
                    429: Newkeys *
                    430: mm_newkeys_from_blob(u_char *blob, int blen)
                    431: {
                    432:        Buffer b;
                    433:        u_int len;
                    434:        Newkeys *newkey = NULL;
                    435:        Enc *enc;
                    436:        Mac *mac;
                    437:        Comp *comp;
                    438:
1.8       markus    439:        debug3("%s: %p(%d)", __func__, blob, blen);
1.1       provos    440: #ifdef DEBUG_PK
                    441:        dump_base64(stderr, blob, blen);
                    442: #endif
                    443:        buffer_init(&b);
                    444:        buffer_append(&b, blob, blen);
                    445:
                    446:        newkey = xmalloc(sizeof(*newkey));
                    447:        enc = &newkey->enc;
                    448:        mac = &newkey->mac;
                    449:        comp = &newkey->comp;
                    450:
                    451:        /* Enc structure */
                    452:        enc->name = buffer_get_string(&b, NULL);
                    453:        buffer_get(&b, &enc->cipher, sizeof(enc->cipher));
                    454:        enc->enabled = buffer_get_int(&b);
                    455:        enc->block_size = buffer_get_int(&b);
                    456:        enc->key = buffer_get_string(&b, &enc->key_len);
                    457:        enc->iv = buffer_get_string(&b, &len);
                    458:        if (len != enc->block_size)
1.12      deraadt   459:                fatal("%s: bad ivlen: expected %u != %u", __func__,
1.1       provos    460:                    enc->block_size, len);
                    461:
                    462:        if (enc->name == NULL || cipher_by_name(enc->name) != enc->cipher)
1.8       markus    463:                fatal("%s: bad cipher name %s or pointer %p", __func__,
1.1       provos    464:                    enc->name, enc->cipher);
                    465:
                    466:        /* Mac structure */
                    467:        mac->name = buffer_get_string(&b, NULL);
1.56      djm       468:        if (mac->name == NULL || mac_setup(mac, mac->name) == -1)
1.57      pvalchev  469:                fatal("%s: can not setup mac %s", __func__, mac->name);
1.1       provos    470:        mac->enabled = buffer_get_int(&b);
                    471:        mac->key = buffer_get_string(&b, &len);
                    472:        if (len > mac->key_len)
1.12      deraadt   473:                fatal("%s: bad mac key length: %u > %d", __func__, len,
1.1       provos    474:                    mac->key_len);
                    475:        mac->key_len = len;
                    476:
                    477:        /* Comp structure */
                    478:        comp->type = buffer_get_int(&b);
                    479:        comp->enabled = buffer_get_int(&b);
                    480:        comp->name = buffer_get_string(&b, NULL);
                    481:
                    482:        len = buffer_len(&b);
                    483:        if (len != 0)
1.12      deraadt   484:                error("newkeys_from_blob: remaining bytes in blob %u", len);
1.1       provos    485:        buffer_free(&b);
                    486:        return (newkey);
                    487: }
                    488:
                    489: int
                    490: mm_newkeys_to_blob(int mode, u_char **blobp, u_int *lenp)
                    491: {
                    492:        Buffer b;
                    493:        int len;
                    494:        Enc *enc;
                    495:        Mac *mac;
                    496:        Comp *comp;
1.66      andreas   497:        Newkeys *newkey = (Newkeys *)packet_get_newkeys(mode);
1.1       provos    498:
1.8       markus    499:        debug3("%s: converting %p", __func__, newkey);
1.1       provos    500:
                    501:        if (newkey == NULL) {
1.8       markus    502:                error("%s: newkey == NULL", __func__);
1.1       provos    503:                return 0;
                    504:        }
                    505:        enc = &newkey->enc;
                    506:        mac = &newkey->mac;
                    507:        comp = &newkey->comp;
                    508:
                    509:        buffer_init(&b);
                    510:        /* Enc structure */
                    511:        buffer_put_cstring(&b, enc->name);
                    512:        /* The cipher struct is constant and shared, you export pointer */
                    513:        buffer_append(&b, &enc->cipher, sizeof(enc->cipher));
                    514:        buffer_put_int(&b, enc->enabled);
                    515:        buffer_put_int(&b, enc->block_size);
                    516:        buffer_put_string(&b, enc->key, enc->key_len);
                    517:        packet_get_keyiv(mode, enc->iv, enc->block_size);
                    518:        buffer_put_string(&b, enc->iv, enc->block_size);
                    519:
                    520:        /* Mac structure */
                    521:        buffer_put_cstring(&b, mac->name);
                    522:        buffer_put_int(&b, mac->enabled);
                    523:        buffer_put_string(&b, mac->key, mac->key_len);
                    524:
                    525:        /* Comp structure */
                    526:        buffer_put_int(&b, comp->type);
                    527:        buffer_put_int(&b, comp->enabled);
                    528:        buffer_put_cstring(&b, comp->name);
                    529:
                    530:        len = buffer_len(&b);
1.16      markus    531:        if (lenp != NULL)
                    532:                *lenp = len;
                    533:        if (blobp != NULL) {
                    534:                *blobp = xmalloc(len);
                    535:                memcpy(*blobp, buffer_ptr(&b), len);
                    536:        }
1.1       provos    537:        memset(buffer_ptr(&b), 0, len);
                    538:        buffer_free(&b);
                    539:        return len;
                    540: }
                    541:
1.2       markus    542: static void
1.1       provos    543: mm_send_kex(Buffer *m, Kex *kex)
                    544: {
                    545:        buffer_put_string(m, kex->session_id, kex->session_id_len);
                    546:        buffer_put_int(m, kex->we_need);
                    547:        buffer_put_int(m, kex->hostkey_type);
                    548:        buffer_put_int(m, kex->kex_type);
                    549:        buffer_put_string(m, buffer_ptr(&kex->my), buffer_len(&kex->my));
                    550:        buffer_put_string(m, buffer_ptr(&kex->peer), buffer_len(&kex->peer));
                    551:        buffer_put_int(m, kex->flags);
                    552:        buffer_put_cstring(m, kex->client_version_string);
                    553:        buffer_put_cstring(m, kex->server_version_string);
                    554: }
                    555:
                    556: void
1.36      avsm      557: mm_send_keystate(struct monitor *monitor)
1.1       provos    558: {
1.66      andreas   559:        Buffer m, *input, *output;
1.1       provos    560:        u_char *blob, *p;
                    561:        u_int bloblen, plen;
1.25      markus    562:        u_int32_t seqnr, packets;
1.63      markus    563:        u_int64_t blocks, bytes;
1.1       provos    564:
                    565:        buffer_init(&m);
                    566:
                    567:        if (!compat20) {
                    568:                u_char iv[24];
1.11      markus    569:                u_char *key;
                    570:                u_int ivlen, keylen;
1.1       provos    571:
                    572:                buffer_put_int(&m, packet_get_protocol_flags());
                    573:
                    574:                buffer_put_int(&m, packet_get_ssh1_cipher());
                    575:
1.11      markus    576:                debug3("%s: Sending ssh1 KEY+IV", __func__);
                    577:                keylen = packet_get_encryption_key(NULL);
                    578:                key = xmalloc(keylen+1);        /* add 1 if keylen == 0 */
                    579:                keylen = packet_get_encryption_key(key);
                    580:                buffer_put_string(&m, key, keylen);
                    581:                memset(key, 0, keylen);
                    582:                xfree(key);
                    583:
1.1       provos    584:                ivlen = packet_get_keyiv_len(MODE_OUT);
                    585:                packet_get_keyiv(MODE_OUT, iv, ivlen);
                    586:                buffer_put_string(&m, iv, ivlen);
                    587:                ivlen = packet_get_keyiv_len(MODE_OUT);
                    588:                packet_get_keyiv(MODE_IN, iv, ivlen);
                    589:                buffer_put_string(&m, iv, ivlen);
                    590:                goto skip;
                    591:        } else {
                    592:                /* Kex for rekeying */
1.36      avsm      593:                mm_send_kex(&m, *monitor->m_pkex);
1.1       provos    594:        }
                    595:
                    596:        debug3("%s: Sending new keys: %p %p",
1.66      andreas   597:            __func__, packet_get_newkeys(MODE_OUT),
                    598:            packet_get_newkeys(MODE_IN));
1.1       provos    599:
                    600:        /* Keys from Kex */
                    601:        if (!mm_newkeys_to_blob(MODE_OUT, &blob, &bloblen))
1.8       markus    602:                fatal("%s: conversion of newkeys failed", __func__);
1.1       provos    603:
                    604:        buffer_put_string(&m, blob, bloblen);
                    605:        xfree(blob);
                    606:
                    607:        if (!mm_newkeys_to_blob(MODE_IN, &blob, &bloblen))
1.8       markus    608:                fatal("%s: conversion of newkeys failed", __func__);
1.1       provos    609:
                    610:        buffer_put_string(&m, blob, bloblen);
                    611:        xfree(blob);
                    612:
1.63      markus    613:        packet_get_state(MODE_OUT, &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);
                    618:        packet_get_state(MODE_IN, &seqnr, &blocks, &packets, &bytes);
1.25      markus    619:        buffer_put_int(&m, seqnr);
                    620:        buffer_put_int64(&m, blocks);
                    621:        buffer_put_int(&m, packets);
1.63      markus    622:        buffer_put_int64(&m, bytes);
1.1       provos    623:
1.8       markus    624:        debug3("%s: New keys have been sent", __func__);
1.1       provos    625:  skip:
                    626:        /* More key context */
                    627:        plen = packet_get_keycontext(MODE_OUT, NULL);
                    628:        p = xmalloc(plen+1);
                    629:        packet_get_keycontext(MODE_OUT, p);
                    630:        buffer_put_string(&m, p, plen);
                    631:        xfree(p);
                    632:
                    633:        plen = packet_get_keycontext(MODE_IN, NULL);
                    634:        p = xmalloc(plen+1);
                    635:        packet_get_keycontext(MODE_IN, p);
                    636:        buffer_put_string(&m, p, plen);
                    637:        xfree(p);
                    638:
                    639:        /* Compression state */
1.8       markus    640:        debug3("%s: Sending compression state", __func__);
1.1       provos    641:        buffer_put_string(&m, &outgoing_stream, sizeof(outgoing_stream));
                    642:        buffer_put_string(&m, &incoming_stream, sizeof(incoming_stream));
                    643:
                    644:        /* Network I/O buffers */
1.66      andreas   645:        input = (Buffer *)packet_get_input();
                    646:        output = (Buffer *)packet_get_output();
                    647:        buffer_put_string(&m, buffer_ptr(input), buffer_len(input));
                    648:        buffer_put_string(&m, buffer_ptr(output), buffer_len(output));
1.67      andreas   649:
                    650:        /* Roaming */
                    651:        if (compat20) {
                    652:                buffer_put_int64(&m, get_sent_bytes());
                    653:                buffer_put_int64(&m, get_recv_bytes());
                    654:        }
1.1       provos    655:
1.36      avsm      656:        mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, &m);
1.8       markus    657:        debug3("%s: Finished sending state", __func__);
1.1       provos    658:
                    659:        buffer_free(&m);
                    660: }
                    661:
                    662: int
1.42      deraadt   663: mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
1.1       provos    664: {
                    665:        Buffer m;
1.39      dtucker   666:        char *p, *msg;
1.62      djm       667:        int success = 0, tmp1 = -1, tmp2 = -1;
                    668:
                    669:        /* Kludge: ensure there are fds free to receive the pty/tty */
                    670:        if ((tmp1 = dup(pmonitor->m_recvfd)) == -1 ||
                    671:            (tmp2 = dup(pmonitor->m_recvfd)) == -1) {
                    672:                error("%s: cannot allocate fds for pty", __func__);
                    673:                if (tmp1 > 0)
                    674:                        close(tmp1);
                    675:                if (tmp2 > 0)
                    676:                        close(tmp2);
                    677:                return 0;
                    678:        }
                    679:        close(tmp1);
                    680:        close(tmp2);
1.1       provos    681:
                    682:        buffer_init(&m);
1.7       mouring   683:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, &m);
1.1       provos    684:
1.8       markus    685:        debug3("%s: waiting for MONITOR_ANS_PTY", __func__);
1.7       mouring   686:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, &m);
1.1       provos    687:
                    688:        success = buffer_get_int(&m);
                    689:        if (success == 0) {
1.8       markus    690:                debug3("%s: pty alloc failed", __func__);
1.1       provos    691:                buffer_free(&m);
                    692:                return (0);
                    693:        }
                    694:        p = buffer_get_string(&m, NULL);
1.39      dtucker   695:        msg = buffer_get_string(&m, NULL);
1.1       provos    696:        buffer_free(&m);
                    697:
                    698:        strlcpy(namebuf, p, namebuflen); /* Possible truncation */
                    699:        xfree(p);
1.39      dtucker   700:
                    701:        buffer_append(&loginmsg, msg, strlen(msg));
                    702:        xfree(msg);
1.1       provos    703:
1.58      djm       704:        if ((*ptyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1 ||
                    705:            (*ttyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1)
                    706:                fatal("%s: receive fds failed", __func__);
1.1       provos    707:
                    708:        /* Success */
                    709:        return (1);
                    710: }
                    711:
                    712: void
1.32      markus    713: mm_session_pty_cleanup2(Session *s)
1.1       provos    714: {
                    715:        Buffer m;
                    716:
                    717:        if (s->ttyfd == -1)
                    718:                return;
                    719:        buffer_init(&m);
                    720:        buffer_put_cstring(&m, s->tty);
1.7       mouring   721:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m);
1.1       provos    722:        buffer_free(&m);
1.3       markus    723:
1.1       provos    724:        /* closed dup'ed master */
1.62      djm       725:        if (s->ptymaster != -1 && close(s->ptymaster) < 0)
                    726:                error("close(s->ptymaster/%d): %s",
                    727:                    s->ptymaster, strerror(errno));
1.1       provos    728:
                    729:        /* unlink pty from session */
                    730:        s->ttyfd = -1;
                    731: }
                    732:
                    733: /* Request process termination */
                    734:
                    735: void
                    736: mm_terminate(void)
                    737: {
                    738:        Buffer m;
                    739:
                    740:        buffer_init(&m);
1.7       mouring   741:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, &m);
1.1       provos    742:        buffer_free(&m);
                    743: }
                    744:
                    745: int
                    746: mm_ssh1_session_key(BIGNUM *num)
                    747: {
                    748:        int rsafail;
                    749:        Buffer m;
                    750:
                    751:        buffer_init(&m);
                    752:        buffer_put_bignum2(&m, num);
1.7       mouring   753:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSKEY, &m);
1.1       provos    754:
1.7       mouring   755:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SESSKEY, &m);
1.1       provos    756:
                    757:        rsafail = buffer_get_int(&m);
                    758:        buffer_get_bignum2(&m, num);
                    759:
                    760:        buffer_free(&m);
                    761:
                    762:        return (rsafail);
                    763: }
                    764:
1.2       markus    765: static void
1.1       provos    766: mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
                    767:     char ***prompts, u_int **echo_on)
                    768: {
1.10      deraadt   769:        *name = xstrdup("");
                    770:        *infotxt = xstrdup("");
1.1       provos    771:        *numprompts = 1;
1.43      djm       772:        *prompts = xcalloc(*numprompts, sizeof(char *));
                    773:        *echo_on = xcalloc(*numprompts, sizeof(u_int));
1.1       provos    774:        (*echo_on)[0] = 0;
                    775: }
                    776:
                    777: int
                    778: mm_bsdauth_query(void *ctx, char **name, char **infotxt,
                    779:    u_int *numprompts, char ***prompts, u_int **echo_on)
                    780: {
                    781:        Buffer m;
1.21      markus    782:        u_int success;
1.1       provos    783:        char *challenge;
                    784:
1.8       markus    785:        debug3("%s: entering", __func__);
1.1       provos    786:
                    787:        buffer_init(&m);
1.7       mouring   788:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m);
1.1       provos    789:
1.7       mouring   790:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY,
1.1       provos    791:            &m);
1.21      markus    792:        success = buffer_get_int(&m);
                    793:        if (success == 0) {
1.8       markus    794:                debug3("%s: no challenge", __func__);
1.1       provos    795:                buffer_free(&m);
                    796:                return (-1);
                    797:        }
                    798:
                    799:        /* Get the challenge, and format the response */
                    800:        challenge  = buffer_get_string(&m, NULL);
                    801:        buffer_free(&m);
                    802:
                    803:        mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
                    804:        (*prompts)[0] = challenge;
                    805:
1.8       markus    806:        debug3("%s: received challenge: %s", __func__, challenge);
1.1       provos    807:
                    808:        return (0);
                    809: }
                    810:
                    811: int
                    812: mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses)
                    813: {
                    814:        Buffer m;
                    815:        int authok;
                    816:
1.8       markus    817:        debug3("%s: entering", __func__);
1.1       provos    818:        if (numresponses != 1)
                    819:                return (-1);
                    820:
                    821:        buffer_init(&m);
                    822:        buffer_put_cstring(&m, responses[0]);
1.7       mouring   823:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m);
1.1       provos    824:
1.7       mouring   825:        mm_request_receive_expect(pmonitor->m_recvfd,
1.1       provos    826:            MONITOR_ANS_BSDAUTHRESPOND, &m);
                    827:
                    828:        authok = buffer_get_int(&m);
                    829:        buffer_free(&m);
                    830:
                    831:        return ((authok == 0) ? -1 : 0);
                    832: }
                    833:
                    834:
                    835: void
                    836: mm_ssh1_session_id(u_char session_id[16])
                    837: {
                    838:        Buffer m;
                    839:        int i;
                    840:
1.8       markus    841:        debug3("%s entering", __func__);
1.1       provos    842:
                    843:        buffer_init(&m);
                    844:        for (i = 0; i < 16; i++)
                    845:                buffer_put_char(&m, session_id[i]);
                    846:
1.7       mouring   847:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSID, &m);
1.1       provos    848:        buffer_free(&m);
                    849: }
                    850:
                    851: int
                    852: mm_auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey)
                    853: {
                    854:        Buffer m;
                    855:        Key *key;
                    856:        u_char *blob;
                    857:        u_int blen;
1.22      markus    858:        int allowed = 0, have_forced = 0;
1.1       provos    859:
1.8       markus    860:        debug3("%s entering", __func__);
1.1       provos    861:
                    862:        buffer_init(&m);
                    863:        buffer_put_bignum2(&m, client_n);
                    864:
1.7       mouring   865:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSAKEYALLOWED, &m);
                    866:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSAKEYALLOWED, &m);
1.1       provos    867:
                    868:        allowed = buffer_get_int(&m);
1.22      markus    869:
                    870:        /* fake forced command */
                    871:        auth_clear_options();
                    872:        have_forced = buffer_get_int(&m);
                    873:        forced_command = have_forced ? xstrdup("true") : NULL;
1.1       provos    874:
                    875:        if (allowed && rkey != NULL) {
                    876:                blob = buffer_get_string(&m, &blen);
                    877:                if ((key = key_from_blob(blob, blen)) == NULL)
1.8       markus    878:                        fatal("%s: key_from_blob failed", __func__);
1.1       provos    879:                *rkey = key;
                    880:                xfree(blob);
                    881:        }
                    882:        buffer_free(&m);
                    883:
                    884:        return (allowed);
                    885: }
                    886:
                    887: BIGNUM *
                    888: mm_auth_rsa_generate_challenge(Key *key)
                    889: {
                    890:        Buffer m;
                    891:        BIGNUM *challenge;
                    892:        u_char *blob;
                    893:        u_int blen;
                    894:
1.8       markus    895:        debug3("%s entering", __func__);
1.1       provos    896:
                    897:        if ((challenge = BN_new()) == NULL)
1.8       markus    898:                fatal("%s: BN_new failed", __func__);
1.1       provos    899:
                    900:        key->type = KEY_RSA;    /* XXX cheat for key_to_blob */
                    901:        if (key_to_blob(key, &blob, &blen) == 0)
1.8       markus    902:                fatal("%s: key_to_blob failed", __func__);
1.1       provos    903:        key->type = KEY_RSA1;
                    904:
                    905:        buffer_init(&m);
                    906:        buffer_put_string(&m, blob, blen);
                    907:        xfree(blob);
                    908:
1.7       mouring   909:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSACHALLENGE, &m);
                    910:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSACHALLENGE, &m);
1.1       provos    911:
                    912:        buffer_get_bignum2(&m, challenge);
                    913:        buffer_free(&m);
                    914:
                    915:        return (challenge);
                    916: }
                    917:
                    918: int
                    919: mm_auth_rsa_verify_response(Key *key, BIGNUM *p, u_char response[16])
                    920: {
                    921:        Buffer m;
                    922:        u_char *blob;
                    923:        u_int blen;
                    924:        int success = 0;
                    925:
1.8       markus    926:        debug3("%s entering", __func__);
1.1       provos    927:
                    928:        key->type = KEY_RSA;    /* XXX cheat for key_to_blob */
                    929:        if (key_to_blob(key, &blob, &blen) == 0)
1.8       markus    930:                fatal("%s: key_to_blob failed", __func__);
1.1       provos    931:        key->type = KEY_RSA1;
                    932:
                    933:        buffer_init(&m);
                    934:        buffer_put_string(&m, blob, blen);
                    935:        buffer_put_string(&m, response, 16);
                    936:        xfree(blob);
                    937:
1.7       mouring   938:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSARESPONSE, &m);
                    939:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSARESPONSE, &m);
1.1       provos    940:
                    941:        success = buffer_get_int(&m);
                    942:        buffer_free(&m);
                    943:
                    944:        return (success);
                    945: }
1.29      markus    946:
                    947: #ifdef GSSAPI
                    948: OM_uint32
1.36      avsm      949: mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid)
1.29      markus    950: {
                    951:        Buffer m;
                    952:        OM_uint32 major;
                    953:
                    954:        /* Client doesn't get to see the context */
                    955:        *ctx = NULL;
                    956:
                    957:        buffer_init(&m);
1.36      avsm      958:        buffer_put_string(&m, goid->elements, goid->length);
1.29      markus    959:
                    960:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, &m);
                    961:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, &m);
                    962:
                    963:        major = buffer_get_int(&m);
                    964:
                    965:        buffer_free(&m);
                    966:        return (major);
                    967: }
                    968:
                    969: OM_uint32
                    970: mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in,
                    971:     gss_buffer_desc *out, OM_uint32 *flags)
                    972: {
                    973:        Buffer m;
                    974:        OM_uint32 major;
1.30      deraadt   975:        u_int len;
1.29      markus    976:
                    977:        buffer_init(&m);
                    978:        buffer_put_string(&m, in->value, in->length);
                    979:
                    980:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, &m);
                    981:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, &m);
                    982:
                    983:        major = buffer_get_int(&m);
1.30      deraadt   984:        out->value = buffer_get_string(&m, &len);
                    985:        out->length = len;
1.29      markus    986:        if (flags)
                    987:                *flags = buffer_get_int(&m);
                    988:
                    989:        buffer_free(&m);
                    990:
                    991:        return (major);
1.35      markus    992: }
                    993:
                    994: OM_uint32
                    995: mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic)
                    996: {
                    997:        Buffer m;
                    998:        OM_uint32 major;
                    999:
                   1000:        buffer_init(&m);
                   1001:        buffer_put_string(&m, gssbuf->value, gssbuf->length);
                   1002:        buffer_put_string(&m, gssmic->value, gssmic->length);
                   1003:
                   1004:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, &m);
                   1005:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSCHECKMIC,
                   1006:            &m);
                   1007:
                   1008:        major = buffer_get_int(&m);
                   1009:        buffer_free(&m);
                   1010:        return(major);
1.29      markus   1011: }
                   1012:
                   1013: int
                   1014: mm_ssh_gssapi_userok(char *user)
                   1015: {
                   1016:        Buffer m;
                   1017:        int authenticated = 0;
                   1018:
                   1019:        buffer_init(&m);
                   1020:
                   1021:        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, &m);
                   1022:        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSUSEROK,
                   1023:                                  &m);
                   1024:
                   1025:        authenticated = buffer_get_int(&m);
                   1026:
                   1027:        buffer_free(&m);
                   1028:        debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not ");
                   1029:        return (authenticated);
                   1030: }
                   1031: #endif /* GSSAPI */
1.64      djm      1032:
                   1033: #ifdef JPAKE
                   1034: void
                   1035: mm_auth2_jpake_get_pwdata(Authctxt *authctxt, BIGNUM **s,
                   1036:     char **hash_scheme, char **salt)
                   1037: {
                   1038:        Buffer m;
                   1039:
                   1040:        debug3("%s entering", __func__);
                   1041:
                   1042:        buffer_init(&m);
                   1043:        mm_request_send(pmonitor->m_recvfd,
                   1044:            MONITOR_REQ_JPAKE_GET_PWDATA, &m);
                   1045:
                   1046:        debug3("%s: waiting for MONITOR_ANS_JPAKE_GET_PWDATA", __func__);
                   1047:        mm_request_receive_expect(pmonitor->m_recvfd,
                   1048:            MONITOR_ANS_JPAKE_GET_PWDATA, &m);
                   1049:
                   1050:        *hash_scheme = buffer_get_string(&m, NULL);
                   1051:        *salt = buffer_get_string(&m, NULL);
                   1052:
                   1053:        buffer_free(&m);
                   1054: }
                   1055:
                   1056: void
1.65      djm      1057: mm_jpake_step1(struct modp_group *grp,
1.64      djm      1058:     u_char **id, u_int *id_len,
                   1059:     BIGNUM **priv1, BIGNUM **priv2, BIGNUM **g_priv1, BIGNUM **g_priv2,
                   1060:     u_char **priv1_proof, u_int *priv1_proof_len,
                   1061:     u_char **priv2_proof, u_int *priv2_proof_len)
                   1062: {
                   1063:        Buffer m;
                   1064:
                   1065:        debug3("%s entering", __func__);
                   1066:
                   1067:        buffer_init(&m);
                   1068:        mm_request_send(pmonitor->m_recvfd,
                   1069:            MONITOR_REQ_JPAKE_STEP1, &m);
                   1070:
                   1071:        debug3("%s: waiting for MONITOR_ANS_JPAKE_STEP1", __func__);
                   1072:        mm_request_receive_expect(pmonitor->m_recvfd,
                   1073:            MONITOR_ANS_JPAKE_STEP1, &m);
                   1074:
                   1075:        if ((*priv1 = BN_new()) == NULL ||
                   1076:            (*priv2 = BN_new()) == NULL ||
                   1077:            (*g_priv1 = BN_new()) == NULL ||
                   1078:            (*g_priv2 = BN_new()) == NULL)
                   1079:                fatal("%s: BN_new", __func__);
                   1080:
                   1081:        *id = buffer_get_string(&m, id_len);
                   1082:        /* priv1 and priv2 are, well, private */
                   1083:        buffer_get_bignum2(&m, *g_priv1);
                   1084:        buffer_get_bignum2(&m, *g_priv2);
                   1085:        *priv1_proof = buffer_get_string(&m, priv1_proof_len);
                   1086:        *priv2_proof = buffer_get_string(&m, priv2_proof_len);
                   1087:
                   1088:        buffer_free(&m);
                   1089: }
                   1090:
                   1091: void
1.65      djm      1092: mm_jpake_step2(struct modp_group *grp, BIGNUM *s,
1.64      djm      1093:     BIGNUM *mypub1, BIGNUM *theirpub1, BIGNUM *theirpub2, BIGNUM *mypriv2,
                   1094:     const u_char *theirid, u_int theirid_len,
                   1095:     const u_char *myid, u_int myid_len,
                   1096:     const u_char *theirpub1_proof, u_int theirpub1_proof_len,
                   1097:     const u_char *theirpub2_proof, u_int theirpub2_proof_len,
                   1098:     BIGNUM **newpub,
                   1099:     u_char **newpub_exponent_proof, u_int *newpub_exponent_proof_len)
                   1100: {
                   1101:        Buffer m;
                   1102:
                   1103:        debug3("%s entering", __func__);
                   1104:
                   1105:        buffer_init(&m);
                   1106:        /* monitor already has all bignums except theirpub1, theirpub2 */
                   1107:        buffer_put_bignum2(&m, theirpub1);
                   1108:        buffer_put_bignum2(&m, theirpub2);
                   1109:        /* monitor already knows our id */
                   1110:        buffer_put_string(&m, theirid, theirid_len);
                   1111:        buffer_put_string(&m, theirpub1_proof, theirpub1_proof_len);
                   1112:        buffer_put_string(&m, theirpub2_proof, theirpub2_proof_len);
                   1113:
                   1114:        mm_request_send(pmonitor->m_recvfd,
                   1115:            MONITOR_REQ_JPAKE_STEP2, &m);
                   1116:
                   1117:        debug3("%s: waiting for MONITOR_ANS_JPAKE_STEP2", __func__);
                   1118:        mm_request_receive_expect(pmonitor->m_recvfd,
                   1119:            MONITOR_ANS_JPAKE_STEP2, &m);
                   1120:
                   1121:        if ((*newpub = BN_new()) == NULL)
                   1122:                fatal("%s: BN_new", __func__);
                   1123:
                   1124:        buffer_get_bignum2(&m, *newpub);
                   1125:        *newpub_exponent_proof = buffer_get_string(&m,
                   1126:            newpub_exponent_proof_len);
                   1127:
                   1128:        buffer_free(&m);
                   1129: }
                   1130:
                   1131: void
1.65      djm      1132: mm_jpake_key_confirm(struct modp_group *grp, BIGNUM *s, BIGNUM *step2_val,
1.64      djm      1133:     BIGNUM *mypriv2, BIGNUM *mypub1, BIGNUM *mypub2,
                   1134:     BIGNUM *theirpub1, BIGNUM *theirpub2,
                   1135:     const u_char *my_id, u_int my_id_len,
                   1136:     const u_char *their_id, u_int their_id_len,
                   1137:     const u_char *sess_id, u_int sess_id_len,
                   1138:     const u_char *theirpriv2_s_proof, u_int theirpriv2_s_proof_len,
                   1139:     BIGNUM **k,
                   1140:     u_char **confirm_hash, u_int *confirm_hash_len)
                   1141: {
                   1142:        Buffer m;
                   1143:
                   1144:        debug3("%s entering", __func__);
                   1145:
                   1146:        buffer_init(&m);
                   1147:        /* monitor already has all bignums except step2_val */
                   1148:        buffer_put_bignum2(&m, step2_val);
                   1149:        /* monitor already knows all the ids */
                   1150:        buffer_put_string(&m, theirpriv2_s_proof, theirpriv2_s_proof_len);
                   1151:
                   1152:        mm_request_send(pmonitor->m_recvfd,
                   1153:            MONITOR_REQ_JPAKE_KEY_CONFIRM, &m);
                   1154:
                   1155:        debug3("%s: waiting for MONITOR_ANS_JPAKE_KEY_CONFIRM", __func__);
                   1156:        mm_request_receive_expect(pmonitor->m_recvfd,
                   1157:            MONITOR_ANS_JPAKE_KEY_CONFIRM, &m);
                   1158:
                   1159:        /* 'k' is sensitive and stays in the monitor */
                   1160:        *confirm_hash = buffer_get_string(&m, confirm_hash_len);
                   1161:
                   1162:        buffer_free(&m);
                   1163: }
                   1164:
                   1165: int
                   1166: mm_jpake_check_confirm(const BIGNUM *k,
                   1167:     const u_char *peer_id, u_int peer_id_len,
                   1168:     const u_char *sess_id, u_int sess_id_len,
                   1169:     const u_char *peer_confirm_hash, u_int peer_confirm_hash_len)
                   1170: {
                   1171:        Buffer m;
                   1172:        int success = 0;
                   1173:
                   1174:        debug3("%s entering", __func__);
                   1175:
                   1176:        buffer_init(&m);
                   1177:        /* k is dummy in slave, ignored */
                   1178:        /* monitor knows all the ids */
                   1179:        buffer_put_string(&m, peer_confirm_hash, peer_confirm_hash_len);
                   1180:        mm_request_send(pmonitor->m_recvfd,
                   1181:            MONITOR_REQ_JPAKE_CHECK_CONFIRM, &m);
                   1182:
                   1183:        debug3("%s: waiting for MONITOR_ANS_JPAKE_CHECK_CONFIRM", __func__);
                   1184:        mm_request_receive_expect(pmonitor->m_recvfd,
                   1185:            MONITOR_ANS_JPAKE_CHECK_CONFIRM, &m);
                   1186:
                   1187:        success = buffer_get_int(&m);
                   1188:        buffer_free(&m);
                   1189:
                   1190:        debug3("%s: success = %d", __func__, success);
                   1191:        return success;
                   1192: }
                   1193: #endif /* JPAKE */