1 /* $OpenBSD: key.c,v 1.92 2010/08/31 11:54:45 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>
58 static struct KeyCert
*
63 cert
= xcalloc(1, sizeof(*cert
));
64 buffer_init(&cert
->certblob
);
65 buffer_init(&cert
->critical
);
66 buffer_init(&cert
->extensions
);
68 cert
->principals
= NULL
;
69 cert
->signature_key
= NULL
;
79 k
= xcalloc(1, sizeof(*k
));
89 case KEY_RSA_CERT_V00
:
91 if ((rsa
= RSA_new()) == NULL
)
92 fatal("key_new: RSA_new failed");
93 if ((rsa
->n
= BN_new()) == NULL
)
94 fatal("key_new: BN_new failed");
95 if ((rsa
->e
= BN_new()) == NULL
)
96 fatal("key_new: BN_new failed");
100 case KEY_DSA_CERT_V00
:
102 if ((dsa
= DSA_new()) == NULL
)
103 fatal("key_new: DSA_new failed");
104 if ((dsa
->p
= BN_new()) == NULL
)
105 fatal("key_new: BN_new failed");
106 if ((dsa
->q
= BN_new()) == NULL
)
107 fatal("key_new: BN_new failed");
108 if ((dsa
->g
= BN_new()) == NULL
)
109 fatal("key_new: BN_new failed");
110 if ((dsa
->pub_key
= BN_new()) == NULL
)
111 fatal("key_new: BN_new failed");
116 /* Cannot do anything until we know the group */
121 fatal("key_new: bad key type %d", k
->type
);
126 k
->cert
= cert_new();
132 key_add_private(Key
*k
)
137 case KEY_RSA_CERT_V00
:
139 if ((k
->rsa
->d
= BN_new()) == NULL
)
140 fatal("key_new_private: BN_new failed");
141 if ((k
->rsa
->iqmp
= BN_new()) == NULL
)
142 fatal("key_new_private: BN_new failed");
143 if ((k
->rsa
->q
= BN_new()) == NULL
)
144 fatal("key_new_private: BN_new failed");
145 if ((k
->rsa
->p
= BN_new()) == NULL
)
146 fatal("key_new_private: BN_new failed");
147 if ((k
->rsa
->dmq1
= BN_new()) == NULL
)
148 fatal("key_new_private: BN_new failed");
149 if ((k
->rsa
->dmp1
= BN_new()) == NULL
)
150 fatal("key_new_private: BN_new failed");
153 case KEY_DSA_CERT_V00
:
155 if ((k
->dsa
->priv_key
= BN_new()) == NULL
)
156 fatal("key_new_private: BN_new failed");
160 /* Cannot do anything until we know the group */
170 key_new_private(int type
)
172 Key
*k
= key_new(type
);
179 cert_free(struct KeyCert
*cert
)
183 buffer_free(&cert
->certblob
);
184 buffer_free(&cert
->critical
);
185 buffer_free(&cert
->extensions
);
186 if (cert
->key_id
!= NULL
)
188 for (i
= 0; i
< cert
->nprincipals
; i
++)
189 xfree(cert
->principals
[i
]);
190 if (cert
->principals
!= NULL
)
191 xfree(cert
->principals
);
192 if (cert
->signature_key
!= NULL
)
193 key_free(cert
->signature_key
);
200 fatal("key_free: key is NULL");
204 case KEY_RSA_CERT_V00
:
211 case KEY_DSA_CERT_V00
:
219 if (k
->ecdsa
!= NULL
)
220 EC_KEY_free(k
->ecdsa
);
226 fatal("key_free: bad key type %d", k
->type
);
229 if (key_is_cert(k
)) {
239 cert_compare(struct KeyCert
*a
, struct KeyCert
*b
)
241 if (a
== NULL
&& b
== NULL
)
243 if (a
== NULL
|| b
== NULL
)
245 if (buffer_len(&a
->certblob
) != buffer_len(&b
->certblob
))
247 if (timingsafe_bcmp(buffer_ptr(&a
->certblob
), buffer_ptr(&b
->certblob
),
248 buffer_len(&a
->certblob
)) != 0)
254 * Compare public portions of key only, allowing comparisons between
255 * certificates and plain keys too.
258 key_equal_public(const Key
*a
, const Key
*b
)
262 if (a
== NULL
|| b
== NULL
||
263 key_type_plain(a
->type
) != key_type_plain(b
->type
))
268 case KEY_RSA_CERT_V00
:
271 return a
->rsa
!= NULL
&& b
->rsa
!= NULL
&&
272 BN_cmp(a
->rsa
->e
, b
->rsa
->e
) == 0 &&
273 BN_cmp(a
->rsa
->n
, b
->rsa
->n
) == 0;
274 case KEY_DSA_CERT_V00
:
277 return a
->dsa
!= NULL
&& b
->dsa
!= NULL
&&
278 BN_cmp(a
->dsa
->p
, b
->dsa
->p
) == 0 &&
279 BN_cmp(a
->dsa
->q
, b
->dsa
->q
) == 0 &&
280 BN_cmp(a
->dsa
->g
, b
->dsa
->g
) == 0 &&
281 BN_cmp(a
->dsa
->pub_key
, b
->dsa
->pub_key
) == 0;
284 if (a
->ecdsa
== NULL
|| b
->ecdsa
== NULL
||
285 EC_KEY_get0_public_key(a
->ecdsa
) == NULL
||
286 EC_KEY_get0_public_key(b
->ecdsa
) == NULL
)
288 if ((bnctx
= BN_CTX_new()) == NULL
)
289 fatal("%s: BN_CTX_new failed", __func__
);
290 if (EC_GROUP_cmp(EC_KEY_get0_group(a
->ecdsa
),
291 EC_KEY_get0_group(b
->ecdsa
), bnctx
) != 0 ||
292 EC_POINT_cmp(EC_KEY_get0_group(a
->ecdsa
),
293 EC_KEY_get0_public_key(a
->ecdsa
),
294 EC_KEY_get0_public_key(b
->ecdsa
), bnctx
) != 0) {
301 fatal("key_equal: bad key type %d", a
->type
);
307 key_equal(const Key
*a
, const Key
*b
)
309 if (a
== NULL
|| b
== NULL
|| a
->type
!= b
->type
)
311 if (key_is_cert(a
)) {
312 if (!cert_compare(a
->cert
, b
->cert
))
315 return key_equal_public(a
, b
);
319 key_fingerprint_raw(Key
*k
, enum fp_type dgst_type
, u_int
*dgst_raw_length
)
321 const EVP_MD
*md
= NULL
;
324 u_char
*retval
= NULL
;
326 int nlen
, elen
, otype
;
328 *dgst_raw_length
= 0;
338 fatal("key_fingerprint_raw: bad digest type %d",
343 nlen
= BN_num_bytes(k
->rsa
->n
);
344 elen
= BN_num_bytes(k
->rsa
->e
);
347 BN_bn2bin(k
->rsa
->n
, blob
);
348 BN_bn2bin(k
->rsa
->e
, blob
+ nlen
);
353 key_to_blob(k
, &blob
, &len
);
355 case KEY_DSA_CERT_V00
:
356 case KEY_RSA_CERT_V00
:
360 /* We want a fingerprint of the _key_ not of the cert */
362 k
->type
= key_type_plain(k
->type
);
363 key_to_blob(k
, &blob
, &len
);
369 fatal("key_fingerprint_raw: bad key type %d", k
->type
);
373 retval
= xmalloc(EVP_MAX_MD_SIZE
);
374 EVP_DigestInit(&ctx
, md
);
375 EVP_DigestUpdate(&ctx
, blob
, len
);
376 EVP_DigestFinal(&ctx
, retval
, dgst_raw_length
);
377 memset(blob
, 0, len
);
380 fatal("key_fingerprint_raw: blob is null");
386 key_fingerprint_hex(u_char
*dgst_raw
, u_int dgst_raw_len
)
391 retval
= xcalloc(1, dgst_raw_len
* 3 + 1);
392 for (i
= 0; i
< dgst_raw_len
; i
++) {
394 snprintf(hex
, sizeof(hex
), "%02x:", dgst_raw
[i
]);
395 strlcat(retval
, hex
, dgst_raw_len
* 3 + 1);
398 /* Remove the trailing ':' character */
399 retval
[(dgst_raw_len
* 3) - 1] = '\0';
404 key_fingerprint_bubblebabble(u_char
*dgst_raw
, u_int dgst_raw_len
)
406 char vowels
[] = { 'a', 'e', 'i', 'o', 'u', 'y' };
407 char consonants
[] = { 'b', 'c', 'd', 'f', 'g', 'h', 'k', 'l', 'm',
408 'n', 'p', 'r', 's', 't', 'v', 'z', 'x' };
409 u_int i
, j
= 0, rounds
, seed
= 1;
412 rounds
= (dgst_raw_len
/ 2) + 1;
413 retval
= xcalloc((rounds
* 6), sizeof(char));
415 for (i
= 0; i
< rounds
; i
++) {
416 u_int idx0
, idx1
, idx2
, idx3
, idx4
;
417 if ((i
+ 1 < rounds
) || (dgst_raw_len
% 2 != 0)) {
418 idx0
= (((((u_int
)(dgst_raw
[2 * i
])) >> 6) & 3) +
420 idx1
= (((u_int
)(dgst_raw
[2 * i
])) >> 2) & 15;
421 idx2
= ((((u_int
)(dgst_raw
[2 * i
])) & 3) +
423 retval
[j
++] = vowels
[idx0
];
424 retval
[j
++] = consonants
[idx1
];
425 retval
[j
++] = vowels
[idx2
];
426 if ((i
+ 1) < rounds
) {
427 idx3
= (((u_int
)(dgst_raw
[(2 * i
) + 1])) >> 4) & 15;
428 idx4
= (((u_int
)(dgst_raw
[(2 * i
) + 1]))) & 15;
429 retval
[j
++] = consonants
[idx3
];
431 retval
[j
++] = consonants
[idx4
];
433 ((((u_int
)(dgst_raw
[2 * i
])) * 7) +
434 ((u_int
)(dgst_raw
[(2 * i
) + 1])))) % 36;
440 retval
[j
++] = vowels
[idx0
];
441 retval
[j
++] = consonants
[idx1
];
442 retval
[j
++] = vowels
[idx2
];
451 * Draw an ASCII-Art representing the fingerprint so human brain can
452 * profit from its built-in pattern recognition ability.
453 * This technique is called "random art" and can be found in some
454 * scientific publications like this original paper:
456 * "Hash Visualization: a New Technique to improve Real-World Security",
457 * Perrig A. and Song D., 1999, International Workshop on Cryptographic
458 * Techniques and E-Commerce (CrypTEC '99)
459 * sparrow.ece.cmu.edu/~adrian/projects/validation/validation.pdf
461 * The subject came up in a talk by Dan Kaminsky, too.
463 * If you see the picture is different, the key is different.
464 * If the picture looks the same, you still know nothing.
466 * The algorithm used here is a worm crawling over a discrete plane,
467 * leaving a trace (augmenting the field) everywhere it goes.
468 * Movement is taken from dgst_raw 2bit-wise. Bumping into walls
469 * makes the respective movement vector be ignored for this turn.
470 * Graphs are not unambiguous, because circles in graphs can be
471 * walked in either direction.
475 * Field sizes for the random art. Have to be odd, so the starting point
476 * can be in the exact middle of the picture, and FLDBASE should be >=8 .
477 * Else pictures would be too dense, and drawing the frame would
478 * fail, too, because the key type would not fit in anymore.
481 #define FLDSIZE_Y (FLDBASE + 1)
482 #define FLDSIZE_X (FLDBASE * 2 + 1)
484 key_fingerprint_randomart(u_char
*dgst_raw
, u_int dgst_raw_len
, const Key
*k
)
487 * Chars to be used after each other every time the worm
488 * intersects with itself. Matter of taste.
490 char *augmentation_string
= " .o+=*BOX@%&#/^SE";
492 u_char field
[FLDSIZE_X
][FLDSIZE_Y
];
495 size_t len
= strlen(augmentation_string
) - 1;
497 retval
= xcalloc(1, (FLDSIZE_X
+ 3) * (FLDSIZE_Y
+ 2));
499 /* initialize field */
500 memset(field
, 0, FLDSIZE_X
* FLDSIZE_Y
* sizeof(char));
504 /* process raw key */
505 for (i
= 0; i
< dgst_raw_len
; i
++) {
507 /* each byte conveys four 2-bit move commands */
509 for (b
= 0; b
< 4; b
++) {
510 /* evaluate 2 bit, rest is shifted later */
511 x
+= (input
& 0x1) ? 1 : -1;
512 y
+= (input
& 0x2) ? 1 : -1;
514 /* assure we are still in bounds */
517 x
= MIN(x
, FLDSIZE_X
- 1);
518 y
= MIN(y
, FLDSIZE_Y
- 1);
520 /* augment the field */
521 if (field
[x
][y
] < len
- 2)
527 /* mark starting point and end point*/
528 field
[FLDSIZE_X
/ 2][FLDSIZE_Y
/ 2] = len
- 1;
532 snprintf(retval
, FLDSIZE_X
, "+--[%4s %4u]", key_type(k
), key_size(k
));
533 p
= strchr(retval
, '\0');
535 /* output upper border */
536 for (i
= p
- retval
- 1; i
< FLDSIZE_X
; i
++)
542 for (y
= 0; y
< FLDSIZE_Y
; y
++) {
544 for (x
= 0; x
< FLDSIZE_X
; x
++)
545 *p
++ = augmentation_string
[MIN(field
[x
][y
], len
)];
550 /* output lower border */
552 for (i
= 0; i
< FLDSIZE_X
; i
++)
560 key_fingerprint(Key
*k
, enum fp_type dgst_type
, enum fp_rep dgst_rep
)
566 dgst_raw
= key_fingerprint_raw(k
, dgst_type
, &dgst_raw_len
);
568 fatal("key_fingerprint: null from key_fingerprint_raw()");
571 retval
= key_fingerprint_hex(dgst_raw
, dgst_raw_len
);
573 case SSH_FP_BUBBLEBABBLE
:
574 retval
= key_fingerprint_bubblebabble(dgst_raw
, dgst_raw_len
);
576 case SSH_FP_RANDOMART
:
577 retval
= key_fingerprint_randomart(dgst_raw
, dgst_raw_len
, k
);
580 fatal("key_fingerprint: bad digest representation %d",
584 memset(dgst_raw
, 0, dgst_raw_len
);
590 * Reads a multiple-precision integer in decimal from the buffer, and advances
591 * the pointer. The integer must already be initialized. This function is
592 * permitted to modify the buffer. This leaves *cpp to point just beyond the
593 * last processed (and maybe modified) character. Note that this may modify
594 * the buffer containing the number.
597 read_bignum(char **cpp
, BIGNUM
* value
)
602 /* Skip any leading whitespace. */
603 for (; *cp
== ' ' || *cp
== '\t'; cp
++)
606 /* Check that it begins with a decimal digit. */
607 if (*cp
< '0' || *cp
> '9')
610 /* Save starting position. */
613 /* Move forward until all decimal digits skipped. */
614 for (; *cp
>= '0' && *cp
<= '9'; cp
++)
617 /* Save the old terminating character, and replace it by \0. */
621 /* Parse the number. */
622 if (BN_dec2bn(&value
, *cpp
) == 0)
625 /* Restore old terminating character. */
628 /* Move beyond the number and return success. */
634 write_bignum(FILE *f
, BIGNUM
*num
)
636 char *buf
= BN_bn2dec(num
);
638 error("write_bignum: BN_bn2dec() failed");
641 fprintf(f
, " %s", buf
);
646 /* returns 1 ok, -1 error */
648 key_read(Key
*ret
, char **cpp
)
653 int len
, n
, type
, curve_nid
= -1;
661 /* Get number of bits. */
662 if (*cp
< '0' || *cp
> '9')
663 return -1; /* Bad bit count... */
664 for (bits
= 0; *cp
>= '0' && *cp
<= '9'; cp
++)
665 bits
= 10 * bits
+ *cp
- '0';
669 /* Get public exponent, public modulus. */
670 if (!read_bignum(cpp
, ret
->rsa
->e
))
672 if (!read_bignum(cpp
, ret
->rsa
->n
))
674 /* validate the claimed number of bits */
675 if ((u_int
)BN_num_bits(ret
->rsa
->n
) != bits
) {
676 verbose("key_read: claimed key size %d does not match "
677 "actual %d", bits
, BN_num_bits(ret
->rsa
->n
));
686 case KEY_DSA_CERT_V00
:
687 case KEY_RSA_CERT_V00
:
691 space
= strchr(cp
, ' ');
693 debug3("key_read: missing whitespace");
697 type
= key_type_from_name(cp
);
698 if (key_type_plain(type
) == KEY_ECDSA
&&
699 (curve_nid
= key_ecdsa_nid_from_name(cp
)) == -1) {
700 debug("key_read: invalid curve");
704 if (type
== KEY_UNSPEC
) {
705 debug3("key_read: missing keytype");
710 debug3("key_read: short string");
713 if (ret
->type
== KEY_UNSPEC
) {
715 } else if (ret
->type
!= type
) {
716 /* is a key, but different type */
717 debug3("key_read: type mismatch");
722 n
= uudecode(cp
, blob
, len
);
724 error("key_read: uudecode %s failed", cp
);
728 k
= key_from_blob(blob
, (u_int
)n
);
731 error("key_read: key_from_blob %s failed", cp
);
734 if (k
->type
!= type
) {
735 error("key_read: type mismatch: encoding error");
739 if (key_type_plain(type
) == KEY_ECDSA
&&
740 curve_nid
!= k
->ecdsa_nid
) {
741 error("key_read: type mismatch: EC curve mismatch");
746 if (key_is_cert(ret
)) {
747 if (!key_is_cert(k
)) {
748 error("key_read: loaded key is not a cert");
752 if (ret
->cert
!= NULL
)
753 cert_free(ret
->cert
);
757 if (key_type_plain(ret
->type
) == KEY_RSA
) {
758 if (ret
->rsa
!= NULL
)
763 RSA_print_fp(stderr
, ret
->rsa
, 8);
766 if (key_type_plain(ret
->type
) == KEY_DSA
) {
767 if (ret
->dsa
!= NULL
)
772 DSA_print_fp(stderr
, ret
->dsa
, 8);
775 if (key_type_plain(ret
->type
) == KEY_ECDSA
) {
776 if (ret
->ecdsa
!= NULL
)
777 EC_KEY_free(ret
->ecdsa
);
778 ret
->ecdsa
= k
->ecdsa
;
779 ret
->ecdsa_nid
= k
->ecdsa_nid
;
783 key_dump_ec_key(ret
->ecdsa
);
791 /* advance cp: skip whitespace and data */
792 while (*cp
== ' ' || *cp
== '\t')
794 while (*cp
!= '\0' && *cp
!= ' ' && *cp
!= '\t')
799 fatal("key_read: bad key type: %d", ret
->type
);
806 key_write(const Key
*key
, FILE *f
)
813 if (key_is_cert(key
)) {
814 if (key
->cert
== NULL
) {
815 error("%s: no cert data", __func__
);
818 if (buffer_len(&key
->cert
->certblob
) == 0) {
819 error("%s: no signed certificate blob", __func__
);
826 if (key
->rsa
== NULL
)
828 /* size of modulus 'n' */
829 bits
= BN_num_bits(key
->rsa
->n
);
830 fprintf(f
, "%u", bits
);
831 if (write_bignum(f
, key
->rsa
->e
) &&
832 write_bignum(f
, key
->rsa
->n
))
834 error("key_write: failed for RSA key");
837 case KEY_DSA_CERT_V00
:
839 if (key
->dsa
== NULL
)
844 if (key
->ecdsa
== NULL
)
848 case KEY_RSA_CERT_V00
:
850 if (key
->rsa
== NULL
)
857 key_to_blob(key
, &blob
, &len
);
859 n
= uuencode(blob
, len
, uu
, 2*len
);
861 fprintf(f
, "%s %s", key_ssh_name(key
), uu
);
871 key_type(const Key
*k
)
882 case KEY_RSA_CERT_V00
:
883 return "RSA-CERT-V00";
884 case KEY_DSA_CERT_V00
:
885 return "DSA-CERT-V00";
897 key_cert_type(const Key
*k
)
899 switch (k
->cert
->type
) {
900 case SSH2_CERT_TYPE_USER
:
902 case SSH2_CERT_TYPE_HOST
:
910 key_ssh_name_from_type_nid(int type
, int nid
)
917 case KEY_RSA_CERT_V00
:
918 return "ssh-rsa-cert-v00@openssh.com";
919 case KEY_DSA_CERT_V00
:
920 return "ssh-dss-cert-v00@openssh.com";
922 return "ssh-rsa-cert-v01@openssh.com";
924 return "ssh-dss-cert-v01@openssh.com";
927 case NID_X9_62_prime256v1
:
928 return "ecdsa-sha2-nistp256";
930 return "ecdsa-sha2-nistp384";
932 return "ecdsa-sha2-nistp521";
939 case NID_X9_62_prime256v1
:
940 return "ecdsa-sha2-nistp256-cert-v01@openssh.com";
942 return "ecdsa-sha2-nistp384-cert-v01@openssh.com";
944 return "ecdsa-sha2-nistp521-cert-v01@openssh.com";
950 return "ssh-unknown";
954 key_ssh_name(const Key
*k
)
956 return key_ssh_name_from_type_nid(k
->type
, k
->ecdsa_nid
);
960 key_ssh_name_plain(const Key
*k
)
962 return key_ssh_name_from_type_nid(key_type_plain(k
->type
),
967 key_size(const Key
*k
)
972 case KEY_RSA_CERT_V00
:
974 return BN_num_bits(k
->rsa
->n
);
976 case KEY_DSA_CERT_V00
:
978 return BN_num_bits(k
->dsa
->p
);
981 switch (k
->ecdsa_nid
) {
982 case NID_X9_62_prime256v1
:
997 rsa_generate_private_key(u_int bits
)
1001 private = RSA_generate_key(bits
, RSA_F4
, NULL
, NULL
);
1002 if (private == NULL
)
1003 fatal("rsa_generate_private_key: key generation failed.");
1008 dsa_generate_private_key(u_int bits
)
1010 DSA
*private = DSA_generate_parameters(bits
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
1012 if (private == NULL
)
1013 fatal("dsa_generate_private_key: DSA_generate_parameters failed");
1014 if (!DSA_generate_key(private))
1015 fatal("dsa_generate_private_key: DSA_generate_key failed.");
1016 if (private == NULL
)
1017 fatal("dsa_generate_private_key: NULL.");
1022 key_ecdsa_bits_to_nid(int bits
)
1026 return NID_X9_62_prime256v1
;
1028 return NID_secp384r1
;
1030 return NID_secp521r1
;
1037 * This is horrid, but OpenSSL's PEM_read_PrivateKey seems not to restore
1038 * the EC_GROUP nid when loading a key...
1041 key_ecdsa_group_to_nid(const EC_GROUP
*g
)
1045 NID_X9_62_prime256v1
,
1053 if ((bnctx
= BN_CTX_new()) == NULL
)
1054 fatal("%s: BN_CTX_new() failed", __func__
);
1055 for (i
= 0; nids
[i
] != -1; i
++) {
1056 if ((eg
= EC_GROUP_new_by_curve_name(nids
[i
])) == NULL
)
1057 fatal("%s: EC_GROUP_new_by_curve_name failed",
1059 if (EC_GROUP_cmp(g
, eg
, bnctx
) == 0) {
1066 debug3("%s: nid = %d", __func__
, nids
[i
]);
1071 ecdsa_generate_private_key(u_int bits
, int *nid
)
1075 if ((*nid
= key_ecdsa_bits_to_nid(bits
)) == -1)
1076 fatal("%s: invalid key length", __func__
);
1077 if ((private = EC_KEY_new_by_curve_name(*nid
)) == NULL
)
1078 fatal("%s: EC_KEY_new_by_curve_name failed", __func__
);
1079 if (EC_KEY_generate_key(private) != 1)
1080 fatal("%s: EC_KEY_generate_key failed", __func__
);
1085 key_generate(int type
, u_int bits
)
1087 Key
*k
= key_new(KEY_UNSPEC
);
1090 k
->dsa
= dsa_generate_private_key(bits
);
1093 k
->ecdsa
= ecdsa_generate_private_key(bits
, &k
->ecdsa_nid
);
1097 k
->rsa
= rsa_generate_private_key(bits
);
1099 case KEY_RSA_CERT_V00
:
1100 case KEY_DSA_CERT_V00
:
1103 fatal("key_generate: cert keys cannot be generated directly");
1105 fatal("key_generate: unknown type %d", type
);
1112 key_cert_copy(const Key
*from_key
, struct Key
*to_key
)
1115 const struct KeyCert
*from
;
1118 if (to_key
->cert
!= NULL
) {
1119 cert_free(to_key
->cert
);
1120 to_key
->cert
= NULL
;
1123 if ((from
= from_key
->cert
) == NULL
)
1126 to
= to_key
->cert
= cert_new();
1128 buffer_append(&to
->certblob
, buffer_ptr(&from
->certblob
),
1129 buffer_len(&from
->certblob
));
1131 buffer_append(&to
->critical
,
1132 buffer_ptr(&from
->critical
), buffer_len(&from
->critical
));
1133 buffer_append(&to
->extensions
,
1134 buffer_ptr(&from
->extensions
), buffer_len(&from
->extensions
));
1136 to
->serial
= from
->serial
;
1137 to
->type
= from
->type
;
1138 to
->key_id
= from
->key_id
== NULL
? NULL
: xstrdup(from
->key_id
);
1139 to
->valid_after
= from
->valid_after
;
1140 to
->valid_before
= from
->valid_before
;
1141 to
->signature_key
= from
->signature_key
== NULL
?
1142 NULL
: key_from_private(from
->signature_key
);
1144 to
->nprincipals
= from
->nprincipals
;
1145 if (to
->nprincipals
> CERT_MAX_PRINCIPALS
)
1146 fatal("%s: nprincipals (%u) > CERT_MAX_PRINCIPALS (%u)",
1147 __func__
, to
->nprincipals
, CERT_MAX_PRINCIPALS
);
1148 if (to
->nprincipals
> 0) {
1149 to
->principals
= xcalloc(from
->nprincipals
,
1150 sizeof(*to
->principals
));
1151 for (i
= 0; i
< to
->nprincipals
; i
++)
1152 to
->principals
[i
] = xstrdup(from
->principals
[i
]);
1157 key_from_private(const Key
*k
)
1162 case KEY_DSA_CERT_V00
:
1164 n
= key_new(k
->type
);
1165 if ((BN_copy(n
->dsa
->p
, k
->dsa
->p
) == NULL
) ||
1166 (BN_copy(n
->dsa
->q
, k
->dsa
->q
) == NULL
) ||
1167 (BN_copy(n
->dsa
->g
, k
->dsa
->g
) == NULL
) ||
1168 (BN_copy(n
->dsa
->pub_key
, k
->dsa
->pub_key
) == NULL
))
1169 fatal("key_from_private: BN_copy failed");
1172 case KEY_ECDSA_CERT
:
1173 n
= key_new(k
->type
);
1174 n
->ecdsa_nid
= k
->ecdsa_nid
;
1175 if ((n
->ecdsa
= EC_KEY_new_by_curve_name(k
->ecdsa_nid
)) == NULL
)
1176 fatal("%s: EC_KEY_new_by_curve_name failed", __func__
);
1177 if (EC_KEY_set_public_key(n
->ecdsa
,
1178 EC_KEY_get0_public_key(k
->ecdsa
)) != 1)
1179 fatal("%s: EC_KEY_set_public_key failed", __func__
);
1183 case KEY_RSA_CERT_V00
:
1185 n
= key_new(k
->type
);
1186 if ((BN_copy(n
->rsa
->n
, k
->rsa
->n
) == NULL
) ||
1187 (BN_copy(n
->rsa
->e
, k
->rsa
->e
) == NULL
))
1188 fatal("key_from_private: BN_copy failed");
1191 fatal("key_from_private: unknown type %d", k
->type
);
1195 key_cert_copy(k
, n
);
1200 key_type_from_name(char *name
)
1202 if (strcmp(name
, "rsa1") == 0) {
1204 } else if (strcmp(name
, "rsa") == 0) {
1206 } else if (strcmp(name
, "dsa") == 0) {
1208 } else if (strcmp(name
, "ssh-rsa") == 0) {
1210 } else if (strcmp(name
, "ssh-dss") == 0) {
1212 } else if (strcmp(name
, "ecdsa") == 0 ||
1213 strcmp(name
, "ecdsa-sha2-nistp256") == 0 ||
1214 strcmp(name
, "ecdsa-sha2-nistp384") == 0 ||
1215 strcmp(name
, "ecdsa-sha2-nistp521") == 0) {
1217 } else if (strcmp(name
, "ssh-rsa-cert-v00@openssh.com") == 0) {
1218 return KEY_RSA_CERT_V00
;
1219 } else if (strcmp(name
, "ssh-dss-cert-v00@openssh.com") == 0) {
1220 return KEY_DSA_CERT_V00
;
1221 } else if (strcmp(name
, "ssh-rsa-cert-v01@openssh.com") == 0) {
1222 return KEY_RSA_CERT
;
1223 } else if (strcmp(name
, "ssh-dss-cert-v01@openssh.com") == 0) {
1224 return KEY_DSA_CERT
;
1225 } else if (strcmp(name
, "ecdsa-sha2-nistp256-cert-v01@openssh.com") == 0 ||
1226 strcmp(name
, "ecdsa-sha2-nistp384-cert-v01@openssh.com") == 0 ||
1227 strcmp(name
, "ecdsa-sha2-nistp521-cert-v01@openssh.com") == 0)
1228 return KEY_ECDSA_CERT
;
1230 debug2("key_type_from_name: unknown key type '%s'", name
);
1235 key_ecdsa_nid_from_name(const char *name
)
1237 if (strcmp(name
, "ecdsa-sha2-nistp256") == 0 ||
1238 strcmp(name
, "ecdsa-sha2-nistp256-cert-v01@openssh.com") == 0)
1239 return NID_X9_62_prime256v1
;
1240 if (strcmp(name
, "ecdsa-sha2-nistp384") == 0 ||
1241 strcmp(name
, "ecdsa-sha2-nistp384-cert-v01@openssh.com") == 0)
1242 return NID_secp384r1
;
1243 if (strcmp(name
, "ecdsa-sha2-nistp521") == 0 ||
1244 strcmp(name
, "ecdsa-sha2-nistp521-cert-v01@openssh.com") == 0)
1245 return NID_secp521r1
;
1247 debug2("%s: unknown/non-ECDSA key type '%s'", __func__
, name
);
1252 key_names_valid2(const char *names
)
1256 if (names
== NULL
|| strcmp(names
, "") == 0)
1258 s
= cp
= xstrdup(names
);
1259 for ((p
= strsep(&cp
, ",")); p
&& *p
!= '\0';
1260 (p
= strsep(&cp
, ","))) {
1261 switch (key_type_from_name(p
)) {
1268 debug3("key names ok: [%s]", names
);
1274 cert_parse(Buffer
*b
, Key
*key
, const u_char
*blob
, u_int blen
)
1276 u_char
*principals
, *critical
, *exts
, *sig_key
, *sig
;
1277 u_int signed_len
, plen
, clen
, sklen
, slen
, kidlen
, elen
;
1281 int v00
= key
->type
== KEY_DSA_CERT_V00
||
1282 key
->type
== KEY_RSA_CERT_V00
;
1286 /* Copy the entire key blob for verification and later serialisation */
1287 buffer_append(&key
->cert
->certblob
, blob
, blen
);
1289 elen
= 0; /* Not touched for v00 certs */
1290 principals
= exts
= critical
= sig_key
= sig
= NULL
;
1291 if ((!v00
&& buffer_get_int64_ret(&key
->cert
->serial
, b
) != 0) ||
1292 buffer_get_int_ret(&key
->cert
->type
, b
) != 0 ||
1293 (key
->cert
->key_id
= buffer_get_cstring_ret(b
, &kidlen
)) == NULL
||
1294 (principals
= buffer_get_string_ret(b
, &plen
)) == NULL
||
1295 buffer_get_int64_ret(&key
->cert
->valid_after
, b
) != 0 ||
1296 buffer_get_int64_ret(&key
->cert
->valid_before
, b
) != 0 ||
1297 (critical
= buffer_get_string_ret(b
, &clen
)) == NULL
||
1298 (!v00
&& (exts
= buffer_get_string_ret(b
, &elen
)) == NULL
) ||
1299 (v00
&& buffer_get_string_ptr_ret(b
, NULL
) == NULL
) || /* nonce */
1300 buffer_get_string_ptr_ret(b
, NULL
) == NULL
|| /* reserved */
1301 (sig_key
= buffer_get_string_ret(b
, &sklen
)) == NULL
) {
1302 error("%s: parse error", __func__
);
1306 if (kidlen
!= strlen(key
->cert
->key_id
)) {
1307 error("%s: key ID contains \\0 character", __func__
);
1311 /* Signature is left in the buffer so we can calculate this length */
1312 signed_len
= buffer_len(&key
->cert
->certblob
) - buffer_len(b
);
1314 if ((sig
= buffer_get_string_ret(b
, &slen
)) == NULL
) {
1315 error("%s: parse error", __func__
);
1319 if (key
->cert
->type
!= SSH2_CERT_TYPE_USER
&&
1320 key
->cert
->type
!= SSH2_CERT_TYPE_HOST
) {
1321 error("Unknown certificate type %u", key
->cert
->type
);
1325 buffer_append(&tmp
, principals
, plen
);
1326 while (buffer_len(&tmp
) > 0) {
1327 if (key
->cert
->nprincipals
>= CERT_MAX_PRINCIPALS
) {
1328 error("%s: Too many principals", __func__
);
1331 if ((principal
= buffer_get_cstring_ret(&tmp
, &plen
)) == NULL
) {
1332 error("%s: Principals data invalid", __func__
);
1335 key
->cert
->principals
= xrealloc(key
->cert
->principals
,
1336 key
->cert
->nprincipals
+ 1, sizeof(*key
->cert
->principals
));
1337 key
->cert
->principals
[key
->cert
->nprincipals
++] = principal
;
1342 buffer_append(&key
->cert
->critical
, critical
, clen
);
1343 buffer_append(&tmp
, critical
, clen
);
1344 /* validate structure */
1345 while (buffer_len(&tmp
) != 0) {
1346 if (buffer_get_string_ptr_ret(&tmp
, NULL
) == NULL
||
1347 buffer_get_string_ptr_ret(&tmp
, NULL
) == NULL
) {
1348 error("%s: critical option data invalid", __func__
);
1354 buffer_append(&key
->cert
->extensions
, exts
, elen
);
1355 buffer_append(&tmp
, exts
, elen
);
1356 /* validate structure */
1357 while (buffer_len(&tmp
) != 0) {
1358 if (buffer_get_string_ptr_ret(&tmp
, NULL
) == NULL
||
1359 buffer_get_string_ptr_ret(&tmp
, NULL
) == NULL
) {
1360 error("%s: extension data invalid", __func__
);
1366 if ((key
->cert
->signature_key
= key_from_blob(sig_key
,
1368 error("%s: Signature key invalid", __func__
);
1371 if (key
->cert
->signature_key
->type
!= KEY_RSA
&&
1372 key
->cert
->signature_key
->type
!= KEY_DSA
&&
1373 key
->cert
->signature_key
->type
!= KEY_ECDSA
) {
1374 error("%s: Invalid signature key type %s (%d)", __func__
,
1375 key_type(key
->cert
->signature_key
),
1376 key
->cert
->signature_key
->type
);
1380 switch (key_verify(key
->cert
->signature_key
, sig
, slen
,
1381 buffer_ptr(&key
->cert
->certblob
), signed_len
)) {
1384 break; /* Good signature */
1386 error("%s: Invalid signature on certificate", __func__
);
1389 error("%s: Certificate signature verification failed",
1396 if (principals
!= NULL
)
1398 if (critical
!= NULL
)
1402 if (sig_key
!= NULL
)
1410 key_from_blob(const u_char
*blob
, u_int blen
)
1413 int rlen
, type
, nid
= -1;
1414 char *ktype
= NULL
, *curve
= NULL
;
1419 dump_base64(stderr
, blob
, blen
);
1422 buffer_append(&b
, blob
, blen
);
1423 if ((ktype
= buffer_get_cstring_ret(&b
, NULL
)) == NULL
) {
1424 error("key_from_blob: can't read key type");
1428 type
= key_type_from_name(ktype
);
1429 if (key_type_plain(type
) == KEY_ECDSA
)
1430 nid
= key_ecdsa_nid_from_name(ktype
);
1434 (void)buffer_get_string_ptr_ret(&b
, NULL
); /* Skip nonce */
1437 case KEY_RSA_CERT_V00
:
1438 key
= key_new(type
);
1439 if (buffer_get_bignum2_ret(&b
, key
->rsa
->e
) == -1 ||
1440 buffer_get_bignum2_ret(&b
, key
->rsa
->n
) == -1) {
1441 error("key_from_blob: can't read rsa key");
1448 RSA_print_fp(stderr
, key
->rsa
, 8);
1452 (void)buffer_get_string_ptr_ret(&b
, NULL
); /* Skip nonce */
1455 case KEY_DSA_CERT_V00
:
1456 key
= key_new(type
);
1457 if (buffer_get_bignum2_ret(&b
, key
->dsa
->p
) == -1 ||
1458 buffer_get_bignum2_ret(&b
, key
->dsa
->q
) == -1 ||
1459 buffer_get_bignum2_ret(&b
, key
->dsa
->g
) == -1 ||
1460 buffer_get_bignum2_ret(&b
, key
->dsa
->pub_key
) == -1) {
1461 error("key_from_blob: can't read dsa key");
1465 DSA_print_fp(stderr
, key
->dsa
, 8);
1468 case KEY_ECDSA_CERT
:
1469 (void)buffer_get_string_ptr_ret(&b
, NULL
); /* Skip nonce */
1472 key
= key_new(type
);
1473 key
->ecdsa_nid
= nid
;
1474 if ((curve
= buffer_get_string_ret(&b
, NULL
)) == NULL
) {
1475 error("key_from_blob: can't read ecdsa curve");
1478 if (key
->ecdsa_nid
!= key_curve_name_to_nid(curve
)) {
1479 error("key_from_blob: ecdsa curve doesn't match type");
1482 if (key
->ecdsa
!= NULL
)
1483 EC_KEY_free(key
->ecdsa
);
1484 if ((key
->ecdsa
= EC_KEY_new_by_curve_name(key
->ecdsa_nid
))
1486 fatal("key_from_blob: EC_KEY_new_by_curve_name failed");
1487 if ((q
= EC_POINT_new(EC_KEY_get0_group(key
->ecdsa
))) == NULL
)
1488 fatal("key_from_blob: EC_POINT_new failed");
1489 if (buffer_get_ecpoint_ret(&b
, EC_KEY_get0_group(key
->ecdsa
),
1491 error("key_from_blob: can't read ecdsa key point");
1494 if (key_ec_validate_public(EC_KEY_get0_group(key
->ecdsa
),
1497 if (EC_KEY_set_public_key(key
->ecdsa
, q
) != 1)
1498 fatal("key_from_blob: EC_KEY_set_public_key failed");
1500 key_dump_ec_point(EC_KEY_get0_group(key
->ecdsa
), q
);
1504 key
= key_new(type
);
1507 error("key_from_blob: cannot handle type %s", ktype
);
1510 if (key_is_cert(key
) && cert_parse(&b
, key
, blob
, blen
) == -1) {
1511 error("key_from_blob: can't parse cert data");
1514 rlen
= buffer_len(&b
);
1515 if (key
!= NULL
&& rlen
!= 0)
1516 error("key_from_blob: remaining bytes in key blob %d", rlen
);
1529 key_to_blob(const Key
*key
, u_char
**blobp
, u_int
*lenp
)
1535 error("key_to_blob: key == NULL");
1539 switch (key
->type
) {
1540 case KEY_DSA_CERT_V00
:
1541 case KEY_RSA_CERT_V00
:
1543 case KEY_ECDSA_CERT
:
1545 /* Use the existing blob */
1546 buffer_append(&b
, buffer_ptr(&key
->cert
->certblob
),
1547 buffer_len(&key
->cert
->certblob
));
1550 buffer_put_cstring(&b
, key_ssh_name(key
));
1551 buffer_put_bignum2(&b
, key
->dsa
->p
);
1552 buffer_put_bignum2(&b
, key
->dsa
->q
);
1553 buffer_put_bignum2(&b
, key
->dsa
->g
);
1554 buffer_put_bignum2(&b
, key
->dsa
->pub_key
);
1557 buffer_put_cstring(&b
, key_ssh_name(key
));
1558 buffer_put_cstring(&b
, key_curve_nid_to_name(key
->ecdsa_nid
));
1559 buffer_put_ecpoint(&b
, EC_KEY_get0_group(key
->ecdsa
),
1560 EC_KEY_get0_public_key(key
->ecdsa
));
1563 buffer_put_cstring(&b
, key_ssh_name(key
));
1564 buffer_put_bignum2(&b
, key
->rsa
->e
);
1565 buffer_put_bignum2(&b
, key
->rsa
->n
);
1568 error("key_to_blob: unsupported key type %d", key
->type
);
1572 len
= buffer_len(&b
);
1575 if (blobp
!= NULL
) {
1576 *blobp
= xmalloc(len
);
1577 memcpy(*blobp
, buffer_ptr(&b
), len
);
1579 memset(buffer_ptr(&b
), 0, len
);
1587 u_char
**sigp
, u_int
*lenp
,
1588 const u_char
*data
, u_int datalen
)
1590 switch (key
->type
) {
1591 case KEY_DSA_CERT_V00
:
1594 return ssh_dss_sign(key
, sigp
, lenp
, data
, datalen
);
1595 case KEY_ECDSA_CERT
:
1597 return ssh_ecdsa_sign(key
, sigp
, lenp
, data
, datalen
);
1598 case KEY_RSA_CERT_V00
:
1601 return ssh_rsa_sign(key
, sigp
, lenp
, data
, datalen
);
1603 error("key_sign: invalid key type %d", key
->type
);
1609 * key_verify returns 1 for a correct signature, 0 for an incorrect signature
1615 const u_char
*signature
, u_int signaturelen
,
1616 const u_char
*data
, u_int datalen
)
1618 if (signaturelen
== 0)
1621 switch (key
->type
) {
1622 case KEY_DSA_CERT_V00
:
1625 return ssh_dss_verify(key
, signature
, signaturelen
, data
, datalen
);
1626 case KEY_ECDSA_CERT
:
1628 return ssh_ecdsa_verify(key
, signature
, signaturelen
, data
, datalen
);
1629 case KEY_RSA_CERT_V00
:
1632 return ssh_rsa_verify(key
, signature
, signaturelen
, data
, datalen
);
1634 error("key_verify: invalid key type %d", key
->type
);
1639 /* Converts a private to a public key */
1641 key_demote(const Key
*k
)
1645 pk
= xcalloc(1, sizeof(*pk
));
1647 pk
->flags
= k
->flags
;
1648 pk
->ecdsa_nid
= k
->ecdsa_nid
;
1654 case KEY_RSA_CERT_V00
:
1656 key_cert_copy(k
, pk
);
1660 if ((pk
->rsa
= RSA_new()) == NULL
)
1661 fatal("key_demote: RSA_new failed");
1662 if ((pk
->rsa
->e
= BN_dup(k
->rsa
->e
)) == NULL
)
1663 fatal("key_demote: BN_dup failed");
1664 if ((pk
->rsa
->n
= BN_dup(k
->rsa
->n
)) == NULL
)
1665 fatal("key_demote: BN_dup failed");
1667 case KEY_DSA_CERT_V00
:
1669 key_cert_copy(k
, pk
);
1672 if ((pk
->dsa
= DSA_new()) == NULL
)
1673 fatal("key_demote: DSA_new failed");
1674 if ((pk
->dsa
->p
= BN_dup(k
->dsa
->p
)) == NULL
)
1675 fatal("key_demote: BN_dup failed");
1676 if ((pk
->dsa
->q
= BN_dup(k
->dsa
->q
)) == NULL
)
1677 fatal("key_demote: BN_dup failed");
1678 if ((pk
->dsa
->g
= BN_dup(k
->dsa
->g
)) == NULL
)
1679 fatal("key_demote: BN_dup failed");
1680 if ((pk
->dsa
->pub_key
= BN_dup(k
->dsa
->pub_key
)) == NULL
)
1681 fatal("key_demote: BN_dup failed");
1683 case KEY_ECDSA_CERT
:
1684 key_cert_copy(k
, pk
);
1687 if ((pk
->ecdsa
= EC_KEY_new_by_curve_name(pk
->ecdsa_nid
)) == NULL
)
1688 fatal("key_demote: EC_KEY_new_by_curve_name failed");
1689 if (EC_KEY_set_public_key(pk
->ecdsa
,
1690 EC_KEY_get0_public_key(k
->ecdsa
)) != 1)
1691 fatal("key_demote: EC_KEY_set_public_key failed");
1694 fatal("key_free: bad key type %d", k
->type
);
1702 key_is_cert(const Key
*k
)
1707 case KEY_RSA_CERT_V00
:
1708 case KEY_DSA_CERT_V00
:
1711 case KEY_ECDSA_CERT
:
1718 /* Return the cert-less equivalent to a certified key type */
1720 key_type_plain(int type
)
1723 case KEY_RSA_CERT_V00
:
1726 case KEY_DSA_CERT_V00
:
1729 case KEY_ECDSA_CERT
:
1736 /* Convert a KEY_RSA or KEY_DSA to their _CERT equivalent */
1738 key_to_certified(Key
*k
, int legacy
)
1742 k
->cert
= cert_new();
1743 k
->type
= legacy
? KEY_RSA_CERT_V00
: KEY_RSA_CERT
;
1746 k
->cert
= cert_new();
1747 k
->type
= legacy
? KEY_DSA_CERT_V00
: KEY_DSA_CERT
;
1750 k
->cert
= cert_new();
1751 k
->type
= KEY_ECDSA_CERT
;
1754 error("%s: key has incorrect type %s", __func__
, key_type(k
));
1759 /* Convert a KEY_RSA_CERT or KEY_DSA_CERT to their raw key equivalent */
1761 key_drop_cert(Key
*k
)
1764 case KEY_RSA_CERT_V00
:
1769 case KEY_DSA_CERT_V00
:
1774 case KEY_ECDSA_CERT
:
1776 k
->type
= KEY_ECDSA
;
1779 error("%s: key has incorrect type %s", __func__
, key_type(k
));
1785 * Sign a KEY_RSA_CERT, KEY_DSA_CERT or KEY_ECDSA_CERT, (re-)generating
1786 * the signed certblob
1789 key_certify(Key
*k
, Key
*ca
)
1792 u_char
*ca_blob
, *sig_blob
, nonce
[32];
1793 u_int i
, ca_len
, sig_len
;
1795 if (k
->cert
== NULL
) {
1796 error("%s: key lacks cert info", __func__
);
1800 if (!key_is_cert(k
)) {
1801 error("%s: certificate has unknown type %d", __func__
,
1806 if (ca
->type
!= KEY_RSA
&& ca
->type
!= KEY_DSA
&&
1807 ca
->type
!= KEY_ECDSA
) {
1808 error("%s: CA key has unsupported type %s", __func__
,
1813 key_to_blob(ca
, &ca_blob
, &ca_len
);
1815 buffer_clear(&k
->cert
->certblob
);
1816 buffer_put_cstring(&k
->cert
->certblob
, key_ssh_name(k
));
1818 /* -v01 certs put nonce first */
1819 if (!key_cert_is_legacy(k
)) {
1820 arc4random_buf(&nonce
, sizeof(nonce
));
1821 buffer_put_string(&k
->cert
->certblob
, nonce
, sizeof(nonce
));
1825 case KEY_DSA_CERT_V00
:
1827 buffer_put_bignum2(&k
->cert
->certblob
, k
->dsa
->p
);
1828 buffer_put_bignum2(&k
->cert
->certblob
, k
->dsa
->q
);
1829 buffer_put_bignum2(&k
->cert
->certblob
, k
->dsa
->g
);
1830 buffer_put_bignum2(&k
->cert
->certblob
, k
->dsa
->pub_key
);
1832 case KEY_ECDSA_CERT
:
1833 buffer_put_cstring(&k
->cert
->certblob
,
1834 key_curve_nid_to_name(k
->ecdsa_nid
));
1835 buffer_put_ecpoint(&k
->cert
->certblob
,
1836 EC_KEY_get0_group(k
->ecdsa
),
1837 EC_KEY_get0_public_key(k
->ecdsa
));
1839 case KEY_RSA_CERT_V00
:
1841 buffer_put_bignum2(&k
->cert
->certblob
, k
->rsa
->e
);
1842 buffer_put_bignum2(&k
->cert
->certblob
, k
->rsa
->n
);
1845 error("%s: key has incorrect type %s", __func__
, key_type(k
));
1846 buffer_clear(&k
->cert
->certblob
);
1851 /* -v01 certs have a serial number next */
1852 if (!key_cert_is_legacy(k
))
1853 buffer_put_int64(&k
->cert
->certblob
, k
->cert
->serial
);
1855 buffer_put_int(&k
->cert
->certblob
, k
->cert
->type
);
1856 buffer_put_cstring(&k
->cert
->certblob
, k
->cert
->key_id
);
1858 buffer_init(&principals
);
1859 for (i
= 0; i
< k
->cert
->nprincipals
; i
++)
1860 buffer_put_cstring(&principals
, k
->cert
->principals
[i
]);
1861 buffer_put_string(&k
->cert
->certblob
, buffer_ptr(&principals
),
1862 buffer_len(&principals
));
1863 buffer_free(&principals
);
1865 buffer_put_int64(&k
->cert
->certblob
, k
->cert
->valid_after
);
1866 buffer_put_int64(&k
->cert
->certblob
, k
->cert
->valid_before
);
1867 buffer_put_string(&k
->cert
->certblob
,
1868 buffer_ptr(&k
->cert
->critical
), buffer_len(&k
->cert
->critical
));
1870 /* -v01 certs have non-critical options here */
1871 if (!key_cert_is_legacy(k
)) {
1872 buffer_put_string(&k
->cert
->certblob
,
1873 buffer_ptr(&k
->cert
->extensions
),
1874 buffer_len(&k
->cert
->extensions
));
1877 /* -v00 certs put the nonce at the end */
1878 if (key_cert_is_legacy(k
))
1879 buffer_put_string(&k
->cert
->certblob
, nonce
, sizeof(nonce
));
1881 buffer_put_string(&k
->cert
->certblob
, NULL
, 0); /* reserved */
1882 buffer_put_string(&k
->cert
->certblob
, ca_blob
, ca_len
);
1885 /* Sign the whole mess */
1886 if (key_sign(ca
, &sig_blob
, &sig_len
, buffer_ptr(&k
->cert
->certblob
),
1887 buffer_len(&k
->cert
->certblob
)) != 0) {
1888 error("%s: signature operation failed", __func__
);
1889 buffer_clear(&k
->cert
->certblob
);
1892 /* Append signature and we are done */
1893 buffer_put_string(&k
->cert
->certblob
, sig_blob
, sig_len
);
1900 key_cert_check_authority(const Key
*k
, int want_host
, int require_principal
,
1901 const char *name
, const char **reason
)
1903 u_int i
, principal_matches
;
1904 time_t now
= time(NULL
);
1907 if (k
->cert
->type
!= SSH2_CERT_TYPE_HOST
) {
1908 *reason
= "Certificate invalid: not a host certificate";
1912 if (k
->cert
->type
!= SSH2_CERT_TYPE_USER
) {
1913 *reason
= "Certificate invalid: not a user certificate";
1918 error("%s: system clock lies before epoch", __func__
);
1919 *reason
= "Certificate invalid: not yet valid";
1922 if ((u_int64_t
)now
< k
->cert
->valid_after
) {
1923 *reason
= "Certificate invalid: not yet valid";
1926 if ((u_int64_t
)now
>= k
->cert
->valid_before
) {
1927 *reason
= "Certificate invalid: expired";
1930 if (k
->cert
->nprincipals
== 0) {
1931 if (require_principal
) {
1932 *reason
= "Certificate lacks principal list";
1935 } else if (name
!= NULL
) {
1936 principal_matches
= 0;
1937 for (i
= 0; i
< k
->cert
->nprincipals
; i
++) {
1938 if (strcmp(name
, k
->cert
->principals
[i
]) == 0) {
1939 principal_matches
= 1;
1943 if (!principal_matches
) {
1944 *reason
= "Certificate invalid: name is not a listed "
1953 key_cert_is_legacy(Key
*k
)
1956 case KEY_DSA_CERT_V00
:
1957 case KEY_RSA_CERT_V00
:
1965 key_curve_name_to_nid(const char *name
)
1967 if (strcmp(name
, "nistp256") == 0)
1968 return NID_X9_62_prime256v1
;
1969 else if (strcmp(name
, "nistp384") == 0)
1970 return NID_secp384r1
;
1971 else if (strcmp(name
, "nistp521") == 0)
1972 return NID_secp521r1
;
1974 debug("%s: unsupported EC curve name \"%.100s\"", __func__
, name
);
1979 key_curve_nid_to_name(int nid
)
1981 if (nid
== NID_X9_62_prime256v1
)
1983 else if (nid
== NID_secp384r1
)
1985 else if (nid
== NID_secp521r1
)
1988 error("%s: unsupported EC curve nid %d", __func__
, nid
);
1993 key_ec_validate_public(const EC_GROUP
*group
, const EC_POINT
*public)
1996 EC_POINT
*nq
= NULL
;
1997 BIGNUM
*order
, *x
, *y
, *tmp
;
2000 if ((bnctx
= BN_CTX_new()) == NULL
)
2001 fatal("%s: BN_CTX_new failed", __func__
);
2002 BN_CTX_start(bnctx
);
2005 * We shouldn't ever hit this case because bignum_get_ecpoint()
2006 * refuses to load GF2m points.
2008 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group
)) !=
2009 NID_X9_62_prime_field
) {
2010 error("%s: group is not a prime field", __func__
);
2015 if (EC_POINT_is_at_infinity(group
, public)) {
2016 error("%s: received degenerate public key (infinity)",
2021 if ((x
= BN_CTX_get(bnctx
)) == NULL
||
2022 (y
= BN_CTX_get(bnctx
)) == NULL
||
2023 (order
= BN_CTX_get(bnctx
)) == NULL
||
2024 (tmp
= BN_CTX_get(bnctx
)) == NULL
)
2025 fatal("%s: BN_CTX_get failed", __func__
);
2027 /* log2(x) > log2(order)/2, log2(y) > log2(order)/2 */
2028 if (EC_GROUP_get_order(group
, order
, bnctx
) != 1)
2029 fatal("%s: EC_GROUP_get_order failed", __func__
);
2030 if (EC_POINT_get_affine_coordinates_GFp(group
, public,
2032 fatal("%s: EC_POINT_get_affine_coordinates_GFp", __func__
);
2033 if (BN_num_bits(x
) <= BN_num_bits(order
) / 2) {
2034 error("%s: public key x coordinate too small: "
2035 "bits(x) = %d, bits(order)/2 = %d", __func__
,
2036 BN_num_bits(x
), BN_num_bits(order
) / 2);
2039 if (BN_num_bits(y
) <= BN_num_bits(order
) / 2) {
2040 error("%s: public key y coordinate too small: "
2041 "bits(y) = %d, bits(order)/2 = %d", __func__
,
2042 BN_num_bits(x
), BN_num_bits(order
) / 2);
2046 /* nQ == infinity (n == order of subgroup) */
2047 if ((nq
= EC_POINT_new(group
)) == NULL
)
2048 fatal("%s: BN_CTX_tmp failed", __func__
);
2049 if (EC_POINT_mul(group
, nq
, NULL
, public, order
, bnctx
) != 1)
2050 fatal("%s: EC_GROUP_mul failed", __func__
);
2051 if (EC_POINT_is_at_infinity(group
, nq
) != 1) {
2052 error("%s: received degenerate public key (nQ != infinity)",
2057 /* x < order - 1, y < order - 1 */
2058 if (!BN_sub(tmp
, order
, BN_value_one()))
2059 fatal("%s: BN_sub failed", __func__
);
2060 if (BN_cmp(x
, tmp
) >= 0) {
2061 error("%s: public key x coordinate >= group order - 1",
2065 if (BN_cmp(y
, tmp
) >= 0) {
2066 error("%s: public key y coordinate >= group order - 1",
2078 key_ec_validate_private(const EC_KEY
*key
)
2081 BIGNUM
*order
, *tmp
;
2084 if ((bnctx
= BN_CTX_new()) == NULL
)
2085 fatal("%s: BN_CTX_new failed", __func__
);
2086 BN_CTX_start(bnctx
);
2088 if ((order
= BN_CTX_get(bnctx
)) == NULL
||
2089 (tmp
= BN_CTX_get(bnctx
)) == NULL
)
2090 fatal("%s: BN_CTX_get failed", __func__
);
2092 /* log2(private) > log2(order)/2 */
2093 if (EC_GROUP_get_order(EC_KEY_get0_group(key
), order
, bnctx
) != 1)
2094 fatal("%s: EC_GROUP_get_order failed", __func__
);
2095 if (BN_num_bits(EC_KEY_get0_private_key(key
)) <=
2096 BN_num_bits(order
) / 2) {
2097 error("%s: private key too small: "
2098 "bits(y) = %d, bits(order)/2 = %d", __func__
,
2099 BN_num_bits(EC_KEY_get0_private_key(key
)),
2100 BN_num_bits(order
) / 2);
2104 /* private < order - 1 */
2105 if (!BN_sub(tmp
, order
, BN_value_one()))
2106 fatal("%s: BN_sub failed", __func__
);
2107 if (BN_cmp(EC_KEY_get0_private_key(key
), tmp
) >= 0) {
2108 error("%s: private key >= group order - 1", __func__
);
2117 #if defined(DEBUG_KEXECDH) || defined(DEBUG_PK)
2119 key_dump_ec_point(const EC_GROUP
*group
, const EC_POINT
*point
)
2124 if (point
== NULL
) {
2125 fputs("point=(NULL)\n", stderr
);
2128 if ((bnctx
= BN_CTX_new()) == NULL
)
2129 fatal("%s: BN_CTX_new failed", __func__
);
2130 BN_CTX_start(bnctx
);
2131 if ((x
= BN_CTX_get(bnctx
)) == NULL
|| (y
= BN_CTX_get(bnctx
)) == NULL
)
2132 fatal("%s: BN_CTX_get failed", __func__
);
2133 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group
)) !=
2134 NID_X9_62_prime_field
)
2135 fatal("%s: group is not a prime field", __func__
);
2136 if (EC_POINT_get_affine_coordinates_GFp(group
, point
, x
, y
, bnctx
) != 1)
2137 fatal("%s: EC_POINT_get_affine_coordinates_GFp", __func__
);
2138 fputs("x=", stderr
);
2139 BN_print_fp(stderr
, x
);
2140 fputs("\ny=", stderr
);
2141 BN_print_fp(stderr
, y
);
2142 fputs("\n", stderr
);
2147 key_dump_ec_key(const EC_KEY
*key
)
2149 const BIGNUM
*exponent
;
2151 key_dump_ec_point(EC_KEY_get0_group(key
), EC_KEY_get0_public_key(key
));
2152 fputs("exponent=", stderr
);
2153 if ((exponent
= EC_KEY_get0_private_key(key
)) == NULL
)
2154 fputs("(NULL)", stderr
);
2156 BN_print_fp(stderr
, EC_KEY_get0_private_key(key
));
2157 fputs("\n", stderr
);
2159 #endif /* defined(DEBUG_KEXECDH) || defined(DEBUG_PK) */