authfile.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
/*
* Author: Tatu Ylonen <ylo@cs.hut.fi>
* Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
* All rights reserved
* This file contains functions for reading and writing identity files, and
* for reading the passphrase from the user.
*
* 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".
*
*
* Copyright (c) 2000 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
*
* 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"
RCSID("$OpenBSD: authfile.c,v 1.50 2002/06/24 14:55:38 markus Exp $");
#pragma ident "%Z%%M% %I% %E% SMI"
#include "cipher.h"
#include "xmalloc.h"
#include "buffer.h"
#include "bufaux.h"
#include "key.h"
#include "ssh.h"
#include "log.h"
#include "authfile.h"
#include "rsa.h"
/* Version identification string for SSH v1 identity files. */
static const char authfile_id_string[] =
"SSH PRIVATE KEY FILE FORMAT 1.1\n";
/*
* Saves the authentication (private) key in a file, encrypting it with
* passphrase. The identification of the file (lowest 64 bits of n) will
* precede the key to provide identification of the key without needing a
* passphrase.
*/
static int
const char *comment)
{
int fd, i, cipher_num;
/*
* If the passphrase is empty, use SSH_CIPHER_NONE to ease converting
* to another cipher; otherwise use SSH_AUTHFILE_CIPHER.
*/
fatal("save_private_key_rsa: bad cipher");
/* This buffer is used to built the secret part of the private key. */
/* Put checkbytes for checking passphrase validity. */
rand = arc4random();
/*
* Store the private key (n and e will not be stored because they
* will be stored in plain text, and storing them also in encrypted
* format would just give known plaintext).
*/
/* Pad the part to be encrypted until its size is a multiple of 8. */
buffer_put_char(&buffer, 0);
/* This buffer will be used to contain the data in the file. */
/* First store keyfile id string. */
for (i = 0; authfile_id_string[i]; i++)
buffer_put_char(&encrypted, 0);
/* Store cipher type. */
/* Store public key. This will be in plain text. */
/* Allocate space for the private part of the key in the buffer. */
/* Destroy temporary data. */
if (fd < 0) {
return 0;
}
buffer_len(&encrypted)) {
return 0;
}
return 1;
}
/* save SSH v2 key in OpenSSL PEM format */
static int
const char *comment)
{
int fd;
int success = 0;
return 0;
}
if (fd < 0) {
return 0;
}
return 0;
}
case KEY_DSA:
break;
case KEY_RSA:
break;
}
return success;
}
int
const char *comment)
{
case KEY_RSA1:
comment);
break;
case KEY_DSA:
case KEY_RSA:
comment);
break;
default:
break;
}
return 0;
}
/*
* Loads the public part of the ssh v1 key file. Returns NULL if an error was
* encountered (the file does not exist or is not readable), and the key
* otherwise.
*/
static Key *
{
char *cp;
int i;
return NULL;
}
/* Check that it is at least big enough to contain the ID string. */
if (len < sizeof(authfile_id_string)) {
return NULL;
}
/*
* Make sure it begins with the id string. Consume the id string
* from the buffer.
*/
for (i = 0; i < sizeof(authfile_id_string); i++)
return NULL;
}
/* Skip cipher type and reserved data. */
/* Read the public key from the buffer. */
(void) buffer_get_int(&buffer);
if (commentp)
/* The encrypted private part is not parsed by this function. */
return pub;
}
/* load public key from private-key file, works only for SSH v1 */
Key *
{
int fd;
if (fd < 0)
return NULL;
return pub;
}
return NULL;
}
/*
* Loads the private key from the file. Returns 0 if an error is encountered
* (file does not exist or is not readable, or passphrase is bad). This
* initializes the private key.
* Assumes we are called under uid of the owner of the file.
*/
static Key *
char **commentp)
{
return NULL;
}
/* Check that it is at least big enough to contain the ID string. */
if (len < sizeof(authfile_id_string)) {
return NULL;
}
/*
* Make sure it begins with the id string. Consume the id string
* from the buffer.
*/
for (i = 0; i < sizeof(authfile_id_string); i++)
return NULL;
}
/* Read cipher type. */
/* Read the public key from the buffer. */
(void) buffer_get_int(&buffer);
if (commentp)
else
/* Check that it is a supported cipher. */
debug("Unsupported cipher %d used in key file %.200s.",
goto fail;
}
/* Initialize space for decrypted data. */
/* Rest of the buffer is encrypted. Decrypt it using the passphrase. */
debug("Bad passphrase supplied for key file %.200s.",
filename);
/* Bad passphrase. */
goto fail;
}
/* Read the rest of the private key. */
/* in SSL and SSH v1 p and q are exchanged */
/* calculate p-1 and q-1 */
return prv;
fail:
if (commentp)
return NULL;
}
Key *
char **commentp)
{
char *name = "<no key>";
return NULL;
}
debug("PEM_read_PrivateKey failed");
(void)ERR_get_error();
name = "rsa w/o comment";
#ifdef DEBUG_PK
#endif
name = "dsa w/o comment";
#ifdef DEBUG_PK
#endif
} else {
error("PEM_read_PrivateKey: mismatch or "
}
debug("read PEM private key done: type %s",
return prv;
}
static int
{
return 0;
/*
* if a key owned by the user is accessed, then we check the
* permissions of the file. if the key owned by a different user,
* then we don't care.
*/
#ifdef HAVE_CYGWIN
if (check_ntsec(filename))
#endif
error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
error("@ WARNING: UNPROTECTED PRIVATE KEY FILE! @");
error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
error("Permissions 0%3.3o for '%s' are too open.",
error("It is recommended that your private key files are NOT accessible by others.");
error("This private key will be ignored.");
return 0;
}
return 1;
}
Key *
char **commentp)
{
int fd;
if (fd < 0)
return NULL;
return NULL;
}
switch (type) {
case KEY_RSA1:
commentp);
/* closes fd */
break;
case KEY_DSA:
case KEY_RSA:
case KEY_UNSPEC:
/* closes fd */
break;
default:
break;
}
return NULL;
}
Key *
char **commentp)
{
int fd;
if (fd < 0)
return NULL;
return NULL;
}
/* closes fd */
/* use the filename as a comment for PEM */
} else {
/* it's a SSH v1 key if the public key part is readable */
/* closes fd */
}
return prv;
}
static int
{
FILE *f;
char line[4096];
char *cp;
if (f != NULL) {
switch (*cp) {
case '#':
case '\n':
case '\0':
continue;
}
/* Skip leading whitespace. */
;
if (*cp) {
if (commentp)
fclose(f);
return 1;
}
}
}
fclose(f);
}
return 0;
}
/* load public key from ssh v1 private or any pubkey file */
Key *
{
char file[MAXPATHLEN];
return pub;
return pub;
return pub;
return NULL;
}