Skip to content

Commit c92c2d5

Browse files
committed
Read long password files properly
Long passphrase files may require more than a single call to BIO_read() to gather the whole string. Signed-off-by: Stephen Gallagher <[email protected]>
1 parent 2017d90 commit c92c2d5

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

src/io_utils.c

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,8 @@ sscg_read_pw_file (TALLOC_CTX *mem_ctx, char *path)
204204
{
205205
int i;
206206
BIO *pwdbio = NULL;
207-
char tpass[MAX_PW_LEN];
207+
char tpass[MAX_PW_LEN + 1];
208+
int offset = 0;
208209
char *tmp = NULL;
209210
char *password = NULL;
210211

@@ -215,11 +216,19 @@ sscg_read_pw_file (TALLOC_CTX *mem_ctx, char *path)
215216
return NULL;
216217
}
217218

218-
i = BIO_gets (pwdbio, tpass, MAX_PW_LEN);
219+
/* Read up to one more character than the MAX_PW_LEN */
220+
for (offset = 0;
221+
(i = BIO_read (pwdbio, tpass + offset, MAX_PW_LEN + 1 - offset)) > 0 &&
222+
offset < (MAX_PW_LEN + 1);
223+
offset += i)
224+
;
225+
226+
tpass[MAX_PW_LEN] = '\0';
227+
219228
BIO_free_all (pwdbio);
220229
pwdbio = NULL;
221230

222-
if (i <= 0)
231+
if (i < 0)
223232
{
224233
fprintf (stderr, "Error reading password from BIO\n");
225234
return NULL;
@@ -231,7 +240,7 @@ sscg_read_pw_file (TALLOC_CTX *mem_ctx, char *path)
231240

232241
password = talloc_strdup (mem_ctx, tpass);
233242

234-
memset (tpass, 0, MAX_PW_LEN);
243+
memset (tpass, 0, MAX_PW_LEN + 1);
235244

236245
return password;
237246
}
@@ -351,7 +360,8 @@ sscg_io_utils_add_output_key (struct sscg_stream **streams,
351360
}
352361
}
353362
ret = validate_passphrase (stream);
354-
if (ret != EOK) goto done;
363+
if (ret != EOK)
364+
goto done;
355365

356366
ret = EOK;
357367

0 commit comments

Comments
 (0)