-
-
Notifications
You must be signed in to change notification settings - Fork 32
Cannot decipher if passphrase was generated with openssl #22
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
Comments
# go run main.go; echo "$?"
failed DecryptBinaryBytes errored invalid padding
0
# git diff
diff --git a/main.go b/main.go
index 02408e1..70f6193 100644
--- a/main.go
+++ b/main.go
@@ -81,7 +81,7 @@ func credsGenerator() openssl.CredsGenerator {
}
func generateKeyWithOpenSSL(keyPath string) error {
- cmd := exec.Command("openssl", "rand", "-hex", "-out", keyPath, "32")
+ cmd := exec.Command("openssl", "rand", "-out", keyPath, "32")
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("OpenSSL rand errored: %w : %s", err, string(out))
# go run main.go; echo "$?"
0
# openssl version
OpenSSL 1.1.1n 15 Mar 2022 I think that's the output you've expected (no error being logged)? I'd suspect OpenSSL to have some detection for hex-encoded keys while this library tries to use the key literally? Need to have a look further into this, just a first short look… |
Yeah that what I though too. I didn't have the courage yet to read openssl source. But I does not seems to be that simple. You have the same bug with the I add option to the program to ease testing (and edit the first post) and I found interesting stuff.
Edit: And with base64 mode I can decode the key but it still not working :
Edit 2: chunk = (num > buflen) ? buflen : num;
r = RAND_bytes(buf, chunk);
if (r <= 0)
goto end;
if (format != FORMAT_TEXT) {
if (BIO_write(out, buf, chunk) != chunk)
goto end;
} else {
for (i = 0; i < chunk; i++)
if (BIO_printf(out, "%02x", buf[i]) != 2)
goto end;
} nor in the base64 code, hex encoding is even simplier. |
Okay I dig a little in openssl source code and did not fnd anything special. First it start here in the enc command around line 350 if (str == NULL && passarg != NULL) {
if (!app_passwd(passarg, NULL, &pass, NULL)) {
BIO_printf(bio_err, "Error getting password\n");
goto end;
}
str = pass;
} I follow the static char *app_get_pass(const char *arg, int keepbio)
{
static BIO *pwdbio = NULL;
char *tmp, tpass[APP_PASS_LEN];
int i;
/* PASS_SOURCE_SIZE_MAX = max number of chars before ':' in below strings */
if (CHECK_AND_SKIP_PREFIX(arg, "pass:"))
return OPENSSL_strdup(arg);
if (CHECK_AND_SKIP_PREFIX(arg, "env:")) {
tmp = getenv(arg);
if (tmp == NULL) {
BIO_printf(bio_err, "No environment variable %s\n", arg);
return NULL;
}
return OPENSSL_strdup(tmp);
} If we get back the the command to see how the real pass is used we can follow var if (pbkdf2 == 1) {
/*
* derive key and default iv
* concatenated into a temporary buffer
*/
unsigned char tmpkeyiv[EVP_MAX_KEY_LENGTH + EVP_MAX_IV_LENGTH];
int iklen = EVP_CIPHER_get_key_length(cipher);
int ivlen = EVP_CIPHER_get_iv_length(cipher);
/* not needed if HASH_UPDATE() is fixed : */
int islen = (sptr != NULL ? sizeof(salt) : 0);
if (!PKCS5_PBKDF2_HMAC(str, str_len, sptr, islen,
iter, dgst, iklen+ivlen, tmpkeyiv)) {
BIO_printf(bio_err, "PKCS5_PBKDF2_HMAC failed\n");
goto end;
}
/* split and move data back to global buffer */
memcpy(key, tmpkeyiv, iklen);
memcpy(iv, tmpkeyiv+iklen, ivlen); I don't think |
I cannot decipher if passphrase was generated with openssl, I get an invalid padding error
Here a minimal code
If you call it with
-basic
it work greatsMy openssl version :
> openssl version OpenSSL 1.1.1k 25 Mar 2021
Any idea on what can change in the passphrase that can have this kind of behavior ?
Edit : I can make a pr with a new test with this logic if that helps.
Edit 2: update code to add more flag
The text was updated successfully, but these errors were encountered: