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

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