How to generate pem with openssl

0 votes

I want to generate pem with openssl in C.

I'm compiling with gcc key_utils.c tests.c -o key_tests -lcrypto -lssl -Wall

My code is as follows:

#include <stdio.h>

#include <stdlib.h>

#include <stdint.h>

#include <string.h>

#include <openssl/ec.h>

#include <openssl/bn.h>

#include <openssl/sha.h>

#include <openssl/ripemd.h>

#include <openssl/ecdsa.h>

#include <openssl/pem.h>

int generatePem(char **pem) {


    char *pemholder = calloc(224, sizeof(char));

    EC_KEY *eckey = NULL;


    BIO *out = BIO_new(BIO_s_mem());

    BUF_MEM *buf = NULL;

    EC_GROUP *group = NULL;


    group = EC_GROUP_new_by_curve_name(NID_secp256k1);

    buf = BUF_MEM_new();

    eckey = EC_KEY_new();


    createNewKey(group, eckey);


    EC_GROUP_clear_free(group);


    PEM_write_bio_ECPrivateKey(out, eckey, NULL, NULL, 0, NULL, NULL);


    BIO_get_mem_ptr(out, &buf);


    memcpy(pemholder, buf->data, 223);


    if ( buf->data[219] == '\n') {

        pemholder[220] = '\0';      

        memcpy(*pem, pemholder, 221);

    } else if ( buf->data[221] == '\n') {

        pemholder[222] = '\0';      

        memcpy(*pem, pemholder, 223);

    } else {

        pemholder[223] = '\0';

        memcpy(*pem, pemholder, 224);

    }


    free(pemholder);

    EC_KEY_free(eckey);

    BIO_free_all(out);


    return NOERROR;

};

The bad pem looks like so:

-----BEGIN EC PRIVATE KEY-----

MHMCAQEEH8kO/hjEyM2hvQk//LSsp1xRBIYNDRjAi4b8N78odyCgBwYFK4EEAAqh

RANCAAQ43017I40ci8YMLJnguD/DHUjohY4blKoJ4lXYbgYqyjWvJfVnsNPMU8H9

o3IdPwAitnJjCOG11n9DIQoS3S/o

-----END EC PRIVATE KEY-----

6T
Aug 23, 2018 in Blockchain by slayer
• 29,350 points
865 views

1 answer to this question.

0 votes

Here’s the working code:

int generatePem(char **pem) {

    int returnError = NOERROR;

    char * errorMessage = "";

    char *pemholder = calloc(240, sizeof(char));

    EC_KEY *eckey = NULL;

    BIO *out = NULL;


    if ((out = BIO_new(BIO_s_mem())) == NULL) {

        returnError = ERROR;

        errorMessage = "Error in BIO_new(BIO_s_mem())";

        goto clearVariables;

    }


    BUF_MEM *buf = NULL;

    EC_GROUP *group = NULL;


    if ((group = EC_GROUP_new_by_curve_name(NID_secp256k1)) == NULL) {

        returnError = ERROR;

        errorMessage = "Error in EC_GROUP_new_by_curve_name(NID_secp256k1))";

        goto clearVariables;

    }


    if (((eckey = EC_KEY_new()) == NULL) ||

        ((buf = BUF_MEM_new()) == NULL)) {

        returnError = ERROR;

        errorMessage = "Error in EC_KEY_new())";

        goto clearVariables;

    };


    if (createNewKey(group, eckey) == ERROR) {

        returnError = ERROR;

        errorMessage = "createNewKey(group, eckey)";

        goto clearVariables;

    }


    if (PEM_write_bio_ECPrivateKey(out, eckey, NULL, NULL, 0, NULL, NULL) == 0) {

        printf("PEM_write_bio_ECPrivateKey error");

    }


    BIO_get_mem_ptr(out, &buf);


    memcpy(pemholder, buf->data, 224);

    if (buf->data[218] == '\n') {

        pemholder[219] = '\0';

        memcpy(*pem, pemholder, 220);

    } else if ( buf->data[219] == '\n') {

        pemholder[220] = '\0';      

        memcpy(*pem, pemholder, 221);

    } else if ( buf->data[221] == '\n') {

        pemholder[222] = '\0';      

        memcpy(*pem, pemholder, 223);

    } else if (buf->data[222] == '\n') {

        pemholder[223] = '\0';

        memcpy(*pem, pemholder, 224);

    } else {

        returnError = ERROR;

        errorMessage = "invalid PEM generated";

        goto clearVariables;

    }


    goto clearVariables;


clearVariables:

    if (group != NULL)

        EC_GROUP_clear_free(group);

    if (pemholder != NULL)

        free(pemholder);

    if (eckey != NULL)

        EC_KEY_free(eckey);

    if (out != NULL)

        BIO_free_all(out);

    if (errorMessage[0] != '\0')

        printf("Error: %s\n", errorMessage);


    return returnError;

};


static int createNewKey(EC_GROUP *group, EC_KEY *eckey) {


    int asn1Flag = OPENSSL_EC_NAMED_CURVE;

    int form = POINT_CONVERSION_UNCOMPRESSED;


    EC_GROUP_set_asn1_flag(group, asn1Flag);

    EC_GROUP_set_point_conversion_form(group, form);

    int setGroupError = EC_KEY_set_group(eckey, group);


    int resultFromKeyGen = EC_KEY_generate_key(eckey);


    if (resultFromKeyGen != 1 || setGroupError != 1){

        return ERROR;

    }


    return NOERROR;

}
answered Aug 23, 2018 by digger
• 26,740 points

Related Questions In Blockchain

0 votes
2 answers

How to generate ethereum qr code address with amount?

You implement this package: ethereum-qr-code. It implements the EIP67 standard which uses ...READ MORE

answered Sep 6, 2018 in Blockchain by Omkar
• 69,210 points
4,640 views
0 votes
1 answer
0 votes
1 answer

How to achieve consensus with peers from different departments?

Defining one MSP to represent each division. ...READ MORE

answered Jul 16, 2018 in Blockchain by digger
• 26,740 points
456 views
0 votes
1 answer

How to send Bitcoins with node.js?

This website https://blockr.io/tx/push will successfully do the ...READ MORE

answered Jul 20, 2018 in Blockchain by Christine
• 15,790 points
3,127 views
+1 vote
1 answer

Protocols used in a distributed/dlt system for the nodes to establish communication

yes all are over TCP/IP connections secured ...READ MORE

answered Aug 6, 2018 in Blockchain by aryya
• 7,450 points
1,129 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,663 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,215 views
0 votes
1 answer

How to interact with blockchain using Java web app?

You can interact with the blockchain using ...READ MORE

answered Jul 16, 2018 in Blockchain by digger
• 26,740 points
907 views
0 votes
1 answer

How to generate new address for blockchain wallet?

You can use the following to generate ...READ MORE

answered Jul 16, 2018 in Blockchain by digger
• 26,740 points
3,052 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP