hello group,
  i want to acheive the following with out certificates.
the client should send its RSA public key to the server for 
authentication. i want to send the key in string format, so that 
the server can verify the client's public key, with its existing 
public keys list. the server stores the public keys in pem 
format(ascii).

so i want to send the clients send the key in ascii format. 
i2d_RSAPublicKey keys me the data in binary format. is there any 
way i can convert the binary data and send it in ascii.

thanks
ganesh

On Thu, 25 Jul 2002 ganesh kumar godavari wrote :
>manish,
>  this is how i created the keys
>
>openssl genrsa -out private.pem 1024
>openssl rsa -in private.pem -pubout -out public.pem
>
>this is the code i used to print the keys
>
>i never used the crypto library(except while trying to port 
>openssl onto VxWorks), but i used a lot of ssl libray previously. 
>i am not able to find out why this is causing me a problem.  can 
>u please tell me why this is causing me a problem?. u try out my 
>code i am pretty sure u can reproduce the error i am getting.
>
>
>
>#include <stdio.h>
>#include <openssl/rsa.h>
>#include <openssl/pem.h>
>
>RSA * readPubKey(char *filename)
>         {
>                 RSA *key;
>                 BIO *bp;
>
>                 ERR_load_crypto_strings();
>                 bp=BIO_new(BIO_s_file());
>
>                 if (BIO_read_filename(bp,filename) <= 0)
>                 {
>                      perror("ERROR: public.pem");
>                      exit(0);
>                 }
>
>                 if ((key=(RSA 
>*)PEM_read_bio_RSA_PUBKEY(bp,NULL,NULL,NULL)) == NULL)
>                   {
>                   ERR_print_errors_fp(stderr);
>                      key = NULL;
>                   }
>
>                 BIO_free(bp);
>                 return key;
>         }
>
>RSA * readPrivKey(char *filename)
>         {
>         RSA *key;
>         BIO *bp;
>
>         SSLeay_add_all_algorithms();
>         ERR_load_PEM_strings();
>
>         bp=BIO_new(BIO_s_file());
>         if (BIO_read_filename(bp,filename) <= 0)
>         {
>              perror("ERROR: rsakey.pem");
>              exit(0);
>         }
>         if ((key=(RSA 
>*)PEM_read_bio_RSAPrivateKey(bp,NULL,NULL,NULL)) == NULL) {
>          ERR_print_errors_fp(stderr);
>          key = NULL;
>         }
>
>         BIO_free(bp);
>          return key;
>
>         }
>
>int main(void)
>  {
>    RSA *pubkey;
>    RSA *privkey;
>    unsigned char *message= "Howdy ganesh talking on behalf of 
>Colorado University !! can u hear me now !!";
>    unsigned char *encryptmess, *decryptmess;
>    int long_message;
>    char **key;
>
>    pubkey = readPubKey("public.pem");
>    privkey = readPrivKey("private.pem");
>
>    printf("size of (in byte)s pu:pr :: %d:%d\n", 
>RSA_size(pubkey),RSA_size(privkey));
>#if 0
>   {
>    size_t size;
>    unsigned char *iend, *keybuf, *tmp;
>
>    size = i2d_RSAPublicKey(pubkey, NULL);
>    printf("size %d",size);
>    keybuf = (unsigned char *) malloc(size * sizeof( unsigned 
>char));
>    size = i2d_RSAPublicKey(pubkey, &keybuf);
>    iend = keybuf;
>    printf("key %0.2X\n", iend);
>    printf("key %s\n",iend);
>
>  }
>#endif
>
>{
>    size_t size;
>    unsigned char *iend, *keybuf;
>
>    size = i2d_RSAPublicKey(pubkey, NULL);
>    printf("size %d\n",size);
>
>    keybuf = (unsigned char *) malloc(size * sizeof( unsigned 
>char));
>    iend = keybuf;
>    size = i2d_RSAPublicKey(pubkey, &iend);
>    printf(" start %x end %x ", keybuf, iend);
>
>    printf("key %0.2X\n", keybuf);
>    printf("key %s\n",keybuf);
>}
>
>
>    /* Encrypt the message */
>    encryptmess= (unsigned char *) malloc (RSA_size(pubkey));
>    long_message= (strlen(message)*sizeof(char)+1);
>    RSA_public_encrypt(long_message, message, encryptmess, 
>pubkey, RSA_PKCS1_OAEP_PADDING);
>
>    /* decrypt the original message */
>    decryptmess= (unsigned char *) malloc(RSA_size(privkey));
>    RSA_private_decrypt(RSA_size(privkey), encryptmess, 
>decryptmess, privkey, RSA_PKCS1_OAEP_PADDING);
>
>    printf (" message %s\n decrypt %s\n", message, 
>decryptmess);
>
>}
>
>  the out put o# ./test4
>size of (in byte)s pu:pr :: 128:128
>size 140
>  start 8050eb0 end 8050f3c key 8050EB0
>key 0
>  message Howdy ganesh talking on behalf of Colorado University 
>!! can u hear me now !!
>  decrypt Howdy ganesh talking on behalf of Colorado University 
>!! can u hear me now !!
>f the whole program is
>
>thanks
>ganesh
>
>
>On Thu, 25 Jul 2002 Manish Ramesh Chablani wrote :
>>Ganesh,
>>
>>change your code to
>>
>>
>>   {
>>     size_t size;
>>     unsigned char *iend, *keybuf;
>>
>>     size = i2d_RSAPublicKey(pubkey, NULL);
>>     printf("size %d\n",size);
>>     keybuf = (unsigned char *) malloc(size * sizeof( unsigned
>>char));
>>     size = i2d_RSAPublicKey(pubkey, &iend);
>>     printf("key %0.2X\n", keybuf);
>>     printf("key %s\n",keybuf);
>>}
>>
>>
>>NOTE:
>>now "iend" points to end of key string (actually one char past 
>>the end) and
>>"keybuff" to begining
>>
>>hope this helps,
>>Manish Chablani
>>------------------------------------------------------
>>Graduate Student, CS Department,
>>Indiana University.
>>
>>Make today a LAM/MPI day !!!
>>http://www.lam-mpi.org/
>>------------------------------------------------------
>>
>>
>> >Date: 24 Jul 2002 23:56:00 -0000
>> >From: "ganesh kumar godavari" <[EMAIL PROTECTED]>
>> >To: [EMAIL PROTECTED]
>> >Cc: "Manish Ramesh Chablani" <[EMAIL PROTECTED]>
>> >Subject: Re: Re: RE: Re: creating RSA private/public key 
>>pair
>> >Content-Disposition: inline
>> >
>> >hello manish,
>> >  that was a typo error. i am attaching the following code 
>>this is
>> >what i was running.
>> >
>> >  {
>> >    size_t size;
>> >    unsigned char *iend, *keybuf;
>> >
>> >    size = i2d_RSAPublicKey(pubkey, NULL);
>> >    printf("size %d\n",size);
>> >    keybuf = (unsigned char *) malloc(size * sizeof( 
>>unsigned
>> >char));
>> >    size = i2d_RSAPublicKey(pubkey, &keybuf);
>> >    iend = keybuf;
>> >    printf("key %0.2X\n", iend);
>> >    printf("key %s\n",iend);
>> >}
>> >the output is
>> >size 140
>> >key 8051064
>> >key I
>> >
>> >Thanks
>> >ganesh
>> >
>> >On Thu, 25 Jul 2002 Manish Ramesh Chablani wrote :
>> >>Hi,
>> >>
>> >>[snip]
>> >> >>  size = i2d_RSAPublicKey(privkey, &iend);
>> >>[snip]
>> >>
>> >>this wont work!!!
>> >>
>> >>use either
>> >>   size = i2d_RSAPrivateKey(privkey, &iend);
>> >>or
>> >>   size = i2d_RSAPublicKey(pubkey, &iend);
>> >>
>> >>
>> >>also check out the value of size returned from these
>> >>functions..
>> >>
>> >>
>> >>oops.. oner more impt thing.. u r getting junk values because 
>>you
>> >>r printing
>> >>iend.. NOTE that iend is advanced by above functions so try
>> >>printing keybuf
>> >>instead..
>> >>
>> >>hope this helps,
>> >>Manish Chablani
>> >>------------------------------------------------------
>> >>Graduate Student, CS Department,
>> >>Indiana University.
>> >>
>> >>Make today a LAM/MPI day !!!
>> >>http://www.lam-mpi.org/
>> >>------------------------------------------------------
>> >>
>> >> >Date: 24 Jul 2002 22:59:59 -0000
>> >> >MIME-Version: 1.0
>> >> >From: "ganesh kumar godavari" <[EMAIL PROTECTED]>
>> >> >To: "'[EMAIL PROTECTED]'" 
>><[EMAIL PROTECTED]>
>> >> >Cc: "Dilkie,Lee" <[EMAIL PROTECTED]>, "Manish Ramesh
>> >>Chablani"
>> >><[EMAIL PROTECTED]>
>> >> >Subject: Re: RE: Re: creating RSA private/public key pair
>> >> >Content-Disposition: inline
>> >> >X-Sender: "ganesh kumar godavari" 
>><[EMAIL PROTECTED]>
>> >> >X-List-Manager: OpenSSL Majordomo [version 1.94.4]
>> >> >X-List-Name: openssl-users
>> >> >X-Perlmx-Spam: Gauge=, Probability=0%, Report=
>> >> >
>> >> >hai,
>> >> >  thanks a lot manish, and lee.
>> >> >
>> >> >Lee: yes the code doesn't work for data longer than key 
>>size.
>> >> >
>> >> >  can anyone tell me how i can store the RSA public key 
>>in
>> >>the
>> >> >string, so that i can send my public key to the other 
>>side.
>> >> >
>> >> >{
>> >> >    size_t size;
>> >> >    unsigned char *iend, *keybuf, *tmp;
>> >> >    size = i2d_RSAPublicKey(pubkey, NULL);
>> >> >    keybuf = (unsigned char *) malloc(size * sizeof( 
>>unsigned
>> >> >char));
>> >> >    iend = keybuf;
>> >> >    size = i2d_RSAPublicKey(privkey, &iend);
>> >> >    printf("key hex %0.2X\n", iend);
>> >> >    printf("key ascii %s\n", iend);
>> >> >}
>> >> >
>> >> >this code gives me junk data.
>> >> >
>> >> >thanks
>> >> >ganesh
>> >> >
>> >> >On Wed, 24 Jul 2002 Dilkie, Lee wrote :
>> >> >>Ganesh,
>> >> >>
>> >> >>Just a small comment. They code you have will work for 
>>small
>> >> >>messages only. Any message longer than the key size(minus 
>>a
>> >>byte
>> >> >>or two) will not be encryptible with RSA.
>> >> >>
>> >> >>-lee
>> >> >>
>> >> >>-----Original Message-----
>> >> >> From: ganesh kumar godavari
>> >>[mailto:[EMAIL PROTECTED]]
>> >> >>Sent: Tuesday, July 23, 2002 7:31 PM
>> >> >>To: [EMAIL PROTECTED]
>> >> >>Cc: Aleix Conchillo
>> >> >>Subject: Re: Re: creating RSA private/public key pair
>> >> >>
>> >> >>
>> >> >>Hi Alex,
>> >> >>thanks for the help now i am able to encrypt and decrypt
>> >>them
>> >> >>using public and private keys.
>> >> >>
>> >> >>i have one more question. i want to send the public key 
>>of
>> >>the
>> >> >>client to the server for authentication. is there any way 
>>i
>> >>can
>> >> >>print the RSA public key into a string so that i can 
>>send
>> >>them
>> >> >>over the sockets?
>> >> >>
>> >> >>i cannot find any functions for printing the RSA public 
>>key
>> >>into
>> >> >>a
>> >> >>string.
>> >> >>
>> >> >>enclosing my code below
>> >> >>
>> >> >>Thanks a ton,
>> >> >>
>> >> >>ganesh
>> >> >>
>> >> >>#include <stdio.h>
>> >> >>#include <openssl/rsa.h>
>> >> >>#include <openssl/pem.h>
>> >> >>
>> >> >>RSA * readPubKey(char *filename)
>> >> >>          {
>> >> >>                  RSA *key;
>> >> >>                  BIO *bp;
>> >> >>
>> >> >>                  ERR_load_crypto_strings();
>> >> >>                  bp=BIO_new(BIO_s_file());
>> >> >>
>> >> >>                  if (BIO_read_filename(bp,filename) <= 
>>0)
>> >> >>                  {
>> >> >>                       perror("ERROR: public.pem");
>> >> >>                       exit(0);
>> >> >>                  }
>> >> >>
>> >> >>                  if ((key=(RSA
>> >> >>*)PEM_read_bio_RSA_PUBKEY(bp,NULL,NULL,NULL)) == NULL)
>> >> >>                    {
>> >> >>                    ERR_print_errors_fp(stderr);
>> >> >>                       key = NULL;
>> >> >>                    }
>> >> >>
>> >> >>                  BIO_free(bp);
>> >> >>                  return key;
>> >> >>          }
>> >> >>
>> >> >>RSA * readPrivKey(char *filename)
>> >> >>          {
>> >> >>          RSA *key;
>> >> >>          BIO *bp;
>> >> >>
>> >> >>          SSLeay_add_all_algorithms();
>> >> >>          ERR_load_PEM_strings();
>> >> >>
>> >> >>          bp=BIO_new(BIO_s_file());
>> >> >>          if (BIO_read_filename(bp,filename) <= 0)
>> >> >>          {
>> >> >>               perror("ERROR: rsakey.pem");
>> >> >>               exit(0);
>> >> >>          }
>> >> >>
>> >> >>
>> >> >>          if ((key=(RSA
>> >> >>*)PEM_read_bio_RSAPrivateKey(bp,NULL,NULL,NULL)) == NULL) 
>>{
>> >> >>           ERR_print_errors_fp(stderr);
>> >> >>           key = NULL;
>> >> >>          }
>> >> >>
>> >> >>          BIO_free(bp);
>> >> >>           return key;
>> >> >>
>> >> >>          }
>> >> >>
>> >> >>int main(void)
>> >> >>   {
>> >> >>     RSA *pubkey;
>> >> >>     RSA *privkey;
>> >> >>     unsigned char *message= "Howdy ganesh talking on 
>>behalf
>> >>of
>> >> >>zeewaves !! can u hear me now !!";
>> >> >>     unsigned char *encryptmess, *decryptmess;
>> >> >>     int long_message;
>> >> >>
>> >> >>     pubkey = readPubKey("public.pem");
>> >> >>     privkey = readPrivKey("private.pem");
>> >> >>
>> >> >>     printf("size of (in byte)s pu:pr :: %d:%d\n",
>> >> >>RSA_size(pubkey),RSA_size(privkey));
>> >> >>
>> >> >>     /* Encrypt the message */
>> >> >>     encryptmess= (unsigned char *) malloc
>> >>(RSA_size(pubkey));
>> >> >>     long_message= (strlen(message)*sizeof(char)+1);
>> >> >>     RSA_public_encrypt(long_message, message, 
>>encryptmess,
>> >> >>pubkey,
>> >> >>RSA_PKCS1_OAEP_PADDING);
>> >> >>
>> >> >>     /* decrypt the original message */
>> >> >>     decryptmess= (unsigned char *)
>> >>malloc(RSA_size(privkey));
>> >> >>     RSA_private_decrypt(RSA_size(privkey), encryptmess,
>> >> >>decryptmess, privkey, RSA_PKCS1_OAEP_PADDING);
>> >> >>
>> >> >>     printf (" message %s\n decrypt %s\n", message,
>> >> >>decryptmess);
>> >> >>
>> >> >>}
>> >> >>
>> >> >>
>> >> >>
>> >> >>On Tue, 23 Jul 2002 Aleix Conchillo wrote :
>> >> >> >On 23 Jul 2002 17:44:15 -0000, ganesh kumar godavari
>> >>wrote:
>> >> >> >
>> >> >> > > hello group,
>> >> >> > >   i have create RSA private key using
>> >> >> > > openssl genrsa -out KEY.pem 1024
>> >> >> > > openssl rsa -in KEY.pem -out private.pem
>> >> >> > > openssl rsa -in KEY.pem -pubout -out public.pem
>> >> >> > >
>> >> >> >
>> >> >> >hi
>> >> >> >
>> >> >> >first, you don't need to call
>> >> >> >
>> >> >> >openssl rsa -in KEY.pem -out private.pem
>> >> >> >
>> >> >> >the first command "genrsa" generates a private key
>> >>already,
>> >> >>so
>> >> >> >you'll
>> >> >> >have:
>> >> >> >
>> >> >> >openssl genrsa -out private.pem 1024
>> >> >> >openssl rsa -in private.pem -pubout -out public.pem
>> >> >> >
>> >> >> > >
>> >> >> > > i try to read the public.pem the following way
>> >> >> > >
>> >> >> >[snip]
>> >> >> > >
>> >> >> > >    if ((x=(RSA *)PEM_read_RSAPublicKey(fp,NULL,
>> >> >>NULL,NULL))
>> >> >> >!=
>> >> >> > > NULL)
>> >> >> >[snip]
>> >> >> > >
>> >> >> > > it gives me an error saying that
>> >> >> > >
>> >> >> >
>> >> >> >that's because there's two ways to read public keys. a
>> >>public
>> >> >>key
>> >> >> >can
>> >> >> >be rsa, dsa and dh (may be more in openssl i don't
>> >>remember
>> >> >>now).
>> >> >> >with
>> >> >> >the function PEM_read_RSAPublicKey OpenSSL is expecting 
>>a
>> >> >> >concrete RSA
>> >> >> >Public key which will have in the header of the PEM 
>>file
>> >> >> >
>> >> >> >-----BEGIN RSA PUBLIC KEY-----
>> >> >> >
>> >> >> >instead of
>> >> >> >
>> >> >> >-----BEGIN PUBLIC KEY-----
>> >> >> >
>> >> >> >if you'd like to load an RSA key with the "BEGIN 
>>PUBLIC
>> >>KEY"
>> >> >> >header,
>> >> >> >you should use PEM_read_RSA_PUBKEY function instead of 
>>the
>> >> >>one
>> >> >> >you use.
>> >> >> >
>> >> >> >this header will be common for dsa, rsa and dh keys.
>> >> >> >
>> >> >> >uppps... got to catch the bus. hope this helps you.
>> >> >> >
>> >> >> >regards,
>> >> >> >
>> >> >> >aleix
>> >> >>
>> >> 
>> >______________________________________________________________________
>> >> >> >OpenSSL Project
>> >> >> >http://www.openssl.org
>> >> >> >User Support Mailing List
>> >> >> >[EMAIL PROTECTED]
>> >> >> >Automated List Manager
>> >> >> >[EMAIL PROTECTED]
>> >> >>
>> >> 
>> >>______________________________________________________________________
>> >> >>OpenSSL Project
>> >> >>http://www.openssl.org
>> >> >>User Support Mailing List
>> >> >>[EMAIL PROTECTED]
>> >> >>Automated List Manager
>> >> >>[EMAIL PROTECTED]
>> >> 
>> >>______________________________________________________________________
>> >> >>OpenSSL Project
>> >> >>http://www.openssl.org
>> >> >>User Support Mailing List
>> >> >>[EMAIL PROTECTED]
>> >> >>Automated List Manager
>> >> >>[EMAIL PROTECTED]
>> >> >
>> >> 
>> >______________________________________________________________________
>> >> >OpenSSL Project
>> >>http://www.openssl.org
>> >> >User Support Mailing List
>> >>[EMAIL PROTECTED]
>> >> >Automated List Manager
>> >>[EMAIL PROTECTED]
>> >>
>> >>
>> >>______________________________________________________________________
>> >>OpenSSL Project
>> >>http://www.openssl.org
>> >>User Support Mailing List
>> >>[EMAIL PROTECTED]
>> >>Automated List Manager
>> >>[EMAIL PROTECTED]
>> >
>> >______________________________________________________________________
>> >OpenSSL Project                                 
>>http://www.openssl.org
>> >User Support Mailing List                    
>>[EMAIL PROTECTED]
>> >Automated List Manager                           
>>[EMAIL PROTECTED]
>>
>>______________________________________________________________________
>>OpenSSL Project                                 
>>http://www.openssl.org
>>User Support Mailing List                    
>>[EMAIL PROTECTED]
>>Automated List Manager                           
>>[EMAIL PROTECTED]
>
>______________________________________________________________________
>OpenSSL Project                                 
>http://www.openssl.org
>User Support Mailing List                    
>[EMAIL PROTECTED]
>Automated List Manager                           
>[EMAIL PROTECTED]

______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    [EMAIL PROTECTED]
Automated List Manager                           [EMAIL PROTECTED]

Reply via email to