Skip to content
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

vapid_import_private_key - add the atual functionality #40

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

nissimeinat
Copy link

current vapid_import_private_key function just return NULL.
adding import form PEM format to EC_KEY*

@codecov-io
Copy link

codecov-io commented Feb 24, 2019

Codecov Report

Merging #40 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master      #40   +/-   ##
=======================================
  Coverage   86.56%   86.56%           
=======================================
  Files          14       14           
  Lines        1407     1407           
=======================================
  Hits         1218     1218           
  Misses        189      189

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9c51ad6...3a16527. Read the comment docs.

@Lakr233
Copy link

Lakr233 commented Aug 11, 2023

here is my approach

static EC_KEY*
vapid_import_private_key(const char* privateKey) {
    if (!privateKey) { return NULL; }
    size_t rawPrivKeyLen = ece_base64url_decode(privateKey,
                                                strlen(privateKey),
                                                ECE_BASE64URL_REJECT_PADDING,
                                                NULL,
                                                0);
    if (!rawPrivKeyLen) { return NULL; }
    
    uint8_t* rawPrivKey = malloc(rawPrivKeyLen);
    if (!rawPrivKey) { return NULL; }
    
    if (ece_base64url_decode(privateKey, strlen(privateKey), ECE_BASE64URL_REJECT_PADDING,
                             rawPrivKey, rawPrivKeyLen) != rawPrivKeyLen) {
        free(rawPrivKey);
        return NULL;
    }
    
    EC_KEY* key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
    if (!key || !EC_KEY_oct2priv(key, rawPrivKey, rawPrivKeyLen)) {
        EC_KEY_free(key);
        free(rawPrivKey);
        return NULL;
    }
    
    free(rawPrivKey);
    return key;
}

@aolo2
Copy link

aolo2 commented Sep 4, 2023

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;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants