@@ -502,35 +502,33 @@ namespace jwt {
502
502
ec.clear ();
503
503
auto certbio = make_mem_buf_bio (certstr);
504
504
auto keybio = make_mem_buf_bio ();
505
- std::string res;
506
505
if (!certbio || !keybio) {
507
506
ec = error::rsa_error::create_mem_bio_failed;
508
- return res ;
507
+ return {} ;
509
508
}
510
509
511
510
std::unique_ptr<X509, decltype (&X509_free)> cert (
512
511
PEM_read_bio_X509 (certbio.get (), nullptr , nullptr , const_cast <char *>(pw.c_str ())), X509_free);
513
512
if (!cert) {
514
513
ec = error::rsa_error::cert_load_failed;
515
- return res ;
514
+ return {} ;
516
515
}
517
516
std::unique_ptr<EVP_PKEY, decltype (&EVP_PKEY_free)> key (X509_get_pubkey (cert.get ()), EVP_PKEY_free);
518
517
if (!key) {
519
518
ec = error::rsa_error::get_key_failed;
520
- return res ;
519
+ return {} ;
521
520
}
522
521
if (PEM_write_bio_PUBKEY (keybio.get (), key.get ()) == 0 ) {
523
522
ec = error::rsa_error::write_key_failed;
524
- return res ;
523
+ return {} ;
525
524
}
526
525
char * ptr = nullptr ;
527
526
auto len = BIO_get_mem_data (keybio.get (), &ptr);
528
527
if (len <= 0 || ptr == nullptr ) {
529
528
ec = error::rsa_error::convert_to_pem_failed;
530
- return res ;
529
+ return {} ;
531
530
}
532
- res.assign (ptr, static_cast <size_t >(len));
533
- return res;
531
+ return {ptr, static_cast <size_t >(len)};
534
532
}
535
533
536
534
/* *
@@ -562,25 +560,23 @@ namespace jwt {
562
560
d2i_X509 (NULL , &c_str, static_cast <int >(cert_der_str.size ())), X509_free);
563
561
auto certbio = make_mem_buf_bio ();
564
562
565
- std::string res;
566
563
if (!cert || !certbio) {
567
564
ec = error::rsa_error::create_mem_bio_failed;
568
- return res ;
565
+ return {} ;
569
566
}
570
567
571
568
if (!PEM_write_bio_X509 (certbio.get (), cert.get ())) {
572
569
ec = error::rsa_error::write_cert_failed;
573
- return res ;
570
+ return {} ;
574
571
}
575
572
576
573
char * ptr = nullptr ;
577
574
const auto len = BIO_get_mem_data (certbio.get (), &ptr);
578
575
if (len <= 0 || ptr == nullptr ) {
579
576
ec = error::rsa_error::convert_to_pem_failed;
580
- return res ;
577
+ return {} ;
581
578
}
582
- res.assign (ptr, static_cast <size_t >(len));
583
- return res;
579
+ return {ptr, static_cast <size_t >(len)};
584
580
}
585
581
586
582
/* *
@@ -877,7 +873,7 @@ namespace jwt {
877
873
#endif
878
874
{
879
875
std::string res (BN_num_bytes (bn), ' \0 ' );
880
- BN_bn2bin (bn, ( unsigned char *)res. data ( )); // NOLINT(google-readability-casting) requires `const_cast`
876
+ BN_bn2bin (bn, reinterpret_cast < unsigned char *>(&res[ 0 ] ));
881
877
return res;
882
878
}
883
879
/* *
@@ -943,7 +939,8 @@ namespace jwt {
943
939
* \param md Pointer to hash function
944
940
* \param name Name of the algorithm
945
941
*/
946
- hmacsha (string_view key, const EVP_MD* (*md)(), string_view name) : secret(key), md(md), alg_name(name) {}
942
+ hmacsha (string_view key, const EVP_MD* (*md)(), std::string name)
943
+ : secret(key), md(md), alg_name(std::move(name)) {}
947
944
/* *
948
945
* Sign jwt data
949
946
* \param data The data to sign
@@ -956,8 +953,7 @@ namespace jwt {
956
953
auto len = static_cast <unsigned int >(res.size ());
957
954
if (HMAC (md (), secret.data (), static_cast <int >(secret.size ()),
958
955
reinterpret_cast <const unsigned char *>(data.data ()), static_cast <int >(data.size ()),
959
- (unsigned char *)res.data (), // NOLINT(google-readability-casting) requires `const_cast`
960
- &len) == nullptr ) {
956
+ reinterpret_cast <unsigned char *>(&res[0 ]), &len) == nullptr ) {
961
957
ec = error::signature_generation_error::hmac_failed;
962
958
res.clear ();
963
959
return res;
@@ -1013,8 +1009,8 @@ namespace jwt {
1013
1009
* \param name Name of the algorithm
1014
1010
*/
1015
1011
rsa (string_view public_key, string_view private_key, const std::string& public_key_password,
1016
- const std::string& private_key_password, const EVP_MD* (*md)(), string_view name)
1017
- : md(md), alg_name(name) {
1012
+ const std::string& private_key_password, const EVP_MD* (*md)(), std::string name)
1013
+ : md(md), alg_name(std::move( name) ) {
1018
1014
if (!private_key.empty ()) {
1019
1015
pkey = helper::load_private_key_from_string (private_key, private_key_password);
1020
1016
} else if (!public_key.empty ()) {
@@ -1031,22 +1027,21 @@ namespace jwt {
1031
1027
std::string sign (string_view data, std::error_code& ec) const {
1032
1028
ec.clear ();
1033
1029
auto ctx = helper::make_evp_md_ctx ();
1034
- std::string res;
1035
1030
if (!ctx) {
1036
1031
ec = error::signature_generation_error::create_context_failed;
1037
- return res ;
1032
+ return {} ;
1038
1033
}
1039
1034
if (!EVP_SignInit (ctx.get (), md ())) {
1040
1035
ec = error::signature_generation_error::signinit_failed;
1041
- return res ;
1036
+ return {} ;
1042
1037
}
1043
1038
if (!EVP_SignUpdate (ctx.get (), data.data (), data.size ())) {
1044
1039
ec = error::signature_generation_error::signupdate_failed;
1045
- return res ;
1040
+ return {} ;
1046
1041
}
1047
- res. assign (EVP_PKEY_size (pkey.get ()), ' \0 ' );
1042
+ std::string res (EVP_PKEY_size (pkey.get ()), ' \0 ' );
1048
1043
unsigned int len = 0 ;
1049
- if (EVP_SignFinal (ctx.get (), ( unsigned char *)res. data ( ), &len, pkey.get ()) == 0 ) {
1044
+ if (EVP_SignFinal (ctx.get (), reinterpret_cast < unsigned char *>(&res[ 0 ] ), &len, pkey.get ()) == 0 ) {
1050
1045
ec = error::signature_generation_error::signfinal_failed;
1051
1046
res.clear ();
1052
1047
return res;
@@ -1113,8 +1108,8 @@ namespace jwt {
1113
1108
* \param siglen The bit length of the signature
1114
1109
*/
1115
1110
ecdsa (string_view public_key, string_view private_key, const std::string& public_key_password,
1116
- const std::string& private_key_password, const EVP_MD* (*md)(), string_view name, size_t siglen)
1117
- : md(md), alg_name(name), signature_length(siglen) {
1111
+ const std::string& private_key_password, const EVP_MD* (*md)(), std::string name, size_t siglen)
1112
+ : md(md), alg_name(std::move( name) ), signature_length(siglen) {
1118
1113
if (!private_key.empty ()) {
1119
1114
pkey = helper::load_private_ec_key_from_string (private_key, private_key_password);
1120
1115
check_private_key (pkey.get ());
@@ -1140,27 +1135,26 @@ namespace jwt {
1140
1135
std::string sign (string_view data, std::error_code& ec) const {
1141
1136
ec.clear ();
1142
1137
auto ctx = helper::make_evp_md_ctx ();
1143
- std::string res;
1144
1138
if (!ctx) {
1145
1139
ec = error::signature_generation_error::create_context_failed;
1146
- return res ;
1140
+ return {} ;
1147
1141
}
1148
1142
if (!EVP_DigestSignInit (ctx.get (), nullptr , md (), nullptr , pkey.get ())) {
1149
1143
ec = error::signature_generation_error::signinit_failed;
1150
- return res ;
1144
+ return {} ;
1151
1145
}
1152
1146
if (!EVP_DigestUpdate (ctx.get (), data.data (), data.size ())) {
1153
1147
ec = error::signature_generation_error::digestupdate_failed;
1154
- return res ;
1148
+ return {} ;
1155
1149
}
1156
1150
1157
1151
size_t len = 0 ;
1158
1152
if (!EVP_DigestSignFinal (ctx.get (), nullptr , &len)) {
1159
1153
ec = error::signature_generation_error::signfinal_failed;
1160
- return res ;
1154
+ return {} ;
1161
1155
}
1162
- res. assign (len, ' \0 ' );
1163
- if (!EVP_DigestSignFinal (ctx.get (), ( unsigned char *)res. data ( ), &len)) {
1156
+ std::string res (len, ' \0 ' );
1157
+ if (!EVP_DigestSignFinal (ctx.get (), reinterpret_cast < unsigned char *>(&res[ 0 ] ), &len)) {
1164
1158
ec = error::signature_generation_error::signfinal_failed;
1165
1159
res.clear ();
1166
1160
return res;
@@ -1346,8 +1340,8 @@ namespace jwt {
1346
1340
* \param name Name of the algorithm
1347
1341
*/
1348
1342
eddsa (string_view public_key, string_view private_key, const std::string& public_key_password,
1349
- const std::string& private_key_password, string_view name)
1350
- : alg_name(name) {
1343
+ const std::string& private_key_password, std::string name)
1344
+ : alg_name(std::move( name) ) {
1351
1345
if (!private_key.empty ()) {
1352
1346
pkey = helper::load_private_key_from_string (private_key, private_key_password);
1353
1347
} else if (!public_key.empty ()) {
@@ -1364,18 +1358,17 @@ namespace jwt {
1364
1358
std::string sign (string_view data, std::error_code& ec) const {
1365
1359
ec.clear ();
1366
1360
auto ctx = helper::make_evp_md_ctx ();
1367
- std::string res;
1368
1361
if (!ctx) {
1369
1362
ec = error::signature_generation_error::create_context_failed;
1370
- return res ;
1363
+ return {} ;
1371
1364
}
1372
1365
if (!EVP_DigestSignInit (ctx.get (), nullptr , nullptr , nullptr , pkey.get ())) {
1373
1366
ec = error::signature_generation_error::signinit_failed;
1374
- return res ;
1367
+ return {} ;
1375
1368
}
1376
1369
1377
1370
size_t len = EVP_PKEY_size (pkey.get ());
1378
- res. assign (len, ' \0 ' );
1371
+ std::string res (len, ' \0 ' );
1379
1372
1380
1373
// LibreSSL is the special kid in the block, as it does not support EVP_DigestSign.
1381
1374
// OpenSSL on the otherhand does not support using EVP_DigestSignUpdate for eddsa, which is why we end up with this
@@ -1475,8 +1468,8 @@ namespace jwt {
1475
1468
* \param name Name of the algorithm
1476
1469
*/
1477
1470
pss (string_view public_key, string_view private_key, const std::string& public_key_password,
1478
- const std::string& private_key_password, const EVP_MD* (*md)(), string_view name)
1479
- : md(md), alg_name(name) {
1471
+ const std::string& private_key_password, const EVP_MD* (*md)(), std::string name)
1472
+ : md(md), alg_name(std::move( name) ) {
1480
1473
if (!private_key.empty ()) {
1481
1474
pkey = helper::load_private_key_from_string (private_key, private_key_password);
1482
1475
} else if (!public_key.empty ()) {
@@ -1494,39 +1487,35 @@ namespace jwt {
1494
1487
std::string sign (string_view data, std::error_code& ec) const {
1495
1488
ec.clear ();
1496
1489
auto md_ctx = helper::make_evp_md_ctx ();
1497
- std::string res;
1498
1490
if (!md_ctx) {
1499
1491
ec = error::signature_generation_error::create_context_failed;
1500
- return res ;
1492
+ return {} ;
1501
1493
}
1502
1494
EVP_PKEY_CTX* ctx = nullptr ;
1503
1495
if (EVP_DigestSignInit (md_ctx.get (), &ctx, md (), nullptr , pkey.get ()) != 1 ) {
1504
1496
ec = error::signature_generation_error::signinit_failed;
1505
- return res ;
1497
+ return {} ;
1506
1498
}
1507
1499
if (EVP_PKEY_CTX_set_rsa_padding (ctx, RSA_PKCS1_PSS_PADDING) <= 0 ) {
1508
1500
ec = error::signature_generation_error::rsa_padding_failed;
1509
- return res ;
1501
+ return {} ;
1510
1502
}
1511
1503
// wolfSSL does not require EVP_PKEY_CTX_set_rsa_pss_saltlen. The default behavior
1512
1504
// sets the salt length to the hash length. Unlike OpenSSL which exposes this functionality.
1513
1505
#ifndef LIBWOLFSSL_VERSION_HEX
1514
1506
if (EVP_PKEY_CTX_set_rsa_pss_saltlen (ctx, -1 ) <= 0 ) {
1515
1507
ec = error::signature_generation_error::set_rsa_pss_saltlen_failed;
1516
- return res ;
1508
+ return {} ;
1517
1509
}
1518
1510
#endif
1519
1511
if (EVP_DigestUpdate (md_ctx.get (), data.data (), data.size ()) != 1 ) {
1520
1512
ec = error::signature_generation_error::digestupdate_failed;
1521
- return res ;
1513
+ return {} ;
1522
1514
}
1523
1515
1524
1516
size_t size = EVP_PKEY_size (pkey.get ());
1525
- res.assign (size, 0x00 );
1526
- if (EVP_DigestSignFinal (
1527
- md_ctx.get (),
1528
- (unsigned char *)res.data (), // NOLINT(google-readability-casting) requires `const_cast`
1529
- &size) <= 0 ) {
1517
+ std::string res (size, ' \0 ' );
1518
+ if (EVP_DigestSignFinal (md_ctx.get (), reinterpret_cast <unsigned char *>(&res[0 ]), &size) <= 0 ) {
1530
1519
ec = error::signature_generation_error::signfinal_failed;
1531
1520
res.clear ();
1532
1521
return res;
0 commit comments