Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2018 Yubico AB. All rights reserved. |
3 | | * Use of this source code is governed by a BSD-style |
4 | | * license that can be found in the LICENSE file. |
5 | | */ |
6 | | |
7 | | #include <openssl/bn.h> |
8 | | #include <openssl/rsa.h> |
9 | | #include <openssl/evp.h> |
10 | | #include <openssl/obj_mac.h> |
11 | | |
12 | | #include <string.h> |
13 | | #include "fido.h" |
14 | | #include "fido/rs256.h" |
15 | | |
16 | | #if OPENSSL_VERSION_NUMBER < 0x10100000L |
17 | | static int |
18 | | RSA_bits(const RSA *r) |
19 | | { |
20 | | return (BN_num_bits(r->n)); |
21 | | } |
22 | | |
23 | | static int |
24 | | RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d) |
25 | | { |
26 | | r->n = n; |
27 | | r->e = e; |
28 | | r->d = d; |
29 | | |
30 | | return (1); |
31 | | } |
32 | | |
33 | | static void |
34 | | RSA_get0_key(const RSA *r, const BIGNUM **n, const BIGNUM **e, const BIGNUM **d) |
35 | | { |
36 | | *n = r->n; |
37 | | *e = r->e; |
38 | | *d = r->d; |
39 | | } |
40 | | #endif /* OPENSSL_VERSION_NUMBER < 0x10100000L */ |
41 | | |
42 | | static int |
43 | | decode_bignum(const cbor_item_t *item, void *ptr, size_t len) |
44 | 120 | { |
45 | 120 | if (cbor_isa_bytestring(item) == false || |
46 | 120 | cbor_bytestring_is_definite(item) == false || |
47 | 120 | cbor_bytestring_length(item) != len) { |
48 | 0 | fido_log_debug("%s: cbor type", __func__); |
49 | 0 | return (-1); |
50 | 0 | } |
51 | 120 | |
52 | 120 | memcpy(ptr, cbor_bytestring_handle(item), len); |
53 | 120 | |
54 | 120 | return (0); |
55 | 120 | } |
56 | | |
57 | | static int |
58 | | decode_rsa_pubkey(const cbor_item_t *key, const cbor_item_t *val, void *arg) |
59 | 242 | { |
60 | 242 | rs256_pk_t *k = arg; |
61 | 242 | |
62 | 242 | if (cbor_isa_negint(key) == false || |
63 | 242 | cbor_int_get_width(key) != CBOR_INT_8) |
64 | 122 | return (0); /* ignore */ |
65 | 120 | |
66 | 120 | switch (cbor_get_uint8(key)) { |
67 | 60 | case 0: /* modulus */ |
68 | 60 | return (decode_bignum(val, &k->n, sizeof(k->n))); |
69 | 60 | case 1: /* public exponent */ |
70 | 60 | return (decode_bignum(val, &k->e, sizeof(k->e))); |
71 | 0 | } |
72 | 0 | |
73 | 0 | return (0); /* ignore */ |
74 | 0 | } |
75 | | |
76 | | int |
77 | | rs256_pk_decode(const cbor_item_t *item, rs256_pk_t *k) |
78 | 65 | { |
79 | 65 | if (cbor_isa_map(item) == false || |
80 | 65 | cbor_map_is_definite(item) == false || |
81 | 65 | cbor_map_iter(item, k, decode_rsa_pubkey) < 0) { |
82 | 4 | fido_log_debug("%s: cbor type", __func__); |
83 | 4 | return (-1); |
84 | 4 | } |
85 | 61 | |
86 | 61 | return (0); |
87 | 61 | } |
88 | | |
89 | | rs256_pk_t * |
90 | | rs256_pk_new(void) |
91 | 820 | { |
92 | 820 | return (calloc(1, sizeof(rs256_pk_t))); |
93 | 820 | } |
94 | | |
95 | | void |
96 | | rs256_pk_free(rs256_pk_t **pkp) |
97 | 1.86k | { |
98 | 1.86k | rs256_pk_t *pk; |
99 | 1.86k | |
100 | 1.86k | if (pkp == NULL || (pk = *pkp) == NULL) |
101 | 1.86k | return; |
102 | 817 | |
103 | 817 | explicit_bzero(pk, sizeof(*pk)); |
104 | 817 | free(pk); |
105 | 817 | |
106 | 817 | *pkp = NULL; |
107 | 817 | } |
108 | | |
109 | | int |
110 | | rs256_pk_from_ptr(rs256_pk_t *pk, const void *ptr, size_t len) |
111 | 423 | { |
112 | 423 | if (len < sizeof(*pk)) |
113 | 385 | return (FIDO_ERR_INVALID_ARGUMENT); |
114 | 38 | |
115 | 38 | memcpy(pk, ptr, sizeof(*pk)); |
116 | 38 | |
117 | 38 | return (FIDO_OK); |
118 | 38 | } |
119 | | |
120 | | EVP_PKEY * |
121 | | rs256_pk_to_EVP_PKEY(const rs256_pk_t *k) |
122 | 516 | { |
123 | 516 | RSA *rsa = NULL; |
124 | 516 | EVP_PKEY *pkey = NULL; |
125 | 516 | BIGNUM *n = NULL; |
126 | 516 | BIGNUM *e = NULL; |
127 | 516 | int ok = -1; |
128 | 516 | |
129 | 516 | if ((n = BN_new()) == NULL || (e = BN_new()) == NULL) |
130 | 516 | goto fail; |
131 | 501 | |
132 | 501 | if (BN_bin2bn(k->n, sizeof(k->n), n) == NULL || |
133 | 501 | BN_bin2bn(k->e, sizeof(k->e), e) == NULL) { |
134 | 12 | fido_log_debug("%s: BN_bin2bn", __func__); |
135 | 12 | goto fail; |
136 | 12 | } |
137 | 489 | |
138 | 489 | if ((rsa = RSA_new()) == NULL || RSA_set0_key(rsa, n, e, NULL) == 0) { |
139 | 9 | fido_log_debug("%s: RSA_set0_key", __func__); |
140 | 9 | goto fail; |
141 | 9 | } |
142 | 480 | |
143 | 480 | /* at this point, n and e belong to rsa */ |
144 | 480 | n = NULL; |
145 | 480 | e = NULL; |
146 | 480 | |
147 | 480 | if ((pkey = EVP_PKEY_new()) == NULL || |
148 | 480 | EVP_PKEY_assign_RSA(pkey, rsa) == 0) { |
149 | 14 | fido_log_debug("%s: EVP_PKEY_assign_RSA", __func__); |
150 | 14 | goto fail; |
151 | 14 | } |
152 | 466 | |
153 | 466 | rsa = NULL; /* at this point, rsa belongs to evp */ |
154 | 466 | |
155 | 466 | ok = 0; |
156 | 516 | fail: |
157 | 516 | if (n != NULL) |
158 | 516 | BN_free(n); |
159 | 516 | if (e != NULL) |
160 | 516 | BN_free(e); |
161 | 516 | if (rsa != NULL) |
162 | 516 | RSA_free(rsa); |
163 | 516 | if (ok < 0 && pkey != NULL) { |
164 | 6 | EVP_PKEY_free(pkey); |
165 | 6 | pkey = NULL; |
166 | 6 | } |
167 | 516 | |
168 | 516 | return (pkey); |
169 | 466 | } |
170 | | |
171 | | int |
172 | | rs256_pk_from_RSA(rs256_pk_t *pk, const RSA *rsa) |
173 | 394 | { |
174 | 394 | const BIGNUM *n = NULL; |
175 | 394 | const BIGNUM *e = NULL; |
176 | 394 | const BIGNUM *d = NULL; |
177 | 394 | int k; |
178 | 394 | |
179 | 394 | if (RSA_bits(rsa) != 2048) { |
180 | 373 | fido_log_debug("%s: invalid key length", __func__); |
181 | 373 | return (FIDO_ERR_INVALID_ARGUMENT); |
182 | 373 | } |
183 | 21 | |
184 | 21 | RSA_get0_key(rsa, &n, &e, &d); |
185 | 21 | |
186 | 21 | if (n == NULL || e == NULL) { |
187 | 0 | fido_log_debug("%s: RSA_get0_key", __func__); |
188 | 0 | return (FIDO_ERR_INTERNAL); |
189 | 0 | } |
190 | 21 | |
191 | 21 | if ((k = BN_num_bytes(n)) < 0 || (size_t)k > sizeof(pk->n) || |
192 | 21 | (k = BN_num_bytes(e)) < 0 || (size_t)k > sizeof(pk->e)) { |
193 | 0 | fido_log_debug("%s: invalid key", __func__); |
194 | 0 | return (FIDO_ERR_INTERNAL); |
195 | 0 | } |
196 | 21 | |
197 | 21 | if ((k = BN_bn2bin(n, pk->n)) < 0 || (size_t)k > sizeof(pk->n) || |
198 | 21 | (k = BN_bn2bin(e, pk->e)) < 0 || (size_t)k > sizeof(pk->e)) { |
199 | 3 | fido_log_debug("%s: BN_bn2bin", __func__); |
200 | 3 | return (FIDO_ERR_INTERNAL); |
201 | 3 | } |
202 | 18 | |
203 | 18 | return (FIDO_OK); |
204 | 18 | } |