Skip to content

Commit

Permalink
Merge pull request #172 from mattosaurus/feature/expose-master-key
Browse files Browse the repository at this point in the history
Expose master key
  • Loading branch information
mattosaurus authored Sep 2, 2022
2 parents 9799127 + ecca07e commit bf6f1cc
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 6 deletions.
4 changes: 2 additions & 2 deletions PgpCore.Tests/PgpCore.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.1" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
5 changes: 5 additions & 0 deletions PgpCore/EncryptionKeys.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class EncryptionKeys : IEncryptionKeys
public PgpPrivateKey SigningPrivateKey => _signingPrivateKey.Value;
public PgpSecretKey SigningSecretKey => _signingSecretKey.Value;
public IEnumerable<PgpPublicKey> PublicKeys => EncryptKeys;
public PgpPublicKey MasterKey => _masterKey.Value;
public PgpPublicKey PublicKey => EncryptKeys.FirstOrDefault();
public PgpPrivateKey PrivateKey => SigningPrivateKey;
public PgpSecretKey SecretKey => SigningSecretKey;
Expand All @@ -28,6 +29,7 @@ public class EncryptionKeys : IEncryptionKeys

private Lazy<IEnumerable<PgpPublicKey>> _encryptKeys;
private Lazy<IEnumerable<PgpPublicKey>> _verificationKeys;
private Lazy<PgpPublicKey> _masterKey;
private Lazy<PgpPrivateKey> _signingPrivateKey;
private Lazy<PgpSecretKey> _signingSecretKey;
private Lazy<PgpSecretKeyRingBundle> _secretKeys;
Expand Down Expand Up @@ -384,12 +386,15 @@ private void
{
if (publicKeyRings == null)
{
_masterKey = new Lazy<PgpPublicKey>(() => null);
_encryptKeys = new Lazy<IEnumerable<PgpPublicKey>>(() => null);
_verificationKeys = new Lazy<IEnumerable<PgpPublicKey>>(() => null);
}
else
{
// Need to consume the stream into a list before it is closed (can happen because of lazy instantiation).
_masterKey = new Lazy<PgpPublicKey>(() =>
Utilities.FindMasterKey(publicKeyRings.First()));
_encryptKeys = new Lazy<IEnumerable<PgpPublicKey>>(() =>
publicKeyRings.Select(Utilities.FindBestEncryptionKey).ToArray());
_verificationKeys = new Lazy<IEnumerable<PgpPublicKey>>(() =>
Expand Down
1 change: 1 addition & 0 deletions PgpCore/IEncryptionKeys.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public interface IEncryptionKeys
IEnumerable<PgpPublicKey> VerificationKeys { get; }
PgpPrivateKey SigningPrivateKey { get; }
PgpSecretKey SigningSecretKey { get; }
PgpPublicKey MasterKey { get; }
PgpPublicKey PublicKey { get; }
IEnumerable<PgpPublicKey> PublicKeys { get; }
PgpPrivateKey PrivateKey { get; }
Expand Down
8 changes: 4 additions & 4 deletions PgpCore/PgpCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
<PackageProjectUrl>https://github.com/mattosaurus/PgpCore</PackageProjectUrl>
<RepositoryUrl>https://github.com/mattosaurus/PgpCore</RepositoryUrl>
<PackageTags>PGP .NET Core</PackageTags>
<Version>5.7.0.0</Version>
<Version>5.8.0.0</Version>
<AssemblyVersion>5.0.0.0</AssemblyVersion>
<FileVersion>5.7.0</FileVersion>
<PackageReleaseNotes>v5.7.0 - Fix to allow encryption by non-master keys</PackageReleaseNotes>
<FileVersion>5.8.0</FileVersion>
<PackageReleaseNotes>v5.8.0 - Expose master key</PackageReleaseNotes>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<IncludeSymbols>true</IncludeSymbols>
Expand All @@ -36,7 +36,7 @@


<ItemGroup>
<PackageReference Include="Portable.BouncyCastle" Version="1.8.9" />
<PackageReference Include="Portable.BouncyCastle" Version="1.9.0" />
</ItemGroup>

</Project>
13 changes: 13 additions & 0 deletions PgpCore/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,19 @@ public static PgpSecretKey FindBestSigningKey(PgpSecretKeyRingBundle secretKeyRi
return secretKeys.First();
}

/// <summary>
/// Finds and returns the master key
/// </summary>
/// <param name="publicKeys"></param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
public static PgpPublicKey FindMasterKey(PgpPublicKeyRing publicKeys)
{
PgpPublicKey[] keys = publicKeys.GetPublicKeys().Cast<PgpPublicKey>().ToArray();

return keys.Single(x => x.IsMasterKey);
}

/// <summary>
/// Checks if the key with the given id is present in the collection of public keys, and if it is, return it.
/// </summary>
Expand Down

0 comments on commit bf6f1cc

Please sign in to comment.