Add RSA key importing functionality for OpenSSL private keys #223

Closed
opened 2017-05-30 01:31:55 -05:00 by beamer159 · 11 comments
beamer159 commented 2017-05-30 01:31:55 -05:00 (Migrated from github.com)

TomCrypt currently supports importing OpenSSL RSA public keys. However, OpenSSL private keys cannot be imported currently. Adding this feature would expand this library's usability.

TomCrypt currently supports importing OpenSSL RSA public keys. However, OpenSSL private keys cannot be imported currently. Adding this feature would expand this library's usability.
karel-m commented 2017-05-30 02:15:29 -05:00 (Migrated from github.com)

Could you please post a sample key that you cannot import? Or even better an openssl command used for "unimportable" key generation e.g. openssl genrsa ...?

Could you please post a sample key that you cannot import? Or even better an openssl command used for "unimportable" key generation e.g. `openssl genrsa ...`?
sjaeckel commented 2017-05-30 04:03:47 -05:00 (Migrated from github.com)

If you're talking about password-protected RSA private keys you're right. If you remove the password from the key you should be able to import it.
But providing am example-key would be best.

If you're talking about password-protected RSA private keys you're right. If you remove the password from the key you should be able to import it. But providing am example-key would be best.
karel-m commented 2017-05-30 07:43:35 -05:00 (Migrated from github.com)

If you generate an RSA key like this:

openssl genrsa -out test_rsa_private.pem 2048
openssl rsa -in test_rsa_private.pem -out test_rsa_private.der -outform der
openssl rsa -in test_rsa_private.pem -out test_rsa_public.der -outform der -pubout

libtomcrypt should be able to load test_rsa_private.der and test_rsa_public.der (if not then it's a bug).

If you generate an RSA key like this: ``` openssl genrsa -out test_rsa_private.pem 2048 openssl rsa -in test_rsa_private.pem -out test_rsa_private.der -outform der openssl rsa -in test_rsa_private.pem -out test_rsa_public.der -outform der -pubout ``` libtomcrypt should be able to load `test_rsa_private.der` and `test_rsa_public.der` (if not then it's a bug).
beamer159 commented 2017-05-30 08:57:27 -05:00 (Migrated from github.com)

Here is the OpenSSL private key I am trying to open. It is not password-protected:

https://pastebin.com/nSZM1Gpt

Some things that I noticed:

For public keys, OpenSSL DER-encodes them differently from TomCrypt keys. A TomCrypt public key has the following structure:

SEQUENCE
    INTEGER
    INTEGER

OpenSSL keys have a different structure:

SEQUENCE
    SEQUENCE
        OBJECT IDENTIFIER
        NULL
    BIT STRING
        SEQUENCE
            INTEGER
            INTEGER

In rsa_import.c, I can find the code that converts the OpenSSL public key structure to the TomCrypt public key structure.

OpenSSL private keys are also encoded differently from TomCrypt private keys. Here's a TomCrypt private key structure:

SEQUENCE
    INTEGER
    INTEGER
    INTEGER
    INTEGER
    INTEGER
    INTEGER
    INTEGER
    INTEGER
    INTEGER

And here's an OpenSSL private key structure:

SEQUENCE
    INTEGER
    SEQUENCE
        OBJECT IDENTIFIER
        NULL
    BIT STRING
        SEQUENCE
            INTEGER
            INTEGER
            INTEGER
            INTEGER
            INTEGER
            INTEGER
            INTEGER
            INTEGER
            INTEGER

However, I don't see any code in rsa_import.c to convert this OpenSSL structure to a TomCrypt structure. It looks like rsa_import.c first tries to decode the key like an OpenSSL public key, then tries to decode the key like a TomCrypt public key, and finally tries to decode the like like a TomCrypt private key.

Here is the OpenSSL private key I am trying to open. It is not password-protected: https://pastebin.com/nSZM1Gpt Some things that I noticed: For public keys, OpenSSL DER-encodes them differently from TomCrypt keys. A TomCrypt public key has the following structure: SEQUENCE INTEGER INTEGER OpenSSL keys have a different structure: SEQUENCE SEQUENCE OBJECT IDENTIFIER NULL BIT STRING SEQUENCE INTEGER INTEGER In rsa_import.c, I can find the code that converts the OpenSSL public key structure to the TomCrypt public key structure. OpenSSL private keys are also encoded differently from TomCrypt private keys. Here's a TomCrypt private key structure: SEQUENCE INTEGER INTEGER INTEGER INTEGER INTEGER INTEGER INTEGER INTEGER INTEGER And here's an OpenSSL private key structure: SEQUENCE INTEGER SEQUENCE OBJECT IDENTIFIER NULL BIT STRING SEQUENCE INTEGER INTEGER INTEGER INTEGER INTEGER INTEGER INTEGER INTEGER INTEGER However, I don't see any code in rsa_import.c to convert this OpenSSL structure to a TomCrypt structure. It looks like rsa_import.c first tries to decode the key like an OpenSSL public key, then tries to decode the key like a TomCrypt public key, and finally tries to decode the like like a TomCrypt private key.
karel-m commented 2017-05-30 09:43:47 -05:00 (Migrated from github.com)

Your keys has PKCS#8 format.

The function rsa_import_pkcs8 from #157 is able to load it.

Your keys has PKCS#8 format. The function `rsa_import_pkcs8` from #157 is able to load it.
beamer159 commented 2017-05-30 10:07:32 -05:00 (Migrated from github.com)

That function is only provided in dev, correct? Not in the latest release, 1.17?

That function is only provided in dev, correct? Not in the latest release, 1.17?
karel-m commented 2017-05-30 10:15:38 -05:00 (Migrated from github.com)

Unfortunately it is not even in develop it's currently a pull request.

Just take this file github.com/libtom/libtomcrypt@fbcad861e4/src/pk/rsa/rsa_import_pkcs8.c

Unfortunately it is not even in `develop` it's currently a pull request. Just take this file https://github.com/libtom/libtomcrypt/blob/fbcad861e47bd5e9fae047f1da0553adb352abdd/src/pk/rsa/rsa_import_pkcs8.c
beamer159 commented 2017-06-01 13:47:53 -05:00 (Migrated from github.com)

Thank you, I may have gotten that code working a bit, it's very helpful.

I have a question about some parts. The code has rsaoid which is an oid_st datatype. The code also uses corresponding functions pk_get_oid(...). This datatype and function are not in the latest release, 1.17. It looks like this variable is only used to compare with something else. What would be the consequences of removing this check from my implementation?

Thank you, I may have gotten that code working a bit, it's very helpful. I have a question about some parts. The code has rsaoid which is an oid_st datatype. The code also uses corresponding functions pk_get_oid(...). This datatype and function are not in the latest release, 1.17. It looks like this variable is only used to compare with something else. What would be the consequences of removing this check from my implementation?
beamer159 commented 2017-06-06 09:24:51 -05:00 (Migrated from github.com)

Is there any similar code to convert OpenSSL ECC private and public keys to be TomCrypt compliant?

Is there any similar code to convert OpenSSL ECC private and public keys to be TomCrypt compliant?
sjaeckel commented 2017-06-06 10:53:17 -05:00 (Migrated from github.com)

Is there any similar code to convert OpenSSL ECC private and public keys to be TomCrypt compliant?

PR #65 has that, but it's also not merged yet as it's dependant on #187 which isn't merged yet

> Is there any similar code to convert OpenSSL ECC private and public keys to be TomCrypt compliant? PR #65 has that, but it's also not merged yet as it's dependant on #187 which isn't merged yet
sjaeckel commented 2017-06-08 07:00:43 -05:00 (Migrated from github.com)

I'm closing this one as it's a duplicate of #157

I'm closing this one as it's a duplicate of #157
Sign in to join this conversation.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
gh-libtom/libtomcrypt#223
No description provided.