1 /* $OpenBSD: key.c,v 1.87 2010/04/16 01:47:26 djm Exp $ */
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
6 * As far as I am concerned, the code I have written for this software
7 * can be used freely for any purpose. Any derived versions of this
8 * software must be clearly marked as such, and if the derived work is
9 * incompatible with the protocol description in the RFC file, it must be
10 * called by a name other than "ssh" or "Secure Shell".
13 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
14 * Copyright (c) 2008 Alexander von Gernler. All rights reserved.
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <sys/param.h>
40 #include <sys/types.h>
42 #include <openssl/evp.h>
43 #include <openbsd-compat/openssl-compat.h>
57 static struct KeyCert
*
62 cert
= xcalloc(1, sizeof(*cert
));
63 buffer_init(&cert
->certblob
);
64 buffer_init(&cert
->critical
);
65 buffer_init(&cert
->extensions
);
67 cert
->principals
= NULL
;
68 cert
->signature_key
= NULL
;
78 k
= xcalloc(1, sizeof(*k
));
86 case KEY_RSA_CERT_V00
:
88 if ((rsa
= RSA_new()) == NULL
)
89 fatal("key_new: RSA_new failed");
90 if ((rsa
->n
= BN_new()) == NULL
)
91 fatal("key_new: BN_new failed");
92 if ((rsa
->e
= BN_new()) == NULL
)
93 fatal("key_new: BN_new failed");
97 case KEY_DSA_CERT_V00
:
99 if ((dsa
= DSA_new()) == NULL
)
100 fatal("key_new: DSA_new failed");
101 if ((dsa
->p
= BN_new()) == NULL
)
102 fatal("key_new: BN_new failed");
103 if ((dsa
->q
= BN_new()) == NULL
)
104 fatal("key_new: BN_new failed");
105 if ((dsa
->g
= BN_new()) == NULL
)
106 fatal("key_new: BN_new failed");
107 if ((dsa
->pub_key
= BN_new()) == NULL
)
108 fatal("key_new: BN_new failed");
114 fatal("key_new: bad key type %d", k
->type
);
119 k
->cert
= cert_new();
125 key_add_private(Key
*k
)
130 case KEY_RSA_CERT_V00
:
132 if ((k
->rsa
->d
= BN_new()) == NULL
)
133 fatal("key_new_private: BN_new failed");
134 if ((k
->rsa
->iqmp
= BN_new()) == NULL
)
135 fatal("key_new_private: BN_new failed");
136 if ((k
->rsa
->q
= BN_new()) == NULL
)
137 fatal("key_new_private: BN_new failed");
138 if ((k
->rsa
->p
= BN_new()) == NULL
)
139 fatal("key_new_private: BN_new failed");
140 if ((k
->rsa
->dmq1
= BN_new()) == NULL
)
141 fatal("key_new_private: BN_new failed");
142 if ((k
->rsa
->dmp1
= BN_new()) == NULL
)
143 fatal("key_new_private: BN_new failed");
146 case KEY_DSA_CERT_V00
:
148 if ((k
->dsa
->priv_key
= BN_new()) == NULL
)
149 fatal("key_new_private: BN_new failed");
159 key_new_private(int type
)
161 Key
*k
= key_new(type
);
168 cert_free(struct KeyCert
*cert
)
172 buffer_free(&cert
->certblob
);
173 buffer_free(&cert
->critical
);
174 buffer_free(&cert
->extensions
);
175 if (cert
->key_id
!= NULL
)
177 for (i
= 0; i
< cert
->nprincipals
; i
++)
178 xfree(cert
->principals
[i
]);
179 if (cert
->principals
!= NULL
)
180 xfree(cert
->principals
);
181 if (cert
->signature_key
!= NULL
)
182 key_free(cert
->signature_key
);
189 fatal("key_free: key is NULL");
193 case KEY_RSA_CERT_V00
:
200 case KEY_DSA_CERT_V00
:
209 fatal("key_free: bad key type %d", k
->type
);
212 if (key_is_cert(k
)) {
222 cert_compare(struct KeyCert
*a
, struct KeyCert
*b
)
224 if (a
== NULL
&& b
== NULL
)
226 if (a
== NULL
|| b
== NULL
)
228 if (buffer_len(&a
->certblob
) != buffer_len(&b
->certblob
))
230 if (memcmp(buffer_ptr(&a
->certblob
), buffer_ptr(&b
->certblob
),
231 buffer_len(&a
->certblob
)) != 0)
237 * Compare public portions of key only, allowing comparisons between
238 * certificates and plain keys too.
241 key_equal_public(const Key
*a
, const Key
*b
)
243 if (a
== NULL
|| b
== NULL
||
244 key_type_plain(a
->type
) != key_type_plain(b
->type
))
249 case KEY_RSA_CERT_V00
:
252 return a
->rsa
!= NULL
&& b
->rsa
!= NULL
&&
253 BN_cmp(a
->rsa
->e
, b
->rsa
->e
) == 0 &&
254 BN_cmp(a
->rsa
->n
, b
->rsa
->n
) == 0;
255 case KEY_DSA_CERT_V00
:
258 return a
->dsa
!= NULL
&& b
->dsa
!= NULL
&&
259 BN_cmp(a
->dsa
->p
, b
->dsa
->p
) == 0 &&
260 BN_cmp(a
->dsa
->q
, b
->dsa
->q
) == 0 &&
261 BN_cmp(a
->dsa
->g
, b
->dsa
->g
) == 0 &&
262 BN_cmp(a
->dsa
->pub_key
, b
->dsa
->pub_key
) == 0;
264 fatal("key_equal: bad key type %d", a
->type
);
270 key_equal(const Key
*a
, const Key
*b
)
272 if (a
== NULL
|| b
== NULL
|| a
->type
!= b
->type
)
274 if (key_is_cert(a
)) {
275 if (!cert_compare(a
->cert
, b
->cert
))
278 return key_equal_public(a
, b
);
282 key_fingerprint_raw(Key
*k
, enum fp_type dgst_type
, u_int
*dgst_raw_length
)
284 const EVP_MD
*md
= NULL
;
287 u_char
*retval
= NULL
;
289 int nlen
, elen
, otype
;
291 *dgst_raw_length
= 0;
301 fatal("key_fingerprint_raw: bad digest type %d",
306 nlen
= BN_num_bytes(k
->rsa
->n
);
307 elen
= BN_num_bytes(k
->rsa
->e
);
310 BN_bn2bin(k
->rsa
->n
, blob
);
311 BN_bn2bin(k
->rsa
->e
, blob
+ nlen
);
315 key_to_blob(k
, &blob
, &len
);
317 case KEY_DSA_CERT_V00
:
318 case KEY_RSA_CERT_V00
:
321 /* We want a fingerprint of the _key_ not of the cert */
323 k
->type
= key_type_plain(k
->type
);
324 key_to_blob(k
, &blob
, &len
);
330 fatal("key_fingerprint_raw: bad key type %d", k
->type
);
334 retval
= xmalloc(EVP_MAX_MD_SIZE
);
335 EVP_DigestInit(&ctx
, md
);
336 EVP_DigestUpdate(&ctx
, blob
, len
);
337 EVP_DigestFinal(&ctx
, retval
, dgst_raw_length
);
338 memset(blob
, 0, len
);
341 fatal("key_fingerprint_raw: blob is null");
347 key_fingerprint_hex(u_char
*dgst_raw
, u_int dgst_raw_len
)
352 retval
= xcalloc(1, dgst_raw_len
* 3 + 1);
353 for (i
= 0; i
< dgst_raw_len
; i
++) {
355 snprintf(hex
, sizeof(hex
), "%02x:", dgst_raw
[i
]);
356 strlcat(retval
, hex
, dgst_raw_len
* 3 + 1);
359 /* Remove the trailing ':' character */
360 retval
[(dgst_raw_len
* 3) - 1] = '\0';
365 key_fingerprint_bubblebabble(u_char
*dgst_raw
, u_int dgst_raw_len
)
367 char vowels
[] = { 'a', 'e', 'i', 'o', 'u', 'y' };
368 char consonants
[] = { 'b', 'c', 'd', 'f', 'g', 'h', 'k', 'l', 'm',
369 'n', 'p', 'r', 's', 't', 'v', 'z', 'x' };
370 u_int i
, j
= 0, rounds
, seed
= 1;
373 rounds
= (dgst_raw_len
/ 2) + 1;
374 retval
= xcalloc((rounds
* 6), sizeof(char));
376 for (i
= 0; i
< rounds
; i
++) {
377 u_int idx0
, idx1
, idx2
, idx3
, idx4
;
378 if ((i
+ 1 < rounds
) || (dgst_raw_len
% 2 != 0)) {
379 idx0
= (((((u_int
)(dgst_raw
[2 * i
])) >> 6) & 3) +
381 idx1
= (((u_int
)(dgst_raw
[2 * i
])) >> 2) & 15;
382 idx2
= ((((u_int
)(dgst_raw
[2 * i
])) & 3) +
384 retval
[j
++] = vowels
[idx0
];
385 retval
[j
++] = consonants
[idx1
];
386 retval
[j
++] = vowels
[idx2
];
387 if ((i
+ 1) < rounds
) {
388 idx3
= (((u_int
)(dgst_raw
[(2 * i
) + 1])) >> 4) & 15;
389 idx4
= (((u_int
)(dgst_raw
[(2 * i
) + 1]))) & 15;
390 retval
[j
++] = consonants
[idx3
];
392 retval
[j
++] = consonants
[idx4
];
394 ((((u_int
)(dgst_raw
[2 * i
])) * 7) +
395 ((u_int
)(dgst_raw
[(2 * i
) + 1])))) % 36;
401 retval
[j
++] = vowels
[idx0
];
402 retval
[j
++] = consonants
[idx1
];
403 retval
[j
++] = vowels
[idx2
];
412 * Draw an ASCII-Art representing the fingerprint so human brain can
413 * profit from its built-in pattern recognition ability.
414 * This technique is called "random art" and can be found in some
415 * scientific publications like this original paper:
417 * "Hash Visualization: a New Technique to improve Real-World Security",
418 * Perrig A. and Song D., 1999, International Workshop on Cryptographic
419 * Techniques and E-Commerce (CrypTEC '99)
420 * sparrow.ece.cmu.edu/~adrian/projects/validation/validation.pdf
422 * The subject came up in a talk by Dan Kaminsky, too.
424 * If you see the picture is different, the key is different.
425 * If the picture looks the same, you still know nothing.
427 * The algorithm used here is a worm crawling over a discrete plane,
428 * leaving a trace (augmenting the field) everywhere it goes.
429 * Movement is taken from dgst_raw 2bit-wise. Bumping into walls
430 * makes the respective movement vector be ignored for this turn.
431 * Graphs are not unambiguous, because circles in graphs can be
432 * walked in either direction.
436 * Field sizes for the random art. Have to be odd, so the starting point
437 * can be in the exact middle of the picture, and FLDBASE should be >=8 .
438 * Else pictures would be too dense, and drawing the frame would
439 * fail, too, because the key type would not fit in anymore.
442 #define FLDSIZE_Y (FLDBASE + 1)
443 #define FLDSIZE_X (FLDBASE * 2 + 1)
445 key_fingerprint_randomart(u_char
*dgst_raw
, u_int dgst_raw_len
, const Key
*k
)
448 * Chars to be used after each other every time the worm
449 * intersects with itself. Matter of taste.
451 char *augmentation_string
= " .o+=*BOX@%&#/^SE";
453 u_char field
[FLDSIZE_X
][FLDSIZE_Y
];
456 size_t len
= strlen(augmentation_string
) - 1;
458 retval
= xcalloc(1, (FLDSIZE_X
+ 3) * (FLDSIZE_Y
+ 2));
460 /* initialize field */
461 memset(field
, 0, FLDSIZE_X
* FLDSIZE_Y
* sizeof(char));
465 /* process raw key */
466 for (i
= 0; i
< dgst_raw_len
; i
++) {
468 /* each byte conveys four 2-bit move commands */
470 for (b
= 0; b
< 4; b
++) {
471 /* evaluate 2 bit, rest is shifted later */
472 x
+= (input
& 0x1) ? 1 : -1;
473 y
+= (input
& 0x2) ? 1 : -1;
475 /* assure we are still in bounds */
478 x
= MIN(x
, FLDSIZE_X
- 1);
479 y
= MIN(y
, FLDSIZE_Y
- 1);
481 /* augment the field */
482 if (field
[x
][y
] < len
- 2)
488 /* mark starting point and end point*/
489 field
[FLDSIZE_X
/ 2][FLDSIZE_Y
/ 2] = len
- 1;
493 snprintf(retval
, FLDSIZE_X
, "+--[%4s %4u]", key_type(k
), key_size(k
));
494 p
= strchr(retval
, '\0');
496 /* output upper border */
497 for (i
= p
- retval
- 1; i
< FLDSIZE_X
; i
++)
503 for (y
= 0; y
< FLDSIZE_Y
; y
++) {
505 for (x
= 0; x
< FLDSIZE_X
; x
++)
506 *p
++ = augmentation_string
[MIN(field
[x
][y
], len
)];
511 /* output lower border */
513 for (i
= 0; i
< FLDSIZE_X
; i
++)
521 key_fingerprint(Key
*k
, enum fp_type dgst_type
, enum fp_rep dgst_rep
)
527 dgst_raw
= key_fingerprint_raw(k
, dgst_type
, &dgst_raw_len
);
529 fatal("key_fingerprint: null from key_fingerprint_raw()");
532 retval
= key_fingerprint_hex(dgst_raw
, dgst_raw_len
);
534 case SSH_FP_BUBBLEBABBLE
:
535 retval
= key_fingerprint_bubblebabble(dgst_raw
, dgst_raw_len
);
537 case SSH_FP_RANDOMART
:
538 retval
= key_fingerprint_randomart(dgst_raw
, dgst_raw_len
, k
);
541 fatal("key_fingerprint: bad digest representation %d",
545 memset(dgst_raw
, 0, dgst_raw_len
);
551 * Reads a multiple-precision integer in decimal from the buffer, and advances
552 * the pointer. The integer must already be initialized. This function is
553 * permitted to modify the buffer. This leaves *cpp to point just beyond the
554 * last processed (and maybe modified) character. Note that this may modify
555 * the buffer containing the number.
558 read_bignum(char **cpp
, BIGNUM
* value
)
563 /* Skip any leading whitespace. */
564 for (; *cp
== ' ' || *cp
== '\t'; cp
++)
567 /* Check that it begins with a decimal digit. */
568 if (*cp
< '0' || *cp
> '9')
571 /* Save starting position. */
574 /* Move forward until all decimal digits skipped. */
575 for (; *cp
>= '0' && *cp
<= '9'; cp
++)
578 /* Save the old terminating character, and replace it by \0. */
582 /* Parse the number. */
583 if (BN_dec2bn(&value
, *cpp
) == 0)
586 /* Restore old terminating character. */
589 /* Move beyond the number and return success. */
595 write_bignum(FILE *f
, BIGNUM
*num
)
597 char *buf
= BN_bn2dec(num
);
599 error("write_bignum: BN_bn2dec() failed");
602 fprintf(f
, " %s", buf
);
607 /* returns 1 ok, -1 error */
609 key_read(Key
*ret
, char **cpp
)
622 /* Get number of bits. */
623 if (*cp
< '0' || *cp
> '9')
624 return -1; /* Bad bit count... */
625 for (bits
= 0; *cp
>= '0' && *cp
<= '9'; cp
++)
626 bits
= 10 * bits
+ *cp
- '0';
630 /* Get public exponent, public modulus. */
631 if (!read_bignum(cpp
, ret
->rsa
->e
))
633 if (!read_bignum(cpp
, ret
->rsa
->n
))
635 /* validate the claimed number of bits */
636 if ((u_int
)BN_num_bits(ret
->rsa
->n
) != bits
) {
637 verbose("key_read: claimed key size %d does not match "
638 "actual %d", bits
, BN_num_bits(ret
->rsa
->n
));
646 case KEY_DSA_CERT_V00
:
647 case KEY_RSA_CERT_V00
:
650 space
= strchr(cp
, ' ');
652 debug3("key_read: missing whitespace");
656 type
= key_type_from_name(cp
);
658 if (type
== KEY_UNSPEC
) {
659 debug3("key_read: missing keytype");
664 debug3("key_read: short string");
667 if (ret
->type
== KEY_UNSPEC
) {
669 } else if (ret
->type
!= type
) {
670 /* is a key, but different type */
671 debug3("key_read: type mismatch");
676 n
= uudecode(cp
, blob
, len
);
678 error("key_read: uudecode %s failed", cp
);
682 k
= key_from_blob(blob
, (u_int
)n
);
685 error("key_read: key_from_blob %s failed", cp
);
688 if (k
->type
!= type
) {
689 error("key_read: type mismatch: encoding error");
694 if (key_is_cert(ret
)) {
695 if (!key_is_cert(k
)) {
696 error("key_read: loaded key is not a cert");
700 if (ret
->cert
!= NULL
)
701 cert_free(ret
->cert
);
705 if (key_type_plain(ret
->type
) == KEY_RSA
) {
706 if (ret
->rsa
!= NULL
)
711 RSA_print_fp(stderr
, ret
->rsa
, 8);
714 if (key_type_plain(ret
->type
) == KEY_DSA
) {
715 if (ret
->dsa
!= NULL
)
720 DSA_print_fp(stderr
, ret
->dsa
, 8);
728 /* advance cp: skip whitespace and data */
729 while (*cp
== ' ' || *cp
== '\t')
731 while (*cp
!= '\0' && *cp
!= ' ' && *cp
!= '\t')
736 fatal("key_read: bad key type: %d", ret
->type
);
743 key_write(const Key
*key
, FILE *f
)
750 if (key_is_cert(key
)) {
751 if (key
->cert
== NULL
) {
752 error("%s: no cert data", __func__
);
755 if (buffer_len(&key
->cert
->certblob
) == 0) {
756 error("%s: no signed certificate blob", __func__
);
763 if (key
->rsa
== NULL
)
765 /* size of modulus 'n' */
766 bits
= BN_num_bits(key
->rsa
->n
);
767 fprintf(f
, "%u", bits
);
768 if (write_bignum(f
, key
->rsa
->e
) &&
769 write_bignum(f
, key
->rsa
->n
))
771 error("key_write: failed for RSA key");
774 case KEY_DSA_CERT_V00
:
776 if (key
->dsa
== NULL
)
780 case KEY_RSA_CERT_V00
:
782 if (key
->rsa
== NULL
)
789 key_to_blob(key
, &blob
, &len
);
791 n
= uuencode(blob
, len
, uu
, 2*len
);
793 fprintf(f
, "%s %s", key_ssh_name(key
), uu
);
803 key_type(const Key
*k
)
812 case KEY_RSA_CERT_V00
:
813 return "RSA-CERT-V00";
814 case KEY_DSA_CERT_V00
:
815 return "DSA-CERT-V00";
825 key_cert_type(const Key
*k
)
827 switch (k
->cert
->type
) {
828 case SSH2_CERT_TYPE_USER
:
830 case SSH2_CERT_TYPE_HOST
:
838 key_ssh_name(const Key
*k
)
845 case KEY_RSA_CERT_V00
:
846 return "ssh-rsa-cert-v00@openssh.com";
847 case KEY_DSA_CERT_V00
:
848 return "ssh-dss-cert-v00@openssh.com";
850 return "ssh-rsa-cert-v01@openssh.com";
852 return "ssh-dss-cert-v01@openssh.com";
854 return "ssh-unknown";
858 key_size(const Key
*k
)
863 case KEY_RSA_CERT_V00
:
865 return BN_num_bits(k
->rsa
->n
);
867 case KEY_DSA_CERT_V00
:
869 return BN_num_bits(k
->dsa
->p
);
875 rsa_generate_private_key(u_int bits
)
879 private = RSA_generate_key(bits
, RSA_F4
, NULL
, NULL
);
881 fatal("rsa_generate_private_key: key generation failed.");
886 dsa_generate_private_key(u_int bits
)
888 DSA
*private = DSA_generate_parameters(bits
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
891 fatal("dsa_generate_private_key: DSA_generate_parameters failed");
892 if (!DSA_generate_key(private))
893 fatal("dsa_generate_private_key: DSA_generate_key failed.");
895 fatal("dsa_generate_private_key: NULL.");
900 key_generate(int type
, u_int bits
)
902 Key
*k
= key_new(KEY_UNSPEC
);
905 k
->dsa
= dsa_generate_private_key(bits
);
909 k
->rsa
= rsa_generate_private_key(bits
);
911 case KEY_RSA_CERT_V00
:
912 case KEY_DSA_CERT_V00
:
915 fatal("key_generate: cert keys cannot be generated directly");
917 fatal("key_generate: unknown type %d", type
);
924 key_cert_copy(const Key
*from_key
, struct Key
*to_key
)
927 const struct KeyCert
*from
;
930 if (to_key
->cert
!= NULL
) {
931 cert_free(to_key
->cert
);
935 if ((from
= from_key
->cert
) == NULL
)
938 to
= to_key
->cert
= cert_new();
940 buffer_append(&to
->certblob
, buffer_ptr(&from
->certblob
),
941 buffer_len(&from
->certblob
));
943 buffer_append(&to
->critical
,
944 buffer_ptr(&from
->critical
), buffer_len(&from
->critical
));
945 buffer_append(&to
->extensions
,
946 buffer_ptr(&from
->extensions
), buffer_len(&from
->extensions
));
948 to
->serial
= from
->serial
;
949 to
->type
= from
->type
;
950 to
->key_id
= from
->key_id
== NULL
? NULL
: xstrdup(from
->key_id
);
951 to
->valid_after
= from
->valid_after
;
952 to
->valid_before
= from
->valid_before
;
953 to
->signature_key
= from
->signature_key
== NULL
?
954 NULL
: key_from_private(from
->signature_key
);
956 to
->nprincipals
= from
->nprincipals
;
957 if (to
->nprincipals
> CERT_MAX_PRINCIPALS
)
958 fatal("%s: nprincipals (%u) > CERT_MAX_PRINCIPALS (%u)",
959 __func__
, to
->nprincipals
, CERT_MAX_PRINCIPALS
);
960 if (to
->nprincipals
> 0) {
961 to
->principals
= xcalloc(from
->nprincipals
,
962 sizeof(*to
->principals
));
963 for (i
= 0; i
< to
->nprincipals
; i
++)
964 to
->principals
[i
] = xstrdup(from
->principals
[i
]);
969 key_from_private(const Key
*k
)
974 case KEY_DSA_CERT_V00
:
976 n
= key_new(k
->type
);
977 if ((BN_copy(n
->dsa
->p
, k
->dsa
->p
) == NULL
) ||
978 (BN_copy(n
->dsa
->q
, k
->dsa
->q
) == NULL
) ||
979 (BN_copy(n
->dsa
->g
, k
->dsa
->g
) == NULL
) ||
980 (BN_copy(n
->dsa
->pub_key
, k
->dsa
->pub_key
) == NULL
))
981 fatal("key_from_private: BN_copy failed");
985 case KEY_RSA_CERT_V00
:
987 n
= key_new(k
->type
);
988 if ((BN_copy(n
->rsa
->n
, k
->rsa
->n
) == NULL
) ||
989 (BN_copy(n
->rsa
->e
, k
->rsa
->e
) == NULL
))
990 fatal("key_from_private: BN_copy failed");
993 fatal("key_from_private: unknown type %d", k
->type
);
1002 key_type_from_name(char *name
)
1004 if (strcmp(name
, "rsa1") == 0) {
1006 } else if (strcmp(name
, "rsa") == 0) {
1008 } else if (strcmp(name
, "dsa") == 0) {
1010 } else if (strcmp(name
, "ssh-rsa") == 0) {
1012 } else if (strcmp(name
, "ssh-dss") == 0) {
1014 } else if (strcmp(name
, "ssh-rsa-cert-v00@openssh.com") == 0) {
1015 return KEY_RSA_CERT_V00
;
1016 } else if (strcmp(name
, "ssh-dss-cert-v00@openssh.com") == 0) {
1017 return KEY_DSA_CERT_V00
;
1018 } else if (strcmp(name
, "ssh-rsa-cert-v01@openssh.com") == 0) {
1019 return KEY_RSA_CERT
;
1020 } else if (strcmp(name
, "ssh-dss-cert-v01@openssh.com") == 0) {
1021 return KEY_DSA_CERT
;
1023 debug2("key_type_from_name: unknown key type '%s'", name
);
1028 key_names_valid2(const char *names
)
1032 if (names
== NULL
|| strcmp(names
, "") == 0)
1034 s
= cp
= xstrdup(names
);
1035 for ((p
= strsep(&cp
, ",")); p
&& *p
!= '\0';
1036 (p
= strsep(&cp
, ","))) {
1037 switch (key_type_from_name(p
)) {
1044 debug3("key names ok: [%s]", names
);
1050 cert_parse(Buffer
*b
, Key
*key
, const u_char
*blob
, u_int blen
)
1052 u_char
*principals
, *critical
, *exts
, *sig_key
, *sig
;
1053 u_int signed_len
, plen
, clen
, sklen
, slen
, kidlen
, elen
;
1057 int v00
= key
->type
== KEY_DSA_CERT_V00
||
1058 key
->type
== KEY_RSA_CERT_V00
;
1062 /* Copy the entire key blob for verification and later serialisation */
1063 buffer_append(&key
->cert
->certblob
, blob
, blen
);
1065 elen
= 0; /* Not touched for v00 certs */
1066 principals
= exts
= critical
= sig_key
= sig
= NULL
;
1067 if ((!v00
&& buffer_get_int64_ret(&key
->cert
->serial
, b
) != 0) ||
1068 buffer_get_int_ret(&key
->cert
->type
, b
) != 0 ||
1069 (key
->cert
->key_id
= buffer_get_string_ret(b
, &kidlen
)) == NULL
||
1070 (principals
= buffer_get_string_ret(b
, &plen
)) == NULL
||
1071 buffer_get_int64_ret(&key
->cert
->valid_after
, b
) != 0 ||
1072 buffer_get_int64_ret(&key
->cert
->valid_before
, b
) != 0 ||
1073 (critical
= buffer_get_string_ret(b
, &clen
)) == NULL
||
1074 (!v00
&& (exts
= buffer_get_string_ret(b
, &elen
)) == NULL
) ||
1075 (v00
&& buffer_get_string_ptr_ret(b
, NULL
) == NULL
) || /* nonce */
1076 buffer_get_string_ptr_ret(b
, NULL
) == NULL
|| /* reserved */
1077 (sig_key
= buffer_get_string_ret(b
, &sklen
)) == NULL
) {
1078 error("%s: parse error", __func__
);
1082 if (kidlen
!= strlen(key
->cert
->key_id
)) {
1083 error("%s: key ID contains \\0 character", __func__
);
1087 /* Signature is left in the buffer so we can calculate this length */
1088 signed_len
= buffer_len(&key
->cert
->certblob
) - buffer_len(b
);
1090 if ((sig
= buffer_get_string_ret(b
, &slen
)) == NULL
) {
1091 error("%s: parse error", __func__
);
1095 if (key
->cert
->type
!= SSH2_CERT_TYPE_USER
&&
1096 key
->cert
->type
!= SSH2_CERT_TYPE_HOST
) {
1097 error("Unknown certificate type %u", key
->cert
->type
);
1101 buffer_append(&tmp
, principals
, plen
);
1102 while (buffer_len(&tmp
) > 0) {
1103 if (key
->cert
->nprincipals
>= CERT_MAX_PRINCIPALS
) {
1104 error("%s: Too many principals", __func__
);
1107 if ((principal
= buffer_get_string_ret(&tmp
, &plen
)) == NULL
) {
1108 error("%s: Principals data invalid", __func__
);
1111 if (strlen(principal
) != plen
) {
1112 error("%s: Principal contains \\0 character",
1116 key
->cert
->principals
= xrealloc(key
->cert
->principals
,
1117 key
->cert
->nprincipals
+ 1, sizeof(*key
->cert
->principals
));
1118 key
->cert
->principals
[key
->cert
->nprincipals
++] = principal
;
1123 buffer_append(&key
->cert
->critical
, critical
, clen
);
1124 buffer_append(&tmp
, critical
, clen
);
1125 /* validate structure */
1126 while (buffer_len(&tmp
) != 0) {
1127 if (buffer_get_string_ptr_ret(&tmp
, NULL
) == NULL
||
1128 buffer_get_string_ptr_ret(&tmp
, NULL
) == NULL
) {
1129 error("%s: critical option data invalid", __func__
);
1135 buffer_append(&key
->cert
->extensions
, exts
, elen
);
1136 buffer_append(&tmp
, exts
, elen
);
1137 /* validate structure */
1138 while (buffer_len(&tmp
) != 0) {
1139 if (buffer_get_string_ptr_ret(&tmp
, NULL
) == NULL
||
1140 buffer_get_string_ptr_ret(&tmp
, NULL
) == NULL
) {
1141 error("%s: extension data invalid", __func__
);
1147 if ((key
->cert
->signature_key
= key_from_blob(sig_key
,
1149 error("%s: Signature key invalid", __func__
);
1152 if (key
->cert
->signature_key
->type
!= KEY_RSA
&&
1153 key
->cert
->signature_key
->type
!= KEY_DSA
) {
1154 error("%s: Invalid signature key type %s (%d)", __func__
,
1155 key_type(key
->cert
->signature_key
),
1156 key
->cert
->signature_key
->type
);
1160 switch (key_verify(key
->cert
->signature_key
, sig
, slen
,
1161 buffer_ptr(&key
->cert
->certblob
), signed_len
)) {
1164 break; /* Good signature */
1166 error("%s: Invalid signature on certificate", __func__
);
1169 error("%s: Certificate signature verification failed",
1176 if (principals
!= NULL
)
1178 if (critical
!= NULL
)
1182 if (sig_key
!= NULL
)
1190 key_from_blob(const u_char
*blob
, u_int blen
)
1198 dump_base64(stderr
, blob
, blen
);
1201 buffer_append(&b
, blob
, blen
);
1202 if ((ktype
= buffer_get_string_ret(&b
, NULL
)) == NULL
) {
1203 error("key_from_blob: can't read key type");
1207 type
= key_type_from_name(ktype
);
1211 (void)buffer_get_string_ptr_ret(&b
, NULL
); /* Skip nonce */
1214 case KEY_RSA_CERT_V00
:
1215 key
= key_new(type
);
1216 if (buffer_get_bignum2_ret(&b
, key
->rsa
->e
) == -1 ||
1217 buffer_get_bignum2_ret(&b
, key
->rsa
->n
) == -1) {
1218 error("key_from_blob: can't read rsa key");
1225 RSA_print_fp(stderr
, key
->rsa
, 8);
1229 (void)buffer_get_string_ptr_ret(&b
, NULL
); /* Skip nonce */
1232 case KEY_DSA_CERT_V00
:
1233 key
= key_new(type
);
1234 if (buffer_get_bignum2_ret(&b
, key
->dsa
->p
) == -1 ||
1235 buffer_get_bignum2_ret(&b
, key
->dsa
->q
) == -1 ||
1236 buffer_get_bignum2_ret(&b
, key
->dsa
->g
) == -1 ||
1237 buffer_get_bignum2_ret(&b
, key
->dsa
->pub_key
) == -1) {
1238 error("key_from_blob: can't read dsa key");
1242 DSA_print_fp(stderr
, key
->dsa
, 8);
1246 key
= key_new(type
);
1249 error("key_from_blob: cannot handle type %s", ktype
);
1252 if (key_is_cert(key
) && cert_parse(&b
, key
, blob
, blen
) == -1) {
1253 error("key_from_blob: can't parse cert data");
1256 rlen
= buffer_len(&b
);
1257 if (key
!= NULL
&& rlen
!= 0)
1258 error("key_from_blob: remaining bytes in key blob %d", rlen
);
1267 key_to_blob(const Key
*key
, u_char
**blobp
, u_int
*lenp
)
1273 error("key_to_blob: key == NULL");
1277 switch (key
->type
) {
1278 case KEY_DSA_CERT_V00
:
1279 case KEY_RSA_CERT_V00
:
1282 /* Use the existing blob */
1283 buffer_append(&b
, buffer_ptr(&key
->cert
->certblob
),
1284 buffer_len(&key
->cert
->certblob
));
1287 buffer_put_cstring(&b
, key_ssh_name(key
));
1288 buffer_put_bignum2(&b
, key
->dsa
->p
);
1289 buffer_put_bignum2(&b
, key
->dsa
->q
);
1290 buffer_put_bignum2(&b
, key
->dsa
->g
);
1291 buffer_put_bignum2(&b
, key
->dsa
->pub_key
);
1294 buffer_put_cstring(&b
, key_ssh_name(key
));
1295 buffer_put_bignum2(&b
, key
->rsa
->e
);
1296 buffer_put_bignum2(&b
, key
->rsa
->n
);
1299 error("key_to_blob: unsupported key type %d", key
->type
);
1303 len
= buffer_len(&b
);
1306 if (blobp
!= NULL
) {
1307 *blobp
= xmalloc(len
);
1308 memcpy(*blobp
, buffer_ptr(&b
), len
);
1310 memset(buffer_ptr(&b
), 0, len
);
1318 u_char
**sigp
, u_int
*lenp
,
1319 const u_char
*data
, u_int datalen
)
1321 switch (key
->type
) {
1322 case KEY_DSA_CERT_V00
:
1325 return ssh_dss_sign(key
, sigp
, lenp
, data
, datalen
);
1326 case KEY_RSA_CERT_V00
:
1329 return ssh_rsa_sign(key
, sigp
, lenp
, data
, datalen
);
1331 error("key_sign: invalid key type %d", key
->type
);
1337 * key_verify returns 1 for a correct signature, 0 for an incorrect signature
1343 const u_char
*signature
, u_int signaturelen
,
1344 const u_char
*data
, u_int datalen
)
1346 if (signaturelen
== 0)
1349 switch (key
->type
) {
1350 case KEY_DSA_CERT_V00
:
1353 return ssh_dss_verify(key
, signature
, signaturelen
, data
, datalen
);
1354 case KEY_RSA_CERT_V00
:
1357 return ssh_rsa_verify(key
, signature
, signaturelen
, data
, datalen
);
1359 error("key_verify: invalid key type %d", key
->type
);
1364 /* Converts a private to a public key */
1366 key_demote(const Key
*k
)
1370 pk
= xcalloc(1, sizeof(*pk
));
1372 pk
->flags
= k
->flags
;
1377 case KEY_RSA_CERT_V00
:
1379 key_cert_copy(k
, pk
);
1383 if ((pk
->rsa
= RSA_new()) == NULL
)
1384 fatal("key_demote: RSA_new failed");
1385 if ((pk
->rsa
->e
= BN_dup(k
->rsa
->e
)) == NULL
)
1386 fatal("key_demote: BN_dup failed");
1387 if ((pk
->rsa
->n
= BN_dup(k
->rsa
->n
)) == NULL
)
1388 fatal("key_demote: BN_dup failed");
1390 case KEY_DSA_CERT_V00
:
1392 key_cert_copy(k
, pk
);
1395 if ((pk
->dsa
= DSA_new()) == NULL
)
1396 fatal("key_demote: DSA_new failed");
1397 if ((pk
->dsa
->p
= BN_dup(k
->dsa
->p
)) == NULL
)
1398 fatal("key_demote: BN_dup failed");
1399 if ((pk
->dsa
->q
= BN_dup(k
->dsa
->q
)) == NULL
)
1400 fatal("key_demote: BN_dup failed");
1401 if ((pk
->dsa
->g
= BN_dup(k
->dsa
->g
)) == NULL
)
1402 fatal("key_demote: BN_dup failed");
1403 if ((pk
->dsa
->pub_key
= BN_dup(k
->dsa
->pub_key
)) == NULL
)
1404 fatal("key_demote: BN_dup failed");
1407 fatal("key_free: bad key type %d", k
->type
);
1415 key_is_cert(const Key
*k
)
1420 case KEY_RSA_CERT_V00
:
1421 case KEY_DSA_CERT_V00
:
1430 /* Return the cert-less equivalent to a certified key type */
1432 key_type_plain(int type
)
1435 case KEY_RSA_CERT_V00
:
1438 case KEY_DSA_CERT_V00
:
1446 /* Convert a KEY_RSA or KEY_DSA to their _CERT equivalent */
1448 key_to_certified(Key
*k
, int legacy
)
1452 k
->cert
= cert_new();
1453 k
->type
= legacy
? KEY_RSA_CERT_V00
: KEY_RSA_CERT
;
1456 k
->cert
= cert_new();
1457 k
->type
= legacy
? KEY_DSA_CERT_V00
: KEY_DSA_CERT
;
1460 error("%s: key has incorrect type %s", __func__
, key_type(k
));
1465 /* Convert a KEY_RSA_CERT or KEY_DSA_CERT to their raw key equivalent */
1467 key_drop_cert(Key
*k
)
1470 case KEY_RSA_CERT_V00
:
1475 case KEY_DSA_CERT_V00
:
1481 error("%s: key has incorrect type %s", __func__
, key_type(k
));
1486 /* Sign a KEY_RSA_CERT or KEY_DSA_CERT, (re-)generating the signed certblob */
1488 key_certify(Key
*k
, Key
*ca
)
1491 u_char
*ca_blob
, *sig_blob
, nonce
[32];
1492 u_int i
, ca_len
, sig_len
;
1494 if (k
->cert
== NULL
) {
1495 error("%s: key lacks cert info", __func__
);
1499 if (!key_is_cert(k
)) {
1500 error("%s: certificate has unknown type %d", __func__
,
1505 if (ca
->type
!= KEY_RSA
&& ca
->type
!= KEY_DSA
) {
1506 error("%s: CA key has unsupported type %s", __func__
,
1511 key_to_blob(ca
, &ca_blob
, &ca_len
);
1513 buffer_clear(&k
->cert
->certblob
);
1514 buffer_put_cstring(&k
->cert
->certblob
, key_ssh_name(k
));
1516 /* -v01 certs put nonce first */
1517 if (k
->type
== KEY_DSA_CERT
|| k
->type
== KEY_RSA_CERT
) {
1518 arc4random_buf(&nonce
, sizeof(nonce
));
1519 buffer_put_string(&k
->cert
->certblob
, nonce
, sizeof(nonce
));
1523 case KEY_DSA_CERT_V00
:
1525 buffer_put_bignum2(&k
->cert
->certblob
, k
->dsa
->p
);
1526 buffer_put_bignum2(&k
->cert
->certblob
, k
->dsa
->q
);
1527 buffer_put_bignum2(&k
->cert
->certblob
, k
->dsa
->g
);
1528 buffer_put_bignum2(&k
->cert
->certblob
, k
->dsa
->pub_key
);
1530 case KEY_RSA_CERT_V00
:
1532 buffer_put_bignum2(&k
->cert
->certblob
, k
->rsa
->e
);
1533 buffer_put_bignum2(&k
->cert
->certblob
, k
->rsa
->n
);
1536 error("%s: key has incorrect type %s", __func__
, key_type(k
));
1537 buffer_clear(&k
->cert
->certblob
);
1542 /* -v01 certs have a serial number next */
1543 if (k
->type
== KEY_DSA_CERT
|| k
->type
== KEY_RSA_CERT
)
1544 buffer_put_int64(&k
->cert
->certblob
, k
->cert
->serial
);
1546 buffer_put_int(&k
->cert
->certblob
, k
->cert
->type
);
1547 buffer_put_cstring(&k
->cert
->certblob
, k
->cert
->key_id
);
1549 buffer_init(&principals
);
1550 for (i
= 0; i
< k
->cert
->nprincipals
; i
++)
1551 buffer_put_cstring(&principals
, k
->cert
->principals
[i
]);
1552 buffer_put_string(&k
->cert
->certblob
, buffer_ptr(&principals
),
1553 buffer_len(&principals
));
1554 buffer_free(&principals
);
1556 buffer_put_int64(&k
->cert
->certblob
, k
->cert
->valid_after
);
1557 buffer_put_int64(&k
->cert
->certblob
, k
->cert
->valid_before
);
1558 buffer_put_string(&k
->cert
->certblob
,
1559 buffer_ptr(&k
->cert
->critical
), buffer_len(&k
->cert
->critical
));
1561 /* -v01 certs have non-critical options here */
1562 if (k
->type
== KEY_DSA_CERT
|| k
->type
== KEY_RSA_CERT
) {
1563 buffer_put_string(&k
->cert
->certblob
,
1564 buffer_ptr(&k
->cert
->extensions
),
1565 buffer_len(&k
->cert
->extensions
));
1568 /* -v00 certs put the nonce at the end */
1569 if (k
->type
== KEY_DSA_CERT_V00
|| k
->type
== KEY_RSA_CERT_V00
)
1570 buffer_put_string(&k
->cert
->certblob
, nonce
, sizeof(nonce
));
1572 buffer_put_string(&k
->cert
->certblob
, NULL
, 0); /* reserved */
1573 buffer_put_string(&k
->cert
->certblob
, ca_blob
, ca_len
);
1576 /* Sign the whole mess */
1577 if (key_sign(ca
, &sig_blob
, &sig_len
, buffer_ptr(&k
->cert
->certblob
),
1578 buffer_len(&k
->cert
->certblob
)) != 0) {
1579 error("%s: signature operation failed", __func__
);
1580 buffer_clear(&k
->cert
->certblob
);
1583 /* Append signature and we are done */
1584 buffer_put_string(&k
->cert
->certblob
, sig_blob
, sig_len
);
1591 key_cert_check_authority(const Key
*k
, int want_host
, int require_principal
,
1592 const char *name
, const char **reason
)
1594 u_int i
, principal_matches
;
1595 time_t now
= time(NULL
);
1598 if (k
->cert
->type
!= SSH2_CERT_TYPE_HOST
) {
1599 *reason
= "Certificate invalid: not a host certificate";
1603 if (k
->cert
->type
!= SSH2_CERT_TYPE_USER
) {
1604 *reason
= "Certificate invalid: not a user certificate";
1609 error("%s: system clock lies before epoch", __func__
);
1610 *reason
= "Certificate invalid: not yet valid";
1613 if ((u_int64_t
)now
< k
->cert
->valid_after
) {
1614 *reason
= "Certificate invalid: not yet valid";
1617 if ((u_int64_t
)now
>= k
->cert
->valid_before
) {
1618 *reason
= "Certificate invalid: expired";
1621 if (k
->cert
->nprincipals
== 0) {
1622 if (require_principal
) {
1623 *reason
= "Certificate lacks principal list";
1627 principal_matches
= 0;
1628 for (i
= 0; i
< k
->cert
->nprincipals
; i
++) {
1629 if (strcmp(name
, k
->cert
->principals
[i
]) == 0) {
1630 principal_matches
= 1;
1634 if (!principal_matches
) {
1635 *reason
= "Certificate invalid: name is not a listed "
1644 key_cert_is_legacy(Key
*k
)
1647 case KEY_DSA_CERT_V00
:
1648 case KEY_RSA_CERT_V00
: