| 1 |
|
| #ifndef CJWS_COMPAT_H |
| 2 |
|
| #define CJWS_COMPAT_H |
| 3 |
|
| |
| 4 |
|
| /* The one file that knows OpenSSL 1.1 and 3.x import/export keys |
| 5 |
|
| * differently. Everything here is selected by the Makefile.PL FEATURE |
| 6 |
|
| * probes (CJWS_HAVE_FROMDATA / CJWS_HAVE_SET0), never by version |
| 7 |
|
| * macros. Four operations diverge: |
| 8 |
|
| * - RSA key from n/e(/d) BIGNUMs |
| 9 |
|
| * - EC key from curve + x/y(/d) |
| 10 |
|
| * - RSA n/e extraction |
| 11 |
|
| * - EC curve/x/y extraction |
| 12 |
|
| * Callers own every BIGNUM they pass in and free what they get back. */ |
| 13 |
|
| |
| 14 |
|
| #include <openssl/evp.h> |
| 15 |
|
| #include <openssl/bn.h> |
| 16 |
|
| #include <openssl/ec.h> |
| 17 |
|
| #include <openssl/obj_mac.h> |
| 18 |
|
| |
| 19 |
|
| #if defined(CJWS_HAVE_FROMDATA) |
| 20 |
|
| #include <openssl/core_names.h> |
| 21 |
|
| #include <openssl/param_build.h> |
| 22 |
|
| #elif defined(CJWS_HAVE_SET0) |
| 23 |
|
| #include <openssl/rsa.h> |
| 24 |
|
| #else |
| 25 |
|
| #error "Crypt::JWS: neither CJWS_HAVE_FROMDATA nor CJWS_HAVE_SET0 defined" |
| 26 |
|
| #endif |
| 27 |
|
| |
| 28 |
20 |
| static int cjws_curve_nid(int bits) { |
| 29 |
20 |
| switch (bits) { |
| 30 |
14 |
| case 256: return NID_X9_62_prime256v1; |
| 31 |
3 |
| case 384: return NID_secp384r1; |
| 32 |
3 |
| case 521: return NID_secp521r1; |
| 33 |
|
| } |
| 34 |
0 |
| return NID_undef; |
| 35 |
|
| } |
| 36 |
|
| |
| 37 |
6 |
| static const char *cjws_curve_group_name(int bits) { |
| 38 |
6 |
| switch (bits) { |
| 39 |
4 |
| case 256: return "prime256v1"; |
| 40 |
1 |
| case 384: return "secp384r1"; |
| 41 |
1 |
| case 521: return "secp521r1"; |
| 42 |
|
| } |
| 43 |
0 |
| return NULL; |
| 44 |
|
| } |
| 45 |
|
| |
| 46 |
|
| /* EC coordinate byte length (P-521 rounds up). */ |
| 47 |
65 |
| static int cjws_curve_coord_len(int bits) { |
| 48 |
65 |
| return (bits + 7) / 8; |
| 49 |
|
| } |
| 50 |
|
| |
| 51 |
|
| #if defined(CJWS_HAVE_FROMDATA) |
| 52 |
|
| |
| 53 |
2 |
| static EVP_PKEY *cjws_pkey_from_rsa(const BIGNUM *n, const BIGNUM *e, |
| 54 |
|
| const BIGNUM *d) { |
| 55 |
2 |
| EVP_PKEY *pkey = NULL; |
| 56 |
2 |
| EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_from_name(NULL, "RSA", NULL); |
| 57 |
2 |
| OSSL_PARAM_BLD *bld = OSSL_PARAM_BLD_new(); |
| 58 |
2 |
| OSSL_PARAM *params = NULL; |
| 59 |
2 |
▶ | if (!ctx || !bld) goto done; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!ctx || !bld) goto done; |
2 |
0 |
2 |
| if (!ctx || !bld) goto done; |
0 |
2 |
2 |
|
| 60 |
2 |
▶ | if (OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_N, n) != 1) goto done; |
Branch: 1 branch
| Branch | True | False | Total |
| if (OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_N, n) != 1) goto done; |
0 |
2 |
2 |
|
| 61 |
2 |
▶ | if (OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_E, e) != 1) goto done; |
Branch: 1 branch
| Branch | True | False | Total |
| if (OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_E, e) != 1) goto done; |
0 |
2 |
2 |
|
| 62 |
2 |
▶ | if (d && OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_D, d) != 1) goto done; |
Branch: 2 branches
| Branch | True | False | Total |
| if (d && OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_D, d) != 1) goto done; |
0 |
2 |
2 |
| if (d && OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_D, d) != 1) goto done; |
0 |
0 |
0 |
|
| 63 |
2 |
| params = OSSL_PARAM_BLD_to_param(bld); |
| 64 |
2 |
▶ | if (!params) goto done; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!params) goto done; |
0 |
2 |
2 |
|
| 65 |
2 |
▶ | if (EVP_PKEY_fromdata_init(ctx) != 1) goto done; |
Branch: 1 branch
| Branch | True | False | Total |
| if (EVP_PKEY_fromdata_init(ctx) != 1) goto done; |
0 |
2 |
2 |
|
| 66 |
2 |
▶ | if (EVP_PKEY_fromdata(ctx, &pkey, |
Branch: 2 branches
| Branch | True | False | Total |
| if (EVP_PKEY_fromdata(ctx, &pkey, |
0 |
2 |
2 |
| if (EVP_PKEY_fromdata(ctx, &pkey, |
2 |
0 |
2 |
|
| 67 |
|
| d ? EVP_PKEY_KEYPAIR : EVP_PKEY_PUBLIC_KEY, params) != 1) |
| 68 |
0 |
| pkey = NULL; |
| 69 |
2 |
| done: |
| 70 |
2 |
| OSSL_PARAM_free(params); |
| 71 |
2 |
| OSSL_PARAM_BLD_free(bld); |
| 72 |
2 |
| EVP_PKEY_CTX_free(ctx); |
| 73 |
2 |
| return pkey; |
| 74 |
|
| } |
| 75 |
|
| |
| 76 |
6 |
| static EVP_PKEY *cjws_pkey_from_ec(int bits, const BIGNUM *x, const BIGNUM *y, |
| 77 |
|
| const BIGNUM *d) { |
| 78 |
6 |
| EVP_PKEY *pkey = NULL; |
| 79 |
6 |
| EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_from_name(NULL, "EC", NULL); |
| 80 |
6 |
| OSSL_PARAM_BLD *bld = OSSL_PARAM_BLD_new(); |
| 81 |
6 |
| OSSL_PARAM *params = NULL; |
| 82 |
6 |
| const char *group = cjws_curve_group_name(bits); |
| 83 |
6 |
| int clen = cjws_curve_coord_len(bits); |
| 84 |
6 |
| unsigned char *pub = NULL; |
| 85 |
6 |
▶ | if (!ctx || !bld || !group) goto done; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!ctx || !bld || !group) goto done; |
6 |
0 |
6 |
| if (!ctx || !bld || !group) goto done; |
6 |
0 |
6 |
| if (!ctx || !bld || !group) goto done; |
0 |
6 |
6 |
|
| 86 |
6 |
| pub = (unsigned char *)OPENSSL_malloc((size_t)(1 + 2 * clen)); |
| 87 |
6 |
▶ | if (!pub) goto done; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!pub) goto done; |
0 |
6 |
6 |
|
| 88 |
6 |
| pub[0] = 0x04; /* uncompressed point */ |
| 89 |
6 |
▶ | if (BN_bn2binpad(x, pub + 1, clen) != clen) goto done; |
Branch: 1 branch
| Branch | True | False | Total |
| if (BN_bn2binpad(x, pub + 1, clen) != clen) goto done; |
0 |
6 |
6 |
|
| 90 |
6 |
▶ | if (BN_bn2binpad(y, pub + 1 + clen, clen) != clen) goto done; |
Branch: 1 branch
| Branch | True | False | Total |
| if (BN_bn2binpad(y, pub + 1 + clen, clen) != clen) goto done; |
0 |
6 |
6 |
|
| 91 |
6 |
▶ | if (OSSL_PARAM_BLD_push_utf8_string(bld, OSSL_PKEY_PARAM_GROUP_NAME, |
Branch: 1 branch
| Branch | True | False | Total |
| if (OSSL_PARAM_BLD_push_utf8_string(bld, OSSL_PKEY_PARAM_GROUP_NAME, |
0 |
6 |
6 |
|
| 92 |
0 |
| group, 0) != 1) goto done; |
| 93 |
6 |
▶ | if (OSSL_PARAM_BLD_push_octet_string(bld, OSSL_PKEY_PARAM_PUB_KEY, |
Branch: 1 branch
| Branch | True | False | Total |
| if (OSSL_PARAM_BLD_push_octet_string(bld, OSSL_PKEY_PARAM_PUB_KEY, |
0 |
6 |
6 |
|
| 94 |
6 |
| pub, (size_t)(1 + 2 * clen)) != 1) goto done; |
| 95 |
6 |
▶ | if (d && OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY, d) != 1) goto done; |
Branch: 2 branches
| Branch | True | False | Total |
| if (d && OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY, d) != 1) goto done; |
1 |
5 |
6 |
| if (d && OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY, d) != 1) goto done; |
0 |
1 |
1 |
|
| 96 |
6 |
| params = OSSL_PARAM_BLD_to_param(bld); |
| 97 |
6 |
▶ | if (!params) goto done; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!params) goto done; |
0 |
6 |
6 |
|
| 98 |
6 |
▶ | if (EVP_PKEY_fromdata_init(ctx) != 1) goto done; |
Branch: 1 branch
| Branch | True | False | Total |
| if (EVP_PKEY_fromdata_init(ctx) != 1) goto done; |
0 |
6 |
6 |
|
| 99 |
6 |
▶ | if (EVP_PKEY_fromdata(ctx, &pkey, |
Branch: 2 branches
| Branch | True | False | Total |
| if (EVP_PKEY_fromdata(ctx, &pkey, |
1 |
5 |
6 |
| if (EVP_PKEY_fromdata(ctx, &pkey, |
5 |
1 |
6 |
|
| 100 |
|
| d ? EVP_PKEY_KEYPAIR : EVP_PKEY_PUBLIC_KEY, params) != 1) |
| 101 |
1 |
| pkey = NULL; |
| 102 |
5 |
| done: |
| 103 |
6 |
| OPENSSL_free(pub); |
| 104 |
6 |
| OSSL_PARAM_free(params); |
| 105 |
6 |
| OSSL_PARAM_BLD_free(bld); |
| 106 |
6 |
| EVP_PKEY_CTX_free(ctx); |
| 107 |
6 |
| return pkey; |
| 108 |
|
| } |
| 109 |
|
| |
| 110 |
|
| /* Extract public RSA params as fresh BIGNUMs (caller frees). */ |
| 111 |
6 |
| static int cjws_pkey_get_rsa(EVP_PKEY *pkey, BIGNUM **n, BIGNUM **e) { |
| 112 |
6 |
| *n = *e = NULL; |
| 113 |
6 |
▶ | if (EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_RSA_N, n) != 1) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_RSA_N, n) != 1) return 0; |
0 |
6 |
6 |
|
| 114 |
6 |
▶ | if (EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_RSA_E, e) != 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_RSA_E, e) != 1) { |
0 |
6 |
6 |
|
| 115 |
0 |
| BN_free(*n); *n = NULL; |
| 116 |
0 |
| return 0; |
| 117 |
|
| } |
| 118 |
6 |
| return 1; |
| 119 |
|
| } |
| 120 |
|
| |
| 121 |
|
| /* Extract EC curve bits + affine coordinates (caller frees x/y). */ |
| 122 |
46 |
| static int cjws_pkey_get_ec(EVP_PKEY *pkey, int *bits, BIGNUM **x, BIGNUM **y) { |
| 123 |
|
| char group[64]; |
| 124 |
46 |
| *x = *y = NULL; |
| 125 |
46 |
▶ | if (EVP_PKEY_get_utf8_string_param(pkey, OSSL_PKEY_PARAM_GROUP_NAME, |
Branch: 1 branch
| Branch | True | False | Total |
| if (EVP_PKEY_get_utf8_string_param(pkey, OSSL_PKEY_PARAM_GROUP_NAME, |
0 |
46 |
46 |
|
| 126 |
|
| group, sizeof group, NULL) != 1) |
| 127 |
0 |
| return 0; |
| 128 |
46 |
▶ | if (strcmp(group, "prime256v1") == 0 || strcmp(group, "P-256") == 0) |
Branch: 2 branches
| Branch | True | False | Total |
| if (strcmp(group, "prime256v1") == 0 || strcmp(group, "P-256") == 0) |
14 |
32 |
46 |
| if (strcmp(group, "prime256v1") == 0 || strcmp(group, "P-256") == 0) |
0 |
14 |
14 |
|
| 129 |
32 |
| *bits = 256; |
| 130 |
14 |
▶ | else if (strcmp(group, "secp384r1") == 0 || strcmp(group, "P-384") == 0) |
Branch: 2 branches
| Branch | True | False | Total |
| else if (strcmp(group, "secp384r1") == 0 || strcmp(group, "P-384") == 0) |
7 |
7 |
14 |
| else if (strcmp(group, "secp384r1") == 0 || strcmp(group, "P-384") == 0) |
0 |
7 |
7 |
|
| 131 |
7 |
| *bits = 384; |
| 132 |
7 |
▶ | else if (strcmp(group, "secp521r1") == 0 || strcmp(group, "P-521") == 0) |
Branch: 2 branches
| Branch | True | False | Total |
| else if (strcmp(group, "secp521r1") == 0 || strcmp(group, "P-521") == 0) |
0 |
7 |
7 |
| else if (strcmp(group, "secp521r1") == 0 || strcmp(group, "P-521") == 0) |
0 |
0 |
0 |
|
| 133 |
7 |
| *bits = 521; |
| 134 |
0 |
| else return 0; |
| 135 |
46 |
▶ | if (EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_EC_PUB_X, x) != 1) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_EC_PUB_X, x) != 1) return 0; |
0 |
46 |
46 |
|
| 136 |
46 |
▶ | if (EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_EC_PUB_Y, y) != 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_EC_PUB_Y, y) != 1) { |
0 |
46 |
46 |
|
| 137 |
0 |
| BN_free(*x); *x = NULL; |
| 138 |
0 |
| return 0; |
| 139 |
|
| } |
| 140 |
46 |
| return 1; |
| 141 |
|
| } |
| 142 |
|
| |
| 143 |
|
| #else /* CJWS_HAVE_SET0: OpenSSL 1.1 struct API */ |
| 144 |
|
| |
| 145 |
|
| static EVP_PKEY *cjws_pkey_from_rsa(const BIGNUM *n, const BIGNUM *e, |
| 146 |
|
| const BIGNUM *d) { |
| 147 |
|
| RSA *rsa = RSA_new(); |
| 148 |
|
| EVP_PKEY *pkey = NULL; |
| 149 |
|
| BIGNUM *nn, *ee, *dd = NULL; |
| 150 |
|
| if (!rsa) return NULL; |
| 151 |
|
| nn = BN_dup(n); |
| 152 |
|
| ee = BN_dup(e); |
| 153 |
|
| if (d) dd = BN_dup(d); |
| 154 |
|
| if (!nn || !ee || (d && !dd) |
| 155 |
|
| || RSA_set0_key(rsa, nn, ee, dd) != 1) { |
| 156 |
|
| BN_free(nn); BN_free(ee); BN_free(dd); |
| 157 |
|
| RSA_free(rsa); |
| 158 |
|
| return NULL; |
| 159 |
|
| } |
| 160 |
|
| pkey = EVP_PKEY_new(); |
| 161 |
|
| if (!pkey || EVP_PKEY_assign_RSA(pkey, rsa) != 1) { |
| 162 |
|
| EVP_PKEY_free(pkey); |
| 163 |
|
| RSA_free(rsa); |
| 164 |
|
| return NULL; |
| 165 |
|
| } |
| 166 |
|
| return pkey; |
| 167 |
|
| } |
| 168 |
|
| |
| 169 |
|
| static EVP_PKEY *cjws_pkey_from_ec(int bits, const BIGNUM *x, const BIGNUM *y, |
| 170 |
|
| const BIGNUM *d) { |
| 171 |
|
| int nid = cjws_curve_nid(bits); |
| 172 |
|
| EC_KEY *ec; |
| 173 |
|
| EVP_PKEY *pkey = NULL; |
| 174 |
|
| if (nid == NID_undef) return NULL; |
| 175 |
|
| ec = EC_KEY_new_by_curve_name(nid); |
| 176 |
|
| if (!ec) return NULL; |
| 177 |
|
| if (EC_KEY_set_public_key_affine_coordinates(ec, (BIGNUM *)x, |
| 178 |
|
| (BIGNUM *)y) != 1) { |
| 179 |
|
| EC_KEY_free(ec); |
| 180 |
|
| return NULL; |
| 181 |
|
| } |
| 182 |
|
| if (d && EC_KEY_set_private_key(ec, d) != 1) { |
| 183 |
|
| EC_KEY_free(ec); |
| 184 |
|
| return NULL; |
| 185 |
|
| } |
| 186 |
|
| pkey = EVP_PKEY_new(); |
| 187 |
|
| if (!pkey || EVP_PKEY_assign_EC_KEY(pkey, ec) != 1) { |
| 188 |
|
| EVP_PKEY_free(pkey); |
| 189 |
|
| EC_KEY_free(ec); |
| 190 |
|
| return NULL; |
| 191 |
|
| } |
| 192 |
|
| return pkey; |
| 193 |
|
| } |
| 194 |
|
| |
| 195 |
|
| static int cjws_pkey_get_rsa(EVP_PKEY *pkey, BIGNUM **n, BIGNUM **e) { |
| 196 |
|
| const RSA *rsa = EVP_PKEY_get0_RSA(pkey); |
| 197 |
|
| const BIGNUM *cn, *ce; |
| 198 |
|
| *n = *e = NULL; |
| 199 |
|
| if (!rsa) return 0; |
| 200 |
|
| RSA_get0_key(rsa, &cn, &ce, NULL); |
| 201 |
|
| if (!cn || !ce) return 0; |
| 202 |
|
| *n = BN_dup(cn); |
| 203 |
|
| *e = BN_dup(ce); |
| 204 |
|
| if (!*n || !*e) { |
| 205 |
|
| BN_free(*n); BN_free(*e); |
| 206 |
|
| *n = *e = NULL; |
| 207 |
|
| return 0; |
| 208 |
|
| } |
| 209 |
|
| return 1; |
| 210 |
|
| } |
| 211 |
|
| |
| 212 |
|
| static int cjws_pkey_get_ec(EVP_PKEY *pkey, int *bits, BIGNUM **x, BIGNUM **y) { |
| 213 |
|
| const EC_KEY *ec = EVP_PKEY_get0_EC_KEY(pkey); |
| 214 |
|
| const EC_GROUP *grp; |
| 215 |
|
| const EC_POINT *pt; |
| 216 |
|
| BN_CTX *bnctx; |
| 217 |
|
| int nid, ok = 0; |
| 218 |
|
| *x = *y = NULL; |
| 219 |
|
| if (!ec) return 0; |
| 220 |
|
| grp = EC_KEY_get0_group(ec); |
| 221 |
|
| pt = EC_KEY_get0_public_key(ec); |
| 222 |
|
| if (!grp || !pt) return 0; |
| 223 |
|
| nid = EC_GROUP_get_curve_name(grp); |
| 224 |
|
| if (nid == NID_X9_62_prime256v1) *bits = 256; |
| 225 |
|
| else if (nid == NID_secp384r1) *bits = 384; |
| 226 |
|
| else if (nid == NID_secp521r1) *bits = 521; |
| 227 |
|
| else return 0; |
| 228 |
|
| *x = BN_new(); |
| 229 |
|
| *y = BN_new(); |
| 230 |
|
| bnctx = BN_CTX_new(); |
| 231 |
|
| if (*x && *y && bnctx |
| 232 |
|
| && EC_POINT_get_affine_coordinates(grp, pt, *x, *y, bnctx) == 1) |
| 233 |
|
| ok = 1; |
| 234 |
|
| BN_CTX_free(bnctx); |
| 235 |
|
| if (!ok) { |
| 236 |
|
| BN_free(*x); BN_free(*y); |
| 237 |
|
| *x = *y = NULL; |
| 238 |
|
| } |
| 239 |
|
| return ok; |
| 240 |
|
| } |
| 241 |
|
| |
| 242 |
|
| #endif |
| 243 |
|
| |
| 244 |
|
| #endif |