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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
|
/*
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <errno.h>
#include "brssl.h"
#include "bearssl.h"
static private_key *
decode_key(const unsigned char *buf, size_t len)
{
br_skey_decoder_context dc;
int err;
private_key *sk;
br_skey_decoder_init(&dc);
br_skey_decoder_push(&dc, buf, len);
err = br_skey_decoder_last_error(&dc);
if (err != 0) {
const char *errname, *errmsg;
fprintf(stderr, "ERROR (decoding): err=%d\n", err);
errname = find_error_name(err, &errmsg);
if (errname != NULL) {
fprintf(stderr, " %s: %s\n", errname, errmsg);
} else {
fprintf(stderr, " (unknown)\n");
}
return NULL;
}
switch (br_skey_decoder_key_type(&dc)) {
const br_rsa_private_key *rk;
const br_ec_private_key *ek;
case BR_KEYTYPE_RSA:
rk = br_skey_decoder_get_rsa(&dc);
sk = xmalloc(sizeof *sk);
sk->key_type = BR_KEYTYPE_RSA;
sk->key.rsa.n_bitlen = rk->n_bitlen;
sk->key.rsa.p = xblobdup(rk->p, rk->plen);
sk->key.rsa.plen = rk->plen;
sk->key.rsa.q = xblobdup(rk->q, rk->qlen);
sk->key.rsa.qlen = rk->qlen;
sk->key.rsa.dp = xblobdup(rk->dp, rk->dplen);
sk->key.rsa.dplen = rk->dplen;
sk->key.rsa.dq = xblobdup(rk->dq, rk->dqlen);
sk->key.rsa.dqlen = rk->dqlen;
sk->key.rsa.iq = xblobdup(rk->iq, rk->iqlen);
sk->key.rsa.iqlen = rk->iqlen;
break;
case BR_KEYTYPE_EC:
ek = br_skey_decoder_get_ec(&dc);
sk = xmalloc(sizeof *sk);
sk->key_type = BR_KEYTYPE_EC;
sk->key.ec.curve = ek->curve;
sk->key.ec.x = xblobdup(ek->x, ek->xlen);
sk->key.ec.xlen = ek->xlen;
break;
default:
fprintf(stderr, "Unknown key type: %d\n",
br_skey_decoder_key_type(&dc));
sk = NULL;
break;
}
return sk;
}
/* see brssl.h */
private_key *
read_private_key(const char *fname)
{
unsigned char *buf;
size_t len;
private_key *sk;
pem_object *pos;
size_t num, u;
buf = NULL;
pos = NULL;
sk = NULL;
buf = read_file(fname, &len);
if (buf == NULL) {
goto deckey_exit;
}
if (looks_like_DER(buf, len)) {
sk = decode_key(buf, len);
goto deckey_exit;
} else {
pos = decode_pem(buf, len, &num);
if (pos == NULL) {
goto deckey_exit;
}
for (u = 0; pos[u].name; u ++) {
const char *name;
name = pos[u].name;
if (eqstr(name, "RSA PRIVATE KEY")
|| eqstr(name, "EC PRIVATE KEY")
|| eqstr(name, "PRIVATE KEY"))
{
sk = decode_key(pos[u].data, pos[u].data_len);
goto deckey_exit;
}
}
fprintf(stderr, "ERROR: no private key in file '%s'\n", fname);
goto deckey_exit;
}
deckey_exit:
if (buf != NULL) {
xfree(buf);
}
if (pos != NULL) {
for (u = 0; pos[u].name; u ++) {
free_pem_object_contents(&pos[u]);
}
xfree(pos);
}
return sk;
}
/* see brssl.h */
void
free_private_key(private_key *sk)
{
if (sk == NULL) {
return;
}
switch (sk->key_type) {
case BR_KEYTYPE_RSA:
xfree(sk->key.rsa.p);
xfree(sk->key.rsa.q);
xfree(sk->key.rsa.dp);
xfree(sk->key.rsa.dq);
xfree(sk->key.rsa.iq);
break;
case BR_KEYTYPE_EC:
xfree(sk->key.ec.x);
break;
}
xfree(sk);
}
/*
* OID for hash functions in RSA signatures.
*/
static const unsigned char HASH_OID_SHA1[] = {
0x05, 0x2B, 0x0E, 0x03, 0x02, 0x1A
};
static const unsigned char HASH_OID_SHA224[] = {
0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x04
};
static const unsigned char HASH_OID_SHA256[] = {
0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01
};
static const unsigned char HASH_OID_SHA384[] = {
0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02
};
static const unsigned char HASH_OID_SHA512[] = {
0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03
};
static const unsigned char *HASH_OID[] = {
HASH_OID_SHA1,
HASH_OID_SHA224,
HASH_OID_SHA256,
HASH_OID_SHA384,
HASH_OID_SHA512
};
/* see brssl.h */
const unsigned char *
get_hash_oid(int id)
{
if (id >= 2 && id <= 6) {
return HASH_OID[id - 2];
} else {
return NULL;
}
}
/* see brssl.h */
const br_hash_class *
get_hash_impl(int hash_id)
{
size_t u;
if (hash_id == 0) {
return &br_md5sha1_vtable;
}
for (u = 0; hash_functions[u].name; u ++) {
const br_hash_class *hc;
int id;
hc = hash_functions[u].hclass;
id = (hc->desc >> BR_HASHDESC_ID_OFF) & BR_HASHDESC_ID_MASK;
if (id == hash_id) {
return hc;
}
}
return NULL;
}
|