Skip to content

Commit

Permalink
Add some more OpenSSL 1.1 APIs for folly/portability/
Browse files Browse the repository at this point in the history
Summary: refs hhvm/packaging#128

Reviewed By: anirudhvr

Differential Revision: D5380271

fbshipit-source-id: 1cc206d9970b843c3582556096f3ceabeb8d01f6
  • Loading branch information
fredemmott authored and facebook-github-bot committed Jul 10, 2017
1 parent d94e4de commit 37ce607
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 3 deletions.
107 changes: 104 additions & 3 deletions folly/portability/OpenSSL.cpp
Expand Up @@ -90,6 +90,20 @@ RSA* EVP_PKEY_get0_RSA(EVP_PKEY* pkey) {
return pkey->pkey.rsa;
}

DSA* EVP_PKEY_get0_DSA(EVP_PKEY* pkey) {
if (pkey->type != EVP_PKEY_DSA) {
return nullptr;
}
return pkey->pkey.dsa;
}

DH* EVP_PKEY_get0_DH(EVP_PKEY* pkey) {
if (pkey->type != EVP_PKEY_DH) {
return nullptr;
}
return pkey->pkey.dh;
}

EC_KEY* EVP_PKEY_get0_EC_KEY(EVP_PKEY* pkey) {
if (pkey->type != EVP_PKEY_EC) {
return nullptr;
Expand Down Expand Up @@ -163,6 +177,66 @@ int DH_set0_pqg(DH* dh, BIGNUM* p, BIGNUM* q, BIGNUM* g) {
return 1;
}

void DH_get0_pqg(
const DH* dh,
const BIGNUM** p,
const BIGNUM** q,
const BIGNUM** g) {
// Based off of https://wiki.openssl.org/index.php/OpenSSL_1.1.0_Changes
if (p != nullptr) {
*p = dh->p;
}
if (q != nullptr) {
*q = dh->q;
}
if (g != nullptr) {
*g = dh->g;
}
}

void DH_get0_key(
const DH* dh,
const BIGNUM** pub_key,
const BIGNUM** priv_key) {
// Based off of https://wiki.openssl.org/index.php/OpenSSL_1.1.0_Changes
if (pub_key != nullptr) {
*pub_key = dh->pub_key;
}
if (priv_key != nullptr) {
*priv_key = dh->priv_key;
}
}

void DSA_get0_pqg(
const DSA* dsa,
const BIGNUM** p,
const BIGNUM** q,
const BIGNUM** g) {
// Based off of https://wiki.openssl.org/index.php/OpenSSL_1.1.0_Changes
if (p != nullptr) {
*p = dsa->p;
}
if (q != nullptr) {
*q = dsa->q;
}
if (g != nullptr) {
*g = dsa->g;
}
}

void DSA_get0_key(
const DSA* dsa,
const BIGNUM** pub_key,
const BIGNUM** priv_key) {
// Based off of https://wiki.openssl.org/index.php/OpenSSL_1.1.0_Changes
if (pub_key != nullptr) {
*pub_key = dsa->pub_key;
}
if (priv_key != nullptr) {
*priv_key = dsa->priv_key;
}
}

X509* X509_STORE_CTX_get0_cert(X509_STORE_CTX* ctx) {
return ctx->cert;
}
Expand Down Expand Up @@ -210,9 +284,9 @@ void HMAC_CTX_free(HMAC_CTX* ctx) {
bool RSA_set0_key(RSA* r, BIGNUM* n, BIGNUM* e, BIGNUM* d) {
// Based off of https://wiki.openssl.org/index.php/OpenSSL_1.1.0_Changes
/**
* If the fields n and e in r are NULL, the corresponding input parameters
* MUST be non-NULL for n and e. d may be left NULL (in case only the public
* key is used).
* If the fields n and e in r are nullptr, the corresponding input parameters
* MUST be non-nullptr for n and e. d may be left NULL (in case only the
* public key is used).
*/
if ((r->n == nullptr && n == nullptr) || (r->e == nullptr && e == nullptr)) {
return false;
Expand All @@ -232,6 +306,33 @@ bool RSA_set0_key(RSA* r, BIGNUM* n, BIGNUM* e, BIGNUM* d) {
return true;
}

void RSA_get0_factors(const RSA* r, const BIGNUM** p, const BIGNUM** q) {
// Based off of https://wiki.openssl.org/index.php/OpenSSL_1.1.0_Changes
if (p != nullptr) {
*p = r->p;
}
if (q != nullptr) {
*q = r->q;
}
}

void RSA_get0_crt_params(
const RSA* r,
const BIGNUM** dmp1,
const BIGNUM** dmq1,
const BIGNUM** iqmp) {
// Based off of https://wiki.openssl.org/index.php/OpenSSL_1.1.0_Changes
if (dmp1 != nullptr) {
*dmp1 = r->dmp1;
}
if (dmq1 != nullptr) {
*dmq1 = r->dmq1;
}
if (iqmp != nullptr) {
*iqmp = r->iqmp;
}
}

#endif
}
}
Expand Down
24 changes: 24 additions & 0 deletions folly/portability/OpenSSL.h
Expand Up @@ -115,6 +115,8 @@ void RSA_get0_key(
const BIGNUM** e,
const BIGNUM** d);
RSA* EVP_PKEY_get0_RSA(EVP_PKEY* pkey);
DSA* EVP_PKEY_get0_DSA(EVP_PKEY* pkey);
DH* EVP_PKEY_get0_DH(EVP_PKEY* pkey);
EC_KEY* EVP_PKEY_get0_EC_KEY(EVP_PKEY* pkey);
#endif

Expand All @@ -135,11 +137,33 @@ void HMAC_CTX_free(HMAC_CTX* ctx);
unsigned long SSL_SESSION_get_ticket_lifetime_hint(const SSL_SESSION* s);
int SSL_SESSION_has_ticket(const SSL_SESSION* s);
int DH_set0_pqg(DH* dh, BIGNUM* p, BIGNUM* q, BIGNUM* g);
void DH_get0_pqg(
const DH* dh,
const BIGNUM** p,
const BIGNUM** q,
const BIGNUM** g);
void DH_get0_key(const DH* dh, const BIGNUM** pub_key, const BIGNUM** priv_key);

void DSA_get0_pqg(
const DSA* dsa,
const BIGNUM** p,
const BIGNUM** q,
const BIGNUM** g);
void DSA_get0_key(
const DSA* dsa,
const BIGNUM** pub_key,
const BIGNUM** priv_key);

X509* X509_STORE_CTX_get0_cert(X509_STORE_CTX* ctx);
STACK_OF(X509) * X509_STORE_CTX_get0_chain(X509_STORE_CTX* ctx);
STACK_OF(X509) * X509_STORE_CTX_get0_untrusted(X509_STORE_CTX* ctx);
bool RSA_set0_key(RSA* r, BIGNUM* n, BIGNUM* e, BIGNUM* d);
void RSA_get0_factors(const RSA* r, const BIGNUM** p, const BIGNUM** q);
void RSA_get0_crt_params(
const RSA* r,
const BIGNUM** dmp1,
const BIGNUM** dmq1,
const BIGNUM** iqmp);
#endif

#if FOLLY_OPENSSL_IS_110
Expand Down

0 comments on commit 37ce607

Please sign in to comment.