Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions rsa/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
all:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indent -kr -4 -nut

gcc genrsa.c -L/opt/openssl/lib -lcrypto -o genrsa
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why /opt/openssl/lib, we have to remove this. And change make file to use Makefile constructs to compiles all c files at ones instead of compiling them individually.

gcc rsa.c -L/opt/openssl/lib -lcrypto -o rsa
gcc rsasign.c -L/opt/openssl/lib -lcrypto -o rsasign
gcc rsaverify.c -L/opt/openssl/lib -lcrypto -o rsaverify


clean:
rm genrsa -rf
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better make it 'rm -rf ' .

rm rsa -rf
rm rsasign -rf
rm rsaverify -rf

124 changes: 124 additions & 0 deletions rsa/genrsa.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <openssl/rsa.h>
#include <openssl/evp.h>
#include <openssl/bn.h>
#include <openssl/err.h>
#include <openssl/pem.h>

char *prtkeyfile=NULL;
BIO *bio_log=NULL;
unsigned int rsabits = 2048;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

take rsabits as input.


void usage(void){
printf("\n rsagen -s <rsa size in bits> -o <private key file>");
printf("\n rsagen -h -- For help");
printf("\n");
}


void generate_rsa(void){
BIGNUM *bn=NULL;
RSA *rsa=NULL;
EVP_PKEY *pkey=NULL;
BIO *prtbio;

if((bn = BN_new()) == NULL) {
BIO_printf(bio_log, "Failed to allocate BN\n");
goto end;
}
BN_set_word(bn, RSA_F4);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we take pub exponent as an option from user?


rsa = RSA_new();
RSA_generate_key_ex(rsa, rsabits, bn, NULL);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

handle return for both RSA_new and RSA_generate_key_ex


if((pkey = EVP_PKEY_new()) == NULL) {
BIO_printf(bio_log, "EVP_PKEY_New failed\n");
goto end;
}
EVP_PKEY_assign_RSA(pkey, rsa);

if (prtkeyfile) {
if ((prtbio = BIO_new_file(prtkeyfile, "w")) == NULL) {
BIO_printf(bio_log, "\n BIO_new_file %s failed\n", prtkeyfile);
goto end;
}
}
else {
if ((prtbio = BIO_new_fp(stdout, BIO_NOCLOSE)) == NULL) {
BIO_printf(bio_log, "\n BIO_new_file failed\n");
goto end;
}
}

if (!PEM_write_bio_PrivateKey(prtbio, pkey, NULL, NULL, 0, NULL, NULL)) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Output pem can be RSAPrivateKey(pkcs1?) or PrivateKey(pkcs8). The above one writes the key in pkcs8, what is our intention here whether to have pkcs1 or pkcs8, do you want to take an option from the user?.

BIO_printf(bio_log, "PEM_write_bio_PrivateKey failed\n");
goto end;
}

end:
if (bn) {
BN_free(bn);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to free 'rsa'.

if(pkey) {
EVP_PKEY_free(pkey);
}


return;
}

int main(int argc, char **argv) {

unsigned int opt;

while( (opt=getopt(argc, argv, "o:s:h")) != -1) {
switch(opt) {
case 's':
rsabits = atoi(optarg);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use strtol

break;

case 'o':
if((prtkeyfile = (char *) OPENSSL_malloc(strlen(optarg)+1)) == NULL) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why global variable prtkeyfile, just pass this as argument to generate_rsa().

printf("\n %s: OPENSSL_malloc failed. \n", optarg);
}

memset(prtkeyfile, 0, strlen(optarg)+1);
memcpy(prtkeyfile, optarg, strlen(optarg));
break;

case 'h':
usage();
goto end;
break;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Break not required;


case '?':
printf("\n Invalid parameter passed\n");
break;
}
}

bio_log = BIO_new_fp(stdout, BIO_NOCLOSE);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bio_log need not to be global.

if(bio_log == NULL) {
printf("BIO_new_fp failed\n");
goto end;
}

generate_rsa();


end:

if(prtkeyfile) {
OPENSSL_free(prtkeyfile);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you don't use prtkeyfile as global we don't need this.

}

if(bio_log) {
BIO_free_all(bio_log);
}

return 0;
}
141 changes: 141 additions & 0 deletions rsa/rsa.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <openssl/rsa.h>
#include <openssl/evp.h>
#include <openssl/bn.h>
#include <openssl/err.h>
#include <openssl/pem.h>

char *pubkeyfile=NULL;
char *prtkeyfile=NULL;
BIO *bio_log=NULL;
unsigned int print;

void usage(void){
printf("\n rsa -i <private key file> -o <public key file> -p ");
printf("\n rsa -h -- For help");
printf("\n");
}


void getrsa(void){

BIO *prtbio, *pubbio;
EVP_PKEY *pkey;

if (prtkeyfile) {
if ((prtbio = BIO_new_file(prtkeyfile, "r")) == NULL) {
BIO_printf(bio_log, "\n BIO_new_file %s failed\n", prtkeyfile);
goto end;
}
}
else {
BIO_printf(bio_log, "\n Private key file not provided\n");
goto end;
}

if (pubkeyfile) {
if ((pubbio = BIO_new_file(pubkeyfile, "w")) == NULL) {
BIO_printf(bio_log, "\n BIO_new_file %s failed\n", pubkeyfile);
goto end;
}
}
else {
pubbio = bio_log;
}

if ((pkey = PEM_read_bio_PrivateKey(prtbio, NULL, NULL, NULL)) == NULL) {
BIO_printf(bio_log, "\n Unable to load\n");
goto end;
}

if( !PEM_write_bio_PUBKEY(pubbio, pkey)) {
BIO_printf(bio_log, "\n Public key extract failed\n");
goto end;
}

if (print) {
EVP_PKEY_print_private(pubbio, pkey, 0, NULL);
}

end:
if(prtbio) {
BIO_free(prtbio);
}

if(pubkeyfile && pubbio) {
BIO_free(pubbio);
}

if(pkey) {
EVP_PKEY_free(pkey);
}

return;
}

int main(int argc, char **argv) {

unsigned int opt;

while( (opt=getopt(argc, argv, "i:o:hp")) != -1) {
switch(opt) {
case 'i':
if((prtkeyfile = (char *) OPENSSL_malloc(strlen(optarg)+1)) == NULL) {
printf("\n %s: OPENSSL_malloc failed. \n", optarg);
}

memset(prtkeyfile, 0, strlen(optarg)+1);
memcpy(prtkeyfile, optarg, strlen(optarg));
break;

case 'o':
if((pubkeyfile = (char *) OPENSSL_malloc(strlen(optarg)+1)) == NULL) {
printf("\n %s: OPENSSL_malloc failed. \n", optarg);
}

memset(pubkeyfile, 0, strlen(optarg)+1);
memcpy(pubkeyfile, optarg, strlen(optarg));
break;

case 'p':
print = 1;
break;
case 'h':
usage();
goto end;
break;

case '?':
printf("\n Invalid parameter passed\n");
break;
}
}

bio_log = BIO_new_fp(stdout, BIO_NOCLOSE);
if(bio_log == NULL) {
printf("BIO_new_fp failed\n");
goto end;
}

getrsa();


end:

if(pubkeyfile) {
OPENSSL_free(pubkeyfile);
}

if(prtkeyfile) {
OPENSSL_free(prtkeyfile);
}

if(bio_log) {
BIO_free_all(bio_log);
}

return 0;
}
Loading