Skip to content

Commit

Permalink
Add configuration for recommended extensions and update console logge…
Browse files Browse the repository at this point in the history
…r parameters

- Added a configuration file for recommended extensions in the `.vscode` folder.
- Updated the console logger parameters in the `tasks.json` file.
- Removed unused imports and refactored code in the `GenerateCSRCommand.cs` file.
- Modified properties in the `GenerateYKCSR.csproj` file.
- Refactored code and added comments in the `YubiKeyKeyCollector.cs` file.
- Modified parameters and refactored code in the `YubiKeySignatureGenerator.cs` file.

Signed-off-by: Bryan Gonzalez <[email protected]>
  • Loading branch information
SheepReaper committed Oct 6, 2023
1 parent 2081ddf commit d3014be
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 32 deletions.
5 changes: 5 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"recommendations": [
"ms-dotnettools.csdevkit"
]
}
4 changes: 2 additions & 2 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"build",
"${workspaceFolder}/src.sln",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
"/consoleloggerparameters:NoSummary;ForceNoAlign"
],
"problemMatcher": "$msCompile"
},
Expand All @@ -21,7 +21,7 @@
"publish",
"${workspaceFolder}/src.sln",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
"/consoleloggerparameters:NoSummary;ForceNoAlign"
],
"problemMatcher": "$msCompile"
},
Expand Down
3 changes: 1 addition & 2 deletions src/yk-csr-cli/GenerateCSRCommand.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.CommandLine;
using System.CommandLine.Binding;
using System.CommandLine.Parsing;
using System.Net;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
Expand Down Expand Up @@ -268,6 +267,6 @@ public static async Task ExecuteAsync(Params boundParams, CancellationToken ctx

if (boundParams.OutPubFile is not null) await File.WriteAllTextAsync(boundParams.OutPubFile.FullName, pubKeyPem, ctx);

if (boundParams.OutputPubToConsole || boundParams.OutPubFile is null) Console.WriteLine(pubKeyPem.ReplaceLineEndings());
if (boundParams.OutputPubToConsole) Console.WriteLine(pubKeyPem.ReplaceLineEndings());
}
}
1 change: 0 additions & 1 deletion src/yk-csr-cli/GenerateYKCSR.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
<Nullable>enable</Nullable>
<LangVersion>preview</LangVersion>
<VersionPrefix>1.0.0</VersionPrefix>
<VersionSuffix>rc1</VersionSuffix>
<UseAppHost>true</UseAppHost>
<PublishSingleFile>true</PublishSingleFile>
<IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract>
Expand Down
8 changes: 5 additions & 3 deletions src/yk-csr-cli/YubiKeyKeyCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@ private static bool VerifyPivPinHandler(KeyEntryData ked)

return true;
}

private static readonly Dictionary<KeyEntryRequest, Func<KeyEntryData, bool>> requestHandlerMap = new() {
{KeyEntryRequest.Release, (_) => true},
{KeyEntryRequest.VerifyPivPin, VerifyPivPinHandler}
};
{KeyEntryRequest.Release, (_) => true},
{KeyEntryRequest.VerifyPivPin, VerifyPivPinHandler}
};

public YubiKeyKeyCollector(Handle parentWindow) { }

public YubiKeyKeyCollector() { }
Expand Down
38 changes: 14 additions & 24 deletions src/yk-csr-cli/YubiKeySignatureGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,20 @@

using AlgConfig = (System.Security.Cryptography.HashAlgorithm digester, int algBits);

sealed class YubiKeySignatureGenerator : X509SignatureGenerator
sealed class YubiKeySignatureGenerator(PivSession pivSession, byte slotNumber, RSA rsaPublicKeyObject, RSASignaturePadding paddingScheme) : X509SignatureGenerator
{
private static readonly Dictionary<HashAlgorithmName, AlgConfig> digesterMap = new()
{
{HashAlgorithmName.SHA1, (CryptographyProviders.Sha1Creator(),RsaFormat.Sha1)},
{HashAlgorithmName.SHA256, (CryptographyProviders.Sha256Creator(),RsaFormat.Sha256)},
{HashAlgorithmName.SHA384, (CryptographyProviders.Sha384Creator(),RsaFormat.Sha384)},
{HashAlgorithmName.SHA512, (CryptographyProviders.Sha512Creator(),RsaFormat.Sha512)},
};

private readonly PivSession _pivSession;
private readonly byte _slotNumber;
private readonly int _keySizeBits;
private static readonly Dictionary<HashAlgorithmName, AlgConfig> digesterMap = new() {
{HashAlgorithmName.SHA1, (CryptographyProviders.Sha1Creator(),RsaFormat.Sha1)},
{HashAlgorithmName.SHA256, (CryptographyProviders.Sha256Creator(),RsaFormat.Sha256)},
{HashAlgorithmName.SHA384, (CryptographyProviders.Sha384Creator(),RsaFormat.Sha384)},
{HashAlgorithmName.SHA512, (CryptographyProviders.Sha512Creator(),RsaFormat.Sha512)},
};

private readonly X509SignatureGenerator _defaultGenerator;
private readonly RSASignaturePaddingMode _paddingMode;
public YubiKeySignatureGenerator(PivSession pivSession, byte slotNumber, RSA rsaPublicKeyObject, RSASignaturePadding paddingScheme)
{
_pivSession = pivSession;
_slotNumber = slotNumber;
_keySizeBits = rsaPublicKeyObject.KeySize;
_defaultGenerator = CreateForRSA(rsaPublicKeyObject, paddingScheme);
_paddingMode = paddingScheme.Mode;
}
private readonly PivSession _pivSession = pivSession;
private readonly byte _slotNumber = slotNumber;
private readonly int _keySizeBits = rsaPublicKeyObject.KeySize;
private readonly X509SignatureGenerator _defaultGenerator = CreateForRSA(rsaPublicKeyObject, paddingScheme);
private readonly RSASignaturePaddingMode _paddingMode = paddingScheme.Mode;

protected override PublicKey BuildPublicKey()
{
Expand All @@ -48,8 +38,8 @@ public override byte[] SignData(byte[] data, HashAlgorithmName hashAlgorithm)
}

private static AlgConfig GetSupportedAlgConfig(HashAlgorithmName hashAlgorithm) => digesterMap.TryGetValue(hashAlgorithm, out var algConfig)
? algConfig
: throw new ArgumentException("Unsupported Hash Algorithm", nameof(hashAlgorithm));
? algConfig
: throw new ArgumentException("Unsupported Hash Algorithm", nameof(hashAlgorithm));

private static byte[] DigestData(byte[] data, HashAlgorithmName hashAlgorithm)
{
Expand Down

0 comments on commit d3014be

Please sign in to comment.