Skip to content

Commit

Permalink
fix(Pki); subject key identifier is the IPFS key ID
Browse files Browse the repository at this point in the history
  • Loading branch information
richardschneider committed Dec 28, 2018
1 parent 581d7a1 commit d609f3a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
13 changes: 8 additions & 5 deletions src/Cryptography/Pki.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public partial class KeyChain
/// <param name="keyName">
/// The key name.
/// </param>
/// <param name="cancel"></param>
/// <returns></returns>
public async Task<byte[]> CreateCertificateAsync(
string keyName,
Expand All @@ -42,7 +43,9 @@ public async Task<byte[]> CreateCertificateAsync(
/// </param>
/// <param name="cancel"></param>
/// <returns></returns>
async Task<X509Certificate> CreateBCCertificateAsync(string keyName, CancellationToken cancel)
public async Task<X509Certificate> CreateBCCertificateAsync(
string keyName,
CancellationToken cancel = default(CancellationToken))
{
// Get the BC key pair for the named key.
var ekey = await Store.TryGetAsync(keyName, cancel);
Expand Down Expand Up @@ -85,7 +88,7 @@ async Task<X509Certificate> CreateBCCertificateAsync(string keyName, Cancellatio

// Build the certificate.
var dn = new X509Name($"CN={ekey.Id}, OU=keystore, O=ipfs");
var ski = new SubjectKeyIdentifier(SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(kp.Public));
var ski = new SubjectKeyIdentifier(Base58.Decode(ekey.Id));
// Not a certificate authority.
// TODO: perhaps the "self" key is a CA and all other keys issued by it.
var bc = new BasicConstraints(false);
Expand All @@ -97,9 +100,9 @@ async Task<X509Certificate> CreateBCCertificateAsync(string keyName, Cancellatio
certGenerator.SetNotAfter(DateTime.UtcNow.AddYears(10));
certGenerator.SetNotBefore(DateTime.UtcNow);
certGenerator.SetPublicKey(kp.Public);
certGenerator.AddExtension(X509Extensions.SubjectKeyIdentifier.Id, false, ski);
certGenerator.AddExtension(X509Extensions.BasicConstraints.Id, true, bc);
certGenerator.AddExtension(X509Extensions.KeyUsage.Id, false, ku);
certGenerator.AddExtension(X509Extensions.SubjectKeyIdentifier, false, ski);
certGenerator.AddExtension(X509Extensions.BasicConstraints, true, bc);
certGenerator.AddExtension(X509Extensions.KeyUsage, false, ku);

return certGenerator.Generate(signatureFactory);
}
Expand Down
17 changes: 14 additions & 3 deletions test/Cryptography/CertTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.X509.Extension;
using System;
using System.Collections.Generic;
using System.IO;
Expand All @@ -19,7 +21,10 @@ public async Task Create_Rsa()
var key = await ipfs.Key.CreateAsync("alice", "rsa", 512);
try
{
var cert = await keychain.CreateCertificateAsync("alice");
var cert = await keychain.CreateBCCertificateAsync(key.Name);
Assert.AreEqual($"CN={key.Id},OU=keystore,O=ipfs", cert.SubjectDN.ToString());
var ski = new SubjectKeyIdentifierStructure(cert.GetExtensionValue(X509Extensions.SubjectKeyIdentifier));
Assert.AreEqual(key.Id.ToBase58(), ski.GetKeyIdentifier().ToBase58());
}
finally
{
Expand All @@ -35,7 +40,10 @@ public async Task Create_Secp256k1()
var key = await ipfs.Key.CreateAsync("alice", "secp256k1", 0);
try
{
var cert = await keychain.CreateCertificateAsync("alice");
var cert = await keychain.CreateBCCertificateAsync("alice");
Assert.AreEqual($"CN={key.Id},OU=keystore,O=ipfs", cert.SubjectDN.ToString());
var ski = new SubjectKeyIdentifierStructure(cert.GetExtensionValue(X509Extensions.SubjectKeyIdentifier));
Assert.AreEqual(key.Id.ToBase58(), ski.GetKeyIdentifier().ToBase58());
}
finally
{
Expand All @@ -51,7 +59,10 @@ public async Task Create_Ed25519()
var key = await ipfs.Key.CreateAsync("alice", "ed25519", 0);
try
{
var cert = await keychain.CreateCertificateAsync("alice");
var cert = await keychain.CreateBCCertificateAsync("alice");
Assert.AreEqual($"CN={key.Id},OU=keystore,O=ipfs", cert.SubjectDN.ToString());
var ski = new SubjectKeyIdentifierStructure(cert.GetExtensionValue(X509Extensions.SubjectKeyIdentifier));
Assert.AreEqual(key.Id.ToBase58(), ski.GetKeyIdentifier().ToBase58());
}
finally
{
Expand Down

0 comments on commit d609f3a

Please sign in to comment.