Add glue code for PEM encrypted password files.
git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@17128 ec53bebd-3082-4978-b11e-865c3cabbd6b
This commit is contained in:
@@ -38,8 +38,68 @@ struct ks_file {
|
|||||||
hx509_certs certs;
|
hx509_certs certs;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct header {
|
||||||
|
char *header;
|
||||||
|
char *value;
|
||||||
|
struct header *next;
|
||||||
|
};
|
||||||
|
|
||||||
|
static int
|
||||||
|
add_headers(struct header **headers, const char *header, const char *value)
|
||||||
|
{
|
||||||
|
struct header *h;
|
||||||
|
h = calloc(1, sizeof(*h));
|
||||||
|
if (h == NULL)
|
||||||
|
return ENOMEM;
|
||||||
|
h->header = strdup(header);
|
||||||
|
if (h->header == NULL) {
|
||||||
|
free(h);
|
||||||
|
return ENOMEM;
|
||||||
|
}
|
||||||
|
h->value = strdup(value);
|
||||||
|
if (h->value == NULL) {
|
||||||
|
free(h->header);
|
||||||
|
free(h);
|
||||||
|
return ENOMEM;
|
||||||
|
}
|
||||||
|
|
||||||
|
h->next = *headers;
|
||||||
|
*headers = h;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
free_headers(struct header *headers)
|
||||||
|
{
|
||||||
|
struct header *h;
|
||||||
|
while (headers) {
|
||||||
|
h = headers;
|
||||||
|
headers = headers->next;
|
||||||
|
free(h->header);
|
||||||
|
free(h->value);
|
||||||
|
free(h);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static const char *
|
||||||
|
find_header(const struct header *headers, const char *header)
|
||||||
|
{
|
||||||
|
while(headers) {
|
||||||
|
if (strcmp(header, headers->header) == 0)
|
||||||
|
return headers->value;
|
||||||
|
headers = headers->next;
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
static int
|
static int
|
||||||
parse_certificate(hx509_context context, struct hx509_collector *c,
|
parse_certificate(hx509_context context, struct hx509_collector *c,
|
||||||
|
const struct header *headers,
|
||||||
const void *data, size_t len)
|
const void *data, size_t len)
|
||||||
{
|
{
|
||||||
hx509_cert cert;
|
hx509_cert cert;
|
||||||
@@ -63,13 +123,42 @@ parse_certificate(hx509_context context, struct hx509_collector *c,
|
|||||||
|
|
||||||
static int
|
static int
|
||||||
parse_rsa_private_key(hx509_context context, struct hx509_collector *c,
|
parse_rsa_private_key(hx509_context context, struct hx509_collector *c,
|
||||||
|
const struct header *headers,
|
||||||
const void *data, size_t len)
|
const void *data, size_t len)
|
||||||
{
|
{
|
||||||
heim_octet_string keydata;
|
heim_octet_string keydata;
|
||||||
int ret;
|
int ret;
|
||||||
|
const char *enc;
|
||||||
|
|
||||||
keydata.data = rk_UNCONST(data);
|
enc = find_header(headers, "Proc-Type");
|
||||||
keydata.length = len;
|
if (enc) {
|
||||||
|
const char *dek;
|
||||||
|
char *type, *iv;
|
||||||
|
|
||||||
|
if (strcmp(enc, "4,ENCRYPTED") != 0)
|
||||||
|
return EINVAL;
|
||||||
|
|
||||||
|
dek = find_header(headers, "DEK-Info");
|
||||||
|
if (dek == NULL)
|
||||||
|
return EINVAL;
|
||||||
|
|
||||||
|
type = strdup(dek);
|
||||||
|
if (type == NULL)
|
||||||
|
return ENOMEM;
|
||||||
|
|
||||||
|
iv = strchr(type, ',');
|
||||||
|
if (iv)
|
||||||
|
*iv++ = '\0';
|
||||||
|
|
||||||
|
/* printf("type: %s iv: %s\n", type, iv); */
|
||||||
|
/* XXX handle use EVP_BytesToKey() and decrypt */
|
||||||
|
|
||||||
|
free(type);
|
||||||
|
return EINVAL;
|
||||||
|
} else {
|
||||||
|
keydata.data = rk_UNCONST(data);
|
||||||
|
keydata.length = len;
|
||||||
|
}
|
||||||
|
|
||||||
ret = _hx509_collector_private_key_add(c,
|
ret = _hx509_collector_private_key_add(c,
|
||||||
hx509_signature_rsa(),
|
hx509_signature_rsa(),
|
||||||
@@ -82,7 +171,8 @@ parse_rsa_private_key(hx509_context context, struct hx509_collector *c,
|
|||||||
|
|
||||||
struct pem_formats {
|
struct pem_formats {
|
||||||
const char *name;
|
const char *name;
|
||||||
int (*func)(hx509_context, struct hx509_collector *, const void *, size_t);
|
int (*func)(hx509_context, struct hx509_collector *,
|
||||||
|
const struct header *, const void *, size_t);
|
||||||
} formats[] = {
|
} formats[] = {
|
||||||
{ "CERTIFICATE", parse_certificate },
|
{ "CERTIFICATE", parse_certificate },
|
||||||
{ "RSA PRIVATE KEY", parse_rsa_private_key }
|
{ "RSA PRIVATE KEY", parse_rsa_private_key }
|
||||||
@@ -95,6 +185,7 @@ parse_pem_file(hx509_context context,
|
|||||||
struct hx509_collector *c,
|
struct hx509_collector *c,
|
||||||
int *found_data)
|
int *found_data)
|
||||||
{
|
{
|
||||||
|
struct header *headers = NULL;
|
||||||
char *type = NULL;
|
char *type = NULL;
|
||||||
void *data = NULL;
|
void *data = NULL;
|
||||||
size_t len = 0;
|
size_t len = 0;
|
||||||
@@ -102,6 +193,7 @@ parse_pem_file(hx509_context context,
|
|||||||
int i, ret;
|
int i, ret;
|
||||||
FILE *f;
|
FILE *f;
|
||||||
|
|
||||||
|
|
||||||
enum { BEFORE, SEARCHHEADER, INHEADER, INDATA, DONE } where;
|
enum { BEFORE, SEARCHHEADER, INHEADER, INDATA, DONE } where;
|
||||||
|
|
||||||
where = BEFORE;
|
where = BEFORE;
|
||||||
@@ -151,8 +243,10 @@ parse_pem_file(hx509_context context,
|
|||||||
}
|
}
|
||||||
p = strchr(buf, ':');
|
p = strchr(buf, ':');
|
||||||
if (p) {
|
if (p) {
|
||||||
printf("Add header: %.*s <- %s\n",
|
*p++ = '\0';
|
||||||
(int)(p - buf) - 1, buf, p + 1);
|
while (isspace((int)*p))
|
||||||
|
p++;
|
||||||
|
add_headers(&headers, buf, p);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case INDATA:
|
case INDATA:
|
||||||
@@ -181,26 +275,30 @@ parse_pem_file(hx509_context context,
|
|||||||
for (i = 0; i < sizeof(formats)/sizeof(formats[0]); i++) {
|
for (i = 0; i < sizeof(formats)/sizeof(formats[0]); i++) {
|
||||||
const char *p = formats[i].name;
|
const char *p = formats[i].name;
|
||||||
if (strncmp(type, p, strlen(p)) == 0)
|
if (strncmp(type, p, strlen(p)) == 0)
|
||||||
ret = (*formats[i].func)(context, c, data, len);
|
ret = (*formats[i].func)(context, c, headers, data, len);
|
||||||
}
|
}
|
||||||
free(data);
|
free(data);
|
||||||
data = NULL;
|
data = NULL;
|
||||||
free(type);
|
free(type);
|
||||||
type = NULL;
|
type = NULL;
|
||||||
where = BEFORE;
|
where = BEFORE;
|
||||||
|
free_headers(headers);
|
||||||
|
headers = NULL;
|
||||||
if (ret)
|
if (ret)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fclose(f);
|
||||||
|
|
||||||
if (where != BEFORE)
|
if (where != BEFORE)
|
||||||
ret = EINVAL;
|
ret = EINVAL;
|
||||||
if (data)
|
if (data)
|
||||||
free(data);
|
free(data);
|
||||||
if (type)
|
if (type)
|
||||||
free(type);
|
free(type);
|
||||||
|
if (headers)
|
||||||
fclose(f);
|
free_headers(headers);
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
@@ -260,7 +358,7 @@ file_init(hx509_context context,
|
|||||||
if (ret)
|
if (ret)
|
||||||
goto out;
|
goto out;
|
||||||
|
|
||||||
ret = parse_certificate(context, c, data, length);
|
ret = parse_certificate(context, c, NULL, data, length);
|
||||||
_hx509_unmap_file(data, length);
|
_hx509_unmap_file(data, length);
|
||||||
if (ret)
|
if (ret)
|
||||||
goto out;
|
goto out;
|
||||||
@@ -270,8 +368,9 @@ file_init(hx509_context context,
|
|||||||
ret = _hx509_collector_collect(context, c, &f->certs);
|
ret = _hx509_collector_collect(context, c, &f->certs);
|
||||||
if (ret == 0)
|
if (ret == 0)
|
||||||
*data = f;
|
*data = f;
|
||||||
|
else
|
||||||
|
free(f);
|
||||||
out:
|
out:
|
||||||
ret = 0;
|
|
||||||
free(files);
|
free(files);
|
||||||
|
|
||||||
_hx509_collector_free(c);
|
_hx509_collector_free(c);
|
||||||
|
Reference in New Issue
Block a user