Skip to content

Commit

Permalink
.NET 8.0 -> .NET 9.0으로 업데이트
Browse files Browse the repository at this point in the history
  • Loading branch information
rkttu committed Dec 30, 2024
1 parent 71d1824 commit 406ae7f
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 123 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ jobs:
env:
Solution_Name: src\TableCloth.sln
Spork_Project_Path: src\Spork\Spork.csproj
Sponge_Project_Path: src\Sponge\Sponge.csproj
TableCloth_Project_Path: src\TableCloth\TableCloth.csproj
Platform: ${{ matrix.platform }}

Expand All @@ -41,7 +40,7 @@ jobs:
- name: Install .NET Core
uses: actions/setup-dotnet@v4
with:
dotnet-version: 8.0.x
dotnet-version: 9.0.x

# Add MSBuild to the PATH: https://github.com/microsoft/setup-msbuild
- name: Setup MSBuild.exe
Expand Down
240 changes: 120 additions & 120 deletions src/TableCloth/Components/Implementations/X509CertPairScanner.cs
Original file line number Diff line number Diff line change
@@ -1,121 +1,121 @@
using Microsoft.Extensions.Logging;
using PnPeople.Security;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using TableCloth.Models.Configuration;
using TableCloth.Resources;

namespace TableCloth.Components.Implementations;

public sealed class X509CertPairScanner(
ILogger<X509CertPairScanner> logger) : IX509CertPairScanner
{
public ILogger Logger { get; init; } = logger;

public IEnumerable<string> GetCandidateDirectories()
using Microsoft.Extensions.Logging;
using PnPeople.Security;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using TableCloth.Models.Configuration;
using TableCloth.Resources;

namespace TableCloth.Components.Implementations;

public sealed class X509CertPairScanner(
ILogger<X509CertPairScanner> logger) : IX509CertPairScanner
{
public ILogger Logger { get; init; } = logger;

public IEnumerable<string> GetCandidateDirectories()
{
var localLowPath = NativeMethods.GetKnownFolderPath(NativeMethods.LocalLowFolderGuid);

if (localLowPath == null)
throw new Exception("Cannot obtain the LocalLow folder path.");

var defaultNpkiPath = Path.Combine(localLowPath, "NPKI");
var directoryCandidates = new List<string>();

if (Directory.Exists(defaultNpkiPath))
directoryCandidates.Add(defaultNpkiPath);

var usbDrives = DriveInfo.GetDrives()
.Where(d => d.DriveType == DriveType.Removable)
.Where(d => Directory.Exists(d.RootDirectory.FullName))
.Select(d => d.RootDirectory.FullName);

directoryCandidates.AddRange(usbDrives);
return directoryCandidates;
}

// https://stackoverflow.com/questions/5098011/directory-enumeratefiles-unauthorizedaccessexception
public IEnumerable<X509CertPair> ScanX509Pairs(IEnumerable<string> rootPathList)
{
var foundFiles = new List<X509CertPair>();

foreach (var eachRootPath in rootPathList)
{
try
{
foreach (var dir in Directory.EnumerateDirectories(eachRootPath))
{
// Add files in subdirectories recursively to the list
foundFiles.AddRange(ScanX509Pairs(new string[] { dir }));
}
}
catch (Exception e)
{
Logger.LogWarning(e, "{message}", StringResources.TableCloth_Log_DirectoryEnumFail_ProhibitTranslation(eachRootPath, e));
}

try
{
// Add files from the current directory
var singleDerFile = Directory.GetFiles(eachRootPath, "signCert.der").FirstOrDefault();
var singleKeyFile = Directory.GetFiles(eachRootPath, "signPri.key").FirstOrDefault();

if (File.Exists(singleDerFile) && File.Exists(singleKeyFile))
foundFiles.Add(CreateX509CertPair(singleDerFile, singleKeyFile));
}
catch (UnauthorizedAccessException uae)
{
Logger.LogWarning(uae, "Cannot load X509 cert pair - {eachRootPath}", eachRootPath);
}
catch (PathTooLongException ptle)
{
Logger.LogWarning(ptle, "Cannot load X509 cert pair - {eachRootPath}", eachRootPath);
}
catch (AggregateException ae)
{
Logger.LogWarning(ae.InnerException ?? ae, "Cannot load X509 cert pair - {eachRootPath}", eachRootPath);
}
catch (Exception e)
{
Logger.LogWarning(e, "Cannot load X509 cert pair - {eachRootPath}", eachRootPath);
}
}

return foundFiles;
}

public X509CertPair CreateX509CertPair(string derFilePath, string keyFilePath)
{
if (!File.Exists(derFilePath))
TableClothAppException.Throw(ErrorStrings.Error_Cannot_Find_CertFile, derFilePath);

if (!File.Exists(keyFilePath))
TableClothAppException.Throw(ErrorStrings.Error_Cannot_Find_KeyFile, keyFilePath);

return new X509CertPair(
File.ReadAllBytes(derFilePath),
File.ReadAllBytes(keyFilePath));
}

public X509CertPair CreateX509Cert(string pfxFilePath, SecureString password)
{
if (!File.Exists(pfxFilePath))
TableClothAppException.Throw(ErrorStrings.Error_Cannot_Find_PfxFile, pfxFilePath);

var copiedPassword = CertPrivateKeyHelper.CopyFromSecureString(password);

using X509Certificate2 cert = new X509Certificate2(pfxFilePath, copiedPassword, X509KeyStorageFlags.Exportable);
var publicKey = cert.Export(X509ContentType.Cert);

var rsaPrivateKey = cert.GetRSAPrivateKey().EnsureNotNull("Cannot obtain RSA private key.");
var privateKey = rsaPrivateKey.ExportEncryptedPkcs8PrivateKey(copiedPassword,
new PbeParameters(PbeEncryptionAlgorithm.TripleDes3KeyPkcs12, HashAlgorithmName.SHA1, 2048));

return new X509CertPair(publicKey, privateKey);
}
}
var localLowPath = NativeMethods.GetKnownFolderPath(NativeMethods.LocalLowFolderGuid);

if (localLowPath == null)
throw new Exception("Cannot obtain the LocalLow folder path.");

var defaultNpkiPath = Path.Combine(localLowPath, "NPKI");
var directoryCandidates = new List<string>();

if (Directory.Exists(defaultNpkiPath))
directoryCandidates.Add(defaultNpkiPath);

var usbDrives = DriveInfo.GetDrives()
.Where(d => d.DriveType == DriveType.Removable)
.Where(d => Directory.Exists(d.RootDirectory.FullName))
.Select(d => d.RootDirectory.FullName);

directoryCandidates.AddRange(usbDrives);
return directoryCandidates;
}

// https://stackoverflow.com/questions/5098011/directory-enumeratefiles-unauthorizedaccessexception
public IEnumerable<X509CertPair> ScanX509Pairs(IEnumerable<string> rootPathList)
{
var foundFiles = new List<X509CertPair>();

foreach (var eachRootPath in rootPathList)
{
try
{
foreach (var dir in Directory.EnumerateDirectories(eachRootPath))
{
// Add files in subdirectories recursively to the list
foundFiles.AddRange(ScanX509Pairs(new string[] { dir }));
}
}
catch (Exception e)
{
Logger.LogWarning(e, "{message}", StringResources.TableCloth_Log_DirectoryEnumFail_ProhibitTranslation(eachRootPath, e));
}

try
{
// Add files from the current directory
var singleDerFile = Directory.GetFiles(eachRootPath, "signCert.der").FirstOrDefault();
var singleKeyFile = Directory.GetFiles(eachRootPath, "signPri.key").FirstOrDefault();

if (File.Exists(singleDerFile) && File.Exists(singleKeyFile))
foundFiles.Add(CreateX509CertPair(singleDerFile, singleKeyFile));
}
catch (UnauthorizedAccessException uae)
{
Logger.LogWarning(uae, "Cannot load X509 cert pair - {eachRootPath}", eachRootPath);
}
catch (PathTooLongException ptle)
{
Logger.LogWarning(ptle, "Cannot load X509 cert pair - {eachRootPath}", eachRootPath);
}
catch (AggregateException ae)
{
Logger.LogWarning(ae.InnerException ?? ae, "Cannot load X509 cert pair - {eachRootPath}", eachRootPath);
}
catch (Exception e)
{
Logger.LogWarning(e, "Cannot load X509 cert pair - {eachRootPath}", eachRootPath);
}
}

return foundFiles;
}

public X509CertPair CreateX509CertPair(string derFilePath, string keyFilePath)
{
if (!File.Exists(derFilePath))
TableClothAppException.Throw(ErrorStrings.Error_Cannot_Find_CertFile, derFilePath);

if (!File.Exists(keyFilePath))
TableClothAppException.Throw(ErrorStrings.Error_Cannot_Find_KeyFile, keyFilePath);

return new X509CertPair(
File.ReadAllBytes(derFilePath),
File.ReadAllBytes(keyFilePath));
}

public X509CertPair CreateX509Cert(string pfxFilePath, SecureString password)
{
if (!File.Exists(pfxFilePath))
TableClothAppException.Throw(ErrorStrings.Error_Cannot_Find_PfxFile, pfxFilePath);

var copiedPassword = CertPrivateKeyHelper.CopyFromSecureString(password);

using X509Certificate2 cert = new X509Certificate2(pfxFilePath, copiedPassword, X509KeyStorageFlags.Exportable);
var publicKey = cert.Export(X509ContentType.Cert);

var rsaPrivateKey = cert.GetRSAPrivateKey().EnsureNotNull("Cannot obtain RSA private key.");
var privateKey = rsaPrivateKey.ExportEncryptedPkcs8PrivateKey(copiedPassword,
new PbeParameters(PbeEncryptionAlgorithm.TripleDes3KeyPkcs12, HashAlgorithmName.SHA1, 2048));

return new X509CertPair(publicKey, privateKey);
}
}
2 changes: 1 addition & 1 deletion src/TableCloth/TableCloth.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net8.0-windows10.0.18362.0</TargetFramework>
<TargetFramework>net9.0-windows10.0.18362.0</TargetFramework>
<StartupObject>TableCloth.Program</StartupObject>
<ApplicationIcon>App.ico</ApplicationIcon>
<ApplicationManifest>app.manifest</ApplicationManifest>
Expand Down

0 comments on commit 406ae7f

Please sign in to comment.