-
Notifications
You must be signed in to change notification settings - Fork 29.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TLS certificate object documentation and support for EC certificates #24358
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,6 +52,15 @@ static const int X509_NAME_FLAGS = ASN1_STRFLGS_ESC_CTRL | |
| XN_FLAG_FN_SN; | ||
|
||
namespace node { | ||
namespace Buffer { | ||
// OpenSSL uses `unsigned char*` for raw data, make this easier for us. | ||
v8::MaybeLocal<v8::Object> New(Environment* env, unsigned char* udata, | ||
size_t length) { | ||
char* data = reinterpret_cast<char*>(udata); | ||
return Buffer::New(env, data, length); | ||
} | ||
} // namespace Buffer | ||
|
||
namespace crypto { | ||
|
||
using v8::Array; | ||
|
@@ -1652,8 +1661,17 @@ static Local<Object> X509ToObject(Environment* env, X509* cert) { | |
|
||
EVPKeyPointer pkey(X509_get_pubkey(cert)); | ||
RSAPointer rsa; | ||
if (pkey) | ||
rsa.reset(EVP_PKEY_get1_RSA(pkey.get())); | ||
ECPointer ec; | ||
if (pkey) { | ||
switch (EVP_PKEY_id(pkey.get())) { | ||
case EVP_PKEY_RSA: | ||
rsa.reset(EVP_PKEY_get1_RSA(pkey.get())); | ||
break; | ||
case EVP_PKEY_EC: | ||
ec.reset(EVP_PKEY_get1_EC_KEY(pkey.get())); | ||
break; | ||
} | ||
} | ||
|
||
if (rsa) { | ||
const BIGNUM* n; | ||
|
@@ -1667,6 +1685,10 @@ static Local<Object> X509ToObject(Environment* env, X509* cert) { | |
mem->length).ToLocalChecked()).FromJust(); | ||
USE(BIO_reset(bio.get())); | ||
|
||
int bits = BN_num_bits(n); | ||
info->Set(context, env->bits_string(), | ||
Integer::New(env->isolate(), bits)).FromJust(); | ||
|
||
uint64_t exponent_word = static_cast<uint64_t>(BN_get_word(e)); | ||
uint32_t lo = static_cast<uint32_t>(exponent_word); | ||
uint32_t hi = static_cast<uint32_t>(exponent_word >> 32); | ||
|
@@ -1689,10 +1711,53 @@ static Local<Object> X509ToObject(Environment* env, X509* cert) { | |
reinterpret_cast<unsigned char*>(Buffer::Data(pubbuff)); | ||
i2d_RSA_PUBKEY(rsa.get(), &pubserialized); | ||
info->Set(env->context(), env->pubkey_string(), pubbuff).FromJust(); | ||
} else if (ec) { | ||
const EC_GROUP* group = EC_KEY_get0_group(ec.get()); | ||
if (group != nullptr) { | ||
int bits = EC_GROUP_order_bits(group); | ||
if (bits > 0) { | ||
info->Set(context, env->bits_string(), | ||
Integer::New(env->isolate(), bits)).FromJust(); | ||
} | ||
} | ||
|
||
unsigned char* pub = nullptr; | ||
size_t publen = EC_KEY_key2buf(ec.get(), EC_KEY_get_conv_form(ec.get()), | ||
&pub, nullptr); | ||
if (publen > 0) { | ||
Local<Object> buf = Buffer::New(env, pub, publen).ToLocalChecked(); | ||
// Ownership of pub pointer accepted by Buffer. | ||
pub = nullptr; | ||
info->Set(context, env->pubkey_string(), buf).FromJust(); | ||
} else { | ||
CHECK_NULL(pub); | ||
} | ||
|
||
if (EC_GROUP_get_asn1_flag(group) != 0) { | ||
// Curve is well-known, get its OID and NIST nick-name (if it has one). | ||
|
||
int nid = EC_GROUP_get_curve_name(group); | ||
if (nid != 0) { | ||
if (const char* sn = OBJ_nid2sn(nid)) { | ||
info->Set(context, env->asn1curve_string(), | ||
OneByteString(env->isolate(), sn)).FromJust(); | ||
} | ||
} | ||
if (nid != 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Merge these two conditionals? |
||
if (const char* nist = EC_curve_nid2nist(nid)) { | ||
info->Set(context, env->nistcurve_string(), | ||
OneByteString(env->isolate(), nist)).FromJust(); | ||
} | ||
} | ||
} else { | ||
// Unnamed curves can be described by their mathematical properties, | ||
// but aren't used much (at all?) with X.509/TLS. Support later if needed. | ||
} | ||
} | ||
|
||
pkey.reset(); | ||
rsa.reset(); | ||
ec.reset(); | ||
|
||
ASN1_TIME_print(bio.get(), X509_get_notBefore(cert)); | ||
BIO_get_mem_ptr(bio.get(), &mem); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this check for
asn1_flag
is unnecessary? It should be enough to just checknid != 0
—if the curve has a name,nid
will be non-zero, and if it doesn't,nid
will be zero. No need to also check theasn1_flag
, right?(The reason I'm asking is because
EC_GROUP_get_asn1_flag
doesn't exist in BoringSSL and I'm working on BoringSSL integration downstream in Electron.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code was directly copied from
node/deps/openssl/openssl/crypto/ec/eck_prn.c
Lines 97 to 119 in 9dfbc39
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see: #25345