vapid_import_private_key - add the atual functionality#40
Open
nissimeinat wants to merge 3 commits intoweb-push-libs:masterfrom
Open
vapid_import_private_key - add the atual functionality#40nissimeinat wants to merge 3 commits intoweb-push-libs:masterfrom
nissimeinat wants to merge 3 commits intoweb-push-libs:masterfrom
Conversation
Codecov Report
@@ Coverage Diff @@
## master #40 +/- ##
=======================================
Coverage 86.56% 86.56%
=======================================
Files 14 14
Lines 1407 1407
=======================================
Hits 1218 1218
Misses 189 189Continue to review full report at Codecov.
|
|
here is my approach |
|
If you have the same usecase as me (generating VAPID tokens for different push services using the same key), then you probably want a function that sets both the private and public keys, not just the private. static EC_KEY*
vapid_import_public_and_private_from_private_key(const char* privateKeyB64Url) {
if (!privateKeyB64Url) { return NULL; }
size_t rawPrivKeyLen = ece_base64url_decode(privateKeyB64Url,
strlen(privateKeyB64Url),
ECE_BASE64URL_REJECT_PADDING,
NULL,
0);
if (!rawPrivKeyLen) { return NULL; }
uint8_t* rawPrivKey = malloc(rawPrivKeyLen);
if (!rawPrivKey) { return NULL; }
if (ece_base64url_decode(privateKeyB64Url, strlen(privateKeyB64Url), ECE_BASE64URL_REJECT_PADDING,
rawPrivKey, rawPrivKeyLen) != rawPrivKeyLen) {
free(rawPrivKey);
return NULL;
}
EC_KEY* key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
// Set private key
if (!key || !EC_KEY_oct2priv(key, rawPrivKey, rawPrivKeyLen)) {
EC_KEY_free(key);
free(rawPrivKey);
return NULL;
}
// Set public key
BN_CTX* ctx = BN_CTX_new();
const BIGNUM *private_key_bn = EC_KEY_get0_private_key(key);
const EC_GROUP *group = EC_KEY_get0_group(key);
EC_POINT *pubkey_point = EC_POINT_new(group);
EC_POINT_mul(group, pubkey_point, private_key_bn, NULL, NULL, ctx);
BN_CTX_free(ctx);
EC_KEY_set_public_key(key, pubkey_point);
free(rawPrivKey);
return key;
} |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
current vapid_import_private_key function just return NULL.
adding import form PEM format to EC_KEY*