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 support for RSA3072 and RSA4096. #114

Merged
merged 3 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/cms_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -1783,11 +1783,12 @@ generate_auth_info(cms_context *cms, SECItem *der, char *url)

int
generate_keys(cms_context *cms, PK11SlotInfo *slot,
SECKEYPrivateKey **privkey, SECKEYPublicKey **pubkey)
SECKEYPrivateKey **privkey, SECKEYPublicKey **pubkey,
int key_bits, unsigned long exponent)
{
PK11RSAGenParams rsaparams = {
.keySizeInBits = 2048,
.pe = 0x010001,
.keySizeInBits = key_bits,
.pe = exponent,
};

SECStatus rv;
Expand Down
3 changes: 2 additions & 1 deletion src/cms_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,8 @@ extern int generate_signature(cms_context *ctx);
extern int unlock_nss_token(cms_context *ctx);
extern int find_certificate(cms_context *ctx, int needs_private_key);
extern int generate_keys(cms_context *cms, PK11SlotInfo *slot,
SECKEYPrivateKey **privkey, SECKEYPublicKey **pubkey);
SECKEYPrivateKey **privkey, SECKEYPublicKey **pubkey,
int key_bits, unsigned long exponent);
extern int is_issuer_of(CERTCertificate *c0, CERTCertificate *c1);

typedef int (find_cert_match_t)(CERTCertificate *cert, void *cbdata);
Expand Down
60 changes: 58 additions & 2 deletions src/efikeygen.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ bundle_signature(cms_context *cms, SECItem *sigder, SECItem *data,
errx(1, "could not encode certificate: %s",
PORT_ErrorToString(PORT_GetError()));

sigder->data[sigder->len - 261] = DER_BIT_STRING;
//Note: offset is signature size + 5 bytes for DER encoding
sigder->data[sigder->len - (signature->len + 5)] = DER_BIT_STRING;

return 0;
}
Expand Down Expand Up @@ -688,6 +689,31 @@ long verbosity(void)
return verbose;
}

struct algorithm {
char name[16];
int key_bits;
unsigned long exponent;
};

struct algorithm algorithms[] = {
{.name = "rsa2048",
.key_bits = 2048,
.exponent = 0x010001ul,
},
{.name = "rsa3072",
.key_bits = 3072,
.exponent = 0x010001ul,
},
{.name = "rsa4096",
.key_bits = 4096,
.exponent = 0x010001ul,
},
{.name = "",
.key_bits = 0,
.exponent = 0,
}
};

int main(int argc, char *argv[])
{
int is_ca = 0;
Expand Down Expand Up @@ -716,6 +742,10 @@ int main(int argc, char *argv[])
PRStatus prstatus;
void *frees[50] = { NULL, };
int nfrees = 0;
int key_bits = 2048;
unsigned long exponent = 0x010001ul;
char *orig_algo = "rsa2048";
char *algo = orig_algo;

cms_context *cms = NULL;

Expand Down Expand Up @@ -758,6 +788,12 @@ int main(int argc, char *argv[])
.descrip = "Generate a self-signed certificate" },

/* stuff about the generated key */
{.longName = "algorithm",
.shortName = 'a',
.argInfo = POPT_ARG_STRING|POPT_ARGFLAG_SHOW_DEFAULT,
.arg = &algo,
.descrip = "Algorithm for keys",
.argDescrip = "<algorithm>" },
{.longName = "kek",
.shortName = 'K',
.argInfo = POPT_ARG_VAL|POPT_ARGFLAG_OR|POPT_ARGFLAG_DOC_HIDDEN,
Expand Down Expand Up @@ -915,6 +951,7 @@ int main(int argc, char *argv[])

while ((rc = poptGetNextOpt(optCon)) > 0) {
switch (rc) {
case 'a': frees[nfrees++] = algo; break;
case 'c': frees[nfrees++] = cn; break;
case 'D': frees[nfrees++] = db_path; break;
case 'd': frees[nfrees++] = dbdir; break;
Expand All @@ -941,6 +978,14 @@ int main(int argc, char *argv[])

poptFreeContext(optCon);

if (strcmp(algo, "help") == 0) {
printf("Supported algorithms:");
for (int i = 0; algorithms[i].name[0] != '\0'; i++)
printf(" %s", algorithms[i].name);
printf("\n");
exit(0);
}

/*
* Scenarios that are okay (x == valid combination)
*
Expand Down Expand Up @@ -969,6 +1014,16 @@ int main(int argc, char *argv[])
if (!is_self_signed && !signer)
errx(1, "signing certificate is required");

for (int i=0; true; i++) {
if (strcmp(algorithms[i].name, "") == 0)
errx(1, "invalid algorithm: \"%s\"", algo);
if (strcmp(algorithms[i].name, algo) == 0) {
key_bits = algorithms[i].key_bits;
exponent = algorithms[i].exponent;
break;
}
}

cms->tokenname = tokenname;
cms->certname = signer;

Expand Down Expand Up @@ -1022,7 +1077,8 @@ int main(int argc, char *argv[])
nsserr(1, "could not find NSS slot for token \"%s\"",
cms->tokenname);

rc = generate_keys(cms, slot, &privkey, &pubkey);
rc = generate_keys(cms, slot, &privkey, &pubkey, key_bits,
exponent);
}
if (rc < 0)
exit(1);
Expand Down
Loading