public bool IsSupportedAlgorithm(string algorithm, params object[] args) { JwtAlg intendedAlg = algorithm; //currently only support signing, these are the expected args if (args.Length > 2 || args.Length < 1 || !(args[0] is KeySetKey key)) { return(false); } var shouldSign = false; if (args.Length == 2) { if (!(args[1] is bool ss)) { return(false); } shouldSign = ss; } var isSymm = key.KeySet.Metadata.Kind == KeyKind.Symmetric; var isPrivate = key.KeySet.Metadata.Kind == KeyKind.Private; var isPublic = key.KeySet.Metadata.Kind == KeyKind.Public; var isSign = key.KeySet.Metadata.Purpose == KeyPurpose.SignAndVerify; var isVerify = key.KeySet.Metadata.Purpose == KeyPurpose.Verify; if (!isSign && !isVerify) { return(false); //Right now only support signing algorithms } // ReSharper disable once ConditionIsAlwaysTrueOrFalse -- code may have other options in future if (isSymm && (isSign || isVerify)) { return(Jwt.IsValidAlg(intendedAlg, key.KeySet.GetPrimaryKey())); } else if (shouldSign && isPrivate && isSign) { return(Jwt.IsValidAlg(intendedAlg, key.KeySet.GetPrimaryKey())); } else if (!shouldSign && (isSign || isVerify) && (isPrivate || isPublic)) { return(key.KeySet.Metadata.Versions.Select(it => key.KeySet.GetKey(it.VersionNumber)) .Any(it => Jwt.AlgForKey(it) == intendedAlg)); } return(false); }
public KeyzureSigningCredentials(IKeySet keySet) : base(new KeySetKey(keySet), Jwt.AlgForKey(keySet.GetPrimaryKey())?.ToString()) { }
public void CreateSignAndPublicSized(string keyType, string topDir, string nestDir) { KeyType type = keyType; topDir += "-sizes"; var kspath = Util.TestDataPath(WRITE_DATA, topDir, nestDir); var writer = new FileSystemKeySetWriter(kspath, overwrite: true); using (var ks = CreateNewKeySet(type.Kind, KeyPurpose.SignAndVerify)) { var success = ks.Save(writer); Expect(success, Is.True); } int i = 0; foreach (int size in type.KeySizeOptions) { if (Util.IsSizeTooSlow(size)) { break; } i++; using (var ks = new MutableKeySet(kspath)) { var ver = ks.AddKey(KeyStatus.Primary, size, type: type); Expect(ver, Is.EqualTo(i)); var success = ks.Save(writer); Expect(success, Is.True); } using (var ks = new FileSystemKeySet(kspath)) using (var signer = new Signer(ks)) using (var jwtSigner = new JwtSigner(ks)) { var ciphertext = signer.Sign(input); File.WriteAllText(Path.Combine(kspath, $"{size}.out"), ciphertext); var key = ks.GetPrimaryKey(); if (Jwt.AlgForKey(key) != null) { var token = jwtSigner.SignCompact(JObject.Parse(jsonInput)); File.WriteAllText(Path.Combine(kspath, $"{size}.jwt"), token); } } } if (type.Asymmetric) { var kspath2 = Util.TestDataPath(WRITE_DATA, topDir + ".public", nestDir); var writer2 = new FileSystemKeySetWriter(kspath2, overwrite: true); using (var ks = new MutableKeySet(kspath)) { var pubKs = ks.PublicKey(); var success = pubKs.Save(writer2); Expect(success, Is.True); } } }