/// <summary> /// Gets the handler for the specified file. If no handler supports this file, throws an exception. /// </summary> /// <param name="filename">Name of the key file</param> /// <returns>The handler for this key file</returns> public IKeyHandler GetHandler(string filename) { try { var keytype = KeyTypeUtils.FromFilename(filename); return(_handlers.First(x => x.KeyType == keytype)); } catch (Exception ex) { throw new Exception($"Unrecognised file extension. Please use .pfx for Authenticode or .gpg for GPG. {ex.Message}"); } }
public IActionResult Index(CertificateStatusRequest request) { AccessToken token; try { token = _accessTokenSerializer.Deserialize(request.AccessToken); } catch (Exception ex) { _logger.LogInformation(ex, "Access token could not be decrypted"); return(Unauthorized()); } switch (KeyTypeUtils.FromFilename(token.KeyName)) { case KeyType.Authenticode: var cert = _secretStorage.LoadAuthenticodeCertificate(token.KeyName, token.Code); return(Ok(new CertificateStatusResponse { CreationDate = cert.NotBefore, ExpiryDate = cert.NotAfter, Issuer = cert.IssuerName.Format(false), Name = cert.FriendlyName, SerialNumber = cert.SerialNumber, Subject = cert.SubjectName.Format(false), Thumbprint = cert.Thumbprint, })); case KeyType.Gpg: var key = _ctx.KeyStore.GetKey(token.KeyFingerprint, secretOnly: false); var subkey = key.Subkeys.First(x => x.KeyId == token.KeyFingerprint); return(Ok(new CertificateStatusResponse { CreationDate = subkey.Timestamp, ExpiryDate = subkey.Expires, Issuer = key.IssuerName, Name = token.KeyName, Subject = key.Uid.Uid, Thumbprint = subkey.KeyId, })); default: return(NotFound("Unknown key type")); } }