├── LibNPKI ├── SysadminsLV.Asn1Parser.dll ├── packages.config ├── Exceptions │ ├── VIDOperationException.cs │ ├── NotSupportedAlgorithmException.cs │ └── IncorretPasswordException.cs ├── CertificateSelecter.cs ├── Properties │ └── AssemblyInfo.cs ├── CertificateSelectionDialog.cs ├── CertificateLoader.cs ├── CertificateFinder.cs ├── LibNPKI.csproj ├── CertificateSelectionDialog.resx ├── VIDVerifier.cs └── CertificateSelectionDialog.Designer.cs ├── LibNPKIExample ├── SysadminsLV.Asn1Parser.dll ├── packages.config ├── App.config ├── Properties │ ├── Settings.settings │ ├── Settings.Designer.cs │ ├── AssemblyInfo.cs │ ├── Resources.Designer.cs │ └── Resources.resx ├── Program.cs ├── Form1.cs ├── LibNPKIExample.csproj ├── Form1.resx └── Form1.Designer.cs ├── LICENSE ├── NPKIIdentication.sln ├── .gitattributes ├── README.md └── .gitignore /LibNPKI/SysadminsLV.Asn1Parser.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LiteHell/NPKI_example/HEAD/LibNPKI/SysadminsLV.Asn1Parser.dll -------------------------------------------------------------------------------- /LibNPKIExample/SysadminsLV.Asn1Parser.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LiteHell/NPKI_example/HEAD/LibNPKIExample/SysadminsLV.Asn1Parser.dll -------------------------------------------------------------------------------- /LibNPKIExample/packages.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /LibNPKIExample/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /LibNPKI/packages.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /LibNPKIExample/Properties/Settings.settings: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /LibNPKI/Exceptions/VIDOperationException.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace LibNPKI.Exceptions 8 | { 9 | public class VIDOperationException : Exception 10 | { 11 | internal VIDOperationException(string message) 12 | { 13 | 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LibNPKI/Exceptions/NotSupportedAlgorithmException.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace LibNPKI.Exceptions 8 | { 9 | public class NotSupportedAlgorithmException : Exception 10 | { 11 | internal NotSupportedAlgorithmException(string message) : base(message) 12 | { 13 | 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LibNPKI/Exceptions/IncorretPasswordException.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace LibNPKI.Exceptions 8 | { 9 | public class IncorretPasswordException : Exception 10 | { 11 | internal IncorretPasswordException() : base("Incorrect password for decrypting private key") 12 | { 13 | 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LibNPKIExample/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | using System.Windows.Forms; 6 | 7 | namespace NPKIIdentication 8 | { 9 | static class Program 10 | { 11 | /// 12 | /// 해당 응용 프로그램의 주 진입점입니다. 13 | /// 14 | [STAThread] 15 | static void Main() 16 | { 17 | Application.EnableVisualStyles(); 18 | Application.SetCompatibleTextRenderingDefault(false); 19 | Application.Run(new Form1()); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /LibNPKI/CertificateSelecter.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace LibNPKI 8 | { 9 | public class CertificateSelecter 10 | { 11 | public static CertificateLocation ShowSelectionDialog(IEnumerable certificates, out string password) 12 | { 13 | CertificateSelectionDialog dialog = new CertificateSelectionDialog(); 14 | dialog.addCertsToList(certificates); 15 | dialog.ShowDialog(); 16 | password = dialog.certPassword; 17 | if (dialog.DialogResult != System.Windows.Forms.DialogResult.OK) 18 | return null; 19 | return dialog.selectedCert; 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2018 LiteHell 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE. -------------------------------------------------------------------------------- /LibNPKIExample/Properties/Settings.Designer.cs: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | // 3 | // 이 코드는 도구를 사용하여 생성되었습니다. 4 | // 런타임 버전:4.0.30319.42000 5 | // 6 | // 파일 내용을 변경하면 잘못된 동작이 발생할 수 있으며, 코드를 다시 생성하면 7 | // 이러한 변경 내용이 손실됩니다. 8 | // 9 | //------------------------------------------------------------------------------ 10 | 11 | namespace LibNPKIExample.Properties { 12 | 13 | 14 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] 15 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "15.6.0.0")] 16 | internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { 17 | 18 | private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); 19 | 20 | public static Settings Default { 21 | get { 22 | return defaultInstance; 23 | } 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /LibNPKI/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // 어셈블리에 대한 일반 정보는 다음 특성 집합을 통해 6 | // 제어됩니다. 어셈블리와 관련된 정보를 수정하려면 7 | // 이러한 특성 값을 변경하세요. 8 | [assembly: AssemblyTitle("LibNPKI")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("LibNPKI")] 13 | [assembly: AssemblyCopyright("Copyright © 2018")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // ComVisible을 false로 설정하면 이 어셈블리의 형식이 COM 구성 요소에 18 | // 표시되지 않습니다. COM에서 이 어셈블리의 형식에 액세스하려면 19 | // 해당 형식에 대해 ComVisible 특성을 true로 설정하세요. 20 | [assembly: ComVisible(false)] 21 | 22 | // 이 프로젝트가 COM에 노출되는 경우 다음 GUID는 typelib의 ID를 나타냅니다. 23 | [assembly: Guid("fa319c3b-365d-430e-9ff7-40aa78d30af1")] 24 | 25 | // 어셈블리의 버전 정보는 다음 네 가지 값으로 구성됩니다. 26 | // 27 | // 주 버전 28 | // 부 버전 29 | // 빌드 번호 30 | // 수정 버전 31 | // 32 | // 모든 값을 지정하거나 아래와 같이 '*'를 사용하여 빌드 번호 및 수정 번호를 33 | // 기본값으로 할 수 있습니다. 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /LibNPKIExample/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // 어셈블리에 대한 일반 정보는 다음 특성 집합을 통해 6 | // 제어됩니다. 어셈블리와 관련된 정보를 수정하려면 7 | // 이러한 특성 값을 변경하세요. 8 | [assembly: AssemblyTitle("NPKIIdentication")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("NPKIIdentication")] 13 | [assembly: AssemblyCopyright("Copyright © 2018")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // ComVisible을 false로 설정하면 이 어셈블리의 형식이 COM 구성 요소에 18 | // 표시되지 않습니다. COM에서 이 어셈블리의 형식에 액세스하려면 19 | // 해당 형식에 대해 ComVisible 특성을 true로 설정하세요. 20 | [assembly: ComVisible(false)] 21 | 22 | // 이 프로젝트가 COM에 노출되는 경우 다음 GUID는 typelib의 ID를 나타냅니다. 23 | [assembly: Guid("c3124558-615c-48d4-b6e3-d2288bee6429")] 24 | 25 | // 어셈블리의 버전 정보는 다음 네 가지 값으로 구성됩니다. 26 | // 27 | // 주 버전 28 | // 부 버전 29 | // 빌드 번호 30 | // 수정 버전 31 | // 32 | // 모든 값을 지정하거나 아래와 같이 '*'를 사용하여 빌드 번호 및 수정 번호가 자동으로 33 | // 지정되도록 할 수 있습니다. 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /NPKIIdentication.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.27428.2043 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LibNPKIExample", "LibNPKIExample\LibNPKIExample.csproj", "{C3124558-615C-48D4-B6E3-D2288BEE6429}" 7 | EndProject 8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LibNPKI", "LibNPKI\LibNPKI.csproj", "{FA319C3B-365D-430E-9FF7-40AA78D30AF1}" 9 | EndProject 10 | Global 11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 12 | Debug|Any CPU = Debug|Any CPU 13 | Release|Any CPU = Release|Any CPU 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {C3124558-615C-48D4-B6E3-D2288BEE6429}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 17 | {C3124558-615C-48D4-B6E3-D2288BEE6429}.Debug|Any CPU.Build.0 = Debug|Any CPU 18 | {C3124558-615C-48D4-B6E3-D2288BEE6429}.Release|Any CPU.ActiveCfg = Release|Any CPU 19 | {C3124558-615C-48D4-B6E3-D2288BEE6429}.Release|Any CPU.Build.0 = Release|Any CPU 20 | {FA319C3B-365D-430E-9FF7-40AA78D30AF1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 21 | {FA319C3B-365D-430E-9FF7-40AA78D30AF1}.Debug|Any CPU.Build.0 = Debug|Any CPU 22 | {FA319C3B-365D-430E-9FF7-40AA78D30AF1}.Release|Any CPU.ActiveCfg = Release|Any CPU 23 | {FA319C3B-365D-430E-9FF7-40AA78D30AF1}.Release|Any CPU.Build.0 = Release|Any CPU 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | GlobalSection(ExtensibilityGlobals) = postSolution 29 | SolutionGuid = {79F8832D-8412-4BB0-A15D-19EF4D0CB5D1} 30 | EndGlobalSection 31 | EndGlobal 32 | -------------------------------------------------------------------------------- /LibNPKI/CertificateSelectionDialog.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.ComponentModel; 4 | using System.Data; 5 | using System.Drawing; 6 | using System.Linq; 7 | using System.Text; 8 | using System.Threading.Tasks; 9 | using System.Windows.Forms; 10 | 11 | namespace LibNPKI 12 | { 13 | partial class CertificateSelectionDialog : Form 14 | { 15 | public CertificateSelectionDialog() 16 | { 17 | InitializeComponent(); 18 | } 19 | public string certPassword { get { return textBox1.Text; } } 20 | public void addCertsToList(IEnumerable certs) 21 | { 22 | foreach(CertificateLocation cert in certs) 23 | { 24 | ListViewItem item = new ListViewItem(); 25 | item.Tag = cert; 26 | item.Text = cert.PublicKeyCertificate.Subject; 27 | item.SubItems.Add(cert.PublicKeyCertificate.NotAfter.ToString()); 28 | item.SubItems.Add(cert.LocationDescription); 29 | listView1.Items.Add(item); 30 | } 31 | } 32 | public CertificateLocation selectedCert 33 | { 34 | get 35 | { 36 | return listView1.SelectedItems.Count == 0 ? null : (CertificateLocation)listView1.SelectedItems[0].Tag; 37 | } 38 | } 39 | 40 | private void button2_Click(object sender, EventArgs e) 41 | { 42 | this.DialogResult = DialogResult.OK; 43 | this.Close(); 44 | } 45 | 46 | private void button3_Click(object sender, EventArgs e) 47 | { 48 | this.DialogResult = DialogResult.No; 49 | this.Close(); 50 | } 51 | 52 | private void textBox1_KeyUp(object sender, KeyEventArgs e) 53 | { 54 | if (e.KeyCode == Keys.IMENonconvert) 55 | button2_Click(null, null); 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /LibNPKIExample/Properties/Resources.Designer.cs: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | // 3 | // 이 코드는 도구를 사용하여 생성되었습니다. 4 | // 런타임 버전:4.0.30319.42000 5 | // 6 | // 파일 내용을 변경하면 잘못된 동작이 발생할 수 있으며, 코드를 다시 생성하면 7 | // 이러한 변경 내용이 손실됩니다. 8 | // 9 | //------------------------------------------------------------------------------ 10 | 11 | namespace LibNPKIExample.Properties { 12 | using System; 13 | 14 | 15 | /// 16 | /// 지역화된 문자열 등을 찾기 위한 강력한 형식의 리소스 클래스입니다. 17 | /// 18 | // 이 클래스는 ResGen 또는 Visual Studio와 같은 도구를 통해 StronglyTypedResourceBuilder 19 | // 클래스에서 자동으로 생성되었습니다. 20 | // 멤버를 추가하거나 제거하려면 .ResX 파일을 편집한 다음 /str 옵션을 사용하여 ResGen을 21 | // 다시 실행하거나 VS 프로젝트를 다시 빌드하십시오. 22 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "15.0.0.0")] 23 | [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] 24 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] 25 | internal class Resources { 26 | 27 | private static global::System.Resources.ResourceManager resourceMan; 28 | 29 | private static global::System.Globalization.CultureInfo resourceCulture; 30 | 31 | [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] 32 | internal Resources() { 33 | } 34 | 35 | /// 36 | /// 이 클래스에서 사용하는 캐시된 ResourceManager 인스턴스를 반환합니다. 37 | /// 38 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] 39 | internal static global::System.Resources.ResourceManager ResourceManager { 40 | get { 41 | if (object.ReferenceEquals(resourceMan, null)) { 42 | global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("LibNPKIExample.Properties.Resources", typeof(Resources).Assembly); 43 | resourceMan = temp; 44 | } 45 | return resourceMan; 46 | } 47 | } 48 | 49 | /// 50 | /// 이 강력한 형식의 리소스 클래스를 사용하여 모든 리소스 조회에 대한 현재 스레드의 CurrentUICulture 51 | /// 속성을 재정의합니다. 52 | /// 53 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] 54 | internal static global::System.Globalization.CultureInfo Culture { 55 | get { 56 | return resourceCulture; 57 | } 58 | set { 59 | resourceCulture = value; 60 | } 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /LibNPKIExample/Form1.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.ComponentModel; 4 | using System.Data; 5 | using System.Drawing; 6 | using System.Linq; 7 | using System.Text; 8 | using System.Threading.Tasks; 9 | using System.Windows.Forms; 10 | using System.IO; 11 | using Mono.Security.Cryptography; 12 | using LibNPKI; 13 | 14 | namespace NPKIIdentication 15 | { 16 | public partial class Form1 : Form 17 | { 18 | private CertificateLocation certificate; 19 | public Form1() 20 | { 21 | InitializeComponent(); 22 | } 23 | 24 | private void button1_Click(object sender, EventArgs e) 25 | { 26 | var certs = CertificateFinder.GetCertificateLocations(); 27 | certificate = CertificateSelecter.ShowSelectionDialog(certs, out string password); 28 | if (certificate == null) 29 | { 30 | MessageBox.Show("선택되지 않음", "오류", MessageBoxButtons.OK, MessageBoxIcon.Error); 31 | return; 32 | } 33 | try 34 | { 35 | certificate.PrivateKeyInfo = CertificateLoader.DecryptPrivateKey(certificate, password); 36 | } 37 | catch (LibNPKI.Exceptions.IncorretPasswordException) 38 | { 39 | MessageBox.Show("잘못된 비밀번호입니다.", "오류", MessageBoxButtons.OK, MessageBoxIcon.Error); 40 | certificate = null; 41 | return; 42 | } 43 | groupBox1.Visible = true; 44 | groupBox1.Enabled = true; 45 | } 46 | 47 | private void button2_Click(object sender, EventArgs e) 48 | { 49 | VIDVerifier verifier = new VIDVerifier(); 50 | if (verifier.VerifyWithID(certificate.PublicKeyCertificate, certificate.PrivateKeyInfo, textBox1.Text)) 51 | { 52 | MessageBox.Show("확인됐습니다.", "결과", MessageBoxButtons.OK, MessageBoxIcon.Information); 53 | } 54 | else 55 | { 56 | MessageBox.Show("확인에 실패했습니다.", "결과", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); 57 | } 58 | } 59 | 60 | private void button3_Click(object sender, EventArgs e) 61 | { 62 | byte[] priKey = PKCS8.PrivateKeyInfo.Encode(CertificateLoader.ConvertPrivateKeyToRSA(certificate.PrivateKeyInfo.PrivateKey)); 63 | if (File.Exists(textBox2.Text)) 64 | { 65 | MessageBox.Show("파일이 이미 존재함", "오류", MessageBoxButtons.OK, MessageBoxIcon.Error); 66 | return; 67 | } 68 | using (FileStream fs = new FileStream(textBox2.Text, FileMode.Create, FileAccess.Write)) 69 | { 70 | fs.Write(priKey, 0, priKey.Length); 71 | fs.Flush(); 72 | } 73 | MessageBox.Show("내보냈습니다.", "결과", MessageBoxButtons.OK, MessageBoxIcon.Information); 74 | } 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /LibNPKI/CertificateLoader.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.IO; 5 | using System.Security.Cryptography; 6 | using System.Security.Cryptography.X509Certificates; 7 | using System.Text; 8 | using System.Threading.Tasks; 9 | using Org.BouncyCastle.Crypto.Modes; 10 | using Org.BouncyCastle.Crypto.Paddings; 11 | using Org.BouncyCastle.Crypto.Engines; 12 | using Org.BouncyCastle.Crypto.Parameters; 13 | using Mono.Security.Cryptography; 14 | using LibNPKI.Exceptions; 15 | 16 | namespace LibNPKI 17 | { 18 | public class CertificateLoader 19 | { 20 | public static PKCS8.PrivateKeyInfo DecryptPrivateKey(CertificateLocation cert, string password) 21 | { 22 | var privateKey = cert.EncryptedPrivateKeyInfo; 23 | PasswordDeriveBytes pbkdf1 = new PasswordDeriveBytes(password, privateKey.Salt, "SHA1", privateKey.IterationCount); 24 | byte[] dk = pbkdf1.GetBytes(20); 25 | byte[] k = new byte[16]; 26 | byte[] iv = new byte[16]; 27 | Array.Copy(dk,0, k,0, 16); 28 | if (privateKey.Algorithm == "1.2.410.200004.1.4") 29 | { 30 | // 고정 IV값 31 | iv = new byte[] { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35 }; 32 | } 33 | else if (privateKey.Algorithm == "1.2.410.200004.1.15") 34 | { 35 | byte[] hash = new byte[20]; 36 | hash = SHA1.Create().ComputeHash(dk,16,4); 37 | Array.Copy(hash, 0, iv, 0, 16); 38 | } else 39 | { 40 | throw new NotSupportedAlgorithmException("Not supported algorithm for private key"); 41 | } 42 | 43 | byte[] decrypted = seedDecrypt(privateKey.EncryptedData, k, iv); 44 | return new PKCS8.PrivateKeyInfo(decrypted); 45 | } 46 | 47 | public static RSA ConvertPrivateKeyToRSA(byte[] privateKey) 48 | { 49 | return PKCS8.PrivateKeyInfo.DecodeRSA(privateKey); 50 | } 51 | 52 | private static byte[] seedDecrypt(byte[] data, byte[] k, byte[] iv) 53 | { 54 | // https://stackoverflow.com/questions/29701401 참고 55 | // http://zest133.tistory.com/entry/%EB%8C%80%EC%B9%AD%ED%82%A4-%EC%95%94%ED%98%B8%ED%99%94-%EC%9D%B4%EC%95%BC%EA%B8%B01 참고 56 | CbcBlockCipher blockCipher = new CbcBlockCipher(new SeedEngine()); 57 | PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(blockCipher, new Pkcs7Padding()); 58 | KeyParameter keyParam = new KeyParameter(k); 59 | ParametersWithIV keyParamWithIV = new ParametersWithIV(keyParam, iv); 60 | cipher.Reset(); 61 | cipher.Init(false, keyParamWithIV); 62 | byte[] result = new byte[cipher.GetOutputSize(data.Length)]; 63 | int length = cipher.ProcessBytes(data, result, 0); try 64 | { 65 | length += cipher.DoFinal(result, length); 66 | } 67 | catch (Org.BouncyCastle.Crypto.InvalidCipherTextException) 68 | { 69 | throw new IncorretPasswordException(); 70 | } 71 | return result; 72 | 73 | } 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 공인인증서 관련 예제 2 | 하드디스크/이동식디스크에서 공인인증서을 읽고, 개인키를 복호화하고, 주민등록번호(및 사업자등록번호 같은 본인식별번호)로 본인확인하는 예제입니다. 3 | 한번 해보고 싶었어요. 4 | 5 | # 사용된 라이브러리들 6 | ## BouncyCastle 7 | MIT X11 License로 배포됨. 자세한 것은 [여기](https://www.bouncycastle.org/csharp/licence.html) 참고 8 | ### 전문 9 | Copyright (c) 2000 - 2017 The Legion of the Bouncy Castle Inc. (http://www.bouncycastle.org) 10 | 11 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 16 | 17 | ## Mono.Security 18 | MIT License로 배포되는 것 같음. 19 | ### 전문 20 | Permission is hereby granted, free of charge, to any person obtaining 21 | a copy of this software and associated documentation files (the 22 | "Software"), to deal in the Software without restriction, including 23 | without limitation the rights to use, copy, modify, merge, publish, 24 | distribute, sublicense, and/or sell copies of the Software, and to 25 | permit persons to whom the Software is furnished to do so, subject to 26 | the following conditions: 27 | 28 | The above copyright notice and this permission notice shall be 29 | included in all copies or substantial portions of the Software. 30 | 31 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 32 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 33 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 34 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 35 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 36 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 37 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 38 | 39 | ## Asn1DerParser.NET 40 | MIT License로 배포됨. 41 | ### 전문 42 | The MIT License (MIT) 43 | 44 | Copyright (c) 2016 Vadims Podans 45 | 46 | Permission is hereby granted, free of charge, to any person obtaining a copy 47 | of this software and associated documentation files (the "Software"), to deal 48 | in the Software without restriction, including without limitation the rights 49 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 50 | copies of the Software, and to permit persons to whom the Software is 51 | furnished to do so, subject to the following conditions: 52 | 53 | The above copyright notice and this permission notice shall be included in all 54 | copies or substantial portions of the Software. 55 | 56 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 57 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 58 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 59 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 60 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 61 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 62 | SOFTWARE. 63 | 64 | # 라이선스 65 | 해당 예제는 MIT License로 배포됨. 66 | ## 전문 67 | LICENSE 파일 참고. -------------------------------------------------------------------------------- /LibNPKI/CertificateFinder.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.IO; 5 | using Mono.Security.Cryptography; 6 | using System.Security.Cryptography; 7 | using System.Security.Cryptography.X509Certificates; 8 | using Org.BouncyCastle.Crypto.Engines; 9 | using System.Text; 10 | using System.Threading.Tasks; 11 | using Org.BouncyCastle.Crypto.Modes; 12 | using Org.BouncyCastle.Crypto.Paddings; 13 | using Org.BouncyCastle.Crypto.Parameters; 14 | 15 | namespace LibNPKI 16 | { 17 | public class CertificateFinder 18 | { 19 | public static IEnumerable GetCertificateLocations() 20 | { 21 | var s = getNPKIDirectories(); 22 | 23 | foreach (DirectoryInfo npkiDirectory in getNPKIDirectories().Select(i => new DirectoryInfo(i))) 24 | { 25 | foreach (var i in getCertDirectoriesFromDir(npkiDirectory)) 26 | yield return new CertificateLocation(Path.Combine(i, "signCert.der"), Path.Combine(i, "signPri.key")); 27 | } 28 | } 29 | private static IEnumerable getCertDirectoriesFromDir(DirectoryInfo dir) 30 | { 31 | foreach (string i in from d in dir.GetDirectories() where Directory.Exists(Path.Combine(d.FullName, "USER")) select Path.Combine(d.FullName, "USER")) 32 | foreach (string j in from d in new DirectoryInfo(i).GetDirectories() select d.FullName) 33 | yield return j; 34 | } 35 | private static IEnumerable getNPKIDirectories() 36 | { 37 | return from i in getNPKIDirectoryCandidiates() 38 | where Directory.Exists(i) 39 | select i; 40 | } 41 | private static IEnumerable getNPKIDirectoryCandidiates() 42 | { 43 | switch (Environment.OSVersion.Platform) 44 | { 45 | case PlatformID.Win32S: 46 | case PlatformID.Win32Windows: 47 | case PlatformID.Win32NT: 48 | case PlatformID.WinCE: 49 | yield return Environment.ExpandEnvironmentVariables(@"%UserProfile%\AppData\LocalLow\NPKI\"); 50 | yield return Environment.ExpandEnvironmentVariables(@"%ProgramFiles%\NPKI"); 51 | break; 52 | case PlatformID.Unix: 53 | yield return Path.Combine(Environment.GetEnvironmentVariable("HOME"), "NPKI"); 54 | break; 55 | case PlatformID.MacOSX: 56 | yield return Path.Combine(Environment.GetEnvironmentVariable("HOME"), "Library/Preferences/NPKI"); 57 | break; 58 | case PlatformID.Xbox: // ? 59 | throw new PlatformNotSupportedException(); 60 | } 61 | foreach (DriveInfo drive in DriveInfo.GetDrives()) 62 | { 63 | if (drive.DriveType == DriveType.Fixed || drive.DriveType == DriveType.Removable) 64 | { 65 | yield return Path.Combine(drive.RootDirectory.FullName, "NPKI"); 66 | } 67 | } 68 | } 69 | } 70 | public class CertificateLocation 71 | { 72 | internal CertificateLocation(string pubKeyPath, string priKeyPath) 73 | { 74 | PublicKeyCertificate = new X509Certificate2(pubKeyPath); 75 | EncryptedPrivateKeyInfo = new PKCS8.EncryptedPrivateKeyInfo(File.ReadAllBytes(priKeyPath)); 76 | LocationDescription = Path.GetPathRoot(pubKeyPath); 77 | } 78 | public string LocationDescription { get; private set; } 79 | public X509Certificate2 PublicKeyCertificate { get; private set; } 80 | public PKCS8.EncryptedPrivateKeyInfo EncryptedPrivateKeyInfo { get; private set; } 81 | public PKCS8.PrivateKeyInfo PrivateKeyInfo { get; set; } 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /LibNPKI/LibNPKI.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {FA319C3B-365D-430E-9FF7-40AA78D30AF1} 8 | Library 9 | Properties 10 | LibNPKI 11 | LibNPKI 12 | v4.6.2 13 | 512 14 | 15 | 16 | 17 | 18 | true 19 | full 20 | false 21 | bin\Debug\ 22 | DEBUG;TRACE 23 | prompt 24 | 4 25 | 26 | 27 | pdbonly 28 | true 29 | bin\Release\ 30 | TRACE 31 | prompt 32 | 4 33 | 34 | 35 | 36 | ..\packages\BouncyCastle.1.8.2\lib\BouncyCastle.Crypto.dll 37 | 38 | 39 | .\SysadminsLV.Asn1Parser.dll 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | Form 58 | 59 | 60 | CertificateSelectionDialog.cs 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | CertificateSelectionDialog.cs 74 | 75 | 76 | 77 | 78 | 79 | 80 | 이 프로젝트는 이 컴퓨터에 없는 NuGet 패키지를 참조합니다. 해당 패키지를 다운로드하려면 NuGet 패키지 복원을 사용하십시오. 자세한 내용은 http://go.microsoft.com/fwlink/?LinkID=322105를 참조하십시오. 누락된 파일은 {0}입니다. 81 | 82 | 83 | 84 | -------------------------------------------------------------------------------- /LibNPKIExample/LibNPKIExample.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {C3124558-615C-48D4-B6E3-D2288BEE6429} 8 | WinExe 9 | LibNPKIExample 10 | LibNPKIExample 11 | v4.6.2 12 | 512 13 | true 14 | 15 | 16 | 17 | 18 | AnyCPU 19 | true 20 | full 21 | false 22 | bin\Debug\ 23 | DEBUG;TRACE 24 | prompt 25 | 4 26 | 27 | 28 | AnyCPU 29 | pdbonly 30 | true 31 | bin\Release\ 32 | TRACE 33 | prompt 34 | 4 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | Form 53 | 54 | 55 | Form1.cs 56 | 57 | 58 | 59 | 60 | Form1.cs 61 | 62 | 63 | ResXFileCodeGenerator 64 | Resources.Designer.cs 65 | Designer 66 | 67 | 68 | True 69 | Resources.resx 70 | True 71 | 72 | 73 | 74 | SettingsSingleFileGenerator 75 | Settings.Designer.cs 76 | 77 | 78 | True 79 | Settings.settings 80 | True 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | {fa319c3b-365d-430e-9ff7-40aa78d30af1} 89 | LibNPKI 90 | 91 | 92 | 93 | 94 | 95 | 96 | 이 프로젝트는 이 컴퓨터에 없는 NuGet 패키지를 참조합니다. 해당 패키지를 다운로드하려면 NuGet 패키지 복원을 사용하십시오. 자세한 내용은 http://go.microsoft.com/fwlink/?LinkID=322105를 참조하십시오. 누락된 파일은 {0}입니다. 97 | 98 | 99 | 100 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.userosscache 8 | *.sln.docstates 9 | 10 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Dd]ebugPublic/ 16 | [Rr]elease/ 17 | [Rr]eleases/ 18 | x64/ 19 | x86/ 20 | bld/ 21 | [Bb]in/ 22 | [Oo]bj/ 23 | [Ll]og/ 24 | 25 | # Visual Studio 2015 cache/options directory 26 | .vs/ 27 | # Uncomment if you have tasks that create the project's static files in wwwroot 28 | #wwwroot/ 29 | 30 | # MSTest test Results 31 | [Tt]est[Rr]esult*/ 32 | [Bb]uild[Ll]og.* 33 | 34 | # NUNIT 35 | *.VisualState.xml 36 | TestResult.xml 37 | 38 | # Build Results of an ATL Project 39 | [Dd]ebugPS/ 40 | [Rr]eleasePS/ 41 | dlldata.c 42 | 43 | # DNX 44 | project.lock.json 45 | project.fragment.lock.json 46 | artifacts/ 47 | 48 | *_i.c 49 | *_p.c 50 | *_i.h 51 | *.ilk 52 | *.meta 53 | *.obj 54 | *.pch 55 | *.pdb 56 | *.pgc 57 | *.pgd 58 | *.rsp 59 | *.sbr 60 | *.tlb 61 | *.tli 62 | *.tlh 63 | *.tmp 64 | *.tmp_proj 65 | *.log 66 | *.vspscc 67 | *.vssscc 68 | .builds 69 | *.pidb 70 | *.svclog 71 | *.scc 72 | 73 | # Chutzpah Test files 74 | _Chutzpah* 75 | 76 | # Visual C++ cache files 77 | ipch/ 78 | *.aps 79 | *.ncb 80 | *.opendb 81 | *.opensdf 82 | *.sdf 83 | *.cachefile 84 | *.VC.db 85 | *.VC.VC.opendb 86 | 87 | # Visual Studio profiler 88 | *.psess 89 | *.vsp 90 | *.vspx 91 | *.sap 92 | 93 | # TFS 2012 Local Workspace 94 | $tf/ 95 | 96 | # Guidance Automation Toolkit 97 | *.gpState 98 | 99 | # ReSharper is a .NET coding add-in 100 | _ReSharper*/ 101 | *.[Rr]e[Ss]harper 102 | *.DotSettings.user 103 | 104 | # JustCode is a .NET coding add-in 105 | .JustCode 106 | 107 | # TeamCity is a build add-in 108 | _TeamCity* 109 | 110 | # DotCover is a Code Coverage Tool 111 | *.dotCover 112 | 113 | # NCrunch 114 | _NCrunch_* 115 | .*crunch*.local.xml 116 | nCrunchTemp_* 117 | 118 | # MightyMoose 119 | *.mm.* 120 | AutoTest.Net/ 121 | 122 | # Web workbench (sass) 123 | .sass-cache/ 124 | 125 | # Installshield output folder 126 | [Ee]xpress/ 127 | 128 | # DocProject is a documentation generator add-in 129 | DocProject/buildhelp/ 130 | DocProject/Help/*.HxT 131 | DocProject/Help/*.HxC 132 | DocProject/Help/*.hhc 133 | DocProject/Help/*.hhk 134 | DocProject/Help/*.hhp 135 | DocProject/Help/Html2 136 | DocProject/Help/html 137 | 138 | # Click-Once directory 139 | publish/ 140 | 141 | # Publish Web Output 142 | *.[Pp]ublish.xml 143 | *.azurePubxml 144 | # TODO: Comment the next line if you want to checkin your web deploy settings 145 | # but database connection strings (with potential passwords) will be unencrypted 146 | #*.pubxml 147 | *.publishproj 148 | 149 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 150 | # checkin your Azure Web App publish settings, but sensitive information contained 151 | # in these scripts will be unencrypted 152 | PublishScripts/ 153 | 154 | # NuGet Packages 155 | *.nupkg 156 | # The packages folder can be ignored because of Package Restore 157 | **/packages/* 158 | # except build/, which is used as an MSBuild target. 159 | !**/packages/build/ 160 | # Uncomment if necessary however generally it will be regenerated when needed 161 | #!**/packages/repositories.config 162 | # NuGet v3's project.json files produces more ignoreable files 163 | *.nuget.props 164 | *.nuget.targets 165 | 166 | # Microsoft Azure Build Output 167 | csx/ 168 | *.build.csdef 169 | 170 | # Microsoft Azure Emulator 171 | ecf/ 172 | rcf/ 173 | 174 | # Windows Store app package directories and files 175 | AppPackages/ 176 | BundleArtifacts/ 177 | Package.StoreAssociation.xml 178 | _pkginfo.txt 179 | 180 | # Visual Studio cache files 181 | # files ending in .cache can be ignored 182 | *.[Cc]ache 183 | # but keep track of directories ending in .cache 184 | !*.[Cc]ache/ 185 | 186 | # Others 187 | ClientBin/ 188 | ~$* 189 | *~ 190 | *.dbmdl 191 | *.dbproj.schemaview 192 | *.jfm 193 | *.pfx 194 | *.publishsettings 195 | node_modules/ 196 | orleans.codegen.cs 197 | 198 | # Since there are multiple workflows, uncomment next line to ignore bower_components 199 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 200 | #bower_components/ 201 | 202 | # RIA/Silverlight projects 203 | Generated_Code/ 204 | 205 | # Backup & report files from converting an old project file 206 | # to a newer Visual Studio version. Backup files are not needed, 207 | # because we have git ;-) 208 | _UpgradeReport_Files/ 209 | Backup*/ 210 | UpgradeLog*.XML 211 | UpgradeLog*.htm 212 | 213 | # SQL Server files 214 | *.mdf 215 | *.ldf 216 | 217 | # Business Intelligence projects 218 | *.rdl.data 219 | *.bim.layout 220 | *.bim_*.settings 221 | 222 | # Microsoft Fakes 223 | FakesAssemblies/ 224 | 225 | # GhostDoc plugin setting file 226 | *.GhostDoc.xml 227 | 228 | # Node.js Tools for Visual Studio 229 | .ntvs_analysis.dat 230 | 231 | # Visual Studio 6 build log 232 | *.plg 233 | 234 | # Visual Studio 6 workspace options file 235 | *.opt 236 | 237 | # Visual Studio LightSwitch build output 238 | **/*.HTMLClient/GeneratedArtifacts 239 | **/*.DesktopClient/GeneratedArtifacts 240 | **/*.DesktopClient/ModelManifest.xml 241 | **/*.Server/GeneratedArtifacts 242 | **/*.Server/ModelManifest.xml 243 | _Pvt_Extensions 244 | 245 | # Paket dependency manager 246 | .paket/paket.exe 247 | paket-files/ 248 | 249 | # FAKE - F# Make 250 | .fake/ 251 | 252 | # JetBrains Rider 253 | .idea/ 254 | *.sln.iml 255 | 256 | # CodeRush 257 | .cr/ 258 | 259 | # Python Tools for Visual Studio (PTVS) 260 | __pycache__/ 261 | *.pyc -------------------------------------------------------------------------------- /LibNPKIExample/Properties/Resources.resx: -------------------------------------------------------------------------------- 1 | 2 | 3 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | text/microsoft-resx 107 | 108 | 109 | 2.0 110 | 111 | 112 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 113 | 114 | 115 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 116 | 117 | -------------------------------------------------------------------------------- /LibNPKIExample/Form1.resx: -------------------------------------------------------------------------------- 1 | 2 | 3 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | text/microsoft-resx 110 | 111 | 112 | 2.0 113 | 114 | 115 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 116 | 117 | 118 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 119 | 120 | -------------------------------------------------------------------------------- /LibNPKI/CertificateSelectionDialog.resx: -------------------------------------------------------------------------------- 1 | 2 | 3 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | text/microsoft-resx 110 | 111 | 112 | 2.0 113 | 114 | 115 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 116 | 117 | 118 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 119 | 120 | -------------------------------------------------------------------------------- /LibNPKI/VIDVerifier.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using System.Security.Cryptography; 7 | using System.Diagnostics; 8 | using System.Security.Cryptography.X509Certificates; 9 | using SysadminsLV.Asn1Parser; 10 | using SysadminsLV.Asn1Parser.Universal; 11 | using Org.BouncyCastle.Asn1; 12 | using Mono.Security; 13 | using Mono.Security.Cryptography; 14 | using LibNPKI.Exceptions; 15 | 16 | namespace LibNPKI 17 | { 18 | public class VIDVerifier 19 | { 20 | public bool VerifyWithID(X509Certificate2 cert, PKCS8.PrivateKeyInfo priKey, string idn) 21 | { 22 | getVIDHash(cert, out string name, out string hashOid, out byte[] hash); 23 | Debug.WriteLine("Name : " + name); 24 | Debug.WriteLine("hashOid : " + hashOid); 25 | Debug.WriteLine("Hash : " + BitConverter.ToString(hash)); 26 | byte[] randomNum = getRandomNum(priKey); 27 | Debug.WriteLine("RandomNum : " + BitConverter.ToString(randomNum)); 28 | byte[] hash2 = generateVIDHash(randomNum, idn, hashOid); 29 | Debug.WriteLine("Hash2 : " + BitConverter.ToString(hash2)); 30 | return Enumerable.SequenceEqual(hash, hash2); 31 | 32 | } 33 | private byte[] doubleHash(byte[] content, string oid) 34 | { 35 | // VID = h(h(VID, R)); 36 | // get hash algorithm obj from oid 37 | string hashName = Oid.FromOidValue(oid, OidGroup.HashAlgorithm).FriendlyName; 38 | Debug.WriteLine("VID Hash Algorithm : " + hashName); 39 | HashAlgorithm hashAlgorithm = HashAlgorithm.Create(hashName); 40 | // return hashed 41 | byte[] resultTmp = hashAlgorithm.ComputeHash(content); 42 | return hashAlgorithm.ComputeHash(resultTmp); 43 | 44 | } 45 | private byte[] generateVIDHash(byte[] randomNum, string idn, string oid) 46 | { 47 | // HashContent ::= SEQEUNCE {idn PrintableString, randomNum BIT_STRING } 48 | DerSequence sequence = new DerSequence(new DerPrintableString(idn), new DerBitString(randomNum)); 49 | return doubleHash(sequence.GetDerEncoded(), oid); 50 | 51 | } 52 | private byte[] getRandomNum(PKCS8.PrivateKeyInfo priKey) 53 | { 54 | foreach(ASN1 i in priKey.Attributes) 55 | { 56 | Asn1Reader reader = new Asn1Reader(i.GetBytes()); 57 | bool isRandomNumAttribute = false, inSET = false; 58 | do 59 | { 60 | if (reader.TagName == "OBJECT_IDENTIFIER") 61 | { 62 | if (((Asn1ObjectIdentifier)reader.GetTagObject()).Value.Value == "1.2.410.200004.10.1.1.3") 63 | { 64 | isRandomNumAttribute = true; 65 | } 66 | } 67 | else if (reader.TagName == "SET" && isRandomNumAttribute) 68 | { 69 | inSET = true; 70 | } 71 | else if (reader.TagName == "BIT_STRING" && inSET) 72 | { 73 | Asn1BitString asn1BitString = new Asn1BitString(reader); 74 | return asn1BitString.Value; 75 | } 76 | } while (reader.MoveNext()); 77 | } 78 | throw new VIDOperationException("RandomNum in private key attributes is missing"); 79 | } 80 | private void getVIDHash(X509Certificate2 cert, out string name, out string hashAlg, out byte[] hash) 81 | { 82 | // ignore warnings 83 | name = ""; 84 | hashAlg = ""; 85 | hash = new byte[] { }; 86 | bool notset_n = true, notset_hal = true, notset_ha = true; 87 | foreach (var ext in cert.Extensions) 88 | { 89 | if (ext.Oid.Value != "2.5.29.17") continue; 90 | Asn1Reader reader = new Asn1Reader(ext.RawData); 91 | bool kisaIdentifyData = false, kisaVid = false; // TO-DO : Caluate Depth 92 | do 93 | { 94 | switch (reader.TagName) 95 | { 96 | case "OBJECT_IDENTIFIER": 97 | Asn1ObjectIdentifier identifier = (Asn1ObjectIdentifier)reader.GetTagObject(); 98 | string oid = identifier.Value.Value; 99 | if (oid == "1.2.410.200004.10.1.1") 100 | kisaIdentifyData = true; 101 | else if (oid == "1.2.410.200004.10.1.1.1") 102 | kisaVid = true; 103 | else if (kisaVid && notset_hal) 104 | { 105 | hashAlg = oid; 106 | notset_hal = false; 107 | } 108 | break; 109 | case "UTF8String": 110 | if (kisaIdentifyData && notset_n) 111 | { 112 | name = Encoding.UTF8.GetString(reader.GetPayload()); 113 | notset_n = false; 114 | } 115 | break; 116 | case "OCTET_STRING": 117 | if (kisaVid && notset_ha) 118 | { 119 | SysadminsLV.Asn1Parser.Universal.Asn1OctetString octetString = new SysadminsLV.Asn1Parser.Universal.Asn1OctetString(reader); 120 | hash = octetString.Value; 121 | notset_ha = false; 122 | } 123 | break; 124 | default: 125 | break; 126 | } 127 | } while (reader.MoveNext()); 128 | if (notset_ha || notset_hal || notset_n) 129 | { 130 | throw new VIDOperationException("Some of vid informations in certificate are missing"); 131 | } 132 | } 133 | } 134 | } 135 | } 136 | -------------------------------------------------------------------------------- /LibNPKIExample/Form1.Designer.cs: -------------------------------------------------------------------------------- 1 | namespace NPKIIdentication 2 | { 3 | partial class Form1 4 | { 5 | /// 6 | /// 필수 디자이너 변수입니다. 7 | /// 8 | private System.ComponentModel.IContainer components = null; 9 | 10 | /// 11 | /// 사용 중인 모든 리소스를 정리합니다. 12 | /// 13 | /// 관리되는 리소스를 삭제해야 하면 true이고, 그렇지 않으면 false입니다. 14 | protected override void Dispose(bool disposing) 15 | { 16 | if (disposing && (components != null)) 17 | { 18 | components.Dispose(); 19 | } 20 | base.Dispose(disposing); 21 | } 22 | 23 | #region Windows Form 디자이너에서 생성한 코드 24 | 25 | /// 26 | /// 디자이너 지원에 필요한 메서드입니다. 27 | /// 이 메서드의 내용을 코드 편집기로 수정하지 마세요. 28 | /// 29 | private void InitializeComponent() 30 | { 31 | this.button1 = new System.Windows.Forms.Button(); 32 | this.groupBox1 = new System.Windows.Forms.GroupBox(); 33 | this.label1 = new System.Windows.Forms.Label(); 34 | this.textBox1 = new System.Windows.Forms.TextBox(); 35 | this.button2 = new System.Windows.Forms.Button(); 36 | this.button3 = new System.Windows.Forms.Button(); 37 | this.textBox2 = new System.Windows.Forms.TextBox(); 38 | this.label2 = new System.Windows.Forms.Label(); 39 | this.groupBox1.SuspendLayout(); 40 | this.SuspendLayout(); 41 | // 42 | // button1 43 | // 44 | this.button1.Location = new System.Drawing.Point(27, 23); 45 | this.button1.Name = "button1"; 46 | this.button1.Size = new System.Drawing.Size(125, 23); 47 | this.button1.TabIndex = 2; 48 | this.button1.Text = "공인인증서 불러오기"; 49 | this.button1.UseVisualStyleBackColor = true; 50 | this.button1.Click += new System.EventHandler(this.button1_Click); 51 | // 52 | // groupBox1 53 | // 54 | this.groupBox1.Controls.Add(this.label2); 55 | this.groupBox1.Controls.Add(this.textBox2); 56 | this.groupBox1.Controls.Add(this.button3); 57 | this.groupBox1.Controls.Add(this.label1); 58 | this.groupBox1.Controls.Add(this.textBox1); 59 | this.groupBox1.Controls.Add(this.button2); 60 | this.groupBox1.Enabled = false; 61 | this.groupBox1.Location = new System.Drawing.Point(27, 52); 62 | this.groupBox1.Name = "groupBox1"; 63 | this.groupBox1.Size = new System.Drawing.Size(511, 250); 64 | this.groupBox1.TabIndex = 3; 65 | this.groupBox1.TabStop = false; 66 | this.groupBox1.Text = "이것저것"; 67 | this.groupBox1.Visible = false; 68 | // 69 | // label1 70 | // 71 | this.label1.AutoSize = true; 72 | this.label1.Location = new System.Drawing.Point(87, 50); 73 | this.label1.Name = "label1"; 74 | this.label1.Size = new System.Drawing.Size(387, 24); 75 | this.label1.TabIndex = 2; 76 | this.label1.Text = "본인식별번호(주민등록번호나 사업자등록번호 같은 거)를 위에 치세요.\r\n-는 치지 마세요."; 77 | // 78 | // textBox1 79 | // 80 | this.textBox1.Location = new System.Drawing.Point(87, 22); 81 | this.textBox1.Name = "textBox1"; 82 | this.textBox1.PasswordChar = '*'; 83 | this.textBox1.Size = new System.Drawing.Size(380, 21); 84 | this.textBox1.TabIndex = 1; 85 | // 86 | // button2 87 | // 88 | this.button2.Location = new System.Drawing.Point(6, 20); 89 | this.button2.Name = "button2"; 90 | this.button2.Size = new System.Drawing.Size(75, 23); 91 | this.button2.TabIndex = 0; 92 | this.button2.Text = "본인식별"; 93 | this.button2.UseVisualStyleBackColor = true; 94 | this.button2.Click += new System.EventHandler(this.button2_Click); 95 | // 96 | // button3 97 | // 98 | this.button3.Location = new System.Drawing.Point(6, 81); 99 | this.button3.Name = "button3"; 100 | this.button3.Size = new System.Drawing.Size(106, 23); 101 | this.button3.TabIndex = 3; 102 | this.button3.Text = "개인키 내보내기"; 103 | this.button3.UseVisualStyleBackColor = true; 104 | this.button3.Click += new System.EventHandler(this.button3_Click); 105 | // 106 | // textBox2 107 | // 108 | this.textBox2.Location = new System.Drawing.Point(118, 83); 109 | this.textBox2.Name = "textBox2"; 110 | this.textBox2.Size = new System.Drawing.Size(349, 21); 111 | this.textBox2.TabIndex = 4; 112 | this.textBox2.Text = "C:\\pri.der"; 113 | // 114 | // label2 115 | // 116 | this.label2.AutoSize = true; 117 | this.label2.Location = new System.Drawing.Point(118, 111); 118 | this.label2.Name = "label2"; 119 | this.label2.Size = new System.Drawing.Size(292, 12); 120 | this.label2.TabIndex = 5; 121 | this.label2.Text = "PKCS#1 RSA Private Key + DER 형식으로 내보내짐."; 122 | // 123 | // Form1 124 | // 125 | this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 12F); 126 | this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 127 | this.ClientSize = new System.Drawing.Size(618, 372); 128 | this.Controls.Add(this.groupBox1); 129 | this.Controls.Add(this.button1); 130 | this.Name = "Form1"; 131 | this.Text = "예시"; 132 | this.groupBox1.ResumeLayout(false); 133 | this.groupBox1.PerformLayout(); 134 | this.ResumeLayout(false); 135 | 136 | } 137 | 138 | #endregion 139 | private System.Windows.Forms.Button button1; 140 | private System.Windows.Forms.GroupBox groupBox1; 141 | private System.Windows.Forms.Button button2; 142 | private System.Windows.Forms.Label label1; 143 | private System.Windows.Forms.TextBox textBox1; 144 | private System.Windows.Forms.TextBox textBox2; 145 | private System.Windows.Forms.Button button3; 146 | private System.Windows.Forms.Label label2; 147 | } 148 | } 149 | 150 | -------------------------------------------------------------------------------- /LibNPKI/CertificateSelectionDialog.Designer.cs: -------------------------------------------------------------------------------- 1 | namespace LibNPKI 2 | { 3 | partial class CertificateSelectionDialog 4 | { 5 | /// 6 | /// Required designer variable. 7 | /// 8 | private System.ComponentModel.IContainer components = null; 9 | 10 | /// 11 | /// Clean up any resources being used. 12 | /// 13 | /// true if managed resources should be disposed; otherwise, false. 14 | protected override void Dispose(bool disposing) 15 | { 16 | if (disposing && (components != null)) 17 | { 18 | components.Dispose(); 19 | } 20 | base.Dispose(disposing); 21 | } 22 | 23 | #region Windows Form Designer generated code 24 | 25 | /// 26 | /// Required method for Designer support - do not modify 27 | /// the contents of this method with the code editor. 28 | /// 29 | private void InitializeComponent() 30 | { 31 | this.groupBox1 = new System.Windows.Forms.GroupBox(); 32 | this.label2 = new System.Windows.Forms.Label(); 33 | this.label1 = new System.Windows.Forms.Label(); 34 | this.textBox1 = new System.Windows.Forms.TextBox(); 35 | this.button2 = new System.Windows.Forms.Button(); 36 | this.button3 = new System.Windows.Forms.Button(); 37 | this.groupBox2 = new System.Windows.Forms.GroupBox(); 38 | this.listView1 = new System.Windows.Forms.ListView(); 39 | this.columnHeader1 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); 40 | this.columnHeader2 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); 41 | this.columnHeader3 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); 42 | this.groupBox1.SuspendLayout(); 43 | this.groupBox2.SuspendLayout(); 44 | this.SuspendLayout(); 45 | // 46 | // groupBox1 47 | // 48 | this.groupBox1.Controls.Add(this.label2); 49 | this.groupBox1.Controls.Add(this.label1); 50 | this.groupBox1.Controls.Add(this.textBox1); 51 | this.groupBox1.Location = new System.Drawing.Point(12, 394); 52 | this.groupBox1.Name = "groupBox1"; 53 | this.groupBox1.Size = new System.Drawing.Size(381, 78); 54 | this.groupBox1.TabIndex = 3; 55 | this.groupBox1.TabStop = false; 56 | this.groupBox1.Text = "비밀번호"; 57 | // 58 | // label2 59 | // 60 | this.label2.AutoSize = true; 61 | this.label2.Location = new System.Drawing.Point(67, 51); 62 | this.label2.Name = "label2"; 63 | this.label2.Size = new System.Drawing.Size(261, 12); 64 | this.label2.TabIndex = 2; 65 | this.label2.Text = "공인인증서 비밀번호는 대소문자를 구별합니다."; 66 | // 67 | // label1 68 | // 69 | this.label1.AutoSize = true; 70 | this.label1.Location = new System.Drawing.Point(8, 24); 71 | this.label1.Name = "label1"; 72 | this.label1.Size = new System.Drawing.Size(53, 12); 73 | this.label1.TabIndex = 1; 74 | this.label1.Text = "비밀번호"; 75 | // 76 | // textBox1 77 | // 78 | this.textBox1.Location = new System.Drawing.Point(67, 21); 79 | this.textBox1.Name = "textBox1"; 80 | this.textBox1.PasswordChar = '*'; 81 | this.textBox1.Size = new System.Drawing.Size(308, 21); 82 | this.textBox1.TabIndex = 0; 83 | this.textBox1.KeyUp += new System.Windows.Forms.KeyEventHandler(this.textBox1_KeyUp); 84 | // 85 | // button2 86 | // 87 | this.button2.Location = new System.Drawing.Point(318, 478); 88 | this.button2.Name = "button2"; 89 | this.button2.Size = new System.Drawing.Size(75, 23); 90 | this.button2.TabIndex = 4; 91 | this.button2.Text = "확인"; 92 | this.button2.UseVisualStyleBackColor = true; 93 | this.button2.Click += new System.EventHandler(this.button2_Click); 94 | // 95 | // button3 96 | // 97 | this.button3.Location = new System.Drawing.Point(237, 478); 98 | this.button3.Name = "button3"; 99 | this.button3.Size = new System.Drawing.Size(75, 23); 100 | this.button3.TabIndex = 5; 101 | this.button3.Text = "닫기"; 102 | this.button3.UseVisualStyleBackColor = true; 103 | this.button3.Click += new System.EventHandler(this.button3_Click); 104 | // 105 | // groupBox2 106 | // 107 | this.groupBox2.Controls.Add(this.listView1); 108 | this.groupBox2.Location = new System.Drawing.Point(13, 12); 109 | this.groupBox2.Name = "groupBox2"; 110 | this.groupBox2.Size = new System.Drawing.Size(380, 376); 111 | this.groupBox2.TabIndex = 6; 112 | this.groupBox2.TabStop = false; 113 | this.groupBox2.Text = "인증서 목록"; 114 | // 115 | // listView1 116 | // 117 | this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] { 118 | this.columnHeader1, 119 | this.columnHeader2, 120 | this.columnHeader3}); 121 | this.listView1.Dock = System.Windows.Forms.DockStyle.Fill; 122 | this.listView1.FullRowSelect = true; 123 | this.listView1.Location = new System.Drawing.Point(3, 17); 124 | this.listView1.MultiSelect = false; 125 | this.listView1.Name = "listView1"; 126 | this.listView1.Size = new System.Drawing.Size(374, 356); 127 | this.listView1.TabIndex = 7; 128 | this.listView1.UseCompatibleStateImageBehavior = false; 129 | this.listView1.View = System.Windows.Forms.View.Details; 130 | // 131 | // columnHeader1 132 | // 133 | this.columnHeader1.Text = "이름"; 134 | this.columnHeader1.Width = 250; 135 | // 136 | // columnHeader2 137 | // 138 | this.columnHeader2.Text = "만료일"; 139 | this.columnHeader2.Width = 70; 140 | // 141 | // columnHeader3 142 | // 143 | this.columnHeader3.Text = "위치"; 144 | this.columnHeader3.Width = 40; 145 | // 146 | // CertificateSelectionDialog 147 | // 148 | this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 12F); 149 | this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 150 | this.ClientSize = new System.Drawing.Size(405, 509); 151 | this.ControlBox = false; 152 | this.Controls.Add(this.groupBox2); 153 | this.Controls.Add(this.button3); 154 | this.Controls.Add(this.button2); 155 | this.Controls.Add(this.groupBox1); 156 | this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog; 157 | this.Name = "CertificateSelectionDialog"; 158 | this.Text = "공인인증서 선택"; 159 | this.groupBox1.ResumeLayout(false); 160 | this.groupBox1.PerformLayout(); 161 | this.groupBox2.ResumeLayout(false); 162 | this.ResumeLayout(false); 163 | 164 | } 165 | 166 | #endregion 167 | private System.Windows.Forms.GroupBox groupBox1; 168 | private System.Windows.Forms.Label label2; 169 | private System.Windows.Forms.Label label1; 170 | private System.Windows.Forms.TextBox textBox1; 171 | private System.Windows.Forms.Button button2; 172 | private System.Windows.Forms.Button button3; 173 | private System.Windows.Forms.GroupBox groupBox2; 174 | private System.Windows.Forms.ListView listView1; 175 | private System.Windows.Forms.ColumnHeader columnHeader1; 176 | private System.Windows.Forms.ColumnHeader columnHeader2; 177 | private System.Windows.Forms.ColumnHeader columnHeader3; 178 | } 179 | } --------------------------------------------------------------------------------