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

Enable RSA-PSS signatures in pkcs11-tool #1146

Merged
merged 20 commits into from
Sep 21, 2017
Merged
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
a7a4e6c
Add missing SHA224 RSA algorithms
Jakuje Sep 6, 2017
811a6b9
Fix wrong replacement in pkcs11-tool manual page
Jakuje Sep 6, 2017
6d37b6c
Add MGF and PSS_PARAMS definitions in PKCS#11 header file
Jakuje Sep 6, 2017
89309a5
Inspect PSS signature parameters in pkcs11-spy
Jakuje Sep 6, 2017
1efb774
Enable RSA-PSS signatures in pkcs11-tool
Jakuje Sep 6, 2017
c2d8ee5
Added short names to RSA-PSS methods
Sep 7, 2017
ec8dd42
Change RSA-PSS salt length default to OpenSSL-compatible, aka digest …
Sep 7, 2017
aaecfdb
Fixed hashAlg but in pkcs11-tool for RSA-PSS
Sep 7, 2017
da9bae0
CHanged opt_salt to salt_len
mouse07410 Sep 8, 2017
7513ec2
Fixed type of salt length from unsigned long to long
mouse07410 Sep 9, 2017
cedbd3e
RSA-PSS: made sure special values for salt length from OpenSSL ("-1" …
mouse07410 Sep 9, 2017
0b7e5f0
Refactored dealing with salt length, and added input check
mouse07410 Sep 10, 2017
f2f53c1
Fix introduced incompatibility with C90 standard (declaration of vari…
mouse07410 Sep 11, 2017
ebd3ca2
Whitespace cleanup of mouse07410 commits
Jakuje Sep 11, 2017
55f0616
Add SHA-224 hash algorithm for RSA-PSS
Jakuje Sep 11, 2017
15659fb
Do not fallback to zero-length salt on unknown hash algorithms
Jakuje Sep 11, 2017
375e1c2
Add SHA224 definitions in pkcs11.h (for completenes)
Jakuje Sep 11, 2017
4450c2c
Reintroduce portable NORETURN indication for functions and use it to …
Jakuje Sep 13, 2017
e870706
Use default SHA-1 mechanisms, use --salt-len, improve wording of docu…
Jakuje Sep 15, 2017
63ae2f9
Check the mechanism type before dereferencing generic parameter
Jakuje Sep 18, 2017
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
Prev Previous commit
Next Next commit
Whitespace cleanup of mouse07410 commits
  • Loading branch information
Jakuje committed Sep 13, 2017
commit ebd3ca2354d5b3c5624b9748a28dd21ee07cbf0e
80 changes: 40 additions & 40 deletions src/tools/pkcs11-tool.c
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ static unsigned long opt_random_bytes = 0;
static CK_MECHANISM_TYPE opt_hash_alg = 0;
static unsigned long opt_mgf = 0;
static long salt_len = 0;
static int salt_len_given = 0; /* 0 - not given, 1 - given with input parameters */
static int salt_len_given = 0; /* 0 - not given, 1 - given with input parameters */

static void *module = NULL;
static CK_FUNCTION_LIST_PTR p11 = NULL;
Expand Down Expand Up @@ -696,7 +696,7 @@ int main(int argc, char * argv[])
break;
case OPT_SALT:
salt_len = (CK_ULONG) strtoul(optarg, NULL, 0);
salt_len_given = 1;
salt_len_given = 1;
break;
case 'o':
opt_output = optarg;
Expand Down Expand Up @@ -1629,25 +1629,25 @@ static int unlock_pin(CK_SLOT_ID slot, CK_SESSION_HANDLE sess, int login_type)

/* return digest length in bytes */
static unsigned long figure_pss_salt_length(const int hash) {
unsigned long sLen = 0;
switch (hash) {
case CKM_SHA_1:
sLen = 20;
break;
case CKM_SHA256:
sLen = 32;
break;
case CKM_SHA384:
sLen = 48;
break;
case CKM_SHA512:
sLen = 64;
break;
default:
sLen = 0;
break;
}
return sLen;
unsigned long sLen = 0;
switch (hash) {
case CKM_SHA_1:
sLen = 20;
break;
case CKM_SHA256:
sLen = 32;
break;
case CKM_SHA384:
sLen = 48;
break;
case CKM_SHA512:
sLen = 64;
break;
default:
sLen = 0;
break;
Copy link
Member Author

Choose a reason for hiding this comment

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

I am not sure if we should silently fall back to 0 length salt in case we got something unknown. It can bite us later (SHA224, SHA3 ?). We should probably exit here.

Copy link
Contributor

Choose a reason for hiding this comment

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

I see your point. But I'm not sure how we'd do a clean exit here without overly complicating things on the caller side.

As for SHA-3 and SHA224 - why don't we add them right now and be done with it?

Copy link
Member Author

Choose a reason for hiding this comment

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

All the way around pkcs11-tool, the we call util_fatal(), which calls exit. It is not nice, but it does its job for these unexpected situations. I

We can add CKM_SHA224 straight away, but there is no CKM_ for SHA3 yet in latest PKCS#11 standard. And when it will be there, we will forget about this switch.

I will add a commits addressing this.

Copy link
Contributor

Choose a reason for hiding this comment

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

OK, thanks. Makes sense to me. For an executable like pkcs11-tool using util_fatal() should be OK.

Copy link
Contributor

Choose a reason for hiding this comment

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

Darn... We must be using different PKCS#11 include files?!

. . . . .
  CC       cardos-tool.o
pkcs11-tool.c:1637:8: error: use of undeclared identifier 'CKM_SHA224'
        case  CKM_SHA224:
              ^
1 error generated.
make[3]: *** [pkcs11-tool.o] Error 1
. . . . .

Copy link
Member Author

Choose a reason for hiding this comment

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

Nope. I just did not check it compiles. I checked only the PKCS#11 specification, which defines them:

http:https://docs.oasis-open.org/pkcs11/pkcs11-curr/v2.40/cs01/pkcs11-curr-v2.40-cs01.html#_Toc399398977

I added them in b051d3b from the above source so it should build fine.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yep, thanks. Now everything compiles OK. ;-)

}
return sLen;
}

static void sign_data(CK_SLOT_ID slot, CK_SESSION_HANDLE session,
Expand Down Expand Up @@ -1693,7 +1693,7 @@ static void sign_data(CK_SLOT_ID slot, CK_SESSION_HANDLE session,
break;
default:
util_fatal("RSA-PKCS-PSS requires explicit hash mechanism");
}
}
pss_params.hashAlg = opt_hash_alg;
break;

Expand Down Expand Up @@ -1726,33 +1726,33 @@ static void sign_data(CK_SLOT_ID slot, CK_SESSION_HANDLE session,
hashlen = figure_pss_salt_length(pss_params.hashAlg);

if (salt_len_given == 1) { /* salt size explicitly given */
if (salt_len < 0 && salt_len != -1 && salt_len != -2)
util_fatal("Salt length must be greater or equal \
if (salt_len < 0 && salt_len != -1 && salt_len != -2)
util_fatal("Salt length must be greater or equal \
to zero, or equal to -1 (meaning: use digest size) or to -2 \
(meaning: use maximum permissible size");

modlen = (get_private_key_length(session, key) + 7) / 8;
switch(salt_len) {
case -1: /* salt size equals to digest size */
pss_params.sLen = hashlen;
break;
case -2: /* maximum permissible salt len */
pss_params.sLen = modlen - hashlen -2;
break;
default: /* use given size but its value must be >= 0 */
pss_params.sLen = salt_len;
break;
} /* end switch (salt_len_given) */
modlen = (get_private_key_length(session, key) + 7) / 8;
switch(salt_len) {
case -1: /* salt size equals to digest size */
pss_params.sLen = hashlen;
break;
case -2: /* maximum permissible salt len */
pss_params.sLen = modlen - hashlen -2;
break;
default: /* use given size but its value must be >= 0 */
pss_params.sLen = salt_len;
break;
} /* end switch (salt_len_given) */
} else { /* use default: salt len of digest size */
pss_params.sLen = hashlen;
pss_params.sLen = hashlen;
}

mech.pParameter = &pss_params;
mech.ulParameterLen = sizeof(pss_params);
fprintf(stderr, "PSS parameters: hashAlg=%s, mgf=%s, salt=%lu B\n",
p11_mechanism_to_name(pss_params.hashAlg),
p11_mgf_to_name(pss_params.mgf),
pss_params.sLen);
p11_mechanism_to_name(pss_params.hashAlg),
p11_mgf_to_name(pss_params.mgf),
pss_params.sLen);
}

if (opt_input == NULL)
Expand Down