Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider implementing magic-based private key file type detection, or a CLI parameter to specify the type #264

Closed
AlexTMjugador opened this issue Dec 4, 2024 · 2 comments · Fixed by #267

Comments

@AlexTMjugador
Copy link
Contributor

Currently, the jsign CLI is hardcoded to infer the format of a private key file from its extension:

try {
if (file.getName().endsWith(".pvk")) {
return PVK.parse(file, password);
} else if (file.getName().endsWith(".pem")) {
return readPrivateKeyPEM(file, password != null ? password.toCharArray() : null);
}
} catch (Exception e) {
throw new KeyException("Failed to load the private key from " + file, e);
}

This logic is fine in the vast majority of cases, but breaks down when private key files do not have a .pem or .pvk extension, and there is no switch to manually specify the private key file type in the CLI. PEM files in particular need not have a file extension altogether; this is common practice with SSH PEM keys, for example. And, for security reasons, strict server-side repository push hooks may deny pushes for files with such extensions in order to prevent leaking secrets by accident, forcing e.g. test keys to be marked as such by having different extensions.

It'd be great for my use cases if jsign either tried parsing the key with the supported formats in sequence until one succeeds, some magic-based format detection was made, or a CLI switch like --keytype was added to specify this.

@ebourg
Copy link
Owner

ebourg commented Dec 4, 2024

Thank you for the suggestion, detecting the type of the key is a good idea. I prefer avoiding extra command line parameters if the detection can be automated though. We could either check the first bytes or simply iterate over the key types supported until one works. Do you want to provide a PR implementing this?

@AlexTMjugador
Copy link
Contributor Author

Sure, I could give the idea of iterating over the types until one works a try in a PR. Thank you for your prompt reply!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants