public void GetCwtKey(CwtPublicKey rpk) { try { CWT cwt = CWT.Decode(rpk.EncodedCwt(), CwtTrustKeySet, CwtTrustKeySet); AsymmetricKeyParameter pub = cwt.Cnf.Key.AsPublicKey(); SubjectPublicKeyInfo spi = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(pub); rpk.SetSubjectPublicKeyInfo(spi); AuthenticationKey = cwt.Cnf.Key; return; } catch { } TlsEvent ev = new TlsEvent(TlsEvent.EventCode.ServerCertificate) { Certificate = rpk }; EventHandler <TlsEvent> handler = TlsEventHandler; if (handler != null) { handler(this, ev); } if (!ev.Processed) { throw new TlsFatalAlert(AlertDescription.certificate_unknown); } AuthenticationKey = ev.KeyValue; }
public override AbstractCertificate ParseServerCertificate(short certificateType, Stream io) { switch (certificateType) { case CertificateType.CwtPublicKey: try { CwtPublicKey cwtPub = CwtPublicKey.Parse(io); CWT cwtServer = CWT.Decode(cwtPub.EncodedCwt(), CwtTrustKeySet, CwtTrustKeySet); AsymmetricKeyParameter pubKey = cwtServer.Cnf.Key.AsPublicKey(); SubjectPublicKeyInfo spi = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(pubKey); cwtPub.SetSubjectPublicKeyInfo(spi); return(cwtPub); } catch { return(null); } default: return(null); } }
public void GetCwtKey(CwtPublicKey rpk) { CWT cwt; try { cwt = CWT.Decode(rpk.EncodedCwt(), CwtTrustRoots, CwtTrustRoots); AuthenticationKey = cwt.Cnf.Key; } catch (Exception e) { TlsEvent ev = new TlsEvent(TlsEvent.EventCode.ClientCertificate) { Certificate = rpk }; EventHandler <TlsEvent> handler = TlsEventHandler; if (handler != null) { handler(this, ev); } if (!ev.Processed) { throw new TlsFatalAlert(AlertDescription.certificate_unknown); } AuthenticationKey = ev.KeyValue; } }
private static void AddTlsCwt(string[] commands) { if (commands.Length != 4) { Console.Write($"Incorrect number of arguments: {commands.Length}"); return; } CBORObject cbor = CBORDiagnostics.Parse(commands[2]); CWT cwt = CWT.Decode(cbor.EncodeToBytes(), CwtRootKeys, CwtRootKeys); cbor = CBORDiagnostics.Parse(commands[3]); _TlsKeys.Add(commands[1], new TlsKeyPair(cwt, new OneKey(cbor))); }
void ProcessFile(FileInfo testCase) { if (testCase.Extension != ".json") { return; } if (testCase.Name[0] == '.') { return; } Debug.Print($"Working on file {testCase}"); Console.WriteLine("Working on file '" + testCase + "'"); string inputText = testCase.OpenText().ReadToEnd(); CBORObject test = CBORObject.FromJSONString(inputText); KeySet decodeKeys = new KeySet(); KeySet signKeys = new KeySet(); CBORObject input = test["input"]; CWT cwt = new CWT(); if (input.ContainsKey("encrypted")) { OneKey key = LoadKey(input["encrypted"]["key"]); cwt.EncryptionKey = key; decodeKeys.AddKey(key); } if (input.ContainsKey("mac0")) { OneKey key = LoadKey(input["mac0"]["key"]); cwt.MacKey = key; decodeKeys.AddKey(key); } if (input.ContainsKey("sign0")) { OneKey key = LoadKey(input["sign0"]["key"]); cwt.SigningKey = key; signKeys.AddKey(key.PublicKey()); } CWT cwt2 = CWT.Decode(FromHex(test["output"]["cbor"].AsString()), decodeKeys, signKeys); CBORObject token = input["token"]; foreach (CBORObject key in token.Keys) { CBORObject value = token[key]; CBORObject key2 = key; if (key.AsString().EndsWith("_hex")) { value = CBORObject.FromObject(FromHex(value.AsString())); key2 = CBORObject.FromObject(key.AsString().Substring(0, key.AsString().Length - 4)); } cwt.SetClaim(key2, value); Assert.True(cwt2.HasClaim(key2), $"Missing Claim {key2}"); Assert.AreEqual(value, cwt.GetClaim(key2)); } byte[] foo = cwt.EncodeToBytes(); cwt2 = CWT.Decode(foo, decodeKeys, signKeys); foreach (CBORObject key in token.Keys) { CBORObject value = token[key]; CBORObject key2 = key; if (key.AsString().EndsWith("_hex")) { value = CBORObject.FromObject(FromHex(value.AsString())); key2 = CBORObject.FromObject(key.AsString().Substring(0, key.AsString().Length - 4)); } Assert.True(cwt2.HasClaim(key2)); Assert.AreEqual(value, cwt.GetClaim(key2)); } }