216 lines
8.8 KiB
Plaintext
216 lines
8.8 KiB
Plaintext
@using System.Security.Cryptography
|
|
@using System.Text
|
|
@using Org.BouncyCastle.Crypto
|
|
@using Org.BouncyCastle.Crypto.Generators
|
|
@using Org.BouncyCastle.Crypto.Parameters
|
|
@using Org.BouncyCastle.OpenSsl
|
|
@using Org.BouncyCastle.Security
|
|
@using SystemX.Core.Communication
|
|
@using VPKI.Library.Packet
|
|
|
|
@inject CertificateService CertificateService
|
|
@inject ApiService ApiService
|
|
@inject VpkiDialogService DialogService
|
|
|
|
<RadzenFieldset Text="Certificate">
|
|
<div style="display:flex">
|
|
<!--generator-->
|
|
<RadzenFieldset Style="width: 100%; margin-right: 20px;" Text="Certificate Generator">
|
|
<div style="margin-bottom: 10px;">
|
|
<RadzenButton Style="width:100%;" Text="@($"Signed Hash ({StrISO})")" Click="OnClickSignedHash"></RadzenButton>
|
|
|
|
<div style="margin-top: 10px;">
|
|
<RadzenTextArea Style="width:100%; height: 130px;" @bind-Value=@StringCsrHashed>
|
|
</RadzenTextArea>
|
|
</div>
|
|
</div>
|
|
<div style="margin-bottom: 10px;">
|
|
<RadzenLabel Style="width: 130px;" Text="Device ID"></RadzenLabel>
|
|
<RadzenTextBox Style="margin-right: 10px;" @bind-Value="@CertificateContainer.RequestCertificate.iftid"></RadzenTextBox>
|
|
</div>
|
|
<div style="margin-bottom: 10px;">
|
|
<RadzenLabel Style="width: 130px;" Text="Tier Code"></RadzenLabel>
|
|
<RadzenTextBox Style="margin-right: 10px;" @bind-Value="@CertificateContainer.RequestCertificate.tierCode"></RadzenTextBox>
|
|
</div>
|
|
<div style="margin-bottom: 10px;">
|
|
<RadzenLabel Style="width: 130px;" Text="Unit Code"></RadzenLabel>
|
|
<RadzenTextBox Style="margin-right: 10px;" @bind-Value="@CertificateContainer.RequestCertificate.unitCode"></RadzenTextBox>
|
|
</div>
|
|
<div style="margin-bottom: 10px;">
|
|
<RadzenLabel Style="width: 130px;" Text="Vehicle Code"></RadzenLabel>
|
|
<RadzenTextBox Style="margin-right: 10px;" @bind-Value="@CertificateContainer.RequestCertificate.vehicleCode"></RadzenTextBox>
|
|
</div>
|
|
<div style="margin-bottom: 10px;">
|
|
<RadzenLabel Style="width: 130px;" Text="Local Code"></RadzenLabel>
|
|
<RadzenTextBox Style="margin-right: 10px;" @bind-Value="@CertificateContainer.RequestCertificate.localCode"></RadzenTextBox>
|
|
</div>
|
|
<div style="margin-bottom: 10px;">
|
|
<RadzenLabel Style="width: 130px;" Text="Brand Code"></RadzenLabel>
|
|
<RadzenTextBox Style="margin-right: 10px;" @bind-Value="@CertificateContainer.RequestCertificate.brandCode"></RadzenTextBox>
|
|
</div>
|
|
</RadzenFieldset>
|
|
|
|
<!--TX RX-->
|
|
<RadzenFieldset Style="width:100%;" Text="Call API">
|
|
<div>
|
|
<RadzenButton Style="width:100%; margin-bottom: 0.5rem;" Text="@($"Generate Request {StrISO}")" Click="@OnClickGenerateCertificate"></RadzenButton>
|
|
<RadzenButton Style="width:100%;" Text="@($"Send Request")" Click="@OnClickSendCertificate"></RadzenButton>
|
|
</div>
|
|
<div>
|
|
<div>
|
|
<RadzenLabel Text="Request"></RadzenLabel>
|
|
</div>
|
|
<div>
|
|
<RadzenTextArea Style="width:100%; height: 180px;" @bind-Value=@CertificateContainer.StrRequest>
|
|
</RadzenTextArea>
|
|
</div>
|
|
<div>
|
|
<RadzenLabel Text="Response"></RadzenLabel>
|
|
</div>
|
|
<div>
|
|
<RadzenTextArea Style="width:100%; height: 180px;" @bind-Value="@CertificateContainer.StrResponse">
|
|
</RadzenTextArea>
|
|
</div>
|
|
</div>
|
|
</RadzenFieldset>
|
|
</div>
|
|
</RadzenFieldset>
|
|
|
|
@code {
|
|
[Parameter, EditorRequired]
|
|
public CertificateContainer CertificateContainer { get; set; } = new CertificateContainer();
|
|
[Parameter]
|
|
public EventCallback<CertificateContainer> CertificateContainerChanged { get; set; }
|
|
|
|
[Parameter, EditorRequired]
|
|
public string ServerAddress { get; set; } = string.Empty;
|
|
|
|
[Parameter]
|
|
public TbscsrContainer TbscsrContainer { get; set; } = new TbscsrContainer();
|
|
|
|
//ISO
|
|
private string StrISO = string.Empty;
|
|
|
|
private string StringCsrHashed = string.Empty;
|
|
|
|
protected override void OnInitialized()
|
|
{
|
|
StrISO = $"{TbscsrContainer.ISOType.ToString().Replace("_", "-")}";
|
|
}
|
|
|
|
protected override void OnParametersSet()
|
|
{
|
|
string ecdsa = "";
|
|
if (TbscsrContainer.VpkiType == VpkiType.prov_v1)
|
|
{
|
|
ecdsa = ECDSAType.SHA256WITHECDSA.ToString();
|
|
}
|
|
else if (TbscsrContainer.VpkiType == VpkiType.prov_cert || TbscsrContainer.VpkiType == VpkiType.vehicle_cert)
|
|
{
|
|
ecdsa = ECDSAType.NONEWITHECDSA.ToString();
|
|
}
|
|
|
|
CreateCsrHash(ecdsa);
|
|
}
|
|
|
|
public void OnClickSignedHash()
|
|
{
|
|
SignedHash();
|
|
}
|
|
|
|
private void SignedHash()
|
|
{
|
|
if (TbscsrContainer.KeyPair != null && TbscsrContainer.ResponseTbscsr.data != null)
|
|
{
|
|
var publicKey = (ECPublicKeyParameters)TbscsrContainer.KeyPair.Public;
|
|
|
|
string ecdsa = "";
|
|
if (TbscsrContainer.VpkiType == VpkiType.prov_v1)
|
|
{
|
|
ecdsa = ECDSAType.SHA256WITHECDSA.ToString();
|
|
var pKey = TbscsrContainer.PublicKey;
|
|
CertificateContainer.CsrHashed = CertificateService.SignHashed02(TbscsrContainer.ResponseTbscsr.data.hashedtbscsr, TbscsrContainer.KeyPair);
|
|
}
|
|
else if (TbscsrContainer.VpkiType == VpkiType.prov_cert || TbscsrContainer.VpkiType == VpkiType.vehicle_cert)
|
|
{
|
|
ecdsa = ECDSAType.NONEWITHECDSA.ToString();
|
|
CertificateContainer.CsrHashed = CertificateService.SignHashed20(TbscsrContainer.ResponseTbscsr.data.hashedtbscsr, TbscsrContainer.KeyPair);
|
|
}
|
|
|
|
CreateCsrHash(ecdsa);
|
|
}
|
|
else
|
|
{
|
|
StringCsrHashed += "Request tbscsr first";
|
|
}
|
|
}
|
|
|
|
private void CreateCsrHash(string ecdsa)
|
|
{
|
|
if(string.IsNullOrEmpty(CertificateContainer.CsrHashed.SignedCsr) == false)
|
|
{
|
|
StringCsrHashed = $"{StrISO} Signed {ecdsa}:{Environment.NewLine}";
|
|
StringCsrHashed += $"{CertificateContainer.CsrHashed.SignedCsr}{Environment.NewLine}";
|
|
StringCsrHashed += $"{Environment.NewLine}";
|
|
|
|
StringCsrHashed += $"{StrISO} Signed {ecdsa}(DER ENCODED):{Environment.NewLine}";
|
|
StringCsrHashed += $"{CertificateContainer.CsrHashed.EncodedSignedCsr}{Environment.NewLine}";
|
|
StringCsrHashed += $"{Environment.NewLine}";
|
|
|
|
StringCsrHashed += $"Verify:{CertificateContainer.CsrHashed.Verify}{Environment.NewLine}";
|
|
}
|
|
}
|
|
|
|
public void OnClickGenerateCertificate()
|
|
{
|
|
CertificateContainer.RequestCertificate.csrsignature = CertificateContainer.CsrHashed.EncodedSignedCsr;
|
|
if (TbscsrContainer.VpkiType == VpkiType.prov_cert || TbscsrContainer.VpkiType == VpkiType.vehicle_cert)
|
|
{
|
|
CertificateContainer.RequestCertificate.csrsignature = CertificateContainer.CsrHashed.EncodedSignedCsr;
|
|
}
|
|
|
|
CertificateContainer.StrRequest = $"{CertificateContainer.RequestCertificate.ToJson()}";
|
|
}
|
|
|
|
public async Task OnClickSendCertificate()
|
|
{
|
|
DialogService.OpenIndicator();
|
|
|
|
try
|
|
{
|
|
var request = JsonConvert.DeserializeObject<Request_Certificate>(CertificateContainer.StrRequest);
|
|
if (request != null)
|
|
{
|
|
string url = $"api/v1/certificate";
|
|
if (TbscsrContainer.VpkiType == VpkiType.prov_cert)
|
|
{
|
|
url = $"api/v2/prov/certificate";
|
|
}
|
|
else if (TbscsrContainer.VpkiType == VpkiType.vehicle_cert)
|
|
{
|
|
url = $"api/v2/vehicle/certificate";
|
|
}
|
|
|
|
var http = new Http();
|
|
var Response = await http.PostJsonAsync<Request_Certificate, Response_Certificate>($"https://{ServerAddress}/{url}", request);
|
|
if (Response != null)
|
|
{
|
|
CertificateContainer.ResponseCertificate = Response;
|
|
CertificateContainer.StrResponse = $"{Response.ToJson()}";
|
|
}
|
|
}
|
|
else
|
|
{
|
|
TbscsrContainer.StrResponse = "Request Context Error";
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log4net.WriteLine(ex);
|
|
TbscsrContainer.StrResponse = $"Request Context Error{Environment.NewLine}{ex.Message}";
|
|
}
|
|
|
|
DialogService.CloseIndicator();
|
|
}
|
|
}
|