Skip to content
This repository has been archived by the owner on Apr 12, 2023. It is now read-only.

Commit

Permalink
Merge pull request #2 from Takym/refactoring/optimization-dsp
Browse files Browse the repository at this point in the history
`DiagnosisSubmissionParameterExtensions` を `DeviceCheckService` に結合
  • Loading branch information
Takym authored May 5, 2021
2 parents e65bd26 + 5d0d031 commit a8a02f5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@
</PackageReference>
</ItemGroup>
<ItemGroup>
<Compile Include="DiagnosisSubmissionParameterExtensions.cs" />
<Compile Include="MainActivity.cs" />
<Compile Include="MainApplication.cs" />
<Compile Include="Resources\Resource.Designer.cs" />
Expand Down

This file was deleted.

46 changes: 32 additions & 14 deletions Covid19Radar/Covid19Radar.Android/Services/DeviceCheckService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,25 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Covid19Radar.Services;
using Android.Gms.SafetyNet;
using Covid19Radar.Droid.Services;
using Xamarin.Forms;
using Covid19Radar.Model;
using Covid19Radar.Common;
using Android.Gms.SafetyNet;
using Covid19Radar.Services;
using Xamarin.Forms;

[assembly: Dependency(typeof(DeviceCheckService))]
namespace Covid19Radar.Droid.Services
{
public class DeviceCheckService : IDeviceVerifier
{

public Task<string> VerifyAsync(Model.DiagnosisSubmissionParameter submission)
public Task<string> VerifyAsync(DiagnosisSubmissionParameter submission)
{
var nonce = submission.GetNonce();
var nonce = GetNonce(submission);
return GetSafetyNetAttestationAsync(nonce);
}

Expand All @@ -42,5 +34,31 @@ async Task<string> GetSafetyNetAttestationAsync(byte[] nonce)
using var response = await client.AttestAsync(nonce, AppSettings.Instance.AndroidSafetyNetApiKey);
return response.JwsResult;
}

public static byte[] GetNonce(DiagnosisSubmissionParameter submission)
{
var cleartext = GetNonceClearText(submission);
var nonce = GetSha256(cleartext);
return nonce;

static string GetNonceClearText(DiagnosisSubmissionParameter submission) =>
string.Join("|", submission.AppPackageName, GetKeyString(submission.Keys), GetRegionString(submission.Regions), submission.VerificationPayload);

static string GetKeyString(IEnumerable<DiagnosisSubmissionParameter.Key> keys) =>
string.Join(",", keys.OrderBy(k => k.KeyData).Select(k => GetKeyStringCore(k)));

static string GetKeyStringCore(DiagnosisSubmissionParameter.Key k) =>
string.Join(".", k.KeyData, k.RollingStartNumber, k.RollingPeriod);

static string GetRegionString(IEnumerable<string> regions) =>
string.Join(",", regions.Select(r => r.ToUpperInvariant()).OrderBy(r => r));

static byte[] GetSha256(string text)
{
using var sha = SHA256.Create();
var textBytes = Encoding.UTF8.GetBytes(text);
return sha.ComputeHash(textBytes);
}
}
}
}

0 comments on commit a8a02f5

Please sign in to comment.