| 1 |
|
| #ifndef CJWS_KEY_H |
| 2 |
|
| #define CJWS_KEY_H |
| 3 |
|
| |
| 4 |
|
| #include <string.h> |
| 5 |
|
| #include <limits.h> |
| 6 |
|
| #include <openssl/pem.h> |
| 7 |
|
| #include <openssl/bio.h> |
| 8 |
|
| #include <openssl/x509.h> |
| 9 |
|
| #include <openssl/err.h> |
| 10 |
|
| #include "cjws_compat.h" |
| 11 |
|
| #include "cjws_digest.h" |
| 12 |
|
| #include "cjws_b64.h" |
| 13 |
|
| |
| 14 |
|
| #define CJWS_KTY_RSA 1 |
| 15 |
|
| #define CJWS_KTY_EC 2 |
| 16 |
|
| #define CJWS_KTY_OCT 3 |
| 17 |
|
| |
| 18 |
|
| typedef struct { |
| 19 |
|
| EVP_PKEY *pkey; /* RSA/EC; NULL for oct */ |
| 20 |
|
| unsigned char *oct; /* oct (HS*) secret bytes; NULL otherwise */ |
| 21 |
|
| STRLEN octlen; |
| 22 |
|
| int kty; /* CJWS_KTY_* */ |
| 23 |
|
| int curve_bits; /* EC only: 256/384/521 */ |
| 24 |
|
| int is_private; |
| 25 |
|
| } cjws_key; |
| 26 |
|
| |
| 27 |
60 |
| static void cjws_key_free(cjws_key *k) { |
| 28 |
60 |
▶ | if (!k) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!k) return; |
0 |
60 |
60 |
|
| 29 |
60 |
| EVP_PKEY_free(k->pkey); |
| 30 |
60 |
▶ | if (k->oct) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (k->oct) { |
12 |
48 |
60 |
|
| 31 |
12 |
| OPENSSL_cleanse(k->oct, k->octlen); |
| 32 |
12 |
| OPENSSL_free(k->oct); |
| 33 |
|
| } |
| 34 |
60 |
| OPENSSL_free(k); |
| 35 |
|
| } |
| 36 |
|
| |
| 37 |
|
| /* Not OPENSSL_zalloc: LibreSSL (OpenBSD base) has no such symbol, and the |
| 38 |
|
| * implicit declaration only bites at dlopen time as an undefined symbol. */ |
| 39 |
60 |
| static cjws_key *cjws_key_alloc(void) { |
| 40 |
60 |
| cjws_key *k = (cjws_key *)OPENSSL_malloc(sizeof *k); |
| 41 |
60 |
▶ | if (k) memset(k, 0, sizeof *k); |
Branch: 1 branch
| Branch | True | False | Total |
| if (k) memset(k, 0, sizeof *k); |
60 |
0 |
60 |
|
| 42 |
60 |
| return k; |
| 43 |
|
| } |
| 44 |
|
| |
| 45 |
|
| /* Fill kty/curve_bits/is_private from a freshly adopted EVP_PKEY. |
| 46 |
|
| * Returns 0 (and does not adopt) for unsupported key types. */ |
| 47 |
48 |
| static int cjws_key_classify(cjws_key *k, EVP_PKEY *pkey, int is_private) { |
| 48 |
48 |
| int base = EVP_PKEY_base_id(pkey); |
| 49 |
48 |
▶ | if (base == EVP_PKEY_RSA) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (base == EVP_PKEY_RSA) { |
17 |
31 |
48 |
|
| 50 |
17 |
| k->kty = CJWS_KTY_RSA; |
| 51 |
|
| } |
| 52 |
31 |
▶ | else if (base == EVP_PKEY_EC) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (base == EVP_PKEY_EC) { |
31 |
0 |
31 |
|
| 53 |
31 |
| BIGNUM *x = NULL, *y = NULL; |
| 54 |
31 |
| int bits = 0; |
| 55 |
31 |
| k->kty = CJWS_KTY_EC; |
| 56 |
31 |
▶ | if (!cjws_pkey_get_ec(pkey, &bits, &x, &y)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!cjws_pkey_get_ec(pkey, &bits, &x, &y)) return 0; |
0 |
31 |
31 |
|
| 57 |
31 |
| BN_free(x); BN_free(y); |
| 58 |
31 |
| k->curve_bits = bits; |
| 59 |
|
| } |
| 60 |
|
| else { |
| 61 |
0 |
| return 0; |
| 62 |
|
| } |
| 63 |
48 |
| k->pkey = pkey; |
| 64 |
48 |
| k->is_private = is_private; |
| 65 |
48 |
| return 1; |
| 66 |
|
| } |
| 67 |
|
| |
| 68 |
|
| /* ---- PEM ------------------------------------------------------------- */ |
| 69 |
|
| |
| 70 |
12 |
| static cjws_key *cjws_key_from_pem(const char *pem, STRLEN len) { |
| 71 |
12 |
| BIO *bio = BIO_new_mem_buf(pem, (int)len); |
| 72 |
12 |
| EVP_PKEY *pkey = NULL; |
| 73 |
12 |
| cjws_key *k = NULL; |
| 74 |
12 |
| int is_private = 0; |
| 75 |
12 |
▶ | if (!bio) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!bio) return NULL; |
0 |
12 |
12 |
|
| 76 |
12 |
| pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL); |
| 77 |
12 |
▶ | if (pkey) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (pkey) { |
5 |
7 |
12 |
|
| 78 |
5 |
| is_private = 1; |
| 79 |
|
| } |
| 80 |
|
| else { |
| 81 |
7 |
| BIO_free(bio); |
| 82 |
7 |
| bio = BIO_new_mem_buf(pem, (int)len); |
| 83 |
7 |
▶ | if (!bio) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!bio) return NULL; |
0 |
7 |
7 |
|
| 84 |
7 |
| pkey = PEM_read_bio_PUBKEY(bio, NULL, NULL, NULL); |
| 85 |
|
| } |
| 86 |
12 |
| BIO_free(bio); |
| 87 |
12 |
▶ | if (!pkey) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!pkey) return NULL; |
3 |
9 |
12 |
|
| 88 |
9 |
| k = cjws_key_alloc(); |
| 89 |
9 |
▶ | if (!k || !cjws_key_classify(k, pkey, is_private)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!k || !cjws_key_classify(k, pkey, is_private)) { |
9 |
0 |
9 |
| if (!k || !cjws_key_classify(k, pkey, is_private)) { |
0 |
9 |
9 |
|
| 90 |
0 |
| EVP_PKEY_free(pkey); |
| 91 |
0 |
| OPENSSL_free(k); |
| 92 |
0 |
| return NULL; |
| 93 |
|
| } |
| 94 |
9 |
| return k; |
| 95 |
|
| } |
| 96 |
|
| |
| 97 |
|
| /* ---- X.509 ------------------------------------------------------------ */ |
| 98 |
|
| |
| 99 |
|
| /* The public key carried by a DER-encoded certificate. |
| 100 |
|
| * |
| 101 |
|
| * PEM_read_bio_PUBKEY above reads a PUBLIC KEY block and nothing else, so |
| 102 |
|
| * a certificate has to be parsed and its SubjectPublicKeyInfo taken out. |
| 103 |
|
| * XML-DSig's <ds:X509Certificate> is base64 of exactly these bytes. |
| 104 |
|
| * |
| 105 |
|
| * Nothing here validates the certificate: not the chain, not the validity |
| 106 |
|
| * dates, not its signature. A relying party pins the certificate it was |
| 107 |
|
| * handed out of band and asks only which key it carries, so a validity |
| 108 |
|
| * judgement made in this function would be a judgement made with none of |
| 109 |
|
| * the information needed to make it. The caller owns that decision. |
| 110 |
|
| * |
| 111 |
|
| * The error queue is cleared on failure because, unlike from_pem, this is |
| 112 |
|
| * called on input an attacker supplies and may be called on it often. */ |
| 113 |
810 |
| static cjws_key *cjws_key_from_x509_der(const unsigned char *der, STRLEN len) { |
| 114 |
810 |
| const unsigned char *p = der; |
| 115 |
|
| X509 *cert; |
| 116 |
|
| EVP_PKEY *pkey; |
| 117 |
|
| cjws_key *k; |
| 118 |
810 |
▶ | if (!der || !len || len > (STRLEN)LONG_MAX) return NULL; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!der || !len || len > (STRLEN)LONG_MAX) return NULL; |
810 |
0 |
810 |
| if (!der || !len || len > (STRLEN)LONG_MAX) return NULL; |
808 |
2 |
810 |
| if (!der || !len || len > (STRLEN)LONG_MAX) return NULL; |
0 |
808 |
808 |
|
| 119 |
808 |
| cert = d2i_X509(NULL, &p, (long)len); |
| 120 |
808 |
▶ | if (!cert) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!cert) { |
804 |
4 |
808 |
|
| 121 |
804 |
| ERR_clear_error(); |
| 122 |
804 |
| return NULL; |
| 123 |
|
| } |
| 124 |
|
| /* d2i_X509 stops at the end of the structure and does not care what |
| 125 |
|
| * follows, so a caller could hand in a certificate with any number of |
| 126 |
|
| * bytes appended and get a key back. Refuse it: the fingerprint of a |
| 127 |
|
| * certificate is a digest over these bytes, so trailing bytes give two |
| 128 |
|
| * inputs that agree on the key and disagree on the fingerprint, and a |
| 129 |
|
| * relying party that pins one and checks the other would be pinning |
| 130 |
|
| * something an attacker can vary. */ |
| 131 |
4 |
▶ | if ((STRLEN)(p - der) != len) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((STRLEN)(p - der) != len) { |
1 |
3 |
4 |
|
| 132 |
1 |
| X509_free(cert); |
| 133 |
1 |
| return NULL; |
| 134 |
|
| } |
| 135 |
3 |
| pkey = X509_get_pubkey(cert); /* +1 reference; ours to free */ |
| 136 |
3 |
| X509_free(cert); |
| 137 |
3 |
▶ | if (!pkey) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!pkey) { |
0 |
3 |
3 |
|
| 138 |
0 |
| ERR_clear_error(); |
| 139 |
0 |
| return NULL; |
| 140 |
|
| } |
| 141 |
|
| /* is_private is 0 and stays 0: a certificate carries a public key by |
| 142 |
|
| * definition, and there is no path here that could produce another. */ |
| 143 |
3 |
| k = cjws_key_alloc(); |
| 144 |
3 |
▶ | if (!k || !cjws_key_classify(k, pkey, 0)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!k || !cjws_key_classify(k, pkey, 0)) { |
3 |
0 |
3 |
| if (!k || !cjws_key_classify(k, pkey, 0)) { |
0 |
3 |
3 |
|
| 145 |
0 |
| EVP_PKEY_free(pkey); |
| 146 |
0 |
| OPENSSL_free(k); |
| 147 |
0 |
| ERR_clear_error(); |
| 148 |
0 |
| return NULL; |
| 149 |
|
| } |
| 150 |
3 |
| return k; |
| 151 |
|
| } |
| 152 |
|
| |
| 153 |
|
| /* Returned PEM is written into a fresh SV by the XS layer; here we hand |
| 154 |
|
| * back an OPENSSL_malloc'd buffer + length. Private keys export as |
| 155 |
|
| * unencrypted PKCS8, public as SPKI. */ |
| 156 |
12 |
| static unsigned char *cjws_key_to_pem(cjws_key *k, int want_private, |
| 157 |
|
| STRLEN *outlen) { |
| 158 |
|
| BIO *bio; |
| 159 |
12 |
| unsigned char *out = NULL; |
| 160 |
|
| char *pdata; |
| 161 |
|
| long plen; |
| 162 |
12 |
| *outlen = 0; |
| 163 |
12 |
▶ | if (!k->pkey) return NULL; /* oct keys have no PEM form */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (!k->pkey) return NULL; /* oct keys have no PEM form */ |
1 |
11 |
12 |
|
| 164 |
11 |
▶ | if (want_private && !k->is_private) return NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| if (want_private && !k->is_private) return NULL; |
4 |
7 |
11 |
| if (want_private && !k->is_private) return NULL; |
1 |
3 |
4 |
|
| 165 |
10 |
| bio = BIO_new(BIO_s_mem()); |
| 166 |
10 |
▶ | if (!bio) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!bio) return NULL; |
0 |
10 |
10 |
|
| 167 |
20 |
▶ | if (want_private |
Branch: 2 branches
| Branch | True | False | Total |
| if (want_private |
3 |
7 |
10 |
| if (want_private |
0 |
10 |
10 |
|
| 168 |
3 |
| ? PEM_write_bio_PKCS8PrivateKey(bio, k->pkey, NULL, NULL, 0, NULL, NULL) != 1 |
| 169 |
7 |
| : PEM_write_bio_PUBKEY(bio, k->pkey) != 1) { |
| 170 |
0 |
| BIO_free(bio); |
| 171 |
0 |
| return NULL; |
| 172 |
|
| } |
| 173 |
10 |
| plen = BIO_get_mem_data(bio, &pdata); |
| 174 |
10 |
▶ | if (plen > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (plen > 0) { |
10 |
0 |
10 |
|
| 175 |
10 |
| out = (unsigned char *)OPENSSL_malloc((size_t)plen); |
| 176 |
10 |
▶ | if (out) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (out) { |
10 |
0 |
10 |
|
| 177 |
10 |
| memcpy(out, pdata, (size_t)plen); |
| 178 |
10 |
| *outlen = (STRLEN)plen; |
| 179 |
|
| } |
| 180 |
|
| } |
| 181 |
10 |
| BIO_free(bio); |
| 182 |
10 |
| return out; |
| 183 |
|
| } |
| 184 |
|
| |
| 185 |
|
| /* ---- JWK params ------------------------------------------------------ */ |
| 186 |
|
| |
| 187 |
|
| /* b64url-decoded big-endian byte strings in, key out. d may be NULL. */ |
| 188 |
2 |
| static cjws_key *cjws_key_from_rsa_params(const unsigned char *n, STRLEN nl, |
| 189 |
|
| const unsigned char *e, STRLEN el, |
| 190 |
|
| const unsigned char *d, STRLEN dl) { |
| 191 |
2 |
| BIGNUM *bn = BN_bin2bn(n, (int)nl, NULL); |
| 192 |
2 |
| BIGNUM *be = BN_bin2bn(e, (int)el, NULL); |
| 193 |
2 |
▶ | BIGNUM *bd = d ? BN_bin2bn(d, (int)dl, NULL) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| BIGNUM *bd = d ? BN_bin2bn(d, (int)dl, NULL) : NULL; |
0 |
2 |
2 |
|
| 194 |
2 |
| EVP_PKEY *pkey = NULL; |
| 195 |
2 |
| cjws_key *k = NULL; |
| 196 |
2 |
▶ | if (bn && be && (!d || bd)) |
Branch: 4 branches
| Branch | True | False | Total |
| if (bn && be && (!d || bd)) |
2 |
0 |
2 |
| if (bn && be && (!d || bd)) |
2 |
0 |
2 |
| if (bn && be && (!d || bd)) |
0 |
2 |
2 |
| if (bn && be && (!d || bd)) |
0 |
0 |
0 |
|
| 197 |
2 |
| pkey = cjws_pkey_from_rsa(bn, be, bd); |
| 198 |
2 |
| BN_clear_free(bd); |
| 199 |
2 |
| BN_free(bn); |
| 200 |
2 |
| BN_free(be); |
| 201 |
2 |
▶ | if (!pkey) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!pkey) return NULL; |
0 |
2 |
2 |
|
| 202 |
2 |
| k = cjws_key_alloc(); |
| 203 |
2 |
▶ | if (!k || !cjws_key_classify(k, pkey, d ? 1 : 0)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!k || !cjws_key_classify(k, pkey, d ? 1 : 0)) { |
2 |
0 |
2 |
| if (!k || !cjws_key_classify(k, pkey, d ? 1 : 0)) { |
0 |
2 |
2 |
|
| 204 |
0 |
| EVP_PKEY_free(pkey); |
| 205 |
0 |
| OPENSSL_free(k); |
| 206 |
0 |
| return NULL; |
| 207 |
|
| } |
| 208 |
2 |
| return k; |
| 209 |
|
| } |
| 210 |
|
| |
| 211 |
6 |
| static cjws_key *cjws_key_from_ec_params(int bits, |
| 212 |
|
| const unsigned char *x, STRLEN xl, |
| 213 |
|
| const unsigned char *y, STRLEN yl, |
| 214 |
|
| const unsigned char *d, STRLEN dl) { |
| 215 |
6 |
| BIGNUM *bx = BN_bin2bn(x, (int)xl, NULL); |
| 216 |
6 |
| BIGNUM *by = BN_bin2bn(y, (int)yl, NULL); |
| 217 |
6 |
▶ | BIGNUM *bd = d ? BN_bin2bn(d, (int)dl, NULL) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| BIGNUM *bd = d ? BN_bin2bn(d, (int)dl, NULL) : NULL; |
1 |
5 |
6 |
|
| 218 |
6 |
| EVP_PKEY *pkey = NULL; |
| 219 |
6 |
| cjws_key *k = NULL; |
| 220 |
6 |
▶ | if (bx && by && (!d || bd)) |
Branch: 4 branches
| Branch | True | False | Total |
| if (bx && by && (!d || bd)) |
6 |
0 |
6 |
| if (bx && by && (!d || bd)) |
6 |
0 |
6 |
| if (bx && by && (!d || bd)) |
1 |
5 |
6 |
| if (bx && by && (!d || bd)) |
1 |
0 |
1 |
|
| 221 |
6 |
| pkey = cjws_pkey_from_ec(bits, bx, by, bd); |
| 222 |
6 |
| BN_clear_free(bd); |
| 223 |
6 |
| BN_free(bx); |
| 224 |
6 |
| BN_free(by); |
| 225 |
6 |
▶ | if (!pkey) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!pkey) return NULL; |
1 |
5 |
6 |
|
| 226 |
5 |
| k = cjws_key_alloc(); |
| 227 |
5 |
▶ | if (!k || !cjws_key_classify(k, pkey, d ? 1 : 0)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!k || !cjws_key_classify(k, pkey, d ? 1 : 0)) { |
5 |
0 |
5 |
| if (!k || !cjws_key_classify(k, pkey, d ? 1 : 0)) { |
0 |
5 |
5 |
|
| 228 |
0 |
| EVP_PKEY_free(pkey); |
| 229 |
0 |
| OPENSSL_free(k); |
| 230 |
0 |
| return NULL; |
| 231 |
|
| } |
| 232 |
5 |
| k->curve_bits = bits; |
| 233 |
5 |
| return k; |
| 234 |
|
| } |
| 235 |
|
| |
| 236 |
12 |
| static cjws_key *cjws_key_from_oct(const unsigned char *secret, STRLEN len) { |
| 237 |
12 |
| cjws_key *k = cjws_key_alloc(); |
| 238 |
12 |
▶ | if (!k) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!k) return NULL; |
0 |
12 |
12 |
|
| 239 |
12 |
▶ | k->oct = (unsigned char *)OPENSSL_malloc(len ? len : 1); |
Branch: 1 branch
| Branch | True | False | Total |
| k->oct = (unsigned char *)OPENSSL_malloc(len ? len : 1); |
12 |
0 |
12 |
|
| 240 |
12 |
▶ | if (!k->oct) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!k->oct) { |
0 |
12 |
12 |
|
| 241 |
0 |
| OPENSSL_free(k); |
| 242 |
0 |
| return NULL; |
| 243 |
|
| } |
| 244 |
12 |
| memcpy(k->oct, secret, len); |
| 245 |
12 |
| k->octlen = len; |
| 246 |
12 |
| k->kty = CJWS_KTY_OCT; |
| 247 |
12 |
| k->is_private = 1; |
| 248 |
12 |
| return k; |
| 249 |
|
| } |
| 250 |
|
| |
| 251 |
|
| /* ---- generation ------------------------------------------------------ */ |
| 252 |
|
| |
| 253 |
9 |
| static cjws_key *cjws_key_generate_rsa(int bits) { |
| 254 |
9 |
| EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL); |
| 255 |
9 |
| EVP_PKEY *pkey = NULL; |
| 256 |
9 |
| cjws_key *k = NULL; |
| 257 |
9 |
▶ | if (!ctx) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ctx) return NULL; |
0 |
9 |
9 |
|
| 258 |
9 |
▶ | if (EVP_PKEY_keygen_init(ctx) != 1 |
Branch: 1 branch
| Branch | True | False | Total |
| if (EVP_PKEY_keygen_init(ctx) != 1 |
9 |
0 |
9 |
|
| 259 |
9 |
▶ | || EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, bits) != 1 |
Branch: 1 branch
| Branch | True | False | Total |
| || EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, bits) != 1 |
9 |
0 |
9 |
|
| 260 |
9 |
▶ | || EVP_PKEY_keygen(ctx, &pkey) != 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| || EVP_PKEY_keygen(ctx, &pkey) != 1) { |
0 |
9 |
9 |
|
| 261 |
0 |
| EVP_PKEY_CTX_free(ctx); |
| 262 |
0 |
| return NULL; |
| 263 |
|
| } |
| 264 |
9 |
| EVP_PKEY_CTX_free(ctx); |
| 265 |
9 |
| k = cjws_key_alloc(); |
| 266 |
9 |
▶ | if (!k || !cjws_key_classify(k, pkey, 1)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!k || !cjws_key_classify(k, pkey, 1)) { |
9 |
0 |
9 |
| if (!k || !cjws_key_classify(k, pkey, 1)) { |
0 |
9 |
9 |
|
| 267 |
0 |
| EVP_PKEY_free(pkey); |
| 268 |
0 |
| OPENSSL_free(k); |
| 269 |
0 |
| return NULL; |
| 270 |
|
| } |
| 271 |
9 |
| return k; |
| 272 |
|
| } |
| 273 |
|
| |
| 274 |
20 |
| static cjws_key *cjws_key_generate_ec(int curve_bits) { |
| 275 |
20 |
| int nid = cjws_curve_nid(curve_bits); |
| 276 |
|
| EVP_PKEY_CTX *ctx; |
| 277 |
20 |
| EVP_PKEY *pkey = NULL; |
| 278 |
20 |
| cjws_key *k = NULL; |
| 279 |
20 |
▶ | if (nid == NID_undef) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (nid == NID_undef) return NULL; |
0 |
20 |
20 |
|
| 280 |
20 |
| ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL); |
| 281 |
20 |
▶ | if (!ctx) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ctx) return NULL; |
0 |
20 |
20 |
|
| 282 |
20 |
▶ | if (EVP_PKEY_keygen_init(ctx) != 1 |
Branch: 1 branch
| Branch | True | False | Total |
| if (EVP_PKEY_keygen_init(ctx) != 1 |
20 |
0 |
20 |
|
| 283 |
20 |
▶ | || EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, nid) != 1 |
Branch: 1 branch
| Branch | True | False | Total |
| || EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, nid) != 1 |
20 |
0 |
20 |
|
| 284 |
20 |
▶ | || EVP_PKEY_keygen(ctx, &pkey) != 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| || EVP_PKEY_keygen(ctx, &pkey) != 1) { |
0 |
20 |
20 |
|
| 285 |
0 |
| EVP_PKEY_CTX_free(ctx); |
| 286 |
0 |
| return NULL; |
| 287 |
|
| } |
| 288 |
20 |
| EVP_PKEY_CTX_free(ctx); |
| 289 |
20 |
| k = cjws_key_alloc(); |
| 290 |
20 |
▶ | if (!k || !cjws_key_classify(k, pkey, 1)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!k || !cjws_key_classify(k, pkey, 1)) { |
20 |
0 |
20 |
| if (!k || !cjws_key_classify(k, pkey, 1)) { |
0 |
20 |
20 |
|
| 291 |
0 |
| EVP_PKEY_free(pkey); |
| 292 |
0 |
| OPENSSL_free(k); |
| 293 |
0 |
| return NULL; |
| 294 |
|
| } |
| 295 |
20 |
| k->curve_bits = curve_bits; |
| 296 |
20 |
| return k; |
| 297 |
|
| } |
| 298 |
|
| |
| 299 |
|
| /* ---- RFC 7638 thumbprint --------------------------------------------- */ |
| 300 |
|
| |
| 301 |
|
| /* sha256 over the canonical JSON of the required public members, in |
| 302 |
|
| * lexicographic key order. Every value is b64url (JSON-safe, no |
| 303 |
|
| * escaping). out must hold 32 bytes; returns 1 on success. */ |
| 304 |
17 |
| static int cjws_key_thumbprint(cjws_key *k, unsigned char *out) { |
| 305 |
17 |
| unsigned char *json = NULL; |
| 306 |
17 |
| STRLEN jlen = 0; |
| 307 |
17 |
| int ok = 0; |
| 308 |
|
| |
| 309 |
17 |
▶ | if (k->kty == CJWS_KTY_RSA) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (k->kty == CJWS_KTY_RSA) { |
5 |
12 |
17 |
|
| 310 |
5 |
| BIGNUM *n = NULL, *e = NULL; |
| 311 |
5 |
▶ | if (!cjws_pkey_get_rsa(k->pkey, &n, &e)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!cjws_pkey_get_rsa(k->pkey, &n, &e)) return 0; |
0 |
5 |
5 |
|
| 312 |
|
| { |
| 313 |
5 |
| int nlen = BN_num_bytes(n), elen = BN_num_bytes(e); |
| 314 |
5 |
▶ | unsigned char *nb = (unsigned char *)OPENSSL_malloc((size_t)(nlen ? nlen : 1)); |
Branch: 1 branch
| Branch | True | False | Total |
| unsigned char *nb = (unsigned char *)OPENSSL_malloc((size_t)(nlen ? nlen : 1)); |
5 |
0 |
5 |
|
| 315 |
5 |
▶ | unsigned char *eb = (unsigned char *)OPENSSL_malloc((size_t)(elen ? elen : 1)); |
Branch: 1 branch
| Branch | True | False | Total |
| unsigned char *eb = (unsigned char *)OPENSSL_malloc((size_t)(elen ? elen : 1)); |
5 |
0 |
5 |
|
| 316 |
5 |
▶ | if (nb && eb) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (nb && eb) { |
5 |
0 |
5 |
| if (nb && eb) { |
5 |
0 |
5 |
|
| 317 |
|
| STRLEN maxj; |
| 318 |
5 |
| BN_bn2bin(n, nb); |
| 319 |
5 |
| BN_bn2bin(e, eb); |
| 320 |
5 |
| maxj = cjws_b64url_encoded_len((STRLEN)nlen) |
| 321 |
5 |
| + cjws_b64url_encoded_len((STRLEN)elen) + 64; |
| 322 |
5 |
| json = (unsigned char *)OPENSSL_malloc(maxj); |
| 323 |
5 |
▶ | if (json) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (json) { |
5 |
0 |
5 |
|
| 324 |
5 |
| STRLEN o = 0; |
| 325 |
5 |
| memcpy(json + o, "{\"e\":\"", 6); o += 6; |
| 326 |
5 |
| o += cjws_b64url_encode((char *)json + o, eb, (STRLEN)elen); |
| 327 |
5 |
| memcpy(json + o, "\",\"kty\":\"RSA\",\"n\":\"", 19); o += 19; |
| 328 |
5 |
| o += cjws_b64url_encode((char *)json + o, nb, (STRLEN)nlen); |
| 329 |
5 |
| memcpy(json + o, "\"}", 2); o += 2; |
| 330 |
5 |
| jlen = o; |
| 331 |
|
| } |
| 332 |
|
| } |
| 333 |
5 |
| OPENSSL_free(nb); |
| 334 |
5 |
| OPENSSL_free(eb); |
| 335 |
|
| } |
| 336 |
5 |
| BN_free(n); |
| 337 |
5 |
| BN_free(e); |
| 338 |
|
| } |
| 339 |
12 |
▶ | else if (k->kty == CJWS_KTY_EC) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (k->kty == CJWS_KTY_EC) { |
11 |
1 |
12 |
|
| 340 |
11 |
| BIGNUM *x = NULL, *y = NULL; |
| 341 |
11 |
| int bits = 0; |
| 342 |
11 |
▶ | if (!cjws_pkey_get_ec(k->pkey, &bits, &x, &y)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!cjws_pkey_get_ec(k->pkey, &bits, &x, &y)) return 0; |
0 |
11 |
11 |
|
| 343 |
|
| { |
| 344 |
11 |
| int clen = cjws_curve_coord_len(bits); |
| 345 |
11 |
| unsigned char *xb = (unsigned char *)OPENSSL_malloc((size_t)clen); |
| 346 |
11 |
| unsigned char *yb = (unsigned char *)OPENSSL_malloc((size_t)clen); |
| 347 |
11 |
▶ | const char *crv = bits == 256 ? "P-256" : bits == 384 ? "P-384" : "P-521"; |
Branch: 2 branches
| Branch | True | False | Total |
| const char *crv = bits == 256 ? "P-256" : bits == 384 ? "P-384" : "P-521"; |
4 |
7 |
11 |
| const char *crv = bits == 256 ? "P-256" : bits == 384 ? "P-384" : "P-521"; |
2 |
2 |
4 |
|
| 348 |
11 |
▶ | if (xb && yb |
Branch: 2 branches
| Branch | True | False | Total |
| if (xb && yb |
11 |
0 |
11 |
| if (xb && yb |
11 |
0 |
11 |
|
| 349 |
11 |
▶ | && BN_bn2binpad(x, xb, clen) == clen |
Branch: 1 branch
| Branch | True | False | Total |
| && BN_bn2binpad(x, xb, clen) == clen |
11 |
0 |
11 |
|
| 350 |
11 |
▶ | && BN_bn2binpad(y, yb, clen) == clen) { |
Branch: 1 branch
| Branch | True | False | Total |
| && BN_bn2binpad(y, yb, clen) == clen) { |
11 |
0 |
11 |
|
| 351 |
11 |
| STRLEN maxj = 2 * cjws_b64url_encoded_len((STRLEN)clen) + 64; |
| 352 |
11 |
| json = (unsigned char *)OPENSSL_malloc(maxj); |
| 353 |
11 |
▶ | if (json) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (json) { |
11 |
0 |
11 |
|
| 354 |
11 |
| STRLEN o = 0; |
| 355 |
11 |
| memcpy(json + o, "{\"crv\":\"", 8); o += 8; |
| 356 |
11 |
| memcpy(json + o, crv, 5); o += 5; |
| 357 |
11 |
| memcpy(json + o, "\",\"kty\":\"EC\",\"x\":\"", 18); o += 18; |
| 358 |
11 |
| o += cjws_b64url_encode((char *)json + o, xb, (STRLEN)clen); |
| 359 |
11 |
| memcpy(json + o, "\",\"y\":\"", 7); o += 7; |
| 360 |
11 |
| o += cjws_b64url_encode((char *)json + o, yb, (STRLEN)clen); |
| 361 |
11 |
| memcpy(json + o, "\"}", 2); o += 2; |
| 362 |
11 |
| jlen = o; |
| 363 |
|
| } |
| 364 |
|
| } |
| 365 |
11 |
| OPENSSL_free(xb); |
| 366 |
11 |
| OPENSSL_free(yb); |
| 367 |
|
| } |
| 368 |
11 |
| BN_free(x); |
| 369 |
11 |
| BN_free(y); |
| 370 |
|
| } |
| 371 |
1 |
▶ | else if (k->kty == CJWS_KTY_OCT) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (k->kty == CJWS_KTY_OCT) { |
1 |
0 |
1 |
|
| 372 |
1 |
| STRLEN maxj = cjws_b64url_encoded_len(k->octlen) + 32; |
| 373 |
1 |
| json = (unsigned char *)OPENSSL_malloc(maxj); |
| 374 |
1 |
▶ | if (json) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (json) { |
1 |
0 |
1 |
|
| 375 |
1 |
| STRLEN o = 0; |
| 376 |
1 |
| memcpy(json + o, "{\"k\":\"", 6); o += 6; |
| 377 |
1 |
| o += cjws_b64url_encode((char *)json + o, k->oct, k->octlen); |
| 378 |
1 |
| memcpy(json + o, "\",\"kty\":\"oct\"}", 14); o += 14; |
| 379 |
1 |
| jlen = o; |
| 380 |
|
| } |
| 381 |
|
| } |
| 382 |
|
| |
| 383 |
17 |
▶ | if (json && jlen) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (json && jlen) { |
17 |
0 |
17 |
| if (json && jlen) { |
17 |
0 |
17 |
|
| 384 |
17 |
| ok = cjws_digest(256, json, jlen, out) == 32; |
| 385 |
17 |
| OPENSSL_cleanse(json, jlen); |
| 386 |
|
| } |
| 387 |
17 |
| OPENSSL_free(json); |
| 388 |
17 |
| return ok; |
| 389 |
|
| } |
| 390 |
|
| |
| 391 |
|
| #endif |