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 | 132 | { |
45 | 132 | if (cbor_isa_bytestring(item) == false || |
46 | 132 | cbor_bytestring_is_definite(item) == false || |
47 | 132 | cbor_bytestring_length(item) != len) { |
48 | 0 | fido_log_debug("%s: cbor type", __func__); |
49 | 0 | return (-1); |
50 | 0 | } |
51 | 132 | |
52 | 132 | memcpy(ptr, cbor_bytestring_handle(item), len); |
53 | 132 | |
54 | 132 | return (0); |
55 | 132 | } |
56 | | |
57 | | static int |
58 | | decode_rsa_pubkey(const cbor_item_t *key, const cbor_item_t *val, void *arg) |
59 | 266 | { |
60 | 266 | rs256_pk_t *k = arg; |
61 | 266 | |
62 | 266 | if (cbor_isa_negint(key) == false || |
63 | 266 | cbor_int_get_width(key) != CBOR_INT_8) |
64 | 134 | return (0); /* ignore */ |
65 | 132 | |
66 | 132 | switch (cbor_get_uint8(key)) { |
67 | 66 | case 0: /* modulus */ |
68 | 66 | return (decode_bignum(val, &k->n, sizeof(k->n))); |
69 | 66 | case 1: /* public exponent */ |
70 | 66 | 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 | 72 | { |
79 | 72 | if (cbor_isa_map(item) == false || |
80 | 72 | cbor_map_is_definite(item) == false || |
81 | 72 | cbor_map_iter(item, k, decode_rsa_pubkey) < 0) { |
82 | 5 | fido_log_debug("%s: cbor type", __func__); |
83 | 5 | return (-1); |
84 | 5 | } |
85 | 67 | |
86 | 67 | return (0); |
87 | 67 | } |
88 | | |
89 | | rs256_pk_t * |
90 | | rs256_pk_new(void) |
91 | 863 | { |
92 | 863 | return (calloc(1, sizeof(rs256_pk_t))); |
93 | 863 | } |
94 | | |
95 | | void |
96 | | rs256_pk_free(rs256_pk_t **pkp) |
97 | 1.95k | { |
98 | 1.95k | rs256_pk_t *pk; |
99 | 1.95k | |
100 | 1.95k | if (pkp == NULL || (pk = *pkp) == NULL) |
101 | 1.95k | return; |
102 | 860 | |
103 | 860 | explicit_bzero(pk, sizeof(*pk)); |
104 | 860 | free(pk); |
105 | 860 | |
106 | 860 | *pkp = NULL; |
107 | 860 | } |
108 | | |
109 | | int |
110 | | rs256_pk_from_ptr(rs256_pk_t *pk, const void *ptr, size_t len) |
111 | 451 | { |
112 | 451 | if (len < sizeof(*pk)) |
113 | 414 | return (FIDO_ERR_INVALID_ARGUMENT); |
114 | 37 | |
115 | 37 | memcpy(pk, ptr, sizeof(*pk)); |
116 | 37 | |
117 | 37 | return (FIDO_OK); |
118 | 37 | } |
119 | | |
120 | | EVP_PKEY * |
121 | | rs256_pk_to_EVP_PKEY(const rs256_pk_t *k) |
122 | 551 | { |
123 | 551 | RSA *rsa = NULL; |
124 | 551 | EVP_PKEY *pkey = NULL; |
125 | 551 | BIGNUM *n = NULL; |
126 | 551 | BIGNUM *e = NULL; |
127 | 551 | int ok = -1; |
128 | 551 | |
129 | 551 | if ((n = BN_new()) == NULL || (e = BN_new()) == NULL) |
130 | 551 | goto fail; |
131 | 527 | |
132 | 527 | if (BN_bin2bn(k->n, sizeof(k->n), n) == NULL || |
133 | 527 | BN_bin2bn(k->e, sizeof(k->e), e) == NULL) { |
134 | 18 | fido_log_debug("%s: BN_bin2bn", __func__); |
135 | 18 | goto fail; |
136 | 18 | } |
137 | 509 | |
138 | 509 | if ((rsa = RSA_new()) == NULL || RSA_set0_key(rsa, n, e, NULL) == 0) { |
139 | 11 | fido_log_debug("%s: RSA_set0_key", __func__); |
140 | 11 | goto fail; |
141 | 11 | } |
142 | 498 | |
143 | 498 | /* at this point, n and e belong to rsa */ |
144 | 498 | n = NULL; |
145 | 498 | e = NULL; |
146 | 498 | |
147 | 498 | if ((pkey = EVP_PKEY_new()) == NULL || |
148 | 498 | EVP_PKEY_assign_RSA(pkey, rsa) == 0) { |
149 | 18 | fido_log_debug("%s: EVP_PKEY_assign_RSA", __func__); |
150 | 18 | goto fail; |
151 | 18 | } |
152 | 480 | |
153 | 480 | rsa = NULL; /* at this point, rsa belongs to evp */ |
154 | 480 | |
155 | 480 | ok = 0; |
156 | 551 | fail: |
157 | 551 | if (n != NULL) |
158 | 551 | BN_free(n); |
159 | 551 | if (e != NULL) |
160 | 551 | BN_free(e); |
161 | 551 | if (rsa != NULL) |
162 | 551 | RSA_free(rsa); |
163 | 551 | if (ok < 0 && pkey != NULL) { |
164 | 8 | EVP_PKEY_free(pkey); |
165 | 8 | pkey = NULL; |
166 | 8 | } |
167 | 551 | |
168 | 551 | return (pkey); |
169 | 480 | } |
170 | | |
171 | | int |
172 | | rs256_pk_from_RSA(rs256_pk_t *pk, const RSA *rsa) |
173 | 409 | { |
174 | 409 | const BIGNUM *n = NULL; |
175 | 409 | const BIGNUM *e = NULL; |
176 | 409 | const BIGNUM *d = NULL; |
177 | 409 | int k; |
178 | 409 | |
179 | 409 | if (RSA_bits(rsa) != 2048) { |
180 | 388 | fido_log_debug("%s: invalid key length", __func__); |
181 | 388 | return (FIDO_ERR_INVALID_ARGUMENT); |
182 | 388 | } |
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 | } |