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

Feature: Security Domain and SCP11a/b/c features #164

Open
wants to merge 37 commits into
base: develop
Choose a base branch
from

Conversation

DennisDyallo
Copy link
Collaborator

@DennisDyallo DennisDyallo commented Nov 27, 2024

Description

This PR implements comprehensive support for SCP11a/b/c secure channels and enhances Security Domain functionality. It also enables OATH, OTP, and YubiHsm applications to operate over SCP connections.

Key Changes

  • Introduced new ../Scp/ namespace for improved secure channel management
  • Added support for SCP11a/b/c secure channels
  • Enabled OATH, OTP, and YubiHsm applications over SCP connections
  • Implemented new Security Domain application through SecurityDomainSession

New Components

  • SecurityDomainSession: Application that manages the Security Domain
  • Scp03KeyParameters: SCP03 key configuration
  • Scp11KeyParameters: SCP11 key configuration
  • KeyReference: Refers to keys on the Yubikey Security Domain
  • Scp03State: SCP03 state management
  • Scp11State: SCP11 state management

Deprecation and misc changes

  • Deprecated all classes in ../Scp03/ namespace
  • Replaced with new implementations in ../Scp11/ namespace
  • Modified some class names and functionality to align with Android SDK (yubikit-android) and Python SDK (yubikey-manager)

Migration Notes

The new architecture favors using SecurityDomainSession for Security Domain operations rather than individual command classes. Update existing implementations accordingly.

Type of change

  • Refactor (non-breaking change which improves code quality or performance)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How has this been tested?

Tested using a 5.7.2 key.
Needs to test more thoroughly on other keys.

Test configuration:

  • OS version: Win 11
  • Firmware version: 5.4.3, 5.7.2 Non FIPS
  • Yubikey model1: 5 MPE

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have run dotnet format to format my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Footnotes

  1. See Yubikey models (Multi-protocol, Security Key, FIPS, Bio, YubiHSM, YubiHSM FIPS)

@DennisDyallo DennisDyallo added the enhancement New feature or request label Nov 27, 2024
@DennisDyallo DennisDyallo self-assigned this Nov 27, 2024
@DennisDyallo DennisDyallo force-pushed the feature/scp11 branch 2 times, most recently from cfa5db9 to 69d6525 Compare November 27, 2024 19:07
feat(scp11): Generate EC Key
feat(sd): Reset Security Domain
misc: Changed static analysis mode
misc: SampleCode projects won't build nuget packages
Store identifiers in dictionary
Utilize current and next TLV values
Improve method functionality
Enhance code readability
update auth-decrypt.md
update auth-sign.md
updated RSA key sizes in user manual files
updated RSA key info in API docs
updated docs and minor adjustments and new tests
support OATH and SCP
support OTP and SCP
support YubiHSM and SCP
throws exception on invalid KeyParameters
add docs
@DennisDyallo DennisDyallo force-pushed the feature/scp11 branch 2 times, most recently from 75a40ae to e122693 Compare November 27, 2024 23:55
ChannelEncyrption: use Span<byte> instead of byte[]
ChannelMac: use Span<byte> instead of byte[]
StoreDataCommand: change order of fields
Copy link
Collaborator Author

@DennisDyallo DennisDyallo left a comment

Choose a reason for hiding this comment

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

Will address these things


namespace Yubico.YubiKey.Scp.Commands
{
/// <summary>
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This should probably be rewritten to account for SCP11


namespace Yubico.YubiKey.Scp
{
/// <summary>
Copy link
Collaborator Author

@DennisDyallo DennisDyallo Nov 27, 2024

Choose a reason for hiding this comment

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

Needs a rewrite to account for SCP11

Yubico.YubiKey/src/Yubico/YubiKey/SmartCardConnection.cs Outdated Show resolved Hide resolved
#pragma warning disable CA5358 // Allow the usage of cipher mode 'ECB'
aesObj.Mode = CipherMode.ECB;
aesObj.Mode = CipherMode.ECB;

Check failure

Code scanning / CodeQL

Encryption using ECB High

The ECB (Electronic Code Book) encryption mode is vulnerable to replay attacks.

Copilot Autofix AI 5 days ago

To fix the problem, we should replace the use of ECB mode with a more secure mode of operation, such as CBC (Cipher Block Chaining) or GCM (Galois/Counter Mode). These modes provide better security by introducing randomness and ensuring that identical plaintext blocks produce different ciphertext blocks.

The best way to fix the problem without changing existing functionality is to use CBC mode with a randomly generated initialization vector (IV). This will require modifying the code to generate a random IV and use it for encryption. The IV should be stored or transmitted along with the ciphertext to allow for decryption.

Changes needed:

  1. Replace CipherMode.ECB with CipherMode.CBC.
  2. Generate a random IV and set it in the AES object.
  3. Ensure the IV is stored or transmitted with the ciphertext.
Suggested changeset 1
Yubico.YubiKey/src/Yubico/YubiKey/Cryptography/AesUtilities.cs

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/Yubico.YubiKey/src/Yubico/YubiKey/Cryptography/AesUtilities.cs b/Yubico.YubiKey/src/Yubico/YubiKey/Cryptography/AesUtilities.cs
--- a/Yubico.YubiKey/src/Yubico/YubiKey/Cryptography/AesUtilities.cs
+++ b/Yubico.YubiKey/src/Yubico/YubiKey/Cryptography/AesUtilities.cs
@@ -54,3 +54,3 @@
 #pragma warning disable CA5358 // Allow the usage of cipher mode 'ECB'
-            aesObj.Mode = CipherMode.ECB;
+            aesObj.Mode = CipherMode.CBC;
 #pragma warning restore CA5358
@@ -59,4 +59,5 @@
             aesObj.Key = aesObjKey;
-            aesObj.IV = new byte[BlockSizeBytes];
             aesObj.Padding = PaddingMode.None;
+            aesObj.GenerateIV();
+            byte[] iv = aesObj.IV;
 #pragma warning disable CA5401 // Justification: Allow the symmetric encryption to use
EOF
@@ -54,3 +54,3 @@
#pragma warning disable CA5358 // Allow the usage of cipher mode 'ECB'
aesObj.Mode = CipherMode.ECB;
aesObj.Mode = CipherMode.CBC;
#pragma warning restore CA5358
@@ -59,4 +59,5 @@
aesObj.Key = aesObjKey;
aesObj.IV = new byte[BlockSizeBytes];
aesObj.Padding = PaddingMode.None;
aesObj.GenerateIV();
byte[] iv = aesObj.IV;
#pragma warning disable CA5401 // Justification: Allow the symmetric encryption to use
Copilot is powered by AI and may make mistakes. Always verify output.
Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

CBC is required and allowed for our use case.

Copy link

github-actions bot commented Nov 28, 2024

Test Results: Windows

    2 files      2 suites   5s ⏱️
3 690 tests 3 690 ✅ 0 💤 0 ❌
3 692 runs  3 692 ✅ 0 💤 0 ❌

Results for commit 15e7efa.

♻️ This comment has been updated with latest results.

Copy link

github-actions bot commented Nov 28, 2024

Test Results: Ubuntu

    2 files      2 suites   9s ⏱️
3 682 tests 3 682 ✅ 0 💤 0 ❌
3 684 runs  3 684 ✅ 0 💤 0 ❌

Results for commit 15e7efa.

♻️ This comment has been updated with latest results.

Copy link

github-actions bot commented Nov 28, 2024

Test Results: MacOS

    2 files      2 suites   4s ⏱️
3 682 tests 3 682 ✅ 0 💤 0 ❌
3 684 runs  3 684 ✅ 0 💤 0 ❌

Results for commit 15e7efa.

♻️ This comment has been updated with latest results.

@DennisDyallo DennisDyallo marked this pull request as ready for review November 28, 2024 09:42
added validation of hostchallenge length
added documentation for the class
refactor around external authenticate for clarity
in: ScpState and SessionKeys.cs
docs: revert auto format of .md files
misc: fixed typo in SecurityDomainSession
YubiKeyDeviceExtensions.cs was not meant to be deleted yet
Scp03 enum is obsolete
removed comments
added SecurityDomain enum to ConnectionManager.cs
Scp03Tests: All tests select transport.
Will fail test if Nfc and Fips are selected
Copy link

Code Coverage

Package Line Rate Branch Rate Complexity Health
Yubico.Core 44% 32% 4311
Yubico.YubiKey 47% 44% 19716
Summary 46% (31697 / 68460) 42% (8050 / 19020) 24027

Minimum allowed line rate is 40%

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

Successfully merging this pull request may close these issues.

1 participant