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

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