Results 1 to 19 of 19

Thread: RSA, Private and Public Key and Digital Signature

  1. #1
    Join Date
    Sep 2009
    Posts
    7
    Thanks
    2

    Default RSA, Private and Public Key and Digital Signature

    I have searched and searched, and made many attempts to solve this, but until now I cannot solve this situation.

    I need a code, that must be cross platform, since I must use it in Linux and Windows, that having a RSA Private and Public Key, that I can generate using openssl, I must be have to create a digital signature using the Private Key with the following parameters: x.509, UTF-8, Base 64, Little Endian, PKCS1 v1.5 padding, 1024 bytes size and Hash format SHA-1.

    I already made some attempts to include openssl in QT, but I cannot find after hours searching good examples that can help me to solve this.

    Any help will be great.

    Paulo

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: RSA, Private and Public Key and Digital Signature

    What exactly are you having problems with?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    pvaz (7th April 2011)

  4. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: RSA, Private and Public Key and Digital Signature

    What do you mean by "include openssl in QT"? Using OpenSSL from any C/C++ program should be trivially straightforward, and has nothing to do with Qt. Here's the process to build one of the examples shipped with OpenSSL using QMake.
    Qt Code:
    1. $ cd /tmp/example
    2.  
    3. # extract and build OpenSSL
    4. $ tar xzf openssl-1.0.0d.tar.gz
    5. $ cd openssl-1.0.0d
    6. $ less INSTALL
    7. $ ./config --prefix=/tmp/example/ssl # statically linked libs only
    8. $ make
    9. $ make test
    10. $ make install
    11. $ cd ..
    12.  
    13. # Grab a small example code that comes with OpenSSL but leave the Makefile behind
    14. $ cp openssl-1.0.0d/demos/sign/{sign.c,*.pem,*.txt} .
    15. # make a PRO file
    16. $ cat example.pro
    17. TEMPLATE = app
    18. TARGET =
    19. INCLUDEPATH += /tmp/example/ssl/include
    20. LIBS += -L/tmp/example/ssl/lib -lcrypto
    21. # Input
    22. SOURCES += sign.c
    23.  
    24. $ qmake
    25. $ make
    26. ...
    27. $ ./example
    28. Signature Verified Ok.
    To copy to clipboard, switch view to plain text mode 
    Then add whatever Qt code you like.

    If you want a Qt-style wrapper around OpenSSL then there is always QCA, but that is just another layer of stuff to get built correctly.

  5. The following user says thank you to ChrisW67 for this useful post:

    pvaz (7th April 2011)

  6. #4
    Join Date
    Sep 2009
    Posts
    7
    Thanks
    2

    Default Re: RSA, Private and Public Key and Digital Signature

    What to say, I need a solution since I could not make any progress.

    I was looking on one possibility that is using QCryptographicHash, but really do not know if it will make possible to do what I really need.

    Do you know if this is a good way to go and also I have to try to find a good example on how to use it.

    Any help will be great.

    Thanks,
    Paulo

  7. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: RSA, Private and Public Key and Digital Signature

    QCryptographicHash by itself will not be enough. You should know that if you have knowledge of appropriate theory. If you don't then I suggest you start with filling this gap.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #6
    Join Date
    Sep 2009
    Posts
    7
    Thanks
    2

    Default Re: RSA, Private and Public Key and Digital Signature

    Thank you Chris

    After a huge fight with my computer and with many attempts, finally I get it all working on my windows qt.

    Now comes another question, this example, uses files to read the private and public key, but in my application, I need to have stored inside the C the private and public key.

    How can I make it ? Can you help me on this task ?

    Paulo

    Thank you wysota

  9. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: RSA, Private and Public Key and Digital Signature

    If you intend to store the private key inside your application code then you may as well don't do anything as extracting your private key from within the code is a matter of seconds.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #8
    Join Date
    Sep 2009
    Posts
    7
    Thanks
    2

    Default Re: RSA, Private and Public Key and Digital Signature

    Hi,

    what is the best to do it, since I need to digital sign parts of outputs, using the Private Key.

    Paulo

  11. #9
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: RSA, Private and Public Key and Digital Signature

    Why do you need to use the private key? Encrypt the output using the public key and decrypt it using the private key.

  12. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: RSA, Private and Public Key and Digital Signature

    Quote Originally Posted by squidge View Post
    Why do you need to use the private key?
    If he wants a signature then it's done using the private key (since only the owner of the private key is able to perform the operation it is certain that if the signature can be decrypted using the public key and the hash is verified, the sender and the plain text contents are correct).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #11
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: RSA, Private and Public Key and Digital Signature

    Yes, I wasn't thinking correctly when I wrote that. It would be pretty pointless to sign with a public key that everyone has access too.

    I was thinking more along the lines of encrypting the contents rather than generating a signature.

  14. #12
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: RSA, Private and Public Key and Digital Signature

    Quote Originally Posted by pvaz View Post
    Now comes another question, this example, uses files to read the private and public key, but in my application, I need to have stored inside the C the private and public key.
    Don't. Store the files outside the application. Protect the private key with a good pass phrase and use that with PEM_read_PrivateKey. The user must supply the pass phrase. This has the advantage that the pass phrase or keys can be changed, and all the security is being managed by OpenSSL code.

    I guess you could embed an ENCRYPTED private key and require the user to provide the pass phrase at run time. You should look at the other variants of the key handling routines to see if this is possible.

  15. #13
    Join Date
    Sep 2009
    Posts
    7
    Thanks
    2

    Default Re: RSA, Private and Public Key and Digital Signature

    I have found one example that does what I need, but I am having trouble trying to make it compatible with QT:

    Qt Code:
    1. #include "openssl\pem.h" // should pullin all the others
    2. #pragma comment(lib, "libeay32MT.lib")
    3. // C++ Linker, delay load DLL: libeay32.dll
    4.  
    5. bool OpenSSLSign(TMemoryStream *in, TMemoryStream *out)
    6. {
    7. bool Result = false;
    8.  
    9. // LEFT AS AN EXERCISE TO THE READER:
    10. // >>> Load Private Key into "out" stream *HERE* before proceeding any further <<<
    11.  
    12. BIO *bKey = BIO_new(BIO_s_mem());
    13. BIO_write(bKey, out->Memory, out->Size);
    14. BIO_flush(bKey);
    15. EVP_PKEY *pKey = PEM_read_bio_PrivateKey(bKey, NULL, NULL, NULL);
    16. BIO_free(bKey);
    17.  
    18. if (pKey) {
    19. EVP_MD_CTX mdctx;
    20. EVP_MD_CTX_init(&mdctx);
    21. EVP_SignInit_ex(&mdctx, EVP_sha1(), NULL);
    22. EVP_SignUpdate(&mdctx, (System::PByte)in->Memory, in->Size);
    23. out->Size = EVP_PKEY_size(pKey);
    24. unsigned int out_len;
    25. Result = EVP_SignFinal(&mdctx, (System::PByte)out->Memory, &out_len, pKey);
    26. SecureZeroMemory(pKey, sizeof(*pKey));
    27. EVP_MD_CTX_cleanup(&mdctx);
    28. }
    29. return Result;
    30. }
    31. //---------------------------------------------------------------------------
    To copy to clipboard, switch view to plain text mode 

    Problem is on TMemoryStream, EVP_SignUpdate(&mdctx, (System::PByte)in->Memory, in->Size) and SecureZeroMemory(pKey, sizeof(*pKey));

    How can I adapt this function is order to use in QT, to be one function that I send the line I want to digitally sign and in return I will receive the hash ?

    Thanks,


    Paulo
    Last edited by wysota; 7th April 2011 at 23:53. Reason: missing [code] tags

  16. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: RSA, Private and Public Key and Digital Signature

    You can use it as it is, you don't have to "adapt" anything.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  17. #15
    Join Date
    Sep 2009
    Posts
    7
    Thanks
    2

    Default Re: RSA, Private and Public Key and Digital Signature

    Dear wysota,

    Its incompatible with QT.

    Paulo

  18. #16
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: RSA, Private and Public Key and Digital Signature

    What is incompatible with Qt?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  19. #17
    Join Date
    Sep 2009
    Posts
    7
    Thanks
    2

    Default Re: RSA, Private and Public Key and Digital Signature

    TMemoryStream <- Does not exist
    EVP_SignUpdate(&mdctx, (System::PByte)in->Memory, in->Size) <-(System::PByte)in->Memory, in->Size not compativel
    SecureZeroMemory(pKey, sizeof(*pKey)); <- This function does not exist

  20. #18
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: RSA, Private and Public Key and Digital Signature

    Well... to me it seems natural that if you copy and paste a part of some program into your own, you will encounter classes and calls that do not exist in your program. This doesn't make this snippet "incompatible with Qt", it makes it incompatible with your program. I'm assuming you can substitute TMemoryStream with QByteArray and SecureZeroMemory() with memset() or an equivalent function from OpenSSL that will wipe out the key from memory.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  21. #19
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: RSA, Private and Public Key and Digital Signature

    Paulo,
    Qt is a library of GUI and other classes that can be used in your program. OpenSSL is a library of cryptographic routines that can be used in your program. You can use either, or both, libraries from C++ code but you have to provide the compiler with correct C++ code.

    Your problems are all related to providing your C++ compiler with syntactically complete and correct code and nothing to do with the Qt or OpenSSL libraries. As you pointed out, you are missing a declaration and implementation of the TMemoryStream class and other bits. You need to supply the compiler with include files and matching libraries that define TMemoryStream, or substitute something for the TMemoryStream class (as Wysota is saying above).

    TMemoryStream looks like it part of Borland C++ Builder
    SecureZeroMemory is a Windows API call.

    Using the C memset() function is not a secure erase mechanism unless precautions are taken to ensure that the compiler does not optimise the call out (because you typically don't use the erased memory again before discarding it).
    Last edited by ChrisW67; 8th April 2011 at 03:30.

Similar Threads

  1. OpenSSL and RSA :: digital signature
    By josecarlosmissias in forum Qt Programming
    Replies: 5
    Last Post: 7th May 2010, 14:23
  2. Digital Signing
    By DiamonDogX in forum Qt Programming
    Replies: 4
    Last Post: 21st May 2009, 22:09
  3. private and public slots ??
    By salmanmanekia in forum Qt Programming
    Replies: 2
    Last Post: 13th August 2008, 11:49
  4. Using QT for Digital TV menu system?
    By mittalpa in forum Newbie
    Replies: 4
    Last Post: 2nd July 2008, 19:23

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.