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 automatic support for p11-kit #260

Merged
merged 8 commits into from
Jun 12, 2023
Merged

Add automatic support for p11-kit #260

merged 8 commits into from
Jun 12, 2023

Conversation

maraino
Copy link
Contributor

@maraino maraino commented Jun 8, 2023

Description

This commit adds support to automatically use the p11-kit-proxy module on the PKCS#11 KMS if no other module has been specified.

Fixes #259

cc: @dwmw2

This commit adds support to automatically use the p11-kit-proxy module
on the PKCS#11 KMS if no other module has been specified.

Fixes #259
Copy link
Member

@hslatman hslatman left a comment

Choose a reason for hiding this comment

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

The test could be improved, because right now the logic is (largely) dependent on the function that is being tested. I don't think it needs something very elaborate.

Other than that, looks good and interesting utility 😄

Comment on lines 48 to 51
if findP11KitProxy(context.Background()) != "" {
wantMissingModule = k
wantErrMissingModule = false
}
Copy link
Member

Choose a reason for hiding this comment

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

This would be a more accurate test if the output of the shell command were mocked instead.

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'll use var findP11KitProxy = func(...) {...}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed with 237a370

@dwmw2
Copy link

dwmw2 commented Jun 8, 2023

Reporting p11-kit-proxy.so instead of .dylib seems like a p11-kit bug. Is it reported and fixed upstream?

In some systems without development packages installed, pkg-config may be absent. Can we try just p11-kit-proxy.(so|dylib) with no path as a fallback?

As a follow-up, it would also be good to remove the explicit module-parh from most of the examples in the documentation, and make it clear that needing do so is a pathological case for a misconfigured system.

@maraino
Copy link
Contributor Author

maraino commented Jun 8, 2023

Reporting p11-kit-proxy.so instead of .dylib seems like a p11-kit bug. Is it reported and fixed upstream?

I haven't.

In some systems without development packages installed, pkg-config may be absent. Can we try just p11-kit-proxy.(so|dylib) with no path as a fallback?

Sure, that worked for me.

As a follow-up, it would also be good to remove the explicit module-parh from most of the examples in the documentation, and make it clear that needing do so is a pathological case for a misconfigured system.

People use this on containers where p11-kit is not available.

hslatman
hslatman previously approved these changes Jun 8, 2023
@maraino
Copy link
Contributor Author

maraino commented Jun 8, 2023

@hslatman @dwmw2: In my last commit, 1e80f66, I removed the pkg-config code and rely on dlopen to find the right library in the default paths.

@dwmw2
Copy link

dwmw2 commented Jun 9, 2023

Looks good to me; thanks for the prompt solution!

People use this on containers where p11-kit is not available.

Well... I might argue that those come under the heading of 'misconfigured'. p11-kit is small and it should be present. And even when you only have one provider that you want to use, its packaging should still ship it with the p11-kit .module file and it shouldn't need to be manually specified.

But that's my crusade, not yours :) And I'll begrudgingly accept the counter-argument if you have only one token then p11-kit, tiny though it may be, is overkill.

I'd still like to see the documentation at least mention that in a properly configured system, module-path should be unnecessary.

// the full path.
if config.Path = u.Get("module-path"); config.Path == "" {
config.Path = defaultModule
}
Copy link

Choose a reason for hiding this comment

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

This is in the wrong place, isn't it? It'll only happen if a URI is given, but there's an alternative code flow where it's all in the config? That should benefit from the default, too.

There's already a check for config.Path == "" a few lines further down, with an error return which is no longer telling the truth because it says that 'module-path is required'.

Also... what's wrong with pkcs11:manufacturer=piv_II;id=%01 which I usually use as my examples in documentation? Why are only token/serial/slot-id accepted, and not model/manufacturer?

(And why is this something you even need to do for yourself; surely "match token against the attributes in a URI" is a generic thing that ought to be somewhere much further down the stack than your code; in ThalesIgnite/crypto11 or miekg/pkcs11. Ideally you just want to call a function like the matchSlots() I wrote here, and in your case as discussed, you want to just bail if len(slots) != 1).

Copy link

Choose a reason for hiding this comment

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

I'd still like to see the documentation at least mention that in a properly configured system, module-path should be unnecessary.

In particular, these two:

.. especially the former. I don't want to live in a world where we have to tell users to explicitly pass a module-path.

All those examples for how to use different HSMs differ mostly in the module-path, which shouldn't be needed anyway.

The packaging of the PKCS#11 provider module itself should come with a p11-kit .module file, the token should automatically appear in the output of things like p11-kit list-tokens or p11tool --list-tokens (and thus also in GUIs when you're selecting a client cert for 802.1x or VPN auth, etc.)

The common case is that user should just be able to select a token from the list, e.g. pkcs11:manufacturer=piv_II.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is in the wrong place, isn't it? It'll only happen if a URI is given, but there's an alternative code flow where it's all in the config? That should benefit from the default, too.

PKCS#11 cannot be configured without a URI, currently you will get the following error:

return nil, errors.New("kms uri 'module-path' are required")

We can make it more explicit and actually fail if URI == "".

Also... what's wrong with pkcs11:manufacturer=piv_II;id=%01 which I usually use as my examples in documentation? Why are only token/serial/slot-id accepted, and not model/manufacturer?

Nothing, but https://github.com/ThalesIgnite/crypto11, which is the one we use on top of github.com/miekg/pkcs11 doesn't support it, see this code.

Regarding the documentation, some of it is managed by a different team, and we haven't integrated this yet, so it will have to wait a bit. But I can change step-kms-plugin README and help once this is integrated.

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've fixed some of this in a23378d

Copy link

Choose a reason for hiding this comment

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

Nothing, but https://github.com/ThalesIgnite/crypto11, which is the one we use on top of github.com/miekg/pkcs11 doesn't support it, see this code.

Thanks. I've added that specific reference to ThalesGroup/crypto11#104

@maraino maraino disabled auto-merge June 9, 2023 17:46
@dwmw2
Copy link

dwmw2 commented Jun 10, 2023

FWIW (although I don't think it matters any more) the proxy_module reported by p11-kit should be fixed with p11-glue/p11-kit#516

@maraino
Copy link
Contributor Author

maraino commented Jun 12, 2023

FWIW (although I don't think it matters any more) the proxy_module reported by p11-kit should be fixed with p11-glue/p11-kit#516

Yes, using pkg-config just make things more complicated, when dlopen can find it in most cases, and if it can't you can always set the right environment variables or change configuration files.

kms/pkcs11/pkcs11.go Outdated Show resolved Hide resolved
kms/pkcs11/pkcs11.go Outdated Show resolved Hide resolved
kms/pkcs11/pkcs11.go Outdated Show resolved Hide resolved
kms/pkcs11/pkcs11.go Outdated Show resolved Hide resolved
kms/pkcs11/pkcs11.go Outdated Show resolved Hide resolved
if err != nil {
return nil, err
return nil, errors.Wrap(err, "kms uri 'slot-id' is not valid")
Copy link
Member

Choose a reason for hiding this comment

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

Could include the value of slot-id

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The wrapped error contains the string.

Copy link
Member

Choose a reason for hiding this comment

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

Fair enough

Co-authored-by: Herman Slatman <hslatman@users.noreply.github.com>
@maraino maraino requested a review from hslatman June 12, 2023 18:25
@maraino maraino merged commit 1e0726a into master Jun 12, 2023
11 of 13 checks passed
@maraino maraino deleted the p11-kit-proxy branch June 12, 2023 18:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use p11-kit-proxy.so as default provider where available
3 participants