public int Run() { var name = ConsoleUtils.Prompt("Key name"); var code = ConsoleUtils.Prompt("Secret code"); try { _secretStorage.LoadSecret(name, code); } catch (Exception ex) { Console.Error.WriteLine($"Could not load key: {ex.Message}"); Console.Error.WriteLine("Please check that the name and secret code are valid."); return(1); } var handler = _keyHandlerFactory.GetHandler(name); var(accessToken, accessTokenConfig) = handler.CreateAccessToken(code, name); var encodedAccessToken = SaveAccessToken(accessToken, accessTokenConfig); Console.WriteLine(); Console.WriteLine("Created new access token:"); Console.WriteLine(encodedAccessToken); return(0); }
public async Task <IActionResult> SignUsingAuthenticode(SignRequest request) { var(token, tokenConfig, tokenError) = _utils.TryGetAccessToken(request); if (tokenError != null) { return(tokenError); } var secretKey = _secretStorage.LoadSecret(token.KeyName, token.Code); var(artifact, artifactError, fileExtension) = await _utils.GetFileFromPayloadAsync(token, tokenConfig, request); if (artifactError != null) { return(artifactError); } var signed = _signer.Sign( artifact, Path.GetFileNameWithoutExtension(token.KeyName), secretKey, token.KeyFingerprint ); return(File(signed, "application/pgp-signature")); }
private int Run(string[] args) { var app = new CommandLineApplication(); app.Name = "SecureSignTools"; app.HelpOption("-?|-h|--help"); app.OnExecute(() => app.ShowHelp()); app.Command("addkey", command => { command.Description = "Add a new key"; var pathArg = command.Argument("path", "Path to the key file to add"); command.OnExecute(() => { var inputPath = pathArg.Value; if (string.IsNullOrWhiteSpace(inputPath)) { Console.WriteLine("Please include the file name to add"); return(1); } // Ensure input file exists if (!File.Exists(inputPath)) { throw new Exception("File does not exist: " + inputPath); } // Ensure output file does not exist var fileName = Path.GetFileName(inputPath); var outputPath = _secretStorage.GetPathForSecret(fileName); if (File.Exists(outputPath)) { throw new Exception(outputPath + " already exists! I'm not going to overwrite it."); } var password = ConsoleUtils.PasswordPrompt("Password"); var cert = new X509Certificate2(File.ReadAllBytes(inputPath), password, X509KeyStorageFlags.Exportable); var code = _passwordGenerator.Generate(); _secretStorage.SaveSecret(fileName, cert, code); Console.WriteLine(); Console.WriteLine($"Saved {fileName} ({cert.FriendlyName})"); Console.WriteLine($"Subject: {cert.SubjectName.Format(false)}"); Console.WriteLine($"Issuer: {cert.IssuerName.Format(false)}"); Console.WriteLine($"Valid from {cert.NotBefore} until {cert.NotAfter}"); Console.WriteLine(); Console.WriteLine($"Secret Code: {code}"); Console.WriteLine(); Console.WriteLine("This secret code is required whenever you create an access token that uses this key."); Console.WriteLine("Store this secret code in a SECURE PLACE! The code is not stored anywhere, "); Console.WriteLine("so if you lose it, you will need to re-install the key."); return(0); }); }); app.Command("addtoken", command => { command.Description = "Add a new access token"; command.OnExecute(() => { var name = ConsoleUtils.Prompt("Key name"); var code = ConsoleUtils.Prompt("Secret code"); try { _secretStorage.LoadSecret(name, code); } catch (Exception ex) { Console.Error.WriteLine($"Could not load key: {ex.Message}"); Console.Error.WriteLine("Please check that the name and secret code are valid."); return(1); } // If we got here, the key is valid var comment = ConsoleUtils.Prompt("Comment (optional)"); Console.WriteLine(); Console.WriteLine("Signing settings:"); var desc = ConsoleUtils.Prompt("Description"); var url = ConsoleUtils.Prompt("Product/Application URL"); var accessToken = new AccessToken { Id = Guid.NewGuid().ToShortGuid(), Code = code, IssuedAt = DateTime.Now, KeyName = name, }; var accessTokenConfig = new AccessTokenConfig { Comment = comment, IssuedAt = accessToken.IssuedAt, Valid = true, SignDescription = desc, SignUrl = url, }; // If this is the first time an access token is being added, we need to create the config file if (!File.Exists(_pathConfig.AccessTokenConfig)) { File.WriteAllText(_pathConfig.AccessTokenConfig, JsonConvert.SerializeObject(new { AccessTokens = new Dictionary <string, AccessToken>() })); } // Save access token config to config file dynamic configFile = JObject.Parse(File.ReadAllText(_pathConfig.AccessTokenConfig)); configFile.AccessTokens[accessToken.Id] = JToken.FromObject(accessTokenConfig); File.WriteAllText(_pathConfig.AccessTokenConfig, JsonConvert.SerializeObject(configFile, Formatting.Indented)); var encodedAccessToken = _accessTokenSerializer.Serialize(accessToken); Console.WriteLine(); Console.WriteLine("Created new access token:"); Console.WriteLine(encodedAccessToken); return(0); }); }); try { return(app.Execute(args)); } catch (Exception ex) { Console.Error.WriteLine("ERROR: " + ex.Message); #if DEBUG throw; #else return(1); #endif } }