OpenSSH
[Top] [All Lists]

Re: ssh-decrypt

To: openssh@lakedaemon.net
Subject: Re: ssh-decrypt
From: Pawel Krupinski <pak76_ml@yahoo.co.uk>
Date: Thu, 16 Nov 2006 17:27:27 +0000 (GMT)
Cc: openssh-unix-dev@mindrot.org
Delivered-to: sp-com-lists@consult.net
Delivered-to: openssh-unix-dev-list1@securepoint.com
Delivered-to: openssh-unix-dev-tmda@mindrot.org
Delivered-to: openssh-unix-dev@mindrot.org
In-reply-to: <455C7F7A.9080908@lakedaemon.net>
List-archive: <http://lists.mindrot.org/pipermail/openssh-unix-dev>
List-help: <mailto:openssh-unix-dev-request@mindrot.org?subject=help>
List-id: Development of portable OpenSSH <openssh-unix-dev.mindrot.org>
List-post: <mailto:openssh-unix-dev@mindrot.org>
List-subscribe: <http://lists.mindrot.org/mailman/listinfo/openssh-unix-dev>, <mailto:openssh-unix-dev-request@mindrot.org?subject=subscribe>
List-unsubscribe: <http://lists.mindrot.org/mailman/listinfo/openssh-unix-dev>, <mailto:openssh-unix-dev-request@mindrot.org?subject=unsubscribe>
Sender: openssh-unix-dev-bounces+openssh-unix-dev-list1=securepoint.com@mindrot.org
Seems that *.zip files are not accepted...
Let's try again.

Hi Jason,

Here you are. Actually there are two things there: 
1. Three PoC files are:
- ssh-encrypt.c - copy of ssh-add.c with an extra
functionality
- diff.txt - differences in other files
- diffmake.txt - differences in the Makefile

Note: Please remember I wrote it as PoC, so please
don't shout too loud on the quality of my C!!!! Didn't
have time to make it properly. It was a quick check to
prove it can be done ;-)))
Definitely I will have to rewrite it. 

2. file-ssh-encrypt.c & myssh.h are the first draft
version for encrypting secrets using ssh keys.
Currently they are using OpenSSH, but are not
integrated (different error handling etc). I
use it to encrypt secrets using keys from files. 
I'm sending it as is - it is under development as we
speak, but hopefully quality is a bit better ;-)))

Give me a shout what you think, please.

Thanks,
- pak76


--- Jason <openssh@lakedaemon.net> wrote:

> Pawel Krupinski wrote:
> [snip]
> > I'm using ssh agent currently just to manage my
> keys
> > and practically they are used only to provide me
> with
> > SSO to other ssh based systems. Why not use these
> keys
> > (or a separate ssh key pair) to protect passwords
> to
> > things such as database? 
> 
> TrueCrypt/dmcrypt volumes?
> 
> > To put it simple the way I see it is as follow.
> Your
> > passwords (apart from your main ssh password) will
> be
> > stored encrypted using your ssh public key. After
> > logon, ssh-agent will be started and relevant
> key(s)
> > added. When a script will require access to a
> > password, it will:
> > 1. Retrieve the data from somewhere (outside the
> > scope);
> > 2. Decrypt using the ssh utlity (ssh-decrypt(?)) -
> > using ssh-agent or a file.
> > 3. Provide credentials back to script. Or will
> create
> > the establised connection to the database. Or
> > ?(anyway, I think it is outside the scope ;-)).
> >  
> > The bit that cannot be done currently is number 2
> -
> > OpenSSH doesn't provide ssh-decrypt functionality,
> but
> > it is relatively easy to change it - I've played
> with
> > OpenSSH 4.4/4.4p1 and it took me one evening
> (sorry -
> > it was my first approach to OpenSSH as a developer
> > ;-)) and 50 lines of code to implement it (based
> on
> > the ssh-add tool using ssh-agent for decryption).
> In
> > my solution, ssh-decrypt tool sends encrypted
> secret
> > to the ssh-agent, which decrypts it (without
> sending
> > any keys to the ssh-decrypt tool) and sends back
> just
> > an error information or the plaintext password. 
> [snip]
> 
> > If it is something of interest for you, I can do
> all
> > the development and provide you with all the code.
> 
> Could you please email me the diff?
> 
> thx,
> 
> Jason.
> 


Send instant messages to your online friends http://uk.messenger.yahoo.com 
#include "myssh.h"

static void usage(void){
        fprintf(stderr, "usage: ssh-encrypt [-i public_key_file] 
hex_encoded_value_to_encrypt\n");
        exit(ERROR_BAD_USAGE);
}

/* Insecure */
static char* ConvertToHex(const char *input, char* outbuffer){
        char* outputvalues = "0123456789ABCDEF";
        int i = 0;
        
        if ( ( strlen(input) << 1 ) > ( BUFFER_LENGTH - 2) )
                return NULL;

        memset(outbuffer, '\0', BUFFER_LENGTH);
        for(i = 0; i < strlen(input); i++){
                outbuffer[(i<<1) + 1] = outputvalues[input[i] & 0x0F];
                outbuffer[(i<<1)] = outputvalues[(input[i] >> 4) & 0x0F];
        }       
        return outbuffer;
}

/* Please note that value returned in the returnvalue must be freed using 
FreeMemory! */
static int EncryptSecret(const char* tobeencrypted, const char* filename, 
char** returnvalue){
        Key * PublicKey = NULL;
        BIGNUM *input = NULL;
        BIGNUM *output = NULL;
        
        CLEARERROR;
        

        /* CHECK PARAMETERS */
        if ( ( filename == NULL ) || ( tobeencrypted == NULL ) || ( returnvalue 
== NULL ) ){
                SETERROR("NULL parameters");
                return ERROR_NULL_PARAMETERS;
        }
        
        /* INITIATE LIBRARIES */
        init_rng();
        seed_rng();
        SSLeay_add_all_algorithms();
        
        /* LOAD PUBLIC KEY FROM A FILE */
        PublicKey = key_load_public(filename, NULL);
        if ( PublicKey == NULL ){
                SETERROR("Could not load the public key");
                return ERROR_COULD_NOT_LOAD_KEY;
        }

        /* INITIATE VARIABLES */
        BN_hex2bn(&input, tobeencrypted);
        /*      Next line is due to a bug (IMHO), but as we have no choice... */
        output = BN_dup(input); 

        /* ENCRYPT USING PUBLIC KEY - OpenSSH deals with error handling */
        rsa_public_encrypt(output, input, PublicKey->rsa);

        /* Copy output */
        *returnvalue = BN_bn2hex(output);

        /* FREE PARAMETERS */
        key_free(PublicKey);
        BN_free(input);
        BN_free(output);
        return NO_ERROR;
}

int main(int ac, char **av){
        char filename[MAX_PATH_LENGTH];
        char* homedir = NULL;
        char* encryptedstring = NULL;
        extern char* optarg;
        extern int optind, opterr, optopt;
        int opt;
        char* hexsecret = NULL;
        char tBuffer[BUFFER_LENGTH];
        struct stat st;
        int ret_value = -1;

        /* Setting length of the filename to 0 */
        filename[0] = '\0';

        /* read options */
        while ((opt = getopt(ac, av, "i:")) != -1){
                switch (opt){
                        case 'i':
                                if ( strlen(optarg) >= MAX_PATH_LENGTH ){
                                        fprintf(stderr, TooLongFileName);
                                        exit(ERROR_TOO_LONG_PATH);
                                }
                                        
                                if (stat(optarg, &st) < 0 ){
                                        fprintf(stderr, "Error: Public key file 
%s not accessible: %s.\n", optarg, strerror(errno));
                                        exit(ERROR_COULD_NOT_ACCESS_KEY_FILE);
                                }
                                strncpy(filename, optarg, MAX_PATH_LENGTH);
                                break;
                        default:
                                usage();                
                }
        }
        if (strlen(filename) == 0){
                homedir = getenv("HOME");
                if ( homedir == NULL ){
                        fprintf(stderr, "HOME variable is not defined\n");
                        exit(ERROR_HOME_NOT_DEFINED);
                }
                if ( (strlen(homedir) + strlen(DefaultIdentityFile)) >= 
MAX_PATH_LENGTH ){
                        fprintf(stderr, TooLongFileName);
                        exit(ERROR_TOO_LONG_PATH);
                } 
                
                strncpy(filename, homedir, MAX_PATH_LENGTH);    
                strncat(filename, DefaultIdentityFile, MAX_PATH_LENGTH);
                if (stat(filename, &st) < 0 ){
                        fprintf(stderr, "Error: Public key file %s not 
accessible: %s.\n", filename, strerror(errno));
                        exit(ERROR_COULD_NOT_ACCESS_KEY_FILE);
                }
        }

        if (optind + 1 != ac)
                usage();
        
        if ( ( hexsecret = ConvertToHex(av[optind], tBuffer) ) == NULL ){
                fprintf(stderr, "Too long input\n");
                return ERROR_TOO_LONG_INPUT;
        }

        if ( ( ret_value = EncryptSecret(hexsecret, filename, &encryptedstring) 
) == NO_ERROR ){
                printf("%s\n", encryptedstring);
                FreeMemory(encryptedstring);
        }
        else
                fprintf(stderr, "%s\n", LastError); 

        return ret_value;
}

#include "includes.h"

#include <sys/types.h>
#ifdef HAVE_SYS_STAT_H
# include <sys/stat.h>
#endif
#include <sys/resource.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/un.h>

#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#ifdef HAVE_PATHS_H
#include <paths.h>
#endif
#include <pwd.h>
#include <signal.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <netinet/in.h>
#include <arpa/inet.h>

#include <openssl/evp.h>
#include <openssl/err.h>

#include "xmalloc.h"
#include "ssh.h"
#include "ssh1.h"
#include "ssh2.h"
#include "compat.h"
#include "cipher.h"
#include "packet.h"
#include "buffer.h"
#include "channels.h"
#include "key.h"
#include "authfd.h"
#include "authfile.h"
#include "pathnames.h"
#include "dispatch.h"
#include "clientloop.h"
#include "log.h"
#include "readconf.h"
#include "sshconnect.h"
#include "misc.h"
#include "kex.h"
#include "mac.h"
#include "sshpty.h"
#include "match.h"
#include "msg.h"
#include "monitor_fdpass.h"
#include "uidswap.h"
#include "version.h"

#ifdef SMARTCARD
#include "scard.h"
#endif

#include <syslog.h>

const char *DefaultIdentityFile = "/.ssh/id_rsa";

const char *TooLongFileName = "Too long path name\n";

#define MAX_PATH_LENGTH 1024
#define BUFFER_LENGTH 8192
#define MAX_SECRET_ID_LENGTH 1024

#define NO_ERROR 0
#define ERROR_BAD_USAGE 1
#define ERROR_TOO_LONG_PATH 2
#define ERROR_COULD_NOT_ACCESS_KEY_FILE 3 
#define ERROR_HOME_NOT_DEFINED 4
#define ERROR_COULD_NOT_LOAD_KEY 5
#define ERROR_NULL_PARAMETERS 6
#define ERROR_BAD_PARAM 7
#define ERROR_SECRET_NOT_FOUND 8
#define ERROR_TOO_LONG_INPUT 9

/* Error handling */
#define MAX_ERROR_SIZE 2048
char LastError[MAX_ERROR_SIZE];
#define SETERROR(x) snprintf(LastError, MAX_ERROR_SIZE, "Error: \"%s\" in %s, 
line %d", (x), __FILE__, __LINE__)
#define CLEARERROR LastError[0] = '\0'  

/* Freeing OpenSSL memory buffers */
#define FreeMemory(x) OPENSSL_free((x))
65c65
< TARGETS=ssh$(EXEEXT) sshd$(EXEEXT) ssh-add$(EXEEXT) ssh-keygen$(EXEEXT) 
ssh-keyscan${EXEEXT} ssh-keysign${EXEEXT} ssh-agent$(EXEEXT) scp$(EXEEXT) 
ssh-rand-helper${EXEEXT} sftp-server$(EXEEXT) sftp$(EXEEXT)
---
> TARGETS=ssh$(EXEEXT) sshd$(EXEEXT) ssh-encrypt$(EXEEXT) ssh-add$(EXEEXT) 
> ssh-keygen$(EXEEXT) ssh-keyscan${EXEEXT} ssh-keysign${EXEEXT} 
> ssh-agent$(EXEEXT) scp$(EXEEXT) ssh-rand-helper${EXEEXT} sftp-server$(EXEEXT) 
> sftp$(EXEEXT)
145a146,148
> ssh-encrypt$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-encrypt.o
>       $(LD) -o $@ ssh-encrypt.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
> 
/* $OpenBSD: ssh-add.c,v 1.89 2006/08/03 03:34:42 deraadt Exp $ */
/*
 * Author: Tatu Ylonen <ylo@cs.hut.fi>
 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
 *                    All rights reserved
 * Adds an identity to the authentication server, or removes an identity.
 *
 * As far as I am concerned, the code I have written for this software
 * can be used freely for any purpose.  Any derived versions of this
 * software must be clearly marked as such, and if the derived work is
 * incompatible with the protocol description in the RFC file, it must be
 * called by a name other than "ssh" or "Secure Shell".
 *
 * SSH2 implementation,
 * Copyright (c) 2000, 2001 Markus Friedl.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "includes.h"

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h>

#include <openssl/evp.h>

#include <fcntl.h>
#include <pwd.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "xmalloc.h"
#include "ssh.h"
#include "rsa.h"
#include "log.h"
#include "key.h"
#include "buffer.h"
#include "authfd.h"
#include "authfile.h"
#include "pathnames.h"
#include "misc.h"

/* argv0 */
extern char *__progname;

/* Default files to add */
static char *default_files[] = {
        _PATH_SSH_CLIENT_ID_RSA,
        _PATH_SSH_CLIENT_ID_DSA,
        _PATH_SSH_CLIENT_IDENTITY,
        NULL
};

/* Default lifetime (0 == forever) */
static int lifetime = 0;

/* User has to confirm key use */
static int confirm = 0;

/* we keep a cache of one passphrases */
static char *pass = NULL;
static void
clear_pass(void)
{
        if (pass) {
                memset(pass, 0, strlen(pass));
                xfree(pass);
                pass = NULL;
        }
}

static int
delete_file(AuthenticationConnection *ac, const char *filename)
{
        Key *public;
        char *comment = NULL;
        int ret = -1;

        public = key_load_public(filename, &comment);
        if (public == NULL) {
                printf("Bad key file %s\n", filename);
                return -1;
        }
        if (ssh_remove_identity(ac, public)) {
                fprintf(stderr, "Identity removed: %s (%s)\n", filename, 
comment);
                ret = 0;
        } else
                fprintf(stderr, "Could not remove identity: %s\n", filename);

        key_free(public);
        xfree(comment);

        return ret;
}

/* Send a request to remove all identities. */
static int
delete_all(AuthenticationConnection *ac)
{
        int ret = -1;

        if (ssh_remove_all_identities(ac, 1))
                ret = 0;
        /* ignore error-code for ssh2 */
        ssh_remove_all_identities(ac, 2);

        if (ret == 0)
                fprintf(stderr, "All identities removed.\n");
        else
                fprintf(stderr, "Failed to remove all identities.\n");

        return ret;
}

static int
add_file(AuthenticationConnection *ac, const char *filename)
{
        Key *private;
        char *comment = NULL;
        char msg[1024];
        int fd, perms_ok, ret = -1;

        if ((fd = open(filename, O_RDONLY)) < 0) {
                perror(filename);
                return -1;
        }

        /*
         * Since we'll try to load a keyfile multiple times, permission errors
         * will occur multiple times, so check perms first and bail if wrong.
         */
        perms_ok = key_perm_ok(fd, filename);
        close(fd);
        if (!perms_ok)
                return -1;

        /* At first, try empty passphrase */
        private = key_load_private(filename, "", &comment);
        if (comment == NULL)
                comment = xstrdup(filename);
        /* try last */
        if (private == NULL && pass != NULL)
                private = key_load_private(filename, pass, NULL);
        if (private == NULL) {
                /* clear passphrase since it did not work */
                clear_pass();
                snprintf(msg, sizeof msg, "Enter passphrase for %.200s: ",
                    comment);
                for (;;) {
                        pass = read_passphrase(msg, RP_ALLOW_STDIN);
                        if (strcmp(pass, "") == 0) {
                                clear_pass();
                                xfree(comment);
                                return -1;
                        }
                        private = key_load_private(filename, pass, &comment);
                        if (private != NULL)
                                break;
                        clear_pass();
                        snprintf(msg, sizeof msg,
                            "Bad passphrase, try again for %.200s: ", comment);
                }
        }

        if (ssh_add_identity_constrained(ac, private, comment, lifetime,
            confirm)) {
                fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
                ret = 0;
                if (lifetime != 0)
                        fprintf(stderr,
                            "Lifetime set to %d seconds\n", lifetime);
                if (confirm != 0)
                        fprintf(stderr,
                            "The user has to confirm each use of the key\n");
        } else if (ssh_add_identity(ac, private, comment)) {
                fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
                ret = 0;
        } else {
                fprintf(stderr, "Could not add identity: %s\n", filename);
        }

        xfree(comment);
        key_free(private);

        return ret;
}

static int
update_card(AuthenticationConnection *ac, int add, const char *id)
{
        char *pin;
        int ret = -1;

        pin = read_passphrase("Enter passphrase for smartcard: ", 
RP_ALLOW_STDIN);
        if (pin == NULL)
                return -1;

        if (ssh_update_card(ac, add, id, pin, lifetime, confirm)) {
                fprintf(stderr, "Card %s: %s\n",
                    add ? "added" : "removed", id);
                ret = 0;
        } else {
                fprintf(stderr, "Could not %s card: %s\n",
                    add ? "add" : "remove", id);
                ret = -1;
        }
        xfree(pin);
        return ret;
}

static int
list_identities(AuthenticationConnection *ac, int do_fp)
{
        Key *key;
        char *comment, *fp;
        int had_identities = 0;
        int version;
        BIGNUM *input = NULL;
        BIGNUM *output = NULL;

        BN_hex2bn(&input, "1234567890ABCDEF");
        printf("Secret: %s\n", BN_bn2hex(input));
        for (version = 1; version <= 2; version++) {
                for (key = ssh_get_first_identity(ac, &comment, version);
                    key != NULL;
                    key = ssh_get_next_identity(ac, &comment, version)) {
                        had_identities = 1;
                        if (do_fp) {
                                fp = key_fingerprint(key, SSH_FP_MD5,
                                    SSH_FP_HEX);
                                printf("%d %s %s (%s)\n",
                                    key_size(key), fp, comment, key_type(key));
                                output = BN_dup(key->rsa->n);
                                rsa_public_encrypt(output, input, key->rsa);
printf("%s (%d)\n", BN_bn2hex(output), BN_num_bits(output));
                                xfree(fp);
                        } else {
                                if (!key_write(key, stdout))
                                        fprintf(stderr, "key_write failed");
                                fprintf(stdout, " %s\n", comment);
                        }
                        key_free(key);
                        xfree(comment);
                }
        }
        if (!had_identities) {
                printf("The agent has no identities.\n");
                return -1;
        }
        return 0;
}

int
decrypt(AuthenticationConnection *ac)
{
        char * tobedecrypted = 
"80C1902B75E0962F274AE3B78B093F6EB6190E414E49E7B4083DA87481A175C8975FF513CD61472337A392BBCC6E4BB8717E1B03A032FF6E7963DE3CD851EBA44577323F82880D2BDEB8CA1BE2D2F5717AB9C26AA31F523E1FA445FBB6BC13AEEBF3EF8066724AC72C3B9C695E9B9F82648499A9541B9E0428DA8F0A3B4CB497D3A3F9123A23B11444FB7442AC6F3B12217AC7B273213A59CCEEE717DE87501B859FDB9DB6547652CFDCD8A8A33D69F18402DBAC3A2EDFF67731ADBB96CF490ED62D3F5339C3298F6E26ED524B6634A1DBA776A42EA35326C693C04416B186047739C3E64F5006F5331FD23F91D4D74C40BBCF639574A7E3DE66110E04CAC5C1";
        char * received = NULL;
        int rec_len = 0;

        ssh_decrypt(ac, tobedecrypted, &received, &rec_len);
        printf("Reply(%d): %s\n", rec_len, received);
        return 0;
}

static int
lock_agent(AuthenticationConnection *ac, int lock)
{
        char prompt[100], *p1, *p2;
        int passok = 1, ret = -1;

        strlcpy(prompt, "Enter lock password: ", sizeof(prompt));
        p1 = read_passphrase(prompt, RP_ALLOW_STDIN);
        if (lock) {
                strlcpy(prompt, "Again: ", sizeof prompt);
                p2 = read_passphrase(prompt, RP_ALLOW_STDIN);
                if (strcmp(p1, p2) != 0) {
                        fprintf(stderr, "Passwords do not match.\n");
                        passok = 0;
                }
                memset(p2, 0, strlen(p2));
                xfree(p2);
        }
        if (passok && ssh_lock_agent(ac, lock, p1)) {
                fprintf(stderr, "Agent %slocked.\n", lock ? "" : "un");
                ret = 0;
        } else
                fprintf(stderr, "Failed to %slock agent.\n", lock ? "" : "un");
        memset(p1, 0, strlen(p1));
        xfree(p1);
        return (ret);
}

static int
do_file(AuthenticationConnection *ac, int deleting, char *file)
{
        if (deleting) {
                if (delete_file(ac, file) == -1)
                        return -1;
        } else {
                if (add_file(ac, file) == -1)
                        return -1;
        }
        return 0;
}

static void
usage(void)
{
        fprintf(stderr, "Usage: %s [options] [file ...]\n", __progname);
        fprintf(stderr, "Options:\n");
        fprintf(stderr, "  -l          List fingerprints of all identities.\n");
        fprintf(stderr, "  -L          List public key parameters of all 
identities.\n");
        fprintf(stderr, "  -d          Delete identity.\n");
        fprintf(stderr, "  -D          Delete all identities.\n");
        fprintf(stderr, "  -x          Lock agent.\n");
        fprintf(stderr, "  -X          Unlock agent.\n");
        fprintf(stderr, "  -t life     Set lifetime (in seconds) when adding 
identities.\n");
        fprintf(stderr, "  -c          Require confirmation to sign using 
identities\n");
#ifdef SMARTCARD
        fprintf(stderr, "  -s reader   Add key in smartcard reader.\n");
        fprintf(stderr, "  -e reader   Remove key in smartcard reader.\n");
#endif
}

int
main(int argc, char **argv)
{
        extern char *optarg;
        extern int optind;
        AuthenticationConnection *ac = NULL;
        char *sc_reader_id = NULL;
        int i, ch, deleting = 0, ret = 0;

        /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
        sanitise_stdfd();

        __progname = ssh_get_progname(argv[0]);
        init_rng();
        seed_rng();

        SSLeay_add_all_algorithms();

        /* At first, get a connection to the authentication agent. */
        ac = ssh_get_authentication_connection();
        if (ac == NULL) {
                fprintf(stderr,
                    "Could not open a connection to your authentication 
agent.\n");
                exit(2);
        }
        while ((ch = getopt(argc, argv, "plLcdDxXe:s:t:")) != -1) {
                switch (ch) {
                case 'p':
                        decrypt(ac);
                        goto done;
                case 'l':
                case 'L':
                        if (list_identities(ac, ch == 'l' ? 1 : 0) == -1)
                                ret = 1;
                        goto done;
                case 'x':
                case 'X':
                        if (lock_agent(ac, ch == 'x' ? 1 : 0) == -1)
                                ret = 1;
                        goto done;
                case 'c':
                        confirm = 1;
                        break;
                case 'd':
                        deleting = 1;
                        break;
                case 'D':
                        if (delete_all(ac) == -1)
                                ret = 1;
                        goto done;
                case 's':
                        sc_reader_id = optarg;
                        break;
                case 'e':
                        deleting = 1;
                        sc_reader_id = optarg;
                        break;
                case 't':
                        if ((lifetime = convtime(optarg)) == -1) {
                                fprintf(stderr, "Invalid lifetime\n");
                                ret = 1;
                                goto done;
                        }
                        break;
                default:
                        usage();
                        ret = 1;
                        goto done;
                }
        }
        argc -= optind;
        argv += optind;
        if (sc_reader_id != NULL) {
                if (update_card(ac, !deleting, sc_reader_id) == -1)
                        ret = 1;
                goto done;
        }
        if (argc == 0) {
                char buf[MAXPATHLEN];
                struct passwd *pw;
                struct stat st;
                int count = 0;

                if ((pw = getpwuid(getuid())) == NULL) {
                        fprintf(stderr, "No user found with uid %u\n",
                            (u_int)getuid());
                        ret = 1;
                        goto done;
                }

                for (i = 0; default_files[i]; i++) {
                        snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir,
                            default_files[i]);
                        if (stat(buf, &st) < 0)
                                continue;
                        if (do_file(ac, deleting, buf) == -1)
                                ret = 1;
                        else
                                count++;
                }
                if (count == 0)
                        ret = 1;
        } else {
                for (i = 0; i < argc; i++) {
                        if (do_file(ac, deleting, argv[i]) == -1)
                                ret = 1;
                }
        }
        clear_pass();

done:
        ssh_close_authentication_connection(ac);
        return ret;
}
diff openssh-4.4p1/authfd.c openssh-4.4p1_my/authfd.c
302a303,318
> int
> ssh_decrypt(AuthenticationConnection *ac, char* input, char** output, int* 
> output_len){
>       Buffer request;
>       int code1 = SSH2_AGENTC_DECRYPT_REQUEST;
>       int code2 = SSH2_AGENTC_DECRYPT_RESPONSE;
>       buffer_init(&request);
>       buffer_put_char(&request, code1);
>       buffer_put_string(&request, input, strlen(input));
>       ssh_request_reply(ac, &request, &request);
>       if ( buffer_get_char(&request) != code2 )
>               printf("NOT EQUAL\n");  
>       *output = buffer_get_string(&request, output_len);
>       buffer_free(&request);
>       return 0;
> }
> 
diff openssh-4.4p1/authfd.h openssh-4.4p1_my/authfd.h
38a39,41
> #define SSH2_AGENTC_DECRYPT_REQUEST           15
> #define SSH2_AGENTC_DECRYPT_RESPONSE          16
> 
94a98,100
> int
> ssh_decrypt(AuthenticationConnection *, char* input, char** ouptput, int* 
> output_len);
> 
Only in openssh-4.4p1_my/: buildpkg.sh
Only in openssh-4.4p1_my/: config.h
Only in openssh-4.4p1_my/: config.status
Common subdirectories: openssh-4.4p1/contrib and openssh-4.4p1_my/contrib
Only in openssh-4.4p1_my/: len
Only in openssh-4.4p1_my/: Makefile
Only in openssh-4.4p1_my/: Makefile.oryg
Only in openssh-4.4p1_my/: n.txt
Common subdirectories: openssh-4.4p1/openbsd-compat and 
openssh-4.4p1_my/openbsd-compat
Only in openssh-4.4p1_my/: opensshd.init
Only in openssh-4.4p1_my/: openssh.xml
Common subdirectories: openssh-4.4p1/regress and openssh-4.4p1_my/regress
Common subdirectories: openssh-4.4p1/scard and openssh-4.4p1_my/scard
Only in openssh-4.4p1_my/: secret.txt
diff openssh-4.4p1/ssh-agent.c openssh-4.4p1_my/ssh-agent.c
305a306,340
> static void
> decrypt_message(SocketEntry *e)
> {
>       Idtab *tab = idtab_lookup(2);
>       Identity *id;
>       Buffer msg;
>       char * blob = NULL;
>       int blen = 0;
>       BIGNUM* input = NULL;
>       BIGNUM* output = NULL;
>       char* rValue = NULL;
> 
>       blob = buffer_get_string(&e->request, &blen);
>       BN_hex2bn(&input, blob);
>       BN_hex2bn(&output, blob);       
>       TAILQ_FOREACH(id, &tab->idlist, next) {
>               if ( rsa_private_decrypt(output, input, id->key->rsa) > 0 ){
>                       buffer_init(&msg);
>                       buffer_put_char(&msg, SSH2_AGENTC_DECRYPT_RESPONSE);
>                       rValue = BN_bn2hex(output);
>                       buffer_put_string(&msg, rValue, strlen(rValue));
>                       buffer_put_int(&e->output, buffer_len(&msg));
>                       buffer_append(&e->output, buffer_ptr(&msg), 
> buffer_len(&msg));
>                       buffer_free(&msg);
>                       xfree(blob);
>                       return;
>               }
>       }
>       // TO DO - send error message
>       buffer_init(&msg);
>       buffer_put_char(&msg, SSH_AGENT_FAILURE);
>       buffer_free(&msg);
>       xfree(blob);
> }
> 
762a798,800
>       case SSH2_AGENTC_DECRYPT_REQUEST:
>               decrypt_message(e);
>               break;
Only in openssh-4.4p1_my/: ssh-encrypt.c
Only in openssh-4.4p1_my/: ssh-encrypt.c.oryg
Only in openssh-4.4p1_my/: .ssh-encrypt.c.swp
Only in openssh-4.4p1_my/: ssh_prng_cmds
Only in openssh-4.4p1_my/: survey.sh
Only in openssh-4.4p1_my/: test.sh
_______________________________________________
openssh-unix-dev mailing list
openssh-unix-dev@mindrot.org
http://lists.mindrot.org/mailman/listinfo/openssh-unix-dev
<Prev in Thread] Current Thread [Next in Thread>