1 /* $OpenBSD: authfile.c,v 1.87 2010/11/29 18:57:04 markus Exp $ */
3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
6 * This file contains functions for reading and writing identity files, and
7 * for reading the passphrase from the user.
9 * As far as I am concerned, the code I have written for this software
10 * can be used freely for any purpose. Any derived versions of this
11 * software must be clearly marked as such, and if the derived work is
12 * incompatible with the protocol description in the RFC file, it must be
13 * called by a name other than "ssh" or "Secure Shell".
16 * Copyright (c) 2000 Markus Friedl. All rights reserved.
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
27 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
28 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
29 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
30 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
31 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
32 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
36 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 #include <sys/types.h>
43 #include <sys/param.h>
46 #include <openssl/err.h>
47 #include <openssl/evp.h>
48 #include <openssl/pem.h>
50 /* compatibility with old or broken OpenSSL versions */
51 #include "openbsd-compat/openssl-compat.h"
71 #include "pathnames.h"
73 /* Version identification string for SSH v1 identity files. */
74 static const char authfile_id_string[] =
75 "SSH PRIVATE KEY FILE FORMAT 1.1\n";
78 * Serialises the authentication (private) key to a blob, encrypting it with
79 * passphrase. The identification of the blob (lowest 64 bits of n) will
80 * precede the key to provide identification of the key without needing a
84 key_private_rsa1_to_blob(Key *key, Buffer *blob, const char *passphrase,
87 Buffer buffer, encrypted;
90 CipherContext ciphercontext;
95 * If the passphrase is empty, use SSH_CIPHER_NONE to ease converting
96 * to another cipher; otherwise use SSH_AUTHFILE_CIPHER.
98 cipher_num = (strcmp(passphrase, "") == 0) ?
99 SSH_CIPHER_NONE : SSH_AUTHFILE_CIPHER;
100 if ((cipher = cipher_by_number(cipher_num)) == NULL)
101 fatal("save_private_key_rsa: bad cipher");
103 /* This buffer is used to built the secret part of the private key. */
104 buffer_init(&buffer);
106 /* Put checkbytes for checking passphrase validity. */
109 buf[1] = (rnd >> 8) & 0xff;
112 buffer_append(&buffer, buf, 4);
115 * Store the private key (n and e will not be stored because they
116 * will be stored in plain text, and storing them also in encrypted
117 * format would just give known plaintext).
119 buffer_put_bignum(&buffer, key->rsa->d);
120 buffer_put_bignum(&buffer, key->rsa->iqmp);
121 buffer_put_bignum(&buffer, key->rsa->q); /* reverse from SSL p */
122 buffer_put_bignum(&buffer, key->rsa->p); /* reverse from SSL q */
124 /* Pad the part to be encrypted until its size is a multiple of 8. */
125 while (buffer_len(&buffer) % 8 != 0)
126 buffer_put_char(&buffer, 0);
128 /* This buffer will be used to contain the data in the file. */
129 buffer_init(&encrypted);
131 /* First store keyfile id string. */
132 for (i = 0; authfile_id_string[i]; i++)
133 buffer_put_char(&encrypted, authfile_id_string[i]);
134 buffer_put_char(&encrypted, 0);
136 /* Store cipher type. */
137 buffer_put_char(&encrypted, cipher_num);
138 buffer_put_int(&encrypted, 0); /* For future extension */
140 /* Store public key. This will be in plain text. */
141 buffer_put_int(&encrypted, BN_num_bits(key->rsa->n));
142 buffer_put_bignum(&encrypted, key->rsa->n);
143 buffer_put_bignum(&encrypted, key->rsa->e);
144 buffer_put_cstring(&encrypted, comment);
146 /* Allocate space for the private part of the key in the buffer. */
147 cp = buffer_append_space(&encrypted, buffer_len(&buffer));
149 cipher_set_key_string(&ciphercontext, cipher, passphrase,
151 cipher_crypt(&ciphercontext, cp,
152 buffer_ptr(&buffer), buffer_len(&buffer));
153 cipher_cleanup(&ciphercontext);
154 memset(&ciphercontext, 0, sizeof(ciphercontext));
156 /* Destroy temporary data. */
157 memset(buf, 0, sizeof(buf));
158 buffer_free(&buffer);
160 buffer_append(blob, buffer_ptr(&encrypted), buffer_len(&encrypted));
161 buffer_free(&encrypted);
166 /* convert SSH v2 key in OpenSSL PEM format */
168 key_private_pem_to_blob(Key *key, Buffer *blob, const char *_passphrase,
172 int blen, len = strlen(_passphrase);
173 u_char *passphrase = (len > 0) ? (u_char *)_passphrase : NULL;
174 #if (OPENSSL_VERSION_NUMBER < 0x00907000L)
175 const EVP_CIPHER *cipher = (len > 0) ? EVP_des_ede3_cbc() : NULL;
177 const EVP_CIPHER *cipher = (len > 0) ? EVP_aes_128_cbc() : NULL;
182 if (len > 0 && len <= 4) {
183 error("passphrase too short: have %d bytes, need > 4", len);
186 if ((bio = BIO_new(BIO_s_mem())) == NULL) {
187 error("%s: BIO_new failed", __func__);
192 success = PEM_write_bio_DSAPrivateKey(bio, key->dsa,
193 cipher, passphrase, len, NULL, NULL);
195 #ifdef OPENSSL_HAS_ECC
197 success = PEM_write_bio_ECPrivateKey(bio, key->ecdsa,
198 cipher, passphrase, len, NULL, NULL);
202 success = PEM_write_bio_RSAPrivateKey(bio, key->rsa,
203 cipher, passphrase, len, NULL, NULL);
207 if ((blen = BIO_get_mem_data(bio, &bptr)) <= 0)
210 buffer_append(blob, bptr, blen);
216 /* Save a key blob to a file */
218 key_save_private_blob(Buffer *keybuf, const char *filename)
222 if ((fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600)) < 0) {
223 error("open %s failed: %s.", filename, strerror(errno));
226 if (atomicio(vwrite, fd, buffer_ptr(keybuf),
227 buffer_len(keybuf)) != buffer_len(keybuf)) {
228 error("write to key file %s failed: %s", filename,
238 /* Serialise "key" to buffer "blob" */
240 key_private_to_blob(Key *key, Buffer *blob, const char *passphrase,
245 return key_private_rsa1_to_blob(key, blob, passphrase, comment);
249 return key_private_pem_to_blob(key, blob, passphrase, comment);
251 error("%s: cannot save key type %d", __func__, key->type);
257 key_save_private(Key *key, const char *filename, const char *passphrase,
263 buffer_init(&keyblob);
264 if (!key_private_to_blob(key, &keyblob, passphrase, comment))
266 if (!key_save_private_blob(&keyblob, filename))
270 buffer_free(&keyblob);
275 * Parse the public, unencrypted portion of a RSA1 key.
278 key_parse_public_rsa1(Buffer *blob, char **commentp)
282 /* Check that it is at least big enough to contain the ID string. */
283 if (buffer_len(blob) < sizeof(authfile_id_string)) {
284 debug3("Truncated RSA1 identifier");
289 * Make sure it begins with the id string. Consume the id string
292 if (memcmp(buffer_ptr(blob), authfile_id_string,
293 sizeof(authfile_id_string)) != 0) {
294 debug3("Incorrect RSA1 identifier");
297 buffer_consume(blob, sizeof(authfile_id_string));
299 /* Skip cipher type and reserved data. */
300 (void) buffer_get_char(blob); /* cipher type */
301 (void) buffer_get_int(blob); /* reserved */
303 /* Read the public key from the buffer. */
304 (void) buffer_get_int(blob);
305 pub = key_new(KEY_RSA1);
306 buffer_get_bignum(blob, pub->rsa->n);
307 buffer_get_bignum(blob, pub->rsa->e);
309 *commentp = buffer_get_string(blob, NULL);
310 /* The encrypted private part is not parsed by this function. */
316 /* Load the contents of a key file into a buffer */
318 key_load_file(int fd, const char *filename, Buffer *blob)
320 size_t len, readcount;
324 if (fstat(fd, &st) < 0) {
325 error("%s: fstat of key file %.200s%sfailed: %.100s", __func__,
326 filename == NULL ? "" : filename,
327 filename == NULL ? "" : " ",
332 if (st.st_size > 1*1024*1024) {
333 error("%s: key file %.200s%stoo large", __func__,
334 filename == NULL ? "" : filename,
335 filename == NULL ? "" : " ");
339 len = (size_t)st.st_size; /* truncated */
340 if (0 == len && S_ISFIFO(st.st_mode))
341 len = 8192; /* we will try reading up to 8KiB from a FIFO */
344 cp = buffer_append_space(blob, len);
346 readcount = atomicio(read, fd, cp, len);
347 if (readcount != len && !(readcount > 0 && S_ISFIFO(st.st_mode))) {
348 debug("%s: read from key file %.200s%sfailed: %.100s", __func__,
349 filename == NULL ? "" : filename,
350 filename == NULL ? "" : " ",
360 * Loads the public part of the ssh v1 key file. Returns NULL if an error was
361 * encountered (the file does not exist or is not readable), and the key
365 key_load_public_rsa1(int fd, const char *filename, char **commentp)
370 buffer_init(&buffer);
371 if (!key_load_file(fd, filename, &buffer)) {
372 buffer_free(&buffer);
376 pub = key_parse_public_rsa1(&buffer, commentp);
378 debug3("Could not load \"%s\" as a RSA1 public key", filename);
379 buffer_free(&buffer);
383 /* load public key from private-key file, works only for SSH v1 */
385 key_load_public_type(int type, const char *filename, char **commentp)
390 if (type == KEY_RSA1) {
391 fd = open(filename, O_RDONLY);
394 pub = key_load_public_rsa1(fd, filename, commentp);
402 key_parse_private_rsa1(Buffer *blob, const char *passphrase, char **commentp)
404 int check1, check2, cipher_type;
407 CipherContext ciphercontext;
411 /* Check that it is at least big enough to contain the ID string. */
412 if (buffer_len(blob) < sizeof(authfile_id_string)) {
413 debug3("Truncated RSA1 identifier");
418 * Make sure it begins with the id string. Consume the id string
421 if (memcmp(buffer_ptr(blob), authfile_id_string,
422 sizeof(authfile_id_string)) != 0) {
423 debug3("Incorrect RSA1 identifier");
426 buffer_consume(blob, sizeof(authfile_id_string));
428 /* Read cipher type. */
429 cipher_type = buffer_get_char(blob);
430 (void) buffer_get_int(blob); /* Reserved data. */
432 /* Read the public key from the buffer. */
433 (void) buffer_get_int(blob);
434 prv = key_new_private(KEY_RSA1);
436 buffer_get_bignum(blob, prv->rsa->n);
437 buffer_get_bignum(blob, prv->rsa->e);
439 *commentp = buffer_get_string(blob, NULL);
441 (void)buffer_get_string_ptr(blob, NULL);
443 /* Check that it is a supported cipher. */
444 cipher = cipher_by_number(cipher_type);
445 if (cipher == NULL) {
446 debug("Unsupported RSA1 cipher %d", cipher_type);
449 /* Initialize space for decrypted data. */
450 buffer_init(&decrypted);
451 cp = buffer_append_space(&decrypted, buffer_len(blob));
453 /* Rest of the buffer is encrypted. Decrypt it using the passphrase. */
454 cipher_set_key_string(&ciphercontext, cipher, passphrase,
456 cipher_crypt(&ciphercontext, cp,
457 buffer_ptr(blob), buffer_len(blob));
458 cipher_cleanup(&ciphercontext);
459 memset(&ciphercontext, 0, sizeof(ciphercontext));
462 check1 = buffer_get_char(&decrypted);
463 check2 = buffer_get_char(&decrypted);
464 if (check1 != buffer_get_char(&decrypted) ||
465 check2 != buffer_get_char(&decrypted)) {
466 if (strcmp(passphrase, "") != 0)
467 debug("Bad passphrase supplied for RSA1 key");
468 /* Bad passphrase. */
469 buffer_free(&decrypted);
472 /* Read the rest of the private key. */
473 buffer_get_bignum(&decrypted, prv->rsa->d);
474 buffer_get_bignum(&decrypted, prv->rsa->iqmp); /* u */
475 /* in SSL and SSH v1 p and q are exchanged */
476 buffer_get_bignum(&decrypted, prv->rsa->q); /* p */
477 buffer_get_bignum(&decrypted, prv->rsa->p); /* q */
479 /* calculate p-1 and q-1 */
480 rsa_generate_additional_parameters(prv->rsa);
482 buffer_free(&decrypted);
484 /* enable blinding */
485 if (RSA_blinding_on(prv->rsa, NULL) != 1) {
486 error("%s: RSA_blinding_on failed", __func__);
499 key_parse_private_pem(Buffer *blob, int type, const char *passphrase,
504 char *name = "<no key>";
507 if ((bio = BIO_new_mem_buf(buffer_ptr(blob),
508 buffer_len(blob))) == NULL) {
509 error("%s: BIO_new_mem_buf failed", __func__);
513 pk = PEM_read_bio_PrivateKey(bio, NULL, NULL, (char *)passphrase);
516 debug("%s: PEM_read_PrivateKey failed", __func__);
517 (void)ERR_get_error();
518 } else if (pk->type == EVP_PKEY_RSA &&
519 (type == KEY_UNSPEC||type==KEY_RSA)) {
520 prv = key_new(KEY_UNSPEC);
521 prv->rsa = EVP_PKEY_get1_RSA(pk);
523 name = "rsa w/o comment";
525 RSA_print_fp(stderr, prv->rsa, 8);
527 if (RSA_blinding_on(prv->rsa, NULL) != 1) {
528 error("%s: RSA_blinding_on failed", __func__);
532 } else if (pk->type == EVP_PKEY_DSA &&
533 (type == KEY_UNSPEC||type==KEY_DSA)) {
534 prv = key_new(KEY_UNSPEC);
535 prv->dsa = EVP_PKEY_get1_DSA(pk);
537 name = "dsa w/o comment";
539 DSA_print_fp(stderr, prv->dsa, 8);
541 #ifdef OPENSSL_HAS_ECC
542 } else if (pk->type == EVP_PKEY_EC &&
543 (type == KEY_UNSPEC||type==KEY_ECDSA)) {
544 prv = key_new(KEY_UNSPEC);
545 prv->ecdsa = EVP_PKEY_get1_EC_KEY(pk);
546 prv->type = KEY_ECDSA;
547 if ((prv->ecdsa_nid = key_ecdsa_key_to_nid(prv->ecdsa)) == -1 ||
548 key_curve_nid_to_name(prv->ecdsa_nid) == NULL ||
549 key_ec_validate_public(EC_KEY_get0_group(prv->ecdsa),
550 EC_KEY_get0_public_key(prv->ecdsa)) != 0 ||
551 key_ec_validate_private(prv->ecdsa) != 0) {
552 error("%s: bad ECDSA key", __func__);
556 name = "ecdsa w/o comment";
558 if (prv != NULL && prv->ecdsa != NULL)
559 key_dump_ec_key(prv->ecdsa);
561 #endif /* OPENSSL_HAS_ECC */
563 error("%s: PEM_read_PrivateKey: mismatch or "
564 "unknown EVP_PKEY save_type %d", __func__, pk->save_type);
568 if (prv != NULL && commentp)
569 *commentp = xstrdup(name);
570 debug("read PEM private key done: type %s",
571 prv ? key_type(prv) : "<unknown>");
576 key_load_private_pem(int fd, int type, const char *passphrase,
582 buffer_init(&buffer);
583 if (!key_load_file(fd, NULL, &buffer)) {
584 buffer_free(&buffer);
587 prv = key_parse_private_pem(&buffer, type, passphrase, commentp);
588 buffer_free(&buffer);
593 key_perm_ok(int fd, const char *filename)
597 if (fstat(fd, &st) < 0)
600 * if a key owned by the user is accessed, then we check the
601 * permissions of the file. if the key owned by a different user,
602 * then we don't care.
605 if (check_ntsec(filename))
607 if ((st.st_uid == getuid()) && (st.st_mode & 077) != 0) {
608 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
609 error("@ WARNING: UNPROTECTED PRIVATE KEY FILE! @");
610 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
611 error("Permissions 0%3.3o for '%s' are too open.",
612 (u_int)st.st_mode & 0777, filename);
613 error("It is recommended that your private key files are NOT accessible by others.");
614 error("This private key will be ignored.");
621 key_parse_private_type(Buffer *blob, int type, const char *passphrase,
626 return key_parse_private_rsa1(blob, passphrase, commentp);
631 return key_parse_private_pem(blob, type, passphrase, commentp);
639 key_load_private_type(int type, const char *filename, const char *passphrase,
640 char **commentp, int *perm_ok)
646 fd = open(filename, O_RDONLY);
648 debug("could not open key file '%s': %s", filename,
654 if (!key_perm_ok(fd, filename)) {
657 error("bad permissions: ignore key: %s", filename);
664 buffer_init(&buffer);
665 if (!key_load_file(fd, filename, &buffer)) {
666 buffer_free(&buffer);
671 ret = key_parse_private_type(&buffer, type, passphrase, commentp);
672 buffer_free(&buffer);
677 key_load_private(const char *filename, const char *passphrase,
681 Buffer buffer, pubcopy;
684 fd = open(filename, O_RDONLY);
686 debug("could not open key file '%s': %s", filename,
690 if (!key_perm_ok(fd, filename)) {
691 error("bad permissions: ignore key: %s", filename);
696 buffer_init(&buffer);
697 if (!key_load_file(fd, filename, &buffer)) {
698 buffer_free(&buffer);
704 buffer_init(&pubcopy);
705 buffer_append(&pubcopy, buffer_ptr(&buffer), buffer_len(&buffer));
706 /* it's a SSH v1 key if the public key part is readable */
707 pub = key_parse_public_rsa1(&pubcopy, commentp);
708 buffer_free(&pubcopy);
710 prv = key_parse_private_type(&buffer, KEY_UNSPEC,
712 /* use the filename as a comment for PEM */
714 *commentp = xstrdup(filename);
717 /* key_parse_public_rsa1() has already loaded the comment */
718 prv = key_parse_private_type(&buffer, KEY_RSA1, passphrase,
721 buffer_free(&buffer);
726 key_try_load_public(Key *k, const char *filename, char **commentp)
729 char line[SSH_MAX_PUBKEY_BYTES];
733 f = fopen(filename, "r");
735 while (read_keyfile_line(f, filename, line, sizeof(line),
744 /* Skip leading whitespace. */
745 for (; *cp && (*cp == ' ' || *cp == '\t'); cp++)
748 if (key_read(k, &cp) == 1) {
750 *commentp=xstrdup(filename);
761 /* load public key from ssh v1 private or any pubkey file */
763 key_load_public(const char *filename, char **commentp)
766 char file[MAXPATHLEN];
768 /* try rsa1 private key */
769 pub = key_load_public_type(KEY_RSA1, filename, commentp);
773 /* try rsa1 public key */
774 pub = key_new(KEY_RSA1);
775 if (key_try_load_public(pub, filename, commentp) == 1)
779 /* try ssh2 public key */
780 pub = key_new(KEY_UNSPEC);
781 if (key_try_load_public(pub, filename, commentp) == 1)
783 if ((strlcpy(file, filename, sizeof file) < sizeof(file)) &&
784 (strlcat(file, ".pub", sizeof file) < sizeof(file)) &&
785 (key_try_load_public(pub, file, commentp) == 1))
791 /* Load the certificate associated with the named private key */
793 key_load_cert(const char *filename)
798 pub = key_new(KEY_UNSPEC);
799 xasprintf(&file, "%s-cert.pub", filename);
800 if (key_try_load_public(pub, file, NULL) == 1) {
809 /* Load private key and certificate */
811 key_load_private_cert(int type, const char *filename, const char *passphrase,
822 error("%s: unsupported key type", __func__);
826 if ((key = key_load_private_type(type, filename,
827 passphrase, NULL, perm_ok)) == NULL)
830 if ((pub = key_load_cert(filename)) == NULL) {
835 /* Make sure the private key matches the certificate */
836 if (key_equal_public(key, pub) == 0) {
837 error("%s: certificate does not match private key %s",
839 } else if (key_to_certified(key, key_cert_is_legacy(pub)) != 0) {
840 error("%s: key_to_certified failed", __func__);
842 key_cert_copy(pub, key);
853 * Returns 1 if the specified "key" is listed in the file "filename",
854 * 0 if the key is not listed or -1 on error.
855 * If strict_type is set then the key type must match exactly,
856 * otherwise a comparison that ignores certficiate data is performed.
859 key_in_file(Key *key, const char *filename, int strict_type)
862 char line[SSH_MAX_PUBKEY_BYTES];
867 int (*key_compare)(const Key *, const Key *) = strict_type ?
868 key_equal : key_equal_public;
870 if ((f = fopen(filename, "r")) == NULL) {
871 if (errno == ENOENT) {
872 debug("%s: keyfile \"%s\" missing", __func__, filename);
875 error("%s: could not open keyfile \"%s\": %s", __func__,
876 filename, strerror(errno));
881 while (read_keyfile_line(f, filename, line, sizeof(line),
885 /* Skip leading whitespace. */
886 for (; *cp && (*cp == ' ' || *cp == '\t'); cp++)
889 /* Skip comments and empty lines */
897 pub = key_new(KEY_UNSPEC);
898 if (key_read(pub, &cp) != 1) {
902 if (key_compare(key, pub)) {
913 /* Scan a blacklist of known-vulnerable keys in blacklist_file. */
915 blacklisted_key_in_file(Key *key, const char *blacklist_file, char **fp)
918 char *dgst_hex = NULL;
919 char *dgst_packed = NULL, *p;
924 off_t start, lower, upper;
927 debug("Checking blacklist file %s", blacklist_file);
928 fd = open(blacklist_file, O_RDONLY);
934 dgst_hex = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
935 /* Remove all colons */
936 dgst_packed = xcalloc(1, strlen(dgst_hex) + 1);
937 for (i = 0, p = dgst_packed; dgst_hex[i]; i++)
938 if (dgst_hex[i] != ':')
940 /* Only compare least-significant 80 bits (to keep the blacklist
943 line_len = strlen(dgst_packed + 12);
947 /* Skip leading comments */
953 r = atomicio(read, fd, buf, sizeof(buf));
959 newline = memchr(buf, '\n', sizeof(buf));
962 start += newline + 1 - buf;
963 if (lseek(fd, start, SEEK_SET) < 0)
967 /* Initialise binary search record numbers */
968 if (fstat(fd, &st) < 0)
971 upper = (st.st_size - start) / (line_len + 1);
973 while (lower != upper) {
977 cur = lower + (upper - lower) / 2;
979 /* Read this line and compare to digest; this is
980 * overflow-safe since cur < max(off_t) / (line_len + 1) */
981 if (lseek(fd, start + cur * (line_len + 1), SEEK_SET) < 0)
983 if (atomicio(read, fd, buf, line_len) != line_len)
985 cmp = memcmp(buf, dgst_packed + 12, line_len);
990 } else if (cmp > 0) {
995 debug("Found %s in blacklist", dgst_hex);
1004 if (ret != 1 && dgst_hex) {
1016 * Scan blacklists of known-vulnerable keys. If a vulnerable key is found,
1017 * its fingerprint is returned in *fp, unless fp is NULL.
1020 blacklisted_key(Key *key, char **fp)
1023 char *blacklist_file;
1026 public = key_demote(key);
1027 if (public->type == KEY_RSA1)
1028 public->type = KEY_RSA;
1030 xasprintf(&blacklist_file, "%s.%s-%u",
1031 _PATH_BLACKLIST, key_type(public), key_size(public));
1032 ret = blacklisted_key_in_file(public, blacklist_file, fp);
1033 xfree(blacklist_file);
1039 xasprintf(&blacklist_file, "%s.%s-%u",
1040 _PATH_BLACKLIST_CONFIG, key_type(public), key_size(public));
1041 ret2 = blacklisted_key_in_file(public, blacklist_file, fp);
1042 xfree(blacklist_file);