I'm attempting to generate a private/public key pair in a few different standards and formats for testing an encryption library I'm working on (i.e. keys with the same "guts", but different formats and standards). Specifically I want a keys generated by OpenSSL in the DER and PEM formats, using the PKCS#1, PKCS#8, and "traditional" standards for each. The "traditional" standard is because apparently OpenSSL did its own thing at one point in time and kind of invented a standard, and it is still possible to generate keys using that "standard" with the -traditional
option. So in the end I would have 12 unique files:
|
DER |
PEM |
PKCS#1 |
pub/priv |
pub/priv |
PKCS#8 |
pub/priv |
pub/priv |
Traditional |
pub/priv |
pub/priv |
I first generated a private/public key pair using the following commands:
openssl genrsa -out private.key 2048
openssl rsa -in private.key -pubout -out public.key
And was given this private key (ASN.1 decoding here):
-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAr2xKE7fuT/VV2Lk7gfCkA4xOTcFXWboTJ6ZGx1zWCP8d1pY5
mYPx/dTUgDtUjaYGIRJy6G8xYLZvj22aY3l/DdfgLfk4Br9katexMSmKR0C9hVBW
DbCk6ROK9dqEXuzGmpXhfcYs/9dL2N+CptjsS3PcBjxslcBJhUM60jLV+13No95D
Bw1f1PCEb3QNffxxVBEYLzv12xgafSjaCo+uY/BUgKbmU3OO6W1w+8z817t+n11m
ufobCHpyx5f9x7O66gEcT8YT6FtYEPSYVbxPqXveBZaVAUe0uKlvd7yZE5ZAfyKH
LNpT85ay/yfA6O4B9hwslM2El5ge3FKL53jVFQIDAQABAoIBACoD+QsXgPX4OB9A
IFtbOGFcK84OSn5kZZ2XwviRiPcfKXWhxfp5oo5t+Racf0As/WdS7KA0r6IvF/HF
qdZ8/VwisiA4wn1FPocjkZ5JiYPO4wWo6+97+UeU5XHErHMy6U+RqfAutMzzM0im
ofuV1aStw4tf0g8c/s7y/wXk5KD/XNQtdaIqIS3lipIFhvVLSdPsUFU8KhiNKU99
QbGNC7rFvrjdU0jbVgIWPuF7ffT3c0aR6x68Y8b8FOLiD140n7grvllSklnUia2S
zctC4mhZT/9Vfa8cUio+ODGeaXiPqmPsWKJ2kUPSOSR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8d1pY5mYPx/dTUgDtUjaYGIRJy6G8xYLZvj22aY3l/
DdfgLfk4Br9katexMSmKR0C9hVBWDbCk6ROK9dqEXuzGmpXhfcYs/9dL2N+Cptjs
S3PcBjxslcBJhUM60jLV+13No95DBw1f1PCEb3QNffxxVBEYLzv12xgafSjaCo+u
Y/BUgKbmU3OO6W1w+8z817t+n11mufobCHpyx5f9x7O66gEcT8YT6FtYEPSYVbxP
qXveBZaVAUe0uKlvd7yZE5ZAfyKHLNpT85ay/yfA6O4B9hwslM2El5ge3FKL53jV
FQIDAQAB
-----END PUBLIC KEY-----
Based on my reading I was expecting the commands I ran to generate PEM formatted PKCS#1 standard keys, but looking at the ASN.1 decoder, I'm seeing extra pieces of structure I wasn't expecting. My expectations were set by reading the Mbed TLS docs here, because I haven't been able to find any better source that clearly identifies what exactly should be expected in the various PKCS structures. The structure of the public key is identical except the sequence which I drew a box around only contains the exponent and modulus:
I then run the following command to see what a "traditional" key looks like:
openssl rsa -in private.key -traditional -out private_traditional.key
And was given this private key (ASN.1 decoding here):
-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAr2xKE7fuT/VV2Lk7gfCkA4xOTcFXWboTJ6ZGx1zWCP8d1pY5
mYPx/dTUgDtUjaYGIRJy6G8xYLZvj22aY3l/DdfgLfk4Br9katexMSmKR0C9hVBW
DbCk6ROK9dqEXuzGmpXhfcYs/9dL2N+CptjsS3PcBjxslcBJhUM60jLV+13No95D
Bw1f1PCEb3QNffxxVBEYLzv12xgafSjaCo+uY/BUgKbmU3OO6W1w+8z817t+n11m
ufobCHpyx5f9x7O66gEcT8YT6FtYEPSYVbxPqXveBZaVAUe0uKlvd7yZE5ZAfyKH
LNpT85ay/yfA6O4B9hwslM2El5ge3FKL53jVFQIDAQABAoIBACoD+QsXgPX4OB9A
IFtbOGFcK84OSn5kZZ2XwviRiPcfKXWhxfp5oo5t+Racf0As/WdS7KA0r6IvF/HF
qdZ8/VwisiA4wn1FPocjkZ5JiYPO4wWo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8KdHv7ZgDmkuSR/fJka8sqjTVzeWvzyKCz7kSx+K0QvIPHqj
-----END RSA PRIVATE KEY-----
And when I load that key into the ASN.1 decoder I see what I was expecting to see when I loaded the PKCS#1 structured key:
I guess my most pressing question is, what is the exact structure expected of PKCS#1 and PKCS#8? I've looked, but I must be searching the wrong term or something because the Mbed TLS docs I linked to earlier was the best description I've found, but at this point I'm not sure it is correct.
My second question is, do the keys I've identified in the table make sense?
Examples:
- Would no one ever use a PEM PKCS#8 key (OpenSSL will only output a PKCS#8 structured key it in DER format unless it is encrypted).
- Would a public key always be PKCS#1 format and never PKCS#8 or "traditional" format?
- Any other similar question that would make the format/structure combination of a key pair identified in the table nonsensical.