Skip to content

Cache the private keys for less overhead when signing messages #93

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions CorePush/Apple/ApnSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,28 @@ private string GetJwtToken()

private string CreateJwtToken()
{
var header = serializer.Serialize(new { alg = "ES256", kid = CryptoHelper.CleanP8Key(settings.P8PrivateKeyId) });
var header = serializer.Serialize(new { alg = "ES256", kid = settings.P8PrivateKeyIdClean });
var payload = serializer.Serialize(new { iss = settings.TeamId, iat = CryptoHelper.GetEpochTimestamp() });
var headerBase64 = Base64UrlEncode(header);
var payloadBase64 = Base64UrlEncode(payload);
var unsignedJwtData = $"{headerBase64}.{payloadBase64}";
var unsignedJwtBytes = Encoding.UTF8.GetBytes(unsignedJwtData);

var privateKeyBytes = Convert.FromBase64String(CryptoHelper.CleanP8Key(settings.P8PrivateKey));
var keyParams = (ECPrivateKeyParameters) PrivateKeyFactory.CreateKey(privateKeyBytes);

ECPrivateKeyParameters keyParams = settings.CachedP8PrivateKeyParams as ECPrivateKeyParameters;
if (keyParams == null)
{
lock(settings)
{
keyParams = settings.CachedP8PrivateKeyParams as ECPrivateKeyParameters;
if (keyParams == null)
{
var privateKeyBytes = Convert.FromBase64String(CryptoHelper.CleanP8Key(settings.P8PrivateKey));
keyParams = (ECPrivateKeyParameters)PrivateKeyFactory.CreateKey(privateKeyBytes);
settings.CachedP8PrivateKeyParams = keyParams;
}
}
}

var q = keyParams.Parameters.G.Multiply(keyParams.D).Normalize();

using var dsa = ECDsa.Create(new ECParameters
Expand Down
47 changes: 43 additions & 4 deletions CorePush/Apple/ApnSettings.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,68 @@
using CorePush.Utils;

namespace CorePush.Apple;

#nullable enable

public class ApnSettings
{
private string? _P8PrivateKey = null;
private string? _P8PrivateKeyId = null;
private string? _P8PrivateKeyIdClean = null;

/// <summary>
/// p8 certificate string
/// </summary>
public string P8PrivateKey { get; set; }
public string? P8PrivateKey
{
get
{
return _P8PrivateKey;
}
set
{
lock (this)
{
_P8PrivateKey = value;
CachedP8PrivateKeyParams = null;
}
}
}

/// <summary>
/// 10 digit p8 certificate id. Usually a part of a downloadable certificate filename
/// </summary>
public string P8PrivateKeyId { get; set; }
public string? P8PrivateKeyId
{
get
{
return _P8PrivateKeyId;
}
set
{
lock (this)
{
_P8PrivateKeyId = value;
_P8PrivateKeyIdClean = CryptoHelper.CleanP8Key(value);
}
}
}

/// <summary>
/// Apple 10 digit team id
/// </summary>
public string TeamId { get; set; }
public string? TeamId { get; set; }

/// <summary>
/// App slug / bundle name
/// </summary>
public string AppBundleIdentifier { get; set; }
public string? AppBundleIdentifier { get; set; }

/// <summary>
/// Development or Production server
/// </summary>
public ApnServerType ServerType { get; set; }

internal string? P8PrivateKeyIdClean { get { return _P8PrivateKeyIdClean; } }
internal object? CachedP8PrivateKeyParams;
}
15 changes: 14 additions & 1 deletion CorePush/Firebase/FirebaseSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,20 @@ private string GetMasterToken()
var unsignedJwtData = $"{headerBase64}.{payloadBase64}";
var unsignedJwtBytes = Encoding.UTF8.GetBytes(unsignedJwtData);

var privateKey = ParsePkcs8PrivateKeyPem(settings.PrivateKey);
var privateKey = settings._CachedPivateKey as AsymmetricKeyParameter;
if (privateKey == null)
{
lock(settings)
{
privateKey = settings._CachedPivateKey as AsymmetricKeyParameter;
if (privateKey == null)
{
privateKey = ParsePkcs8PrivateKeyPem(settings.PrivateKey);
settings._CachedPivateKey = privateKey;
}
}
}

var signer = new RsaDigestSigner(new Org.BouncyCastle.Crypto.Digests.Sha256Digest());
signer.Init(true, privateKey);
signer.BlockUpdate(unsignedJwtBytes, 0, unsignedJwtBytes.Length);
Expand Down
7 changes: 6 additions & 1 deletion CorePush/Firebase/FirebaseSettings.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
using System.Text.Json.Serialization;

#nullable enable

namespace CorePush.Firebase;

public record FirebaseSettings(
[property: JsonPropertyName("project_id")] string ProjectId,
[property: JsonPropertyName("private_key")] string PrivateKey,
[property: JsonPropertyName("client_email")] string ClientEmail,
[property: JsonPropertyName("token_uri")] string TokenUri
);
)
{
internal object? _CachedPivateKey;
}