An error occurred while fetching merge requests data.
Commit dcf42c4b authored by Josh Ji's avatar Josh Ji

Add project files.

parent 0a968ae5

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.1.32210.238
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Simulation", "Simulation\Simulation.csproj", "{C02C0C2C-B3D8-437C-BE49-7C2AEB60E6E4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{C02C0C2C-B3D8-437C-BE49-7C2AEB60E6E4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C02C0C2C-B3D8-437C-BE49-7C2AEB60E6E4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C02C0C2C-B3D8-437C-BE49-7C2AEB60E6E4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C02C0C2C-B3D8-437C-BE49-7C2AEB60E6E4}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {E1177C9B-0A84-4981-9B57-3F182A3C6DC8}
EndGlobalSection
EndGlobal
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
using System.Diagnostics;
using System.Security.Cryptography.X509Certificates;
namespace Simulation {
internal class Authenticator {
private string IDx;
private byte[] PrKx;
private byte[] PuKx;
private int Cx;
private int Rx;
private int Rp;
private Random random = new Random();
private RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
private Aes aes = Aes.Create();
// called from RP
public X509Certificate2 getCertificate(string IDr, string Nickr, object Rr) {
Cx += 1;
string Subject = "cn=" + getHMAC(IDx, Rx, Rp, Cx);
string Issuer = "cn=ExampleProvider:" + Convert.ToHexString(aes.EncryptCbc(BitConverter.GetBytes(Cx), aes.Key.Take(16).ToArray()));
CertificateRequest certificateRequest = new CertificateRequest(Subject, rsa,HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
X509SignatureGenerator signatureGenerator = X509SignatureGenerator.CreateForRSA(rsa, RSASignaturePadding.Pkcs1);
X509Certificate2 x509Certificate = certificateRequest.Create(new X500DistinguishedName(Issuer),signatureGenerator, DateTimeOffset.Now, DateTimeOffset.MaxValue, new byte[] { 0x00});
return x509Certificate;
}
// this method is same as the one in IdP
private string getHMAC(string IDx, int Rx, int Rp, int Cx) {
byte[] hashKey = SHA256.HashData(BitConverter.GetBytes(Rx * Rp));
string hmac = Convert.ToHexString(HMACSHA256.HashData(hashKey, Encoding.ASCII.GetBytes(IDx + Cx.ToString())));
return hmac;
}
// called from IdP
public object[] getPuKxAndRx(string IDx) {
this.IDx = IDx;
Rx = generateRx();
PuKx = rsa.ExportRSAPublicKey();
PrKx = rsa.ExportRSAPrivateKey();
return new object[] {PuKx, Rx};
}
private int generateRx() {
return random.Next();
}
// called from IdP
public byte[] getCounter(byte[] encryptedRp) {
byte[] decryptData = rsa.Decrypt(encryptedRp, RSAEncryptionPadding.Pkcs1);
Rp = BitConverter.ToInt32(decryptData, 0);
Cx = random.Next();
aes.Key = SHA256.HashData(BitConverter.GetBytes(Rx * Rp));
byte[] encryptCounter = aes.EncryptCbc(BitConverter.GetBytes(Cx), aes.Key.Take(16).ToArray());
Debug.WriteLine("autenticator: PuKx=\n" + BitConverter.ToString(PuKx));
Debug.WriteLine("autenticator: Rx=" + Rx);
Debug.WriteLine("autenticator: Rp=" + Rp);
Debug.WriteLine("autenticator: Cx=" + Cx);
return encryptCounter;
}
}
}
namespace Simulation {
partial class Form1 {
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing) {
if (disposing && (components != null)) {
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent() {
this.SuspendLayout();
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(482, 355);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
}
}
\ No newline at end of file
namespace Simulation {
public partial class Form1 : Form {
Authenticator authenticator = new Authenticator();
IdentityProvider identityProvider = new IdentityProvider();
RelyingParty relyingParty = new RelyingParty();
public Form1() {
InitializeComponent();
/**
* NauthnticatorBobjb@_
*/
identityProvider.initialize(authenticator, "Bob");
relyingParty.register(authenticator, new User("Bob NickName"), identityProvider);
}
}
}
\ No newline at end of file
<root>
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>
\ No newline at end of file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Simulation {
/**
* Identitiy in IdP
*/
internal class Identity {
public string IDx;
public byte[] PuKx;
public int Rx;
public int Rp;
public int Cx;
public string HMAC;
public Identity(string IDx) {
this.IDx = IDx;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
using System.Diagnostics;
namespace Simulation {
internal class IdentityProvider {
List<Identity> _identityList = new List<Identity>();
Random random = new Random();
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
Aes aes = Aes.Create();
// called from Form1
public void initialize(Authenticator authenticator, string IDx) {
Identity identity = new Identity(IDx);
// get public key and Rx
object[] PuKxAndRx = authenticator.getPuKxAndRx(IDx);
byte[] PuKx = (byte[])(PuKxAndRx[0]);
identity.PuKx = PuKx;
int Rx = (int)(PuKxAndRx[1]);
identity.Rx = Rx;
int Rp = generateRp();
identity.Rp = Rp;
// get counter
byte[] encryptedCounter = authenticator.getCounter(encryptRp(PuKx, Rp));
int Cx = decryptCounter(Rx,Rp, encryptedCounter);
identity.Cx = Cx+1;//預先加一
// calculate hmac
identity.HMAC = getHMAC(IDx, Rx, Rp, identity.Cx);
Debug.WriteLine("IdP: PuKx=\n" + BitConverter.ToString(PuKx));
Debug.WriteLine("IdP: Rx=" + Rx);
Debug.WriteLine("IdP: Rp=" + Rp);
Debug.WriteLine("IdP: Cx=" + Cx);
Debug.WriteLine("IdP: HMAC=" + identity.HMAC);
_identityList.Add(identity);
}
private int generateRp() {
return random.Next();
}
private byte[] encryptRp(byte[] PuKx, int Rp) {
rsa.ImportRSAPublicKey(PuKx, out _);
byte[] encryptedRp = rsa.Encrypt(BitConverter.GetBytes(Rp), RSAEncryptionPadding.Pkcs1);
return encryptedRp;
}
private int decryptCounter(int Rx, int Rp, byte[] encryptedCounter) {
aes.Key = SHA256.HashData(BitConverter.GetBytes(Rx*Rp));
byte[] decryptedData = aes.DecryptCbc(encryptedCounter, aes.Key.Take(16).ToArray());
int counter = BitConverter.ToInt32(decryptedData);
return counter;
}
// this method is same as the one in authenticator
private string getHMAC(string IDx, int Rx, int Rp, int Cx) {
byte[] hashKey = SHA256.HashData(BitConverter.GetBytes(Rx * Rp));
string hmac = Convert.ToHexString(HMACSHA256.HashData(hashKey, Encoding.ASCII.GetBytes(IDx + Cx.ToString())));
return hmac;
}
// called from RP
public string verify(string subject, string issuer) {
string hmac = subject.Split("=")[1];//"cn=[hmac]"
byte[] encryptedCounter = Convert.FromHexString(issuer.Split(":")[1]);//"cn=ExampleProvider:[encryptedCounter]"
Debug.WriteLine(hmac);
foreach(Identity i in _identityList) {
if (i.HMAC == hmac) {
if (i.Cx == decryptCounter(i.Rx, i.Rp, encryptedCounter))
return i.IDx;
else {
Debug.WriteLine("Cx not match");
return null;
}
}
}
Debug.WriteLine("HMAC not match");
return null;
}
}
}
\ No newline at end of file
namespace Simulation {
internal static class Program {
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main() {
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize();
Application.Run(new Form1());
}
}
}
\ No newline at end of file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Diagnostics;
namespace Simulation {
internal class RelyingParty {
Random random = new Random();
string IDr = "www.exampleRP.com";
// called from Form1
public void register(Authenticator authenticator, User user, IdentityProvider identityProvider) {
string Nickr = user.nickname;
int Rr = random.Next();
// get credential from authenticator
X509Certificate2 x509Certificate = authenticator.getCertificate(IDr, Nickr, Rr);
string subject = x509Certificate.Subject;
string issuer = x509Certificate.Issuer;
Debug.WriteLine("subject= "+subject);
Debug.WriteLine("issuer= "+issuer);
verify(identityProvider, subject, issuer);
}
// get identity from IdP
private void verify(IdentityProvider identityProvider, string subject, string issuer) {
String IDx = identityProvider.verify(subject, issuer);
Debug.WriteLine("verify: IDx=" + IDx);
}
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWindowsForms>true</UseWindowsForms>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
</Project>
\ No newline at end of file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Simulation {
/**
* User in Relying Party
*/
internal class User {
public string nickname;
public User(string nickname) {
this.nickname = nickname;
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment