Skip to content

Commit c2b43e6

Browse files
Added convenience function for binary DER to PEM (#283)
1 parent 73f2341 commit c2b43e6

File tree

2 files changed

+52
-17
lines changed

2 files changed

+52
-17
lines changed

include/jwt-cpp/jwt.h

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -543,28 +543,18 @@ namespace jwt {
543543
}
544544

545545
/**
546-
* \brief Convert the certificate provided as base64 DER to PEM.
547-
*
548-
* This is useful when using with JWKs as x5c claim is encoded as base64 DER. More info
549-
* (here)[https://tools.ietf.org/html/rfc7517#section-4.7]
546+
* \brief Convert the certificate provided as DER to PEM.
550547
*
551-
* \tparam Decode is callabled, taking a string_type and returns a string_type.
552-
* It should ensure the padding of the input and then base64 decode and return
553-
* the results.
554-
*
555-
* \param cert_base64_der_str String containing the certificate encoded as base64 DER
556-
* \param decode The function to decode the cert
557-
* \param ec error_code for error_detection (gets cleared if no error occures)
548+
* \param cert_der_str String containing the certificate encoded as base64 DER
549+
* \param ec error_code for error_detection (gets cleared if no error occures)
558550
*/
559-
template<typename Decode>
560-
std::string convert_base64_der_to_pem(const std::string& cert_base64_der_str, Decode decode,
561-
std::error_code& ec) {
551+
inline std::string convert_der_to_pem(const std::string& cert_der_str, std::error_code& ec) {
562552
ec.clear();
563-
const auto decodedStr = decode(cert_base64_der_str);
564-
auto c_str = reinterpret_cast<const unsigned char*>(decodedStr.c_str());
553+
554+
auto c_str = reinterpret_cast<const unsigned char*>(cert_der_str.c_str());
565555

566556
std::unique_ptr<X509, decltype(&X509_free)> cert(
567-
d2i_X509(NULL, &c_str, static_cast<int>(decodedStr.size())), X509_free);
557+
d2i_X509(NULL, &c_str, static_cast<int>(cert_der_str.size())), X509_free);
568558
auto certbio = make_mem_buf_bio();
569559
if (!cert || !certbio) {
570560
ec = error::rsa_error::create_mem_bio_failed;
@@ -586,6 +576,28 @@ namespace jwt {
586576
return {ptr, static_cast<size_t>(len)};
587577
}
588578

579+
/**
580+
* \brief Convert the certificate provided as base64 DER to PEM.
581+
*
582+
* This is useful when using with JWKs as x5c claim is encoded as base64 DER. More info
583+
* (here)[https://tools.ietf.org/html/rfc7517#section-4.7]
584+
*
585+
* \tparam Decode is callabled, taking a string_type and returns a string_type.
586+
* It should ensure the padding of the input and then base64 decode and return
587+
* the results.
588+
*
589+
* \param cert_base64_der_str String containing the certificate encoded as base64 DER
590+
* \param decode The function to decode the cert
591+
* \param ec error_code for error_detection (gets cleared if no error occures)
592+
*/
593+
template<typename Decode>
594+
std::string convert_base64_der_to_pem(const std::string& cert_base64_der_str, Decode decode,
595+
std::error_code& ec) {
596+
ec.clear();
597+
const auto decoded_str = decode(cert_base64_der_str);
598+
return convert_der_to_pem(decoded_str, ec);
599+
}
600+
589601
/**
590602
* \brief Convert the certificate provided as base64 DER to PEM.
591603
*
@@ -607,6 +619,21 @@ namespace jwt {
607619
error::throw_if_error(ec);
608620
return res;
609621
}
622+
623+
/**
624+
* \brief Convert the certificate provided as DER to PEM.
625+
*
626+
* \param cert_der_str String containing the DER certificate
627+
* \param decode The function to decode the cert
628+
* \throw rsa_exception if an error occurred
629+
*/
630+
inline std::string convert_der_to_pem(const std::string& cert_der_str) {
631+
std::error_code ec;
632+
auto res = convert_der_to_pem(cert_der_str, ec);
633+
error::throw_if_error(ec);
634+
return res;
635+
}
636+
610637
#ifndef JWT_DISABLE_BASE64
611638
/**
612639
* \brief Convert the certificate provided as base64 DER to PEM.

tests/HelperTest.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ TEST(HelperTest, Base64DER2PemCert) {
1717
ASSERT_EQ(google_cert, cert_pem);
1818
}
1919

20+
TEST(HelperTest, DER2PemCert) {
21+
auto decoded = jwt::base::decode<jwt::alphabet::base64>(
22+
jwt::base::pad<jwt::alphabet::base64>(google_cert_base64_der)
23+
);
24+
auto cert_pem = jwt::helper::convert_der_to_pem(decoded);
25+
ASSERT_EQ(google_cert, cert_pem);
26+
}
27+
2028
TEST(HelperTest, ErrorCodeMessages) {
2129
ASSERT_EQ(std::error_code(jwt::error::rsa_error::ok).message(), "no error");
2230
ASSERT_EQ(std::error_code(static_cast<jwt::error::rsa_error>(-1)).message(), "unknown RSA error");

0 commit comments

Comments
 (0)