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

Annotation of src/usr.bin/ssh/authfd.c, Revision 1.94

1.94    ! djm         1: /* $OpenBSD: authfd.c,v 1.93 2014/04/29 18:01:49 markus Exp $ */
1.1       deraadt     2: /*
1.13      deraadt     3:  * Author: Tatu Ylonen <ylo@cs.hut.fi>
                      4:  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
                      5:  *                    All rights reserved
1.27      deraadt     6:  * Functions for connecting the local authentication agent.
1.18      markus      7:  *
1.27      deraadt     8:  * As far as I am concerned, the code I have written for this software
                      9:  * can be used freely for any purpose.  Any derived versions of this
                     10:  * software must be clearly marked as such, and if the derived work is
                     11:  * incompatible with the protocol description in the RFC file, it must be
                     12:  * called by a name other than "ssh" or "Secure Shell".
1.18      markus     13:  *
1.25      markus     14:  * SSH2 implementation,
1.27      deraadt    15:  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
1.25      markus     16:  *
1.27      deraadt    17:  * Redistribution and use in source and binary forms, with or without
                     18:  * modification, are permitted provided that the following conditions
                     19:  * are met:
                     20:  * 1. Redistributions of source code must retain the above copyright
                     21:  *    notice, this list of conditions and the following disclaimer.
                     22:  * 2. Redistributions in binary form must reproduce the above copyright
                     23:  *    notice, this list of conditions and the following disclaimer in the
                     24:  *    documentation and/or other materials provided with the distribution.
                     25:  *
                     26:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     27:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     28:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     29:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     30:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     31:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     32:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     33:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     34:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     35:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.13      deraadt    36:  */
1.1       deraadt    37:
1.67      stevesk    38:
                     39: #include <sys/types.h>
                     40: #include <sys/un.h>
1.75      stevesk    41: #include <sys/socket.h>
1.33      markus     42:
1.76      stevesk    43: #include <fcntl.h>
1.79      stevesk    44: #include <stdlib.h>
1.80      deraadt    45: #include <signal.h>
1.78      stevesk    46: #include <string.h>
1.77      stevesk    47: #include <unistd.h>
1.94    ! djm        48: #include <errno.h>
1.1       deraadt    49:
1.80      deraadt    50: #include "xmalloc.h"
1.1       deraadt    51: #include "ssh.h"
                     52: #include "rsa.h"
1.94    ! djm        53: #include "sshbuf.h"
        !            54: #include "sshkey.h"
1.22      markus     55: #include "authfd.h"
1.33      markus     56: #include "cipher.h"
1.28      markus     57: #include "compat.h"
1.33      markus     58: #include "log.h"
                     59: #include "atomicio.h"
1.74      djm        60: #include "misc.h"
1.94    ! djm        61: #include "ssherr.h"
1.2       provos     62:
1.94    ! djm        63: #define MAX_AGENT_IDENTITIES   2048            /* Max keys in agent reply */
        !            64: #define MAX_AGENT_REPLY_LEN    (256 * 1024)    /* Max bytes in agent reply */
1.21      markus     65:
1.29      markus     66: /* macro to check for "agent failure" message */
                     67: #define agent_failed(x) \
1.94    ! djm        68:     ((x == SSH_AGENT_FAILURE) || \
        !            69:     (x == SSH_COM_AGENT2_FAILURE) || \
1.55      deraadt    70:     (x == SSH2_AGENT_FAILURE))
1.29      markus     71:
1.94    ! djm        72: /* Convert success/failure response from agent to a err.h status */
        !            73: static int
        !            74: decode_reply(u_char type)
1.57      stevesk    75: {
1.94    ! djm        76:        if (agent_failed(type))
        !            77:                return SSH_ERR_AGENT_FAILURE;
        !            78:        else if (type == SSH_AGENT_SUCCESS)
1.57      stevesk    79:                return 0;
1.94    ! djm        80:        else
        !            81:                return SSH_ERR_INVALID_FORMAT;
1.57      stevesk    82: }
                     83:
1.1       deraadt    84: /* Returns the number of the authentication fd, or -1 if there is none. */
1.2       provos     85: int
1.94    ! djm        86: ssh_get_authentication_socket(int *fdp)
1.1       deraadt    87: {
1.12      markus     88:        const char *authsocket;
1.94    ! djm        89:        int sock, oerrno;
1.12      markus     90:        struct sockaddr_un sunaddr;
                     91:
1.94    ! djm        92:        if (fdp != NULL)
        !            93:                *fdp = -1;
        !            94:
1.12      markus     95:        authsocket = getenv(SSH_AUTHSOCKET_ENV_NAME);
                     96:        if (!authsocket)
1.94    ! djm        97:                return SSH_ERR_AGENT_NOT_PRESENT;
1.12      markus     98:
1.92      tedu       99:        memset(&sunaddr, 0, sizeof(sunaddr));
1.12      markus    100:        sunaddr.sun_family = AF_UNIX;
                    101:        strlcpy(sunaddr.sun_path, authsocket, sizeof(sunaddr.sun_path));
                    102:
1.94    ! djm       103:        if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
        !           104:                return SSH_ERR_SYSTEM_ERROR;
1.12      markus    105:
                    106:        /* close on exec */
1.94    ! djm       107:        if (fcntl(sock, F_SETFD, FD_CLOEXEC) == -1 ||
        !           108:            connect(sock, (struct sockaddr *)&sunaddr, sizeof(sunaddr)) < 0) {
        !           109:                oerrno = errno;
1.12      markus    110:                close(sock);
1.94    ! djm       111:                errno = oerrno;
        !           112:                return SSH_ERR_SYSTEM_ERROR;
1.12      markus    113:        }
1.94    ! djm       114:        if (fdp != NULL)
        !           115:                *fdp = sock;
        !           116:        else
1.12      markus    117:                close(sock);
1.94    ! djm       118:        return 0;
1.1       deraadt   119: }
                    120:
1.94    ! djm       121: /* Communicate with agent: send request and read reply */
1.41      itojun    122: static int
1.94    ! djm       123: ssh_request_reply(int sock, struct sshbuf *request, struct sshbuf *reply)
1.24      markus    124: {
1.94    ! djm       125:        int r;
        !           126:        size_t l, len;
1.24      markus    127:        char buf[1024];
                    128:
                    129:        /* Get the length of the message, and format it in the buffer. */
1.94    ! djm       130:        len = sshbuf_len(request);
1.74      djm       131:        put_u32(buf, len);
1.24      markus    132:
                    133:        /* Send the length and then the packet to the agent. */
1.94    ! djm       134:        if (atomicio(vwrite, sock, buf, 4) != 4 ||
        !           135:            atomicio(vwrite, sock, (u_char *)sshbuf_ptr(request),
        !           136:            sshbuf_len(request)) != sshbuf_len(request))
        !           137:                return SSH_ERR_AGENT_COMMUNICATION;
1.24      markus    138:        /*
                    139:         * Wait for response from the agent.  First read the length of the
                    140:         * response packet.
                    141:         */
1.94    ! djm       142:        if (atomicio(read, sock, buf, 4) != 4)
        !           143:            return SSH_ERR_AGENT_COMMUNICATION;
1.24      markus    144:
                    145:        /* Extract the length, and check it for sanity. */
1.74      djm       146:        len = get_u32(buf);
1.94    ! djm       147:        if (len > MAX_AGENT_REPLY_LEN)
        !           148:                return SSH_ERR_INVALID_FORMAT;
1.24      markus    149:
                    150:        /* Read the rest of the response in to the buffer. */
1.94    ! djm       151:        sshbuf_reset(reply);
1.24      markus    152:        while (len > 0) {
                    153:                l = len;
                    154:                if (l > sizeof(buf))
                    155:                        l = sizeof(buf);
1.94    ! djm       156:                if (atomicio(read, sock, buf, l) != l)
        !           157:                        return SSH_ERR_AGENT_COMMUNICATION;
        !           158:                if ((r = sshbuf_put(reply, buf, l)) != 0)
        !           159:                        return r;
1.24      markus    160:                len -= l;
                    161:        }
1.94    ! djm       162:        return 0;
1.24      markus    163: }
                    164:
1.14      markus    165: /*
                    166:  * Closes the agent socket if it should be closed (depends on how it was
                    167:  * obtained).  The argument must have been returned by
                    168:  * ssh_get_authentication_socket().
                    169:  */
1.18      markus    170: void
1.12      markus    171: ssh_close_authentication_socket(int sock)
1.1       deraadt   172: {
1.12      markus    173:        if (getenv(SSH_AUTHSOCKET_ENV_NAME))
                    174:                close(sock);
1.1       deraadt   175: }
                    176:
1.94    ! djm       177: /* Lock/unlock agent */
        !           178: int
        !           179: ssh_lock_agent(int sock, int lock, const char *password)
1.1       deraadt   180: {
1.94    ! djm       181:        int r;
        !           182:        u_char type = lock ? SSH_AGENTC_LOCK : SSH_AGENTC_UNLOCK;
        !           183:        struct sshbuf *msg;
        !           184:
        !           185:        if ((msg = sshbuf_new()) == NULL)
        !           186:                return SSH_ERR_ALLOC_FAIL;
        !           187:        if ((r = sshbuf_put_u8(msg, type)) != 0 ||
        !           188:            (r = sshbuf_put_cstring(msg, password)) != 0)
        !           189:                goto out;
        !           190:        if ((r = ssh_request_reply(sock, msg, msg)) != 0)
        !           191:                goto out;
        !           192:        if ((r = sshbuf_get_u8(msg, &type)) != 0)
        !           193:                goto out;
        !           194:        r = decode_reply(type);
        !           195:  out:
        !           196:        sshbuf_free(msg);
        !           197:        return r;
1.1       deraadt   198: }
                    199:
1.94    ! djm       200: #ifdef WITH_SSH1
        !           201: static int
        !           202: deserialise_identity1(struct sshbuf *ids, struct sshkey **keyp, char **commentp)
1.1       deraadt   203: {
1.94    ! djm       204:        struct sshkey *key;
        !           205:        int r, keybits;
        !           206:        u_int32_t bits;
        !           207:        char *comment = NULL;
        !           208:
        !           209:        if ((key = sshkey_new(KEY_RSA1)) == NULL)
        !           210:                return SSH_ERR_ALLOC_FAIL;
        !           211:        if ((r = sshbuf_get_u32(ids, &bits)) != 0 ||
        !           212:            (r = sshbuf_get_bignum1(ids, key->rsa->e)) != 0 ||
        !           213:            (r = sshbuf_get_bignum1(ids, key->rsa->n)) != 0 ||
        !           214:            (r = sshbuf_get_cstring(ids, &comment, NULL)) != 0)
        !           215:                goto out;
        !           216:        keybits = BN_num_bits(key->rsa->n);
        !           217:        /* XXX previously we just warned here. I think we should be strict */
        !           218:        if (keybits < 0 || bits != (u_int)keybits) {
        !           219:                r = SSH_ERR_KEY_BITS_MISMATCH;
        !           220:                goto out;
        !           221:        }
        !           222:        if (keyp != NULL) {
        !           223:                *keyp = key;
        !           224:                key = NULL;
        !           225:        }
        !           226:        if (commentp != NULL) {
        !           227:                *commentp = comment;
        !           228:                comment = NULL;
        !           229:        }
        !           230:        r = 0;
        !           231:  out:
        !           232:        sshkey_free(key);
        !           233:        free(comment);
        !           234:        return r;
1.50      markus    235: }
1.94    ! djm       236: #endif
1.50      markus    237:
1.94    ! djm       238: static int
        !           239: deserialise_identity2(struct sshbuf *ids, struct sshkey **keyp, char **commentp)
1.50      markus    240: {
1.94    ! djm       241:        int r;
        !           242:        char *comment = NULL;
        !           243:        const u_char *blob;
        !           244:        size_t blen;
        !           245:
        !           246:        if ((r = sshbuf_get_string_direct(ids, &blob, &blen)) != 0 ||
        !           247:            (r = sshbuf_get_cstring(ids, &comment, NULL)) != 0)
        !           248:                goto out;
        !           249:        if ((r = sshkey_from_blob(blob, blen, keyp)) != 0)
        !           250:                goto out;
        !           251:        if (commentp != NULL) {
        !           252:                *commentp = comment;
        !           253:                comment = NULL;
        !           254:        }
        !           255:        r = 0;
        !           256:  out:
        !           257:        free(comment);
        !           258:        return r;
1.1       deraadt   259: }
                    260:
1.14      markus    261: /*
1.94    ! djm       262:  * Fetch list of identities held by the agent.
1.14      markus    263:  */
1.30      markus    264: int
1.94    ! djm       265: ssh_fetch_identitylist(int sock, int version, struct ssh_identitylist **idlp)
1.1       deraadt   266: {
1.94    ! djm       267:        u_char type, code1 = 0, code2 = 0;
        !           268:        u_int32_t num, i;
        !           269:        struct sshbuf *msg;
        !           270:        struct ssh_identitylist *idl = NULL;
        !           271:        int r;
1.25      markus    272:
1.94    ! djm       273:        /* Determine request and expected response types */
1.46      deraadt   274:        switch (version) {
1.25      markus    275:        case 1:
                    276:                code1 = SSH_AGENTC_REQUEST_RSA_IDENTITIES;
                    277:                code2 = SSH_AGENT_RSA_IDENTITIES_ANSWER;
                    278:                break;
                    279:        case 2:
                    280:                code1 = SSH2_AGENTC_REQUEST_IDENTITIES;
                    281:                code2 = SSH2_AGENT_IDENTITIES_ANSWER;
                    282:                break;
                    283:        default:
1.94    ! djm       284:                return SSH_ERR_INVALID_ARGUMENT;
1.25      markus    285:        }
1.1       deraadt   286:
1.14      markus    287:        /*
                    288:         * Send a message to the agent requesting for a list of the
                    289:         * identities it can represent.
                    290:         */
1.94    ! djm       291:        if ((msg = sshbuf_new()) == NULL)
        !           292:                return SSH_ERR_ALLOC_FAIL;
        !           293:        if ((r = sshbuf_put_u8(msg, code1)) != 0)
        !           294:                goto out;
1.12      markus    295:
1.94    ! djm       296:        if ((r = ssh_request_reply(sock, msg, msg)) != 0)
        !           297:                goto out;
1.1       deraadt   298:
1.12      markus    299:        /* Get message type, and verify that we got a proper answer. */
1.94    ! djm       300:        if ((r = sshbuf_get_u8(msg, &type)) != 0)
        !           301:                goto out;
1.29      markus    302:        if (agent_failed(type)) {
1.94    ! djm       303:                r = SSH_ERR_AGENT_FAILURE;
        !           304:                goto out;
1.25      markus    305:        } else if (type != code2) {
1.94    ! djm       306:                r = SSH_ERR_INVALID_FORMAT;
        !           307:                goto out;
1.25      markus    308:        }
1.12      markus    309:
                    310:        /* Get the number of entries in the response and check it for sanity. */
1.94    ! djm       311:        if ((r = sshbuf_get_u32(msg, &num)) != 0)
        !           312:                goto out;
        !           313:        if (num > MAX_AGENT_IDENTITIES) {
        !           314:                r = SSH_ERR_INVALID_FORMAT;
        !           315:                goto out;
        !           316:        }
        !           317:        if (num == 0) {
        !           318:                r = SSH_ERR_AGENT_NO_IDENTITIES;
        !           319:                goto out;
        !           320:        }
        !           321:
        !           322:        /* Deserialise the response into a list of keys/comments */
        !           323:        if ((idl = calloc(1, sizeof(*idl))) == NULL ||
        !           324:            (idl->keys = calloc(num, sizeof(*idl->keys))) == NULL ||
        !           325:            (idl->comments = calloc(num, sizeof(*idl->comments))) == NULL) {
        !           326:                r = SSH_ERR_ALLOC_FAIL;
        !           327:                goto out;
        !           328:        }
        !           329:        for (i = 0; i < num;) {
        !           330:                switch (version) {
        !           331:                case 1:
        !           332: #ifdef WITH_SSH1
        !           333:                        if ((r = deserialise_identity1(msg,
        !           334:                            &(idl->keys[i]), &(idl->comments[i]))) != 0)
        !           335:                                goto out;
        !           336: #endif
        !           337:                        break;
        !           338:                case 2:
        !           339:                        if ((r = deserialise_identity2(msg,
        !           340:                            &(idl->keys[i]), &(idl->comments[i]))) != 0) {
        !           341:                                if (r == SSH_ERR_KEY_TYPE_UNKNOWN) {
        !           342:                                        /* Gracefully skip unknown key types */
        !           343:                                        num--;
        !           344:                                        continue;
        !           345:                                } else
        !           346:                                        goto out;
        !           347:                        }
        !           348:                        break;
        !           349:                }
        !           350:                i++;
        !           351:        }
        !           352:        idl->nkeys = num;
        !           353:        *idlp = idl;
        !           354:        idl = NULL;
        !           355:        r = 0;
        !           356:  out:
        !           357:        sshbuf_free(msg);
        !           358:        if (idl != NULL)
        !           359:                ssh_free_identitylist(idl);
        !           360:        return r;
1.30      markus    361: }
                    362:
1.94    ! djm       363: void
        !           364: ssh_free_identitylist(struct ssh_identitylist *idl)
1.30      markus    365: {
1.94    ! djm       366:        size_t i;
1.1       deraadt   367:
1.94    ! djm       368:        if (idl == NULL)
        !           369:                return;
        !           370:        for (i = 0; i < idl->nkeys; i++) {
        !           371:                if (idl->keys != NULL)
        !           372:                        sshkey_free(idl->keys[i]);
        !           373:                if (idl->comments != NULL)
        !           374:                        free(idl->comments[i]);
1.25      markus    375:        }
1.94    ! djm       376:        free(idl);
1.1       deraadt   377: }
                    378:
1.14      markus    379: /*
1.94    ! djm       380:  * Sends a challenge (typically from a server via ssh(1)) to the agent,
        !           381:  * and waits for a response from the agent.
        !           382:  * Returns true (non-zero) if the agent gave the correct answer, zero
        !           383:  * otherwise.
1.14      markus    384:  */
1.1       deraadt   385:
1.93      markus    386: #ifdef WITH_SSH1
1.2       provos    387: int
1.94    ! djm       388: ssh_decrypt_challenge(int sock, struct sshkey* key, BIGNUM *challenge,
        !           389:     u_char session_id[16], u_char response[16])
        !           390: {
        !           391:        struct sshbuf *msg;
        !           392:        int r;
        !           393:        u_char type;
1.12      markus    394:
1.30      markus    395:        if (key->type != KEY_RSA1)
1.94    ! djm       396:                return SSH_ERR_INVALID_ARGUMENT;
        !           397:        if ((msg = sshbuf_new()) == NULL)
        !           398:                return SSH_ERR_ALLOC_FAIL;
        !           399:        if ((r = sshbuf_put_u8(msg, SSH_AGENTC_RSA_CHALLENGE)) != 0 ||
        !           400:            (r = sshbuf_put_u32(msg, BN_num_bits(key->rsa->n))) != 0 ||
        !           401:            (r = sshbuf_put_bignum1(msg, key->rsa->e)) != 0 ||
        !           402:            (r = sshbuf_put_bignum1(msg, key->rsa->n)) != 0 ||
        !           403:            (r = sshbuf_put_bignum1(msg, challenge)) != 0 ||
        !           404:            (r = sshbuf_put(msg, session_id, 16)) != 0 ||
        !           405:            (r = sshbuf_put_u32(msg, 1)) != 0) /* Response type for proto 1.1 */
        !           406:                goto out;
        !           407:        if ((r = ssh_request_reply(sock, msg, msg)) != 0)
        !           408:                goto out;
        !           409:        if ((r = sshbuf_get_u8(msg, &type)) != 0)
        !           410:                goto out;
1.29      markus    411:        if (agent_failed(type)) {
1.94    ! djm       412:                r = SSH_ERR_AGENT_FAILURE;
        !           413:                goto out;
1.24      markus    414:        } else if (type != SSH_AGENT_RSA_RESPONSE) {
1.94    ! djm       415:                r = SSH_ERR_INVALID_FORMAT;
        !           416:                goto out;
1.12      markus    417:        }
1.94    ! djm       418:        if ((r = sshbuf_get(msg, response, 16)) != 0)
        !           419:                goto out;
        !           420:        r = 0;
        !           421:  out:
        !           422:        sshbuf_free(msg);
        !           423:        return r;
1.12      markus    424: }
1.93      markus    425: #endif
1.1       deraadt   426:
1.94    ! djm       427: /* ask agent to sign data, returns err.h code on error, 0 on success */
1.25      markus    428: int
1.94    ! djm       429: ssh_agent_sign(int sock, struct sshkey *key,
        !           430:     u_char **sigp, size_t *lenp,
        !           431:     const u_char *data, size_t datalen, u_int compat)
        !           432: {
        !           433:        struct sshbuf *msg;
        !           434:        u_char *blob = NULL, type;
        !           435:        size_t blen = 0, len = 0;
        !           436:        u_int flags = 0;
        !           437:        int r = SSH_ERR_INTERNAL_ERROR;
        !           438:
        !           439:        if (sigp != NULL)
        !           440:                *sigp = NULL;
        !           441:        if (lenp != NULL)
        !           442:                *lenp = 0;
        !           443:
        !           444:        if (datalen > SSH_KEY_MAX_SIGN_DATA_SIZE)
        !           445:                return SSH_ERR_INVALID_ARGUMENT;
        !           446:        if (compat & SSH_BUG_SIGBLOB)
        !           447:                flags |= SSH_AGENT_OLD_SIGNATURE;
        !           448:        if ((msg = sshbuf_new()) == NULL)
        !           449:                return SSH_ERR_ALLOC_FAIL;
        !           450:        if ((r = sshkey_to_blob(key, &blob, &blen)) != 0)
        !           451:                goto out;
        !           452:        if ((r = sshbuf_put_u8(msg, SSH2_AGENTC_SIGN_REQUEST)) != 0 ||
        !           453:            (r = sshbuf_put_string(msg, blob, blen)) != 0 ||
        !           454:            (r = sshbuf_put_string(msg, data, datalen)) != 0 ||
        !           455:            (r = sshbuf_put_u32(msg, flags)) != 0)
        !           456:                goto out;
        !           457:        if ((r = ssh_request_reply(sock, msg, msg) != 0))
        !           458:                goto out;
        !           459:        if ((r = sshbuf_get_u8(msg, &type)) != 0)
        !           460:                goto out;
1.29      markus    461:        if (agent_failed(type)) {
1.94    ! djm       462:                r = SSH_ERR_AGENT_FAILURE;
        !           463:                goto out;
1.25      markus    464:        } else if (type != SSH2_AGENT_SIGN_RESPONSE) {
1.94    ! djm       465:                r = SSH_ERR_INVALID_FORMAT;
        !           466:                goto out;
        !           467:        }
        !           468:        if ((r = sshbuf_get_string(msg, sigp, &len)) != 0)
        !           469:                goto out;
        !           470:        *lenp = len;
        !           471:        r = 0;
        !           472:  out:
        !           473:        if (blob != NULL) {
        !           474:                explicit_bzero(blob, blen);
        !           475:                free(blob);
1.25      markus    476:        }
1.94    ! djm       477:        sshbuf_free(msg);
        !           478:        return r;
1.25      markus    479: }
                    480:
1.22      markus    481: /* Encode key for a message to the agent. */
                    482:
1.93      markus    483: #ifdef WITH_SSH1
1.94    ! djm       484: static int
        !           485: ssh_encode_identity_rsa1(struct sshbuf *b, RSA *key, const char *comment)
1.22      markus    486: {
1.94    ! djm       487:        int r;
        !           488:
1.22      markus    489:        /* To keep within the protocol: p < q for ssh. in SSL p > q */
1.94    ! djm       490:        if ((r = sshbuf_put_u32(b, BN_num_bits(key->n))) != 0 ||
        !           491:            (r = sshbuf_put_bignum1(b, key->n)) != 0 ||
        !           492:            (r = sshbuf_put_bignum1(b, key->e)) != 0 ||
        !           493:            (r = sshbuf_put_bignum1(b, key->d)) != 0 ||
        !           494:            (r = sshbuf_put_bignum1(b, key->iqmp)) != 0 ||
        !           495:            (r = sshbuf_put_bignum1(b, key->q)) != 0 ||
        !           496:            (r = sshbuf_put_bignum1(b, key->p)) != 0 ||
        !           497:            (r = sshbuf_put_cstring(b, comment)) != 0)
        !           498:                return r;
        !           499:        return 0;
1.22      markus    500: }
1.93      markus    501: #endif
1.22      markus    502:
1.94    ! djm       503: static int
        !           504: ssh_encode_identity_ssh2(struct sshbuf *b, struct sshkey *key,
        !           505:     const char *comment)
1.22      markus    506: {
1.94    ! djm       507:        int r;
        !           508:
        !           509:        if ((r = sshkey_private_serialize(key, b)) != 0 ||
        !           510:            (r = sshbuf_put_cstring(b, comment)) != 0)
        !           511:                return r;
        !           512:        return 0;
        !           513: }
        !           514:
        !           515: static int
        !           516: encode_constraints(struct sshbuf *m, u_int life, u_int confirm)
        !           517: {
        !           518:        int r;
        !           519:
        !           520:        if (life != 0) {
        !           521:                if ((r = sshbuf_put_u8(m, SSH_AGENT_CONSTRAIN_LIFETIME)) != 0 ||
        !           522:                    (r = sshbuf_put_u32(m, life)) != 0)
        !           523:                        goto out;
        !           524:        }
        !           525:        if (confirm != 0) {
        !           526:                if ((r = sshbuf_put_u8(m, SSH_AGENT_CONSTRAIN_CONFIRM)) != 0)
        !           527:                        goto out;
        !           528:        }
        !           529:        r = 0;
        !           530:  out:
        !           531:        return r;
1.22      markus    532: }
                    533:
1.14      markus    534: /*
1.94    ! djm       535:  * Adds an identity to the authentication server.
        !           536:  * This call is intended only for use by ssh-add(1) and like applications.
1.14      markus    537:  */
1.18      markus    538: int
1.94    ! djm       539: ssh_add_identity_constrained(int sock, struct sshkey *key, const char *comment,
        !           540:     u_int life, u_int confirm)
1.1       deraadt   541: {
1.94    ! djm       542:        struct sshbuf *msg;
        !           543:        int r, constrained = (life || confirm);
        !           544:        u_char type;
1.12      markus    545:
1.94    ! djm       546:        if ((msg = sshbuf_new()) == NULL)
        !           547:                return SSH_ERR_ALLOC_FAIL;
1.22      markus    548:
                    549:        switch (key->type) {
1.93      markus    550: #ifdef WITH_SSH1
1.30      markus    551:        case KEY_RSA1:
1.54      markus    552:                type = constrained ?
                    553:                    SSH_AGENTC_ADD_RSA_ID_CONSTRAINED :
                    554:                    SSH_AGENTC_ADD_RSA_IDENTITY;
1.94    ! djm       555:                if ((r = sshbuf_put_u8(msg, type)) != 0 ||
        !           556:                    (r = ssh_encode_identity_rsa1(msg, key->rsa, comment)) != 0)
        !           557:                        goto out;
1.30      markus    558:                break;
1.93      markus    559: #endif
                    560: #ifdef WITH_OPENSSL
1.22      markus    561:        case KEY_RSA:
1.82      djm       562:        case KEY_RSA_CERT:
1.83      djm       563:        case KEY_RSA_CERT_V00:
1.22      markus    564:        case KEY_DSA:
1.82      djm       565:        case KEY_DSA_CERT:
1.83      djm       566:        case KEY_DSA_CERT_V00:
1.84      djm       567:        case KEY_ECDSA:
                    568:        case KEY_ECDSA_CERT:
1.93      markus    569: #endif
1.90      markus    570:        case KEY_ED25519:
                    571:        case KEY_ED25519_CERT:
1.54      markus    572:                type = constrained ?
                    573:                    SSH2_AGENTC_ADD_ID_CONSTRAINED :
                    574:                    SSH2_AGENTC_ADD_IDENTITY;
1.94    ! djm       575:                if ((r = sshbuf_put_u8(msg, type)) != 0 ||
        !           576:                    (r = ssh_encode_identity_ssh2(msg, key, comment)) != 0)
        !           577:                        goto out;
1.22      markus    578:                break;
                    579:        default:
1.94    ! djm       580:                r = SSH_ERR_INVALID_ARGUMENT;
        !           581:                goto out;
1.12      markus    582:        }
1.94    ! djm       583:        if (constrained &&
        !           584:            (r = encode_constraints(msg, life, confirm)) != 0)
        !           585:                goto out;
        !           586:        if ((r = ssh_request_reply(sock, msg, msg)) != 0)
        !           587:                goto out;
        !           588:        if ((r = sshbuf_get_u8(msg, &type)) != 0)
        !           589:                goto out;
        !           590:        r = decode_reply(type);
        !           591:  out:
        !           592:        sshbuf_free(msg);
        !           593:        return r;
1.54      markus    594: }
                    595:
1.14      markus    596: /*
1.94    ! djm       597:  * Removes an identity from the authentication server.
        !           598:  * This call is intended only for use by ssh-add(1) and like applications.
1.14      markus    599:  */
1.18      markus    600: int
1.94    ! djm       601: ssh_remove_identity(int sock, struct sshkey *key)
1.1       deraadt   602: {
1.94    ! djm       603:        struct sshbuf *msg;
        !           604:        int r;
        !           605:        u_char type, *blob = NULL;
        !           606:        size_t blen;
1.12      markus    607:
1.94    ! djm       608:        if ((msg = sshbuf_new()) == NULL)
        !           609:                return SSH_ERR_ALLOC_FAIL;
1.12      markus    610:
1.93      markus    611: #ifdef WITH_SSH1
1.30      markus    612:        if (key->type == KEY_RSA1) {
1.94    ! djm       613:                if ((r = sshbuf_put_u8(msg,
        !           614:                    SSH_AGENTC_REMOVE_RSA_IDENTITY)) != 0 ||
        !           615:                    (r = sshbuf_put_u32(msg, BN_num_bits(key->rsa->n))) != 0 ||
        !           616:                    (r = sshbuf_put_bignum1(msg, key->rsa->e)) != 0 ||
        !           617:                    (r = sshbuf_put_bignum1(msg, key->rsa->n)) != 0)
        !           618:                        goto out;
1.93      markus    619:        } else
                    620: #endif
                    621:        if (key->type != KEY_UNSPEC) {
1.94    ! djm       622:                if ((r = sshkey_to_blob(key, &blob, &blen)) != 0)
        !           623:                        goto out;
        !           624:                if ((r = sshbuf_put_u8(msg,
        !           625:                    SSH2_AGENTC_REMOVE_IDENTITY)) != 0 ||
        !           626:                    (r = sshbuf_put_string(msg, blob, blen)) != 0)
        !           627:                        goto out;
1.51      markus    628:        } else {
1.94    ! djm       629:                r = SSH_ERR_INVALID_ARGUMENT;
        !           630:                goto out;
1.51      markus    631:        }
1.94    ! djm       632:        if ((r = ssh_request_reply(sock, msg, msg)) != 0)
        !           633:                goto out;
        !           634:        if ((r = sshbuf_get_u8(msg, &type)) != 0)
        !           635:                goto out;
        !           636:        r = decode_reply(type);
        !           637:  out:
        !           638:        if (blob != NULL) {
        !           639:                explicit_bzero(blob, blen);
        !           640:                free(blob);
1.42      markus    641:        }
1.94    ! djm       642:        sshbuf_free(msg);
        !           643:        return r;
1.42      markus    644: }
                    645:
1.94    ! djm       646: /*
        !           647:  * Add/remove an token-based identity from the authentication server.
        !           648:  * This call is intended only for use by ssh-add(1) and like applications.
        !           649:  */
1.42      markus    650: int
1.94    ! djm       651: ssh_update_card(int sock, int add, const char *reader_id, const char *pin,
        !           652:     u_int life, u_int confirm)
1.42      markus    653: {
1.94    ! djm       654:        struct sshbuf *msg;
        !           655:        int r, constrained = (life || confirm);
        !           656:        u_char type;
1.60      djm       657:
                    658:        if (add) {
                    659:                type = constrained ?
                    660:                    SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED :
                    661:                    SSH_AGENTC_ADD_SMARTCARD_KEY;
                    662:        } else
                    663:                type = SSH_AGENTC_REMOVE_SMARTCARD_KEY;
1.42      markus    664:
1.94    ! djm       665:        if ((msg = sshbuf_new()) == NULL)
        !           666:                return SSH_ERR_ALLOC_FAIL;
        !           667:        if ((r = sshbuf_put_u8(msg, type)) != 0 ||
        !           668:            (r = sshbuf_put_cstring(msg, reader_id)) != 0 ||
        !           669:            (r = sshbuf_put_cstring(msg, pin)) != 0)
        !           670:                goto out;
        !           671:        if (constrained &&
        !           672:            (r = encode_constraints(msg, life, confirm)) != 0)
        !           673:                goto out;
        !           674:        if ((r = ssh_request_reply(sock, msg, msg)) != 0)
        !           675:                goto out;
        !           676:        if ((r = sshbuf_get_u8(msg, &type)) != 0)
        !           677:                goto out;
        !           678:        r = decode_reply(type);
        !           679:  out:
        !           680:        sshbuf_free(msg);
        !           681:        return r;
1.12      markus    682: }
                    683:
1.14      markus    684: /*
1.94    ! djm       685:  * Removes all identities from the agent.
        !           686:  * This call is intended only for use by ssh-add(1) and like applications.
1.14      markus    687:  */
1.18      markus    688: int
1.94    ! djm       689: ssh_remove_all_identities(int sock, int version)
1.1       deraadt   690: {
1.94    ! djm       691:        struct sshbuf *msg;
        !           692:        u_char type = (version == 1) ?
        !           693:            SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES :
        !           694:            SSH2_AGENTC_REMOVE_ALL_IDENTITIES;
        !           695:        int r;
        !           696:
        !           697:        if ((msg = sshbuf_new()) == NULL)
        !           698:                return SSH_ERR_ALLOC_FAIL;
        !           699:        if ((r = sshbuf_put_u8(msg, type)) != 0)
        !           700:                goto out;
        !           701:        if ((r = ssh_request_reply(sock, msg, msg)) != 0)
        !           702:                goto out;
        !           703:        if ((r = sshbuf_get_u8(msg, &type)) != 0)
        !           704:                goto out;
        !           705:        r = decode_reply(type);
        !           706:  out:
        !           707:        sshbuf_free(msg);
        !           708:        return r;
1.12      markus    709: }