| 1 |
|
| #ifndef CJWS_JWS_H |
| 2 |
|
| #define CJWS_JWS_H |
| 3 |
|
| |
| 4 |
|
| #include <openssl/ecdsa.h> |
| 5 |
|
| #include "cjws_key.h" |
| 6 |
|
| #include "cjws_digest.h" |
| 7 |
|
| #include "cjws_util.h" |
| 8 |
|
| |
| 9 |
|
| /* Raw JWS crypto over signing-input bytes. Compact serialization, header |
| 10 |
|
| * JSON, and the alg allowlist policy live in the Perl layer; this file |
| 11 |
|
| * signs and verifies, and refuses key/alg mismatches. */ |
| 12 |
|
| |
| 13 |
|
| #define CJWS_FAM_HS 'H' |
| 14 |
|
| #define CJWS_FAM_RS 'R' |
| 15 |
|
| #define CJWS_FAM_ES 'E' |
| 16 |
|
| |
| 17 |
|
| /* "RS256" -> ('R', 256). Returns 0 on anything else. */ |
| 18 |
179 |
| static int cjws_alg_parse(const char *alg, STRLEN len, char *fam, int *bits) { |
| 19 |
179 |
▶ | if (len != 5) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (len != 5) return 0; |
3 |
176 |
179 |
|
| 20 |
176 |
▶ | if (alg[0] == 'H' && alg[1] == 'S') *fam = CJWS_FAM_HS; |
Branch: 2 branches
| Branch | True | False | Total |
| if (alg[0] == 'H' && alg[1] == 'S') *fam = CJWS_FAM_HS; |
35 |
141 |
176 |
| if (alg[0] == 'H' && alg[1] == 'S') *fam = CJWS_FAM_HS; |
35 |
0 |
35 |
|
| 21 |
141 |
▶ | else if (alg[0] == 'R' && alg[1] == 'S') *fam = CJWS_FAM_RS; |
Branch: 2 branches
| Branch | True | False | Total |
| else if (alg[0] == 'R' && alg[1] == 'S') *fam = CJWS_FAM_RS; |
42 |
99 |
141 |
| else if (alg[0] == 'R' && alg[1] == 'S') *fam = CJWS_FAM_RS; |
42 |
0 |
42 |
|
| 22 |
99 |
▶ | else if (alg[0] == 'E' && alg[1] == 'S') *fam = CJWS_FAM_ES; |
Branch: 2 branches
| Branch | True | False | Total |
| else if (alg[0] == 'E' && alg[1] == 'S') *fam = CJWS_FAM_ES; |
99 |
0 |
99 |
| else if (alg[0] == 'E' && alg[1] == 'S') *fam = CJWS_FAM_ES; |
99 |
0 |
99 |
|
| 23 |
0 |
| else return 0; |
| 24 |
176 |
▶ | if (memcmp(alg + 2, "256", 3) == 0) *bits = 256; |
Branch: 1 branch
| Branch | True | False | Total |
| if (memcmp(alg + 2, "256", 3) == 0) *bits = 256; |
134 |
42 |
176 |
|
| 25 |
42 |
▶ | else if (memcmp(alg + 2, "384", 3) == 0) *bits = 384; |
Branch: 1 branch
| Branch | True | False | Total |
| else if (memcmp(alg + 2, "384", 3) == 0) *bits = 384; |
21 |
21 |
42 |
|
| 26 |
21 |
▶ | else if (memcmp(alg + 2, "512", 3) == 0) *bits = 512; |
Branch: 1 branch
| Branch | True | False | Total |
| else if (memcmp(alg + 2, "512", 3) == 0) *bits = 512; |
21 |
0 |
21 |
|
| 27 |
0 |
| else return 0; |
| 28 |
176 |
| return 1; |
| 29 |
|
| } |
| 30 |
|
| |
| 31 |
|
| /* ES curve for an alg: ES256=P-256, ES384=P-384, ES512=P-521. */ |
| 32 |
83 |
| static int cjws_es_curve_bits(int alg_bits) { |
| 33 |
83 |
▶ | return alg_bits == 512 ? 521 : alg_bits; |
Branch: 1 branch
| Branch | True | False | Total |
| return alg_bits == 512 ? 521 : alg_bits; |
72 |
11 |
83 |
|
| 34 |
|
| } |
| 35 |
|
| |
| 36 |
|
| /* The key/alg cross-check: an HS alg demands an oct key, RS demands RSA, |
| 37 |
|
| * ES demands EC on the exact curve. This is what makes alg-confusion |
| 38 |
|
| * attacks structurally impossible downstream. */ |
| 39 |
67 |
| static int cjws_key_matches_alg(const cjws_key *k, char fam, int bits) { |
| 40 |
67 |
| switch (fam) { |
| 41 |
17 |
| case CJWS_FAM_HS: return k->kty == CJWS_KTY_OCT; |
| 42 |
16 |
| case CJWS_FAM_RS: return k->kty == CJWS_KTY_RSA; |
| 43 |
34 |
| case CJWS_FAM_ES: return k->kty == CJWS_KTY_EC |
| 44 |
34 |
▶ | && k->curve_bits == cjws_es_curve_bits(bits); |
Branch: 2 branches
| Branch | True | False | Total |
| && k->curve_bits == cjws_es_curve_bits(bits); |
32 |
2 |
34 |
| && k->curve_bits == cjws_es_curve_bits(bits); |
32 |
0 |
32 |
|
| 45 |
|
| } |
| 46 |
0 |
| return 0; |
| 47 |
|
| } |
| 48 |
|
| |
| 49 |
|
| /* DER ECDSA_SIG -> JOSE r||s (each BN_bn2binpad'd to the coordinate |
| 50 |
|
| * length). Returns JOSE length or 0. */ |
| 51 |
13 |
| static STRLEN cjws_es_der_to_jose(const unsigned char *der, STRLEN derlen, |
| 52 |
|
| int curve_bits, unsigned char *jose) { |
| 53 |
13 |
| const unsigned char *p = der; |
| 54 |
13 |
| ECDSA_SIG *sig = d2i_ECDSA_SIG(NULL, &p, (long)derlen); |
| 55 |
|
| const BIGNUM *r, *s; |
| 56 |
13 |
| int clen = cjws_curve_coord_len(curve_bits); |
| 57 |
13 |
▶ | if (!sig) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sig) return 0; |
0 |
13 |
13 |
|
| 58 |
13 |
| ECDSA_SIG_get0(sig, &r, &s); |
| 59 |
13 |
▶ | if (BN_bn2binpad(r, jose, clen) != clen |
Branch: 1 branch
| Branch | True | False | Total |
| if (BN_bn2binpad(r, jose, clen) != clen |
13 |
0 |
13 |
|
| 60 |
13 |
▶ | || BN_bn2binpad(s, jose + clen, clen) != clen) { |
Branch: 1 branch
| Branch | True | False | Total |
| || BN_bn2binpad(s, jose + clen, clen) != clen) { |
0 |
13 |
13 |
|
| 61 |
0 |
| ECDSA_SIG_free(sig); |
| 62 |
0 |
| return 0; |
| 63 |
|
| } |
| 64 |
13 |
| ECDSA_SIG_free(sig); |
| 65 |
13 |
| return (STRLEN)(2 * clen); |
| 66 |
|
| } |
| 67 |
|
| |
| 68 |
|
| /* JOSE r||s -> DER. dst must hold ~(2*clen + 16). Returns DER length |
| 69 |
|
| * or 0. A JOSE signature whose length is not exactly 2*clen fails here, |
| 70 |
|
| * which is the RFC 7515 length check. */ |
| 71 |
18 |
| static STRLEN cjws_es_jose_to_der(const unsigned char *jose, STRLEN joselen, |
| 72 |
|
| int curve_bits, unsigned char *dst) { |
| 73 |
18 |
| int clen = cjws_curve_coord_len(curve_bits); |
| 74 |
|
| ECDSA_SIG *sig; |
| 75 |
|
| BIGNUM *r, *s; |
| 76 |
18 |
| unsigned char *p = dst; |
| 77 |
|
| int derlen; |
| 78 |
18 |
▶ | if (joselen != (STRLEN)(2 * clen)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (joselen != (STRLEN)(2 * clen)) return 0; |
3 |
15 |
18 |
|
| 79 |
15 |
| r = BN_bin2bn(jose, clen, NULL); |
| 80 |
15 |
| s = BN_bin2bn(jose + clen, clen, NULL); |
| 81 |
15 |
| sig = ECDSA_SIG_new(); |
| 82 |
15 |
▶ | if (!r || !s || !sig || ECDSA_SIG_set0(sig, r, s) != 1) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (!r || !s || !sig || ECDSA_SIG_set0(sig, r, s) != 1) { |
15 |
0 |
15 |
| if (!r || !s || !sig || ECDSA_SIG_set0(sig, r, s) != 1) { |
15 |
0 |
15 |
| if (!r || !s || !sig || ECDSA_SIG_set0(sig, r, s) != 1) { |
15 |
0 |
15 |
| if (!r || !s || !sig || ECDSA_SIG_set0(sig, r, s) != 1) { |
0 |
15 |
15 |
|
| 83 |
0 |
| BN_free(r); BN_free(s); |
| 84 |
0 |
| ECDSA_SIG_free(sig); |
| 85 |
0 |
| return 0; |
| 86 |
|
| } |
| 87 |
15 |
| derlen = i2d_ECDSA_SIG(sig, &p); |
| 88 |
15 |
| ECDSA_SIG_free(sig); |
| 89 |
15 |
| return derlen > 0 ? (STRLEN)derlen : 0; |
| 90 |
|
| } |
| 91 |
|
| |
| 92 |
|
| /* Sign signing-input bytes. out is OPENSSL_malloc'd, caller frees; length |
| 93 |
|
| * in *outlen. Returns NULL on failure (wrong key type, public key for a |
| 94 |
|
| * signing op, OpenSSL error). */ |
| 95 |
29 |
| static unsigned char *cjws_sign_raw(pTHX_ cjws_key *k, |
| 96 |
|
| const char *alg, STRLEN alglen, |
| 97 |
|
| const unsigned char *input, STRLEN inlen, |
| 98 |
|
| STRLEN *outlen) { |
| 99 |
|
| char fam; |
| 100 |
|
| int bits; |
| 101 |
29 |
| unsigned char *out = NULL; |
| 102 |
29 |
| *outlen = 0; |
| 103 |
29 |
▶ | if (!cjws_alg_parse(alg, alglen, &fam, &bits)) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!cjws_alg_parse(alg, alglen, &fam, &bits)) return NULL; |
0 |
29 |
29 |
|
| 104 |
29 |
▶ | if (!cjws_key_matches_alg(k, fam, bits)) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!cjws_key_matches_alg(k, fam, bits)) return NULL; |
0 |
29 |
29 |
|
| 105 |
29 |
▶ | if (!k->is_private) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!k->is_private) return NULL; |
1 |
28 |
29 |
|
| 106 |
|
| |
| 107 |
28 |
▶ | if (fam == CJWS_FAM_HS) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (fam == CJWS_FAM_HS) { |
7 |
21 |
28 |
|
| 108 |
|
| unsigned char mac[EVP_MAX_MD_SIZE]; |
| 109 |
7 |
| STRLEN maclen = cjws_hmac(bits, k->oct, k->octlen, input, inlen, mac); |
| 110 |
7 |
▶ | if (!maclen) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!maclen) return NULL; |
0 |
7 |
7 |
|
| 111 |
7 |
| out = (unsigned char *)OPENSSL_malloc(maclen); |
| 112 |
7 |
▶ | if (!out) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!out) return NULL; |
0 |
7 |
7 |
|
| 113 |
7 |
| memcpy(out, mac, maclen); |
| 114 |
7 |
| *outlen = maclen; |
| 115 |
7 |
| return out; |
| 116 |
|
| } |
| 117 |
|
| |
| 118 |
|
| { |
| 119 |
21 |
| const EVP_MD *md = cjws_md_by_bits(bits); |
| 120 |
21 |
| EVP_MD_CTX *ctx = EVP_MD_CTX_new(); |
| 121 |
21 |
| size_t siglen = 0; |
| 122 |
21 |
| unsigned char *sig = NULL; |
| 123 |
21 |
▶ | if (!md || !ctx) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!md || !ctx) { |
21 |
0 |
21 |
| if (!md || !ctx) { |
0 |
21 |
21 |
|
| 124 |
0 |
| EVP_MD_CTX_free(ctx); |
| 125 |
0 |
| return NULL; |
| 126 |
|
| } |
| 127 |
21 |
▶ | if (EVP_DigestSignInit(ctx, NULL, md, NULL, k->pkey) != 1 |
Branch: 1 branch
| Branch | True | False | Total |
| if (EVP_DigestSignInit(ctx, NULL, md, NULL, k->pkey) != 1 |
21 |
0 |
21 |
|
| 128 |
21 |
▶ | || EVP_DigestSign(ctx, NULL, &siglen, input, inlen) != 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| || EVP_DigestSign(ctx, NULL, &siglen, input, inlen) != 1) { |
0 |
21 |
21 |
|
| 129 |
0 |
| EVP_MD_CTX_free(ctx); |
| 130 |
0 |
| return NULL; |
| 131 |
|
| } |
| 132 |
21 |
| sig = (unsigned char *)OPENSSL_malloc(siglen); |
| 133 |
21 |
▶ | if (!sig || EVP_DigestSign(ctx, sig, &siglen, input, inlen) != 1) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!sig || EVP_DigestSign(ctx, sig, &siglen, input, inlen) != 1) { |
21 |
0 |
21 |
| if (!sig || EVP_DigestSign(ctx, sig, &siglen, input, inlen) != 1) { |
0 |
21 |
21 |
|
| 134 |
0 |
| OPENSSL_free(sig); |
| 135 |
0 |
| EVP_MD_CTX_free(ctx); |
| 136 |
0 |
| return NULL; |
| 137 |
|
| } |
| 138 |
21 |
| EVP_MD_CTX_free(ctx); |
| 139 |
|
| |
| 140 |
21 |
▶ | if (fam == CJWS_FAM_ES) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (fam == CJWS_FAM_ES) { |
13 |
8 |
21 |
|
| 141 |
13 |
| int curve = cjws_es_curve_bits(bits); |
| 142 |
13 |
| int clen = cjws_curve_coord_len(curve); |
| 143 |
13 |
| out = (unsigned char *)OPENSSL_malloc((size_t)(2 * clen)); |
| 144 |
13 |
▶ | if (out) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (out) { |
13 |
0 |
13 |
|
| 145 |
13 |
| *outlen = cjws_es_der_to_jose(sig, (STRLEN)siglen, curve, out); |
| 146 |
13 |
▶ | if (!*outlen) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!*outlen) { |
0 |
13 |
13 |
|
| 147 |
0 |
| OPENSSL_free(out); |
| 148 |
0 |
| out = NULL; |
| 149 |
|
| } |
| 150 |
|
| } |
| 151 |
13 |
| OPENSSL_free(sig); |
| 152 |
13 |
| return out; |
| 153 |
|
| } |
| 154 |
|
| |
| 155 |
8 |
| *outlen = (STRLEN)siglen; |
| 156 |
8 |
| return sig; |
| 157 |
|
| } |
| 158 |
|
| } |
| 159 |
|
| |
| 160 |
|
| /* Verify. Returns 1 valid, 0 invalid (any reason - never says why). */ |
| 161 |
38 |
| static int cjws_verify_raw(pTHX_ cjws_key *k, |
| 162 |
|
| const char *alg, STRLEN alglen, |
| 163 |
|
| const unsigned char *input, STRLEN inlen, |
| 164 |
|
| const unsigned char *sig, STRLEN siglen) { |
| 165 |
|
| char fam; |
| 166 |
|
| int bits; |
| 167 |
38 |
▶ | if (!cjws_alg_parse(alg, alglen, &fam, &bits)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!cjws_alg_parse(alg, alglen, &fam, &bits)) return 0; |
0 |
38 |
38 |
|
| 168 |
38 |
▶ | if (!cjws_key_matches_alg(k, fam, bits)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!cjws_key_matches_alg(k, fam, bits)) return 0; |
3 |
35 |
38 |
|
| 169 |
|
| |
| 170 |
35 |
▶ | if (fam == CJWS_FAM_HS) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (fam == CJWS_FAM_HS) { |
9 |
26 |
35 |
|
| 171 |
|
| unsigned char mac[EVP_MAX_MD_SIZE]; |
| 172 |
9 |
| STRLEN maclen = cjws_hmac(bits, k->oct, k->octlen, input, inlen, mac); |
| 173 |
9 |
▶ | if (!maclen) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!maclen) return 0; |
0 |
9 |
9 |
|
| 174 |
9 |
| return cjws_ct_eq(mac, maclen, sig, siglen); |
| 175 |
|
| } |
| 176 |
|
| |
| 177 |
|
| { |
| 178 |
26 |
| const EVP_MD *md = cjws_md_by_bits(bits); |
| 179 |
|
| EVP_MD_CTX *ctx; |
| 180 |
|
| unsigned char derbuf[256]; |
| 181 |
26 |
| const unsigned char *use_sig = sig; |
| 182 |
26 |
| STRLEN use_len = siglen; |
| 183 |
|
| int ok; |
| 184 |
26 |
▶ | if (!md) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!md) return 0; |
0 |
26 |
26 |
|
| 185 |
26 |
▶ | if (fam == CJWS_FAM_ES) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (fam == CJWS_FAM_ES) { |
18 |
8 |
26 |
|
| 186 |
18 |
| int curve = cjws_es_curve_bits(bits); |
| 187 |
18 |
| use_len = cjws_es_jose_to_der(sig, siglen, curve, derbuf); |
| 188 |
18 |
▶ | if (!use_len) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!use_len) return 0; |
3 |
15 |
18 |
|
| 189 |
15 |
| use_sig = derbuf; |
| 190 |
|
| } |
| 191 |
23 |
| ctx = EVP_MD_CTX_new(); |
| 192 |
23 |
▶ | if (!ctx) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ctx) return 0; |
0 |
23 |
23 |
|
| 193 |
23 |
| ok = EVP_DigestVerifyInit(ctx, NULL, md, NULL, k->pkey) == 1 |
| 194 |
23 |
▶ | && EVP_DigestVerify(ctx, use_sig, use_len, input, inlen) == 1; |
Branch: 2 branches
| Branch | True | False | Total |
| && EVP_DigestVerify(ctx, use_sig, use_len, input, inlen) == 1; |
23 |
0 |
23 |
| && EVP_DigestVerify(ctx, use_sig, use_len, input, inlen) == 1; |
19 |
4 |
23 |
|
| 195 |
23 |
| EVP_MD_CTX_free(ctx); |
| 196 |
23 |
| return ok ? 1 : 0; |
| 197 |
|
| } |
| 198 |
|
| } |
| 199 |
|
| |
| 200 |
|
| #endif |