Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add rsa key import methods to handle pem and der encoding directly #252

Merged
merged 6 commits into from Jan 27, 2023

Conversation

jpbland1
Copy link
Contributor

@jpbland1 jpbland1 commented Jan 11, 2023

ZD 14996

@jpbland1 jpbland1 requested a review from dgarske January 11, 2023 23:55
@jpbland1 jpbland1 marked this pull request as ready for review January 11, 2023 23:55
@dgarske dgarske self-assigned this Jan 12, 2023
@@ -33,16 +33,47 @@

#ifndef WOLFTPM2_NO_WRAPPER

static const char* kRsaKeyPrivPem =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a comment about where this RSA private key came from. Like ./certs/server-key.pem?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved it to a file instead, I was just copying the way the der key was included raw

@@ -55,6 +86,8 @@ int TPM2_Keyimport_Example(void* userCtx, int argc, char *argv[])
TPM_ALG_ID paramEncAlg = TPM_ALG_NULL;
WOLFTPM2_SESSION tpmSession;
const char* outputFile = "keyblob.bin";
uint8_t derEncode = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like stdint.h types, but please use byte. Thanks

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so is it only wolfBoot that uses uint types?

}
else if (pemEncode == 1) {
rc = wolfTPM2_RsaPrivateKeyImportPem(&dev, &storage, &impKey,
kRsaKeyPrivPem, strlen(kRsaKeyPrivPem), NULL,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use (word32)XSTRLEN(kRsaKeyPrivPem).

src/tpm2_wrap.c Outdated
word32 pSz = sizeof(p);
word32 qSz = sizeof(q);

if (dev && parentKey && keyBlob && input && inSz != 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confusing logic. Would prefer init rc = 0 and set rc = BAD_FUNC_ARG; on failure.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw it in some wolfssl function and thought it was concise, if it's not preferred I'll change it

src/tpm2_wrap.c Outdated
byte d[RSA_MAX_SIZE / 8];
byte p[RSA_MAX_SIZE / 8];
byte q[RSA_MAX_SIZE / 8];
word32 eSz = sizeof(e);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Must cast these: word32 eSz = (word32)sizeof(e);

src/tpm2_wrap.c Show resolved Hide resolved
src/tpm2_wrap.c Outdated
/* der size is base 64 decode length */
derSz = inSz * 3 / 4 + 1;

derBuf = XMALLOC(derSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

derBuf = (byte*)XMALLOC

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to remember that, you've corrected me on it before but I keep doing it

src/tpm2_wrap.c Outdated
if (rc == 0)
rc = wc_KeyPemToDer((byte*)input, inSz, derBuf, derSz, pass);

/* returns the number of bytes*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: space between bytes*/

src/tpm2_wrap.c Outdated
rc = wc_KeyPemToDer((byte*)input, inSz, derBuf, derSz, pass);

/* returns the number of bytes*/
if (rc > 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about rc == 0 case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you have a der key with 0 byte length?I thought that would return bad arg error, I looked at the underlying function PemToDer and if it doesn't find an asn header it returns ASN_NO_PEM_HEADER which is less than 0

@@ -77,6 +110,15 @@ int TPM2_Keyimport_Example(void* userCtx, int argc, char *argv[])
else if (XSTRCMP(argv[argc-1], "-xor") == 0) {
paramEncAlg = TPM_ALG_XOR;
}
else if (XSTRCMP(argv[argc-1], "-pem") == 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-pem is in here twice. Looks like copy/paste issue. This one should be deleted.

@dgarske dgarske assigned jpbland1 and unassigned dgarske Jan 13, 2023
@dgarske dgarske self-requested a review January 23, 2023 21:30
@dgarske dgarske self-assigned this Jan 23, 2023
certs/example-rsa-key.pem Show resolved Hide resolved
byte derEncode = 0;
byte pemEncode = 0;
FILE* pemFile = NULL;
char pemBuf[2048];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just use WOLFTPM2_MAX_BUFFER

rc = wolfTPM2_RsaPrivateKeyImportPem(&dev, &storage, &impKey,
kRsaKeyPrivPem, strlen(kRsaKeyPrivPem), NULL,
TPM_ALG_NULL, TPM_ALG_NULL);
pemFile = fopen("./certs/example-rsa-key.pem", "r");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use XFOPEN

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add support to take the key filename as argument. Default to this example.

TPM_ALG_NULL, TPM_ALG_NULL);
pemFile = fopen("./certs/example-rsa-key.pem", "r");

rc = fread(pemBuf, 1, 2048, pemFile);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use XFREAD and use sizeof(pemBuf)

pemBuf, rc, NULL, TPM_ALG_NULL, TPM_ALG_NULL);
}

fclose(pemFile);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

XFCLOSE

src/tpm2_wrap.c Outdated
if (derBuf == NULL)
return MEMORY_E;

if (rc == 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This rc == 0 is not needed. Remove please

examples/keygen/keyimport.c Show resolved Hide resolved
@dgarske dgarske removed their assignment Jan 23, 2023
@jpbland1 jpbland1 requested a review from dgarske January 25, 2023 02:02
@dgarske dgarske self-assigned this Jan 25, 2023
certs/include.am Outdated
@@ -7,4 +7,5 @@ EXTRA_DIST += \
certs/ca-rsa.cnf \
certs/ca-ecc.cnf \
certs/wolf-ca-ecc-cert.pem \
certs/wolf-ca-rsa-cert.pem
certs/wolf-ca-rsa-cert.pem \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Indentation is off here.

printf("* -ecc: Use RSA or ECC for keys\n");
printf("* -aes/xor: Use Parameter Encryption\n");
printf("* -pem/der: Key encoding type, none for binary\n");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: I'd prefer -pem= support where the =name is optional.

byte pemEncode = 0;
FILE* pemFile = NULL;
char pemBuf[WOLFTPM2_MAX_BUFFER];
char pemName[WOLFTPM2_MAX_BUFFER];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is too long for filename and it should just be a char* with default that can be changed.

const char* pemName = "./certs/example-rsa-key.pem";

/* then later just set this to the argv */
pemName = (const char*)argv[i + 1];`

printf("Failed to read pem file %s\n", pemName);

if (rc == 0)
rc = XFREAD(pemBuf, 1, sizeof(pemBuf), pemFile);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add explicit cast to int:

examples/keygen/keyimport.c:167:22: error: implicit conversion loses integer precision: 'unsigned long' to 'int' [-Werror,-Wshorten-64-to-32]
                rc = XFREAD(pemBuf, 1, sizeof(pemBuf), pemFile);
                   ~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how do I test this, I need to set CC to clang?

printf("Warning: No pem file specified, using default: %s\n", pemName);
}
else {
XMEMCPY(pemName, argv[i + 1], XSTRLEN(argv[i + 1]) + 1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please print the name of the file used in the console output.

examples/keygen/keyimport.c Show resolved Hide resolved
src/tpm2_wrap.c Show resolved Hide resolved
src/tpm2_wrap.c Outdated
derSz = inSz * 3 / 4 + 1;

derBuf = (byte*)XMALLOC(derSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the extra line here.

@dgarske dgarske removed their assignment Jan 26, 2023
@jpbland1 jpbland1 requested a review from dgarske January 27, 2023 00:02
Copy link
Contributor

@dgarske dgarske left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very close! Looks great, just a couple comments.

src/tpm2_wrap.c Outdated
word32 pSz = (word32)sizeof(p);
word32 qSz = (word32)sizeof(q);

wc_InitRsaKey(key, NULL);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check the return code here. Only call free if rc == 0.

src/tpm2_wrap.c Show resolved Hide resolved
@jpbland1 jpbland1 requested a review from dgarske January 27, 2023 21:49
@dgarske dgarske assigned dgarske and unassigned jpbland1 Jan 27, 2023
@dgarske dgarske merged commit 3a1ece8 into wolfSSL:master Jan 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants