1 /* $OpenBSD: tls_keypair.c,v 1.6 2018/04/07 16:35:34 jsing Exp $ */
3 * Copyright (c) 2014 Joel Sing <jsing@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <openssl/bio.h>
19 #include <openssl/err.h>
20 #include <openssl/pem.h>
24 #include "tls_internal.h"
29 return calloc(1, sizeof(struct tls_keypair
));
33 tls_keypair_pubkey_hash(struct tls_keypair
*keypair
, struct tls_error
*error
)
38 free(keypair
->pubkey_hash
);
39 keypair
->pubkey_hash
= NULL
;
41 if (keypair
->cert_mem
== NULL
) {
46 if (tls_keypair_load_cert(keypair
, error
, &cert
) == -1)
48 if (tls_cert_pubkey_hash(cert
, &keypair
->pubkey_hash
) == -1)
60 tls_keypair_clear_key(struct tls_keypair
*keypair
)
62 freezero(keypair
->key_mem
, keypair
->key_len
);
63 keypair
->key_mem
= NULL
;
68 tls_keypair_set_cert_file(struct tls_keypair
*keypair
, struct tls_error
*error
,
69 const char *cert_file
)
71 if (tls_config_load_file(error
, "certificate", cert_file
,
72 &keypair
->cert_mem
, &keypair
->cert_len
) == -1)
74 return tls_keypair_pubkey_hash(keypair
, error
);
78 tls_keypair_set_cert_mem(struct tls_keypair
*keypair
, struct tls_error
*error
,
79 const uint8_t *cert
, size_t len
)
81 if (tls_set_mem(&keypair
->cert_mem
, &keypair
->cert_len
, cert
, len
) == -1)
83 return tls_keypair_pubkey_hash(keypair
, error
);
87 tls_keypair_set_key_file(struct tls_keypair
*keypair
, struct tls_error
*error
,
90 tls_keypair_clear_key(keypair
);
91 return tls_config_load_file(error
, "key", key_file
,
92 &keypair
->key_mem
, &keypair
->key_len
);
96 tls_keypair_set_key_mem(struct tls_keypair
*keypair
, struct tls_error
*error
,
97 const uint8_t *key
, size_t len
)
99 tls_keypair_clear_key(keypair
);
100 return tls_set_mem(&keypair
->key_mem
, &keypair
->key_len
, key
, len
);
104 tls_keypair_set_ocsp_staple_file(struct tls_keypair
*keypair
,
105 struct tls_error
*error
, const char *ocsp_file
)
107 return tls_config_load_file(error
, "ocsp", ocsp_file
,
108 &keypair
->ocsp_staple
, &keypair
->ocsp_staple_len
);
112 tls_keypair_set_ocsp_staple_mem(struct tls_keypair
*keypair
,
113 struct tls_error
*error
, const uint8_t *staple
, size_t len
)
115 return tls_set_mem(&keypair
->ocsp_staple
, &keypair
->ocsp_staple_len
,
120 tls_keypair_free(struct tls_keypair
*keypair
)
125 tls_keypair_clear_key(keypair
);
127 free(keypair
->cert_mem
);
128 free(keypair
->ocsp_staple
);
129 free(keypair
->pubkey_hash
);
135 tls_keypair_load_cert(struct tls_keypair
*keypair
, struct tls_error
*error
,
138 char *errstr
= "unknown";
139 BIO
*cert_bio
= NULL
;
146 if (keypair
->cert_mem
== NULL
) {
147 tls_error_set(error
, "keypair has no certificate");
150 if ((cert_bio
= BIO_new_mem_buf(keypair
->cert_mem
,
151 keypair
->cert_len
)) == NULL
) {
152 tls_error_set(error
, "failed to create certificate bio");
155 if ((*cert
= PEM_read_bio_X509(cert_bio
, NULL
, tls_password_cb
,
157 if ((ssl_err
= ERR_peek_error()) != 0)
158 errstr
= ERR_error_string(ssl_err
, NULL
);
159 tls_error_set(error
, "failed to load certificate: %s", errstr
);