1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
|
/* $OpenBSD: key.c,v 1.19 2004/09/17 13:53:08 ho Exp $ */
/*
* The author of this code is Angelos D. Keromytis (angelos@cis.upenn.edu)
*
* Copyright (c) 2000-2001 Angelos D. Keromytis.
*
* Permission to use, copy, and modify this software with or without fee
* is hereby granted, provided that this entire notice is included in
* all copies of any software which is or includes a copy or
* modification of this software.
* You may use this code under the GNU public license if you so wish. Please
* contribute changes back to the authors under this freer than GPL license
* so that we may further the use of strong encryption without limitations to
* all.
*
* THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
* REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
* MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
* PURPOSE.
*/
#include <string.h>
#include <stdlib.h>
#include "sysdep.h"
#include "key.h"
#include "libcrypto.h"
#include "log.h"
#include "util.h"
#ifdef USE_X509
#include "x509.h"
#endif
void
key_free(int type, int private, void *key)
{
switch (type) {
case ISAKMP_KEY_PASSPHRASE:
free(key);
break;
case ISAKMP_KEY_RSA:
#ifdef USE_X509
RSA_free(key);
break;
#endif
case ISAKMP_KEY_NONE:
default:
log_error("key_free: unknown/unsupportedkey type %d", type);
break;
}
}
/* Convert from internal form to serialized */
void
key_serialize(int type, int private, void *key, u_int8_t **data,
size_t *datalenp)
{
#ifdef USE_X509
u_int8_t *p;
size_t datalen;
#endif
switch (type) {
case ISAKMP_KEY_PASSPHRASE:
*datalenp = strlen((char *)key);
*data = (u_int8_t *)strdup((char *)key);
break;
case ISAKMP_KEY_RSA:
#ifdef USE_X509
switch (private) {
case ISAKMP_KEYTYPE_PUBLIC:
datalen = i2d_RSAPublicKey((RSA *)key, NULL);
*data = p = malloc(datalen);
if (!p) {
log_error("key_serialize: malloc (%lu) failed",
(unsigned long)datalen);
return;
}
*datalenp = i2d_RSAPublicKey((RSA *) key, &p);
break;
case ISAKMP_KEYTYPE_PRIVATE:
datalen = i2d_RSAPrivateKey((RSA *)key, NULL);
*data = p = malloc(datalen);
if (!p) {
log_error("key_serialize: malloc (%lu) failed",
(unsigned long)datalen);
return;
}
*datalenp = i2d_RSAPrivateKey((RSA *)key, &p);
break;
}
#endif
break;
default:
log_error("key_serialize: unknown/unsupported key type %d",
type);
break;
}
}
/* Convert from serialized to printable */
char *
key_printable(int type, int private, u_int8_t *data, int datalen)
{
#ifdef USE_X509
char *s;
int i;
#endif
switch (type) {
case ISAKMP_KEY_PASSPHRASE:
return strdup((char *)data);
case ISAKMP_KEY_RSA:
#ifdef USE_X509
s = malloc(datalen * 2 + 1);
if (!s) {
log_error("key_printable: malloc (%d) failed",
datalen * 2 + 1);
return 0;
}
for (i = 0; i < datalen; i++)
snprintf(s + (2 * i), 2 * (datalen - i) + 1, "%02x",
data[i]);
return s;
#endif
default:
log_error("key_printable: unknown/unsupported key type %d",
type);
return 0;
}
}
/* Convert from serialized to internal. */
void *
key_internalize(int type, int private, u_int8_t *data, int datalen)
{
switch (type) {
case ISAKMP_KEY_PASSPHRASE:
return strdup((char *)data);
case ISAKMP_KEY_RSA:
#ifdef USE_X509
switch (private) {
#if OPENSSL_VERSION_NUMBER >= 0x00907000L
case ISAKMP_KEYTYPE_PUBLIC:
return d2i_RSAPublicKey(NULL,
(const u_int8_t **)&data, datalen);
case ISAKMP_KEYTYPE_PRIVATE:
return d2i_RSAPrivateKey(NULL,
(const u_int8_t **)&data, datalen);
#else
case ISAKMP_KEYTYPE_PUBLIC:
return d2i_RSAPublicKey(NULL, &data, datalen);
case ISAKMP_KEYTYPE_PRIVATE:
return d2i_RSAPrivateKey(NULL, &data, datalen);
#endif
default:
log_error("key_internalize: not public or private "
"RSA key passed");
return 0;
}
break;
#endif /* USE_X509 */
default:
log_error("key_internalize: unknown/unsupported key type %d",
type);
break;
}
return 0;
}
/* Convert from printable to serialized */
void
key_from_printable(int type, int private, char *key, u_int8_t **data,
u_int32_t *datalenp)
{
#ifdef USE_X509
u_int32_t datalen;
#endif
switch (type) {
case ISAKMP_KEY_PASSPHRASE:
*datalenp = strlen(key);
*data = (u_int8_t *) strdup(key);
break;
case ISAKMP_KEY_RSA:
#ifdef USE_X509
datalen = (strlen(key) + 1) / 2; /* Round up, just in case */
*data = malloc(datalen);
if (!*data) {
log_error("key_from_printable: malloc (%d) failed",
datalen);
*datalenp = 0;
return;
}
*datalenp = hex2raw(key, *data, datalen);
break;
#endif
default:
log_error("key_from_printable: "
"unknown/unsupported key type %d", type);
*data = NULL;
*datalenp = 0;
break;
}
}
|