LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Security
User Name
Password
Linux - Security This forum is for all security related questions.
Questions, tips, system compromises, firewalls, etc. are all included here.

Notices


Reply
  Search this Thread
Old 03-12-2017, 07:34 PM   #1
MrUmunhum
Member
 
Registered: May 2006
Location: Mt Umunhum, CA, USA, Earth
Distribution: Debian/ Fedora/ Ubuntu/ Raspbian
Posts: 549

Rep: Reputation: 40
OpenSSL, C program, passing a Public RSA to a remote peer.


Hi group,

This question is in reference to my post http://www.linuxquestions.org/questi...am-4175601569/which I solved but don't like the solution. My solution was to add this code to populate the public and private RSAs.
Code:
  RSA* RSA1 = RSA_new();
  RSA* RSA2 = RSA_new();

  RSA2->n = RSA1->n = My_RSA->n;
  RSA2->e = RSA1->e = My_RSA->e;
  RSA2->d = RSA1->d = My_RSA->d;
  RSA2->p = RSA1->p = My_RSA->p;
  RSA2->q = RSA1->q = My_RSA->q;
Where My_RSA was created with:
Code:
 RSA_generate_key_ex( My_RSA, 2048, bne, NULL );
The complete program is at:http://mt-umunhum-wireless.net/Sources/rsa

Now I need to be able to send the public RSA to a remote peer. Which means I need to send the key, n, e, d, p and q.

Is there a better solution?

Thanks for your time.
 
Old 03-13-2017, 04:29 AM   #2
camp0
Member
 
Registered: Dec 2016
Location: Dublin
Distribution: Fedora
Posts: 70

Rep: Reputation: 4
Hi,

If you need to transfer the public key to a peer why don't you write your public key on disk (PEM_write_bio_RSAPublicKey) and use scp?

Regards,
Luis
 
Old 03-13-2017, 08:15 AM   #3
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
Quote:
If you need to transfer the public key to a peer why don't you write your public key on disk (PEM_write_bio_RSAPublicKey) and use scp?
I agree. PEM-files are a very standard way to conveniently exchange key material with a peer. Arrange your software so that it generates such a file. And, on the client side, it reads such a file at an agreed-upon location. "Exactly like everybody else does!"

Be sure that you have designed your system to use OpenSSL properly: to use the RSA-key to negotiate a private per-conversation session key.

Use libraries like OpenSSL only at the surface layers: "don't dive down into the guts of the thing, even though you can." There's a surface-layer API that's intended to be "the thing that you're supposed to use," along with lower-level routines that are intended for special cases. But ... "your case, whatever it is, is not 'special!'"

In matters of cryptography, key management is the Achilles heel.

Last edited by sundialsvcs; 03-13-2017 at 09:24 AM.
 
Old 03-13-2017, 08:38 AM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by MrUmunhum View Post
Now I need to be able to send the public RSA to a remote peer. Which means I need to send the key, n, e, d, p and q.
I'm not familiar with the API you're using, but assuming standard terminology, d, p, and q are part of the private key, don't send them to anyone else! Only n and e should be shared.
 
Old 03-13-2017, 08:50 PM   #5
MrUmunhum
Member
 
Registered: May 2006
Location: Mt Umunhum, CA, USA, Earth
Distribution: Debian/ Fedora/ Ubuntu/ Raspbian
Posts: 549

Original Poster
Rep: Reputation: 40
Quote:
Originally Posted by camp0 View Post
Hi,

If you need to transfer the public key to a peer why don't you write your public key on disk (PEM_write_bio_RSAPublicKey) and use scp?

Regards,
Luis
I'm using a program I wrote to talk to the peer. I would prefer using the network connection to transfer the public RSA.
 
Old 03-13-2017, 08:55 PM   #6
MrUmunhum
Member
 
Registered: May 2006
Location: Mt Umunhum, CA, USA, Earth
Distribution: Debian/ Fedora/ Ubuntu/ Raspbian
Posts: 549

Original Poster
Rep: Reputation: 40
Quote:
Originally Posted by ntubski View Post
I'm not familiar with the API you're using, but assuming standard terminology, d, p, and q are part of the private key, don't send them to anyone else! Only n and e should be shared.
I changed my code:
Code:
  RSA* RSA1 = RSA_new();
  RSA* RSA2 = RSA_new();

  RSA2->n = RSA1->n = My_RSA->n;
  RSA2->e = RSA1->e = My_RSA->e;
  //  RSA2->d = RSA1->d = My_RSA->d;
  //  RSA2->p = RSA1->p = My_RSA->p;
  //  RSA2->q = RSA1->q = My_RSA->q;
Now I get this:
Code:
128 (Key) Make Key Failed!
error:0407B093:rsa routines:RSA_check_key:value missing
130 (Key) Make Key Failed!
error:0407B093:rsa routines:RSA_check_key:value missing
 
Old 03-13-2017, 10:14 PM   #7
MrUmunhum
Member
 
Registered: May 2006
Location: Mt Umunhum, CA, USA, Earth
Distribution: Debian/ Fedora/ Ubuntu/ Raspbian
Posts: 549

Original Poster
Rep: Reputation: 40
I posted too quickly. The Encrypt RSA worked OK even though it failed the RSA_check_key? Changed the decrypt RSA to the RSA pair and it worked.

So you are right about the encrot RSA only needing n and e.
 
Old 03-13-2017, 11:54 PM   #8
MrUmunhum
Member
 
Registered: May 2006
Location: Mt Umunhum, CA, USA, Earth
Distribution: Debian/ Fedora/ Ubuntu/ Raspbian
Posts: 549

Original Poster
Rep: Reputation: 40
Quote:
Originally Posted by sundialsvcs View Post
I agree. PEM-files are a very standard way to conveniently exchange key material with a peer. Arrange your software so that it generates such a file. And, on the client side, it reads such a file at an agreed-upon location. "Exactly like everybody else does!"

Be sure that you have designed your system to use OpenSSL properly: to use the RSA-key to negotiate a private per-conversation session key.

Use libraries like OpenSSL only at the surface layers: "don't dive down into the guts of the thing, even though you can." There's a surface-layer API that's intended to be "the thing that you're supposed to use," along with lower-level routines that are intended for special cases. But ... "your case, whatever it is, is not 'special!'"

In matters of cryptography, key management is the Achilles heel.
I agree completely. But I could not find out what the proper way to send a public RSA to the peer. If you could point me to an example written in C, I would be very great full.

Thanks.
 
Old 03-14-2017, 07:23 AM   #9
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by MrUmunhum View Post
The Encrypt RSA worked OK even though it failed the RSA_check_key?
Probably RSA_check_key checks that the private and public values match each other.
 
Old 03-14-2017, 08:54 AM   #10
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
Quote:
Originally Posted by MrUmunhum View Post
I agree completely. But I could not find out what the proper way to send a public RSA to the peer. If you could point me to an example written in C, I would be very great full.
One way would be to write the file to disk and just scp or rsync it over to its destination.

Another way would be to copy the content of the file into memory and send it by means of your existing socket connection with the peer. (This exchange does not have to be encrypted.)

The peer expects to find its certificate-file at an agreed-upon location in its own filesystem.

Since certificates are uniquely identifiable, the host maintains a list of certificates that it will recognize. (Or, that have been revoked.) Again, use the full OpenSSL standard and "do exactly what everyone else does."
 
Old 03-14-2017, 03:41 PM   #11
MrUmunhum
Member
 
Registered: May 2006
Location: Mt Umunhum, CA, USA, Earth
Distribution: Debian/ Fedora/ Ubuntu/ Raspbian
Posts: 549

Original Poster
Rep: Reputation: 40
Quote:
Originally Posted by sundialsvcs View Post
One way would be to write the file to disk and just scp or rsync it over to its destination.

Another way would be to copy the content of the file into memory and send it by means of your existing socket connection with the peer. (This exchange does not have to be encrypted.)
Transferring files is not a problem. The problem is which files to move?
Quote:
Since certificates are uniquely identifiable, the host maintains a list of certificates that it will recognize. (Or, that have been revoked.) Again, use the full OpenSSL standard and "do exactly what everyone else does."
May be I am missing something? I thought the only way to encrypt a packet was to use RSA_public_encrypt()? Which requires a public RSA. If I use certificates can I use them to encrypt a packet?

Rebuilding the public RSA is a real PITA!

Thanks for your time.
 
Old 03-14-2017, 04:29 PM   #12
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
Use .pem files at an agreed-upon location to contain the public-key material that is required by the client.

Build these files to contain the necessary public-key material, then transfer them to the client. Arrange for the client software to require the file to be at some agreed-upon location. The client reads the public key from the file and uses it to perform the initial session-key negotiations.

Remember that public-key techniques are not used to final-encrypt the traffic: they are used to securely negotiate a (symmetric ...) session key which is then used to do so. All of which OpenSSL can handle for you.

[i]Use the highest-level functions within the (vast ...) OpenSSL library, and do not dive down into the "primitive functions" which are also available.

I highly encourage you to spend some time, say, at "github.com," to review how applications that you can find there employ the SSL libraries. You will find both "best practices" and "code that you can steal" in those places. Avoid wasted time spent "blundering through how-to-do-it, as though no one before you had ever done it before," because they have, and they provided their source code on places like github.

Last edited by sundialsvcs; 03-14-2017 at 04:38 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Attempting to make a public rsa with test C program. MrUmunhum Linux - Security 1 03-12-2017 06:23 PM
[SOLVED] Unable to load Public Key (OpenSSL RSA, Debian Squeeze) gacanepa Linux - Newbie 1 11-29-2012 12:17 PM
I am serching for a chat program that works Peer-to-peer (that has no server) kironban Linux - Newbie 2 01-27-2008 12:22 PM
a GUI of a peer to peer program in Java manolakis Programming 1 09-30-2007 12:29 PM
RSA implementation in openssl-0.9.8-a sapana Programming 2 02-21-2006 07:50 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Security

All times are GMT -5. The time now is 07:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration