@@ -854,6 +854,40 @@ namespace jwt {
854
854
}
855
855
} // namespace helper
856
856
857
+ class key {
858
+ public:
859
+ static key symmetric (const std::string& bytes) { return key (bytes); }
860
+
861
+ static key asymmetric (helper::evp_pkey_handle pkey) { return key (pkey); }
862
+
863
+ std::string get_symmetric_key () const {
864
+ if (!is_symmetric) { throw std::logic_error (" not a symmetric key" ); }
865
+
866
+ return oct_key;
867
+ }
868
+
869
+ helper::evp_pkey_handle get_asymmetric_key () const {
870
+ if (is_symmetric) { throw std::logic_error (" not an asymmetric key" ); }
871
+
872
+ return pkey;
873
+ }
874
+
875
+ private:
876
+ key (const std::string& key) {
877
+ is_symmetric = true ;
878
+ oct_key = key;
879
+ }
880
+
881
+ key (helper::evp_pkey_handle key) {
882
+ is_symmetric = false ;
883
+ pkey = key;
884
+ }
885
+
886
+ bool is_symmetric;
887
+ helper::evp_pkey_handle pkey;
888
+ std::string oct_key;
889
+ };
890
+
857
891
/* *
858
892
* \brief Various cryptographic algorithms when working with JWT
859
893
*
@@ -1100,6 +1134,9 @@ namespace jwt {
1100
1134
throw ecdsa_exception (error::ecdsa_error::invalid_key_size);
1101
1135
}
1102
1136
1137
+ ecdsa (helper::evp_pkey_handle pkey, const EVP_MD* (*md)(), std::string name, size_t siglen)
1138
+ : pkey(pkey), md(md), alg_name(std::move(name)), signature_length(siglen) {}
1139
+
1103
1140
/* *
1104
1141
* Sign jwt data
1105
1142
* \param data The data to sign
@@ -1459,6 +1496,9 @@ namespace jwt {
1459
1496
throw rsa_exception (error::rsa_error::no_key_provided);
1460
1497
}
1461
1498
1499
+ pss (helper::evp_pkey_handle pkey, const EVP_MD* (*md)(), std::string name)
1500
+ : pkey(pkey), md(md), alg_name(std::move(name)) {}
1501
+
1462
1502
/* *
1463
1503
* Sign jwt data
1464
1504
* \param data The data to sign
@@ -1670,6 +1710,8 @@ namespace jwt {
1670
1710
explicit es256 (const std::string& public_key, const std::string& private_key = " " ,
1671
1711
const std::string& public_key_password = " " , const std::string& private_key_password = " " )
1672
1712
: ecdsa(public_key, private_key, public_key_password, private_key_password, EVP_sha256, " ES256" , 64 ) {}
1713
+
1714
+ explicit es256 (helper::evp_pkey_handle pkey) : ecdsa(pkey, EVP_sha256, " ES256" , 64 ) {}
1673
1715
};
1674
1716
/* *
1675
1717
* ES384 algorithm
@@ -1687,6 +1729,8 @@ namespace jwt {
1687
1729
explicit es384 (const std::string& public_key, const std::string& private_key = " " ,
1688
1730
const std::string& public_key_password = " " , const std::string& private_key_password = " " )
1689
1731
: ecdsa(public_key, private_key, public_key_password, private_key_password, EVP_sha384, " ES384" , 96 ) {}
1732
+
1733
+ explicit es384 (helper::evp_pkey_handle pkey) : ecdsa(pkey, EVP_sha384, " ES384" , 96 ) {}
1690
1734
};
1691
1735
/* *
1692
1736
* ES512 algorithm
@@ -1704,6 +1748,8 @@ namespace jwt {
1704
1748
explicit es512 (const std::string& public_key, const std::string& private_key = " " ,
1705
1749
const std::string& public_key_password = " " , const std::string& private_key_password = " " )
1706
1750
: ecdsa(public_key, private_key, public_key_password, private_key_password, EVP_sha512, " ES512" , 132 ) {}
1751
+
1752
+ explicit es512 (helper::evp_pkey_handle pkey) : ecdsa(pkey, EVP_sha512, " ES512" , 132 ) {}
1707
1753
};
1708
1754
/* *
1709
1755
* ES256K algorithm
@@ -1720,6 +1766,8 @@ namespace jwt {
1720
1766
explicit es256k (const std::string& public_key, const std::string& private_key = " " ,
1721
1767
const std::string& public_key_password = " " , const std::string& private_key_password = " " )
1722
1768
: ecdsa(public_key, private_key, public_key_password, private_key_password, EVP_sha256, " ES256K" , 64 ) {}
1769
+
1770
+ explicit es256k (helper::evp_pkey_handle pkey) : ecdsa(pkey, EVP_sha256, " ES256K" , 64 ) {}
1723
1771
};
1724
1772
1725
1773
#if !defined(JWT_OPENSSL_1_0_0) && !defined(JWT_OPENSSL_1_1_0)
@@ -1782,6 +1830,8 @@ namespace jwt {
1782
1830
explicit ps256 (const std::string& public_key, const std::string& private_key = " " ,
1783
1831
const std::string& public_key_password = " " , const std::string& private_key_password = " " )
1784
1832
: pss(public_key, private_key, public_key_password, private_key_password, EVP_sha256, " PS256" ) {}
1833
+
1834
+ explicit ps256 (helper::evp_pkey_handle pkey) : pss(pkey, EVP_sha256, " PS256" ) {}
1785
1835
};
1786
1836
/* *
1787
1837
* PS384 algorithm
@@ -1797,6 +1847,8 @@ namespace jwt {
1797
1847
explicit ps384 (const std::string& public_key, const std::string& private_key = " " ,
1798
1848
const std::string& public_key_password = " " , const std::string& private_key_password = " " )
1799
1849
: pss(public_key, private_key, public_key_password, private_key_password, EVP_sha384, " PS384" ) {}
1850
+
1851
+ explicit ps384 (helper::evp_pkey_handle pkey) : pss(pkey, EVP_sha384, " PS384" ) {}
1800
1852
};
1801
1853
/* *
1802
1854
* PS512 algorithm
@@ -1812,6 +1864,8 @@ namespace jwt {
1812
1864
explicit ps512 (const std::string& public_key, const std::string& private_key = " " ,
1813
1865
const std::string& public_key_password = " " , const std::string& private_key_password = " " )
1814
1866
: pss(public_key, private_key, public_key_password, private_key_password, EVP_sha512, " PS512" ) {}
1867
+
1868
+ explicit ps512 (helper::evp_pkey_handle pkey) : pss(pkey, EVP_sha512, " PS512" ) {}
1815
1869
};
1816
1870
} // namespace algorithm
1817
1871
@@ -3158,7 +3212,7 @@ namespace jwt {
3158
3212
JWT_CLAIM_EXPLICIT jwk (const typename json_traits::value_type& json) : jwk(json_traits::as_object(json)) {}
3159
3213
3160
3214
JWT_CLAIM_EXPLICIT jwk (const typename json_traits::object_type& json)
3161
- : jwk_claims(json), key (build_key(jwk_claims)) {
3215
+ : jwk_claims(json), k (build_key(jwk_claims)) {
3162
3216
// https://datatracker.ietf.org/doc/html/rfc7518#section-6.1
3163
3217
// * indicate required params
3164
3218
// "kty"* : "EC", "RSA", "oct"
@@ -3354,50 +3408,16 @@ namespace jwt {
3354
3408
3355
3409
bool empty () const noexcept { return jwk_claims.empty (); }
3356
3410
3357
- helper::evp_pkey_handle get_pkey () const { return key .get_asymmetric_key (); }
3411
+ helper::evp_pkey_handle get_pkey () const { return k .get_asymmetric_key (); }
3358
3412
3359
- std::string get_oct_key () const { return key .get_symmetric_key (); }
3413
+ std::string get_oct_key () const { return k .get_symmetric_key (); }
3360
3414
3361
3415
bool supports (const std::string& alg_name) const {
3362
3416
const alg_list& x = supported_alg.find (get_key_type ())->second ;
3363
3417
return std::find (x.begin (), x.end (), alg_name) != x.end ();
3364
3418
}
3365
3419
3366
3420
private:
3367
- class key {
3368
- public:
3369
- static key symmetric (const std::string& bytes) { return key (bytes); }
3370
-
3371
- static key asymmetric (helper::evp_pkey_handle pkey) { return key (pkey); }
3372
-
3373
- std::string get_symmetric_key () const {
3374
- if (!is_symmetric) { throw std::logic_error (" not a symmetric key" ); }
3375
-
3376
- return oct_key;
3377
- }
3378
-
3379
- helper::evp_pkey_handle get_asymmetric_key () const {
3380
- if (is_symmetric) { throw std::logic_error (" not an asymmetric key" ); }
3381
-
3382
- return pkey;
3383
- }
3384
-
3385
- private:
3386
- key (const std::string& key) {
3387
- is_symmetric = true ;
3388
- oct_key = key;
3389
- }
3390
-
3391
- key (helper::evp_pkey_handle key) {
3392
- is_symmetric = false ;
3393
- pkey = key;
3394
- }
3395
-
3396
- bool is_symmetric;
3397
- helper::evp_pkey_handle pkey;
3398
- std::string oct_key;
3399
- };
3400
-
3401
3421
static helper::evp_pkey_handle build_rsa_key (const details::map_of_claims<json_traits>& claims) {
3402
3422
EVP_PKEY* evp_key = nullptr ;
3403
3423
auto n = jwt::helper::raw2bn (
@@ -3460,7 +3480,7 @@ namespace jwt {
3460
3480
}
3461
3481
}
3462
3482
3463
- key key ;
3483
+ key k ;
3464
3484
};
3465
3485
3466
3486
/* *
@@ -3553,6 +3573,8 @@ namespace jwt {
3553
3573
return std::make_unique<algo<jwt::algorithm::es384>>(jwt::algorithm::es384 (key.get_pkey ()));
3554
3574
} else if (alg_name == " ES512" ) {
3555
3575
return std::make_unique<algo<jwt::algorithm::es512>>(jwt::algorithm::es512 (key.get_pkey ()));
3576
+ } else if (alg_name == " ES256K" ) {
3577
+ return std::make_unique<algo<jwt::algorithm::es256k>>(jwt::algorithm::es256k (key.get_pkey ()));
3556
3578
} else if (alg_name == " HS256" ) {
3557
3579
return std::make_unique<algo<jwt::algorithm::hs256>>(jwt::algorithm::hs256 (key.get_oct_key ()));
3558
3580
} else if (alg_name == " HS384" ) {
0 commit comments