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

Smart card credentials handling #206

Merged
merged 20 commits into from
Feb 7, 2024

Conversation

TheBestTvarynka
Copy link
Collaborator

@TheBestTvarynka TheBestTvarynka commented Feb 5, 2024

Hi,
In this PR I implemented the smart card credentials handling on the SSPI side. How it works: if the provided username is NT_ENTERPRISE and all needed environment variables with smart card credentials are present, then the SSPI will try to authenticate using an emulated smart card.
Also, I made a small refactoring in the ./ffi/src/sspi module.

All needed env vars are listed in this module of the winscard crate: https://github.com/Devolutions/sspi-rs/blob/winscard-rs-impl/crates/winscard/src/env.rs.

This is the last PR with a new implemented logic in the scope of this winscard implementation and integration.

Here is a small demo:

20240206005148.mp4

It took a username and PIN code (password) from the UI and the rest parameters (container name, reader name, certificate, private file, and so on) from environment variables.

cc @awakecoding

@TheBestTvarynka TheBestTvarynka self-assigned this Feb 5, 2024
Comment on lines 885 to 890
// the original flag is
// GSS_C_MUTUAL_FLAG | GSS_C_REPLAY_FLAG | GSS_C_SEQUENCE_FLAG | GSS_C_CONF_FLAG | GSS_C_INTEG_FLAG
// we want to be able to turn of sign and seal, so we leave confidentiality and integrity flags out
let flags = builder.context_requirements.into();
let mut checksum_value = ChecksumValues::default();
checksum_value.set_flags(flags);
// let flags = builder.context_requirements.into();
let checksum_value = ChecksumValues::default();
// checksum_value.set_flags(flags);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We have a problem here. During the dev testing, I had a problem with this code. When I uncomment these flags, then the CredSSP server returns an IvalidToken error. I'm not sure why, but when commented on it, then authentication succeeded.

Copy link
Member

@CBenoit CBenoit Feb 7, 2024

Choose a reason for hiding this comment

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

That’s suspicious.
For now, can you leave a FIXME at this place? We’ll have to address this before merging into master.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I added a comment here.
Can I take this issue today? I want to merge smart card implementation into a master quickly.

@TheBestTvarynka TheBestTvarynka marked this pull request as ready for review February 5, 2024 23:33
Copy link
Member

@CBenoit CBenoit left a comment

Choose a reason for hiding this comment

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

Looking quite good. We’ll have to look into the Kerberos flag thing before merging though. I don’t know if this is okay to just comment out this code.

Comment on lines 596 to 598
// remove null
let new_len = password.as_ref().len() - 2;
password.as_mut().truncate(new_len);
Copy link
Member

@CBenoit CBenoit Feb 6, 2024

Choose a reason for hiding this comment

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

nitpick: I recommend writing "proper sentences" to create a more appropriate frame of mind when writing comments. The expectation is to write down more of the context you had in mind when writing this code.

For instance, in this case what I really want to know as someone not familiar with the smart card protocol is "why we need to remove the null terminator at this place?" or "why is there a null terminator", etc. It’s okay to not always explain when it’s just a C-style UTF-16 string → Rust string conversion, but this is not what it is. (And if it was, using the appropriate function would be explicit enough.)

This comment can be applied at several places, but I leave you decide if and how much you do it.

(All that being said, the code you wrote is generally well documented, thank you again.)

Copy link
Collaborator Author

@TheBestTvarynka TheBestTvarynka Feb 6, 2024

Choose a reason for hiding this comment

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

I agree with you. Can you check it again? I updated some comments

Copy link
Member

Choose a reason for hiding this comment

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

It’s looking much better! For this place specifically, I would like something more like that:

The "pin" buffer of the SmartCardIdentityBuffers structure must not be NULL-terminated because / as specified in […] (protocol spec reference? empirical observation?).
Since the password data returned by CredUnPackAuthenticationBufferW is a NULL-terminated wide C string, we need to remove the last two bytes.

The conversion itself is a detail, and what is important is the surroundings, the things we can’t see just by looking at the code.

(Again, I’m really being pedantic here.)

Copy link
Collaborator Author

@TheBestTvarynka TheBestTvarynka Feb 7, 2024

Choose a reason for hiding this comment

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

Again, I’m really being pedantic here.

I can understand you and I like it 😃.
I added more comprehensive explanations

Copy link
Member

@CBenoit CBenoit left a comment

Choose a reason for hiding this comment

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

LGTM!

I’ll approve the changes as-is.
The flag problem mentioned here must be addressed before we merge into master, but it’s okay to merge this PR into the winscard dev branch when you are ready @TheBestTvarynka
If you can add a FIXME just to not forget about it before, that would be perfect.

Thank you!

@TheBestTvarynka TheBestTvarynka merged commit 6dae2a5 into winscard-rs-impl Feb 7, 2024
40 checks passed
@TheBestTvarynka TheBestTvarynka deleted the feature/winscard-credentials branch February 7, 2024 09:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants