1 /* $OpenBSD: key.c,v 1.94 2010/10/28 11:22:09 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");
114 #ifdef OPENSSL_HAS_ECC
117 /* Cannot do anything until we know the group */
123 fatal("key_new: bad key type %d", k
->type
);
128 k
->cert
= cert_new();
134 key_add_private(Key
*k
)
139 case KEY_RSA_CERT_V00
:
141 if ((k
->rsa
->d
= BN_new()) == NULL
)
142 fatal("key_new_private: BN_new failed");
143 if ((k
->rsa
->iqmp
= BN_new()) == NULL
)
144 fatal("key_new_private: BN_new failed");
145 if ((k
->rsa
->q
= BN_new()) == NULL
)
146 fatal("key_new_private: BN_new failed");
147 if ((k
->rsa
->p
= BN_new()) == NULL
)
148 fatal("key_new_private: BN_new failed");
149 if ((k
->rsa
->dmq1
= BN_new()) == NULL
)
150 fatal("key_new_private: BN_new failed");
151 if ((k
->rsa
->dmp1
= BN_new()) == NULL
)
152 fatal("key_new_private: BN_new failed");
155 case KEY_DSA_CERT_V00
:
157 if ((k
->dsa
->priv_key
= BN_new()) == NULL
)
158 fatal("key_new_private: BN_new failed");
162 /* Cannot do anything until we know the group */
172 key_new_private(int type
)
174 Key
*k
= key_new(type
);
181 cert_free(struct KeyCert
*cert
)
185 buffer_free(&cert
->certblob
);
186 buffer_free(&cert
->critical
);
187 buffer_free(&cert
->extensions
);
188 if (cert
->key_id
!= NULL
)
190 for (i
= 0; i
< cert
->nprincipals
; i
++)
191 xfree(cert
->principals
[i
]);
192 if (cert
->principals
!= NULL
)
193 xfree(cert
->principals
);
194 if (cert
->signature_key
!= NULL
)
195 key_free(cert
->signature_key
);
202 fatal("key_free: key is NULL");
206 case KEY_RSA_CERT_V00
:
213 case KEY_DSA_CERT_V00
:
219 #ifdef OPENSSL_HAS_ECC
222 if (k
->ecdsa
!= NULL
)
223 EC_KEY_free(k
->ecdsa
);
230 fatal("key_free: bad key type %d", k
->type
);
233 if (key_is_cert(k
)) {
243 cert_compare(struct KeyCert
*a
, struct KeyCert
*b
)
245 if (a
== NULL
&& b
== NULL
)
247 if (a
== NULL
|| b
== NULL
)
249 if (buffer_len(&a
->certblob
) != buffer_len(&b
->certblob
))
251 if (timingsafe_bcmp(buffer_ptr(&a
->certblob
), buffer_ptr(&b
->certblob
),
252 buffer_len(&a
->certblob
)) != 0)
258 * Compare public portions of key only, allowing comparisons between
259 * certificates and plain keys too.
262 key_equal_public(const Key
*a
, const Key
*b
)
264 #ifdef OPENSSL_HAS_ECC
268 if (a
== NULL
|| b
== NULL
||
269 key_type_plain(a
->type
) != key_type_plain(b
->type
))
274 case KEY_RSA_CERT_V00
:
277 return a
->rsa
!= NULL
&& b
->rsa
!= NULL
&&
278 BN_cmp(a
->rsa
->e
, b
->rsa
->e
) == 0 &&
279 BN_cmp(a
->rsa
->n
, b
->rsa
->n
) == 0;
280 case KEY_DSA_CERT_V00
:
283 return a
->dsa
!= NULL
&& b
->dsa
!= NULL
&&
284 BN_cmp(a
->dsa
->p
, b
->dsa
->p
) == 0 &&
285 BN_cmp(a
->dsa
->q
, b
->dsa
->q
) == 0 &&
286 BN_cmp(a
->dsa
->g
, b
->dsa
->g
) == 0 &&
287 BN_cmp(a
->dsa
->pub_key
, b
->dsa
->pub_key
) == 0;
288 #ifdef OPENSSL_HAS_ECC
291 if (a
->ecdsa
== NULL
|| b
->ecdsa
== NULL
||
292 EC_KEY_get0_public_key(a
->ecdsa
) == NULL
||
293 EC_KEY_get0_public_key(b
->ecdsa
) == NULL
)
295 if ((bnctx
= BN_CTX_new()) == NULL
)
296 fatal("%s: BN_CTX_new failed", __func__
);
297 if (EC_GROUP_cmp(EC_KEY_get0_group(a
->ecdsa
),
298 EC_KEY_get0_group(b
->ecdsa
), bnctx
) != 0 ||
299 EC_POINT_cmp(EC_KEY_get0_group(a
->ecdsa
),
300 EC_KEY_get0_public_key(a
->ecdsa
),
301 EC_KEY_get0_public_key(b
->ecdsa
), bnctx
) != 0) {
307 #endif /* OPENSSL_HAS_ECC */
309 fatal("key_equal: bad key type %d", a
->type
);
315 key_equal(const Key
*a
, const Key
*b
)
317 if (a
== NULL
|| b
== NULL
|| a
->type
!= b
->type
)
319 if (key_is_cert(a
)) {
320 if (!cert_compare(a
->cert
, b
->cert
))
323 return key_equal_public(a
, b
);
327 key_fingerprint_raw(Key
*k
, enum fp_type dgst_type
, u_int
*dgst_raw_length
)
329 const EVP_MD
*md
= NULL
;
332 u_char
*retval
= NULL
;
334 int nlen
, elen
, otype
;
336 *dgst_raw_length
= 0;
346 fatal("key_fingerprint_raw: bad digest type %d",
351 nlen
= BN_num_bytes(k
->rsa
->n
);
352 elen
= BN_num_bytes(k
->rsa
->e
);
355 BN_bn2bin(k
->rsa
->n
, blob
);
356 BN_bn2bin(k
->rsa
->e
, blob
+ nlen
);
361 key_to_blob(k
, &blob
, &len
);
363 case KEY_DSA_CERT_V00
:
364 case KEY_RSA_CERT_V00
:
368 /* We want a fingerprint of the _key_ not of the cert */
370 k
->type
= key_type_plain(k
->type
);
371 key_to_blob(k
, &blob
, &len
);
377 fatal("key_fingerprint_raw: bad key type %d", k
->type
);
381 retval
= xmalloc(EVP_MAX_MD_SIZE
);
382 EVP_DigestInit(&ctx
, md
);
383 EVP_DigestUpdate(&ctx
, blob
, len
);
384 EVP_DigestFinal(&ctx
, retval
, dgst_raw_length
);
385 memset(blob
, 0, len
);
388 fatal("key_fingerprint_raw: blob is null");
394 key_fingerprint_hex(u_char
*dgst_raw
, u_int dgst_raw_len
)
399 retval
= xcalloc(1, dgst_raw_len
* 3 + 1);
400 for (i
= 0; i
< dgst_raw_len
; i
++) {
402 snprintf(hex
, sizeof(hex
), "%02x:", dgst_raw
[i
]);
403 strlcat(retval
, hex
, dgst_raw_len
* 3 + 1);
406 /* Remove the trailing ':' character */
407 retval
[(dgst_raw_len
* 3) - 1] = '\0';
412 key_fingerprint_bubblebabble(u_char
*dgst_raw
, u_int dgst_raw_len
)
414 char vowels
[] = { 'a', 'e', 'i', 'o', 'u', 'y' };
415 char consonants
[] = { 'b', 'c', 'd', 'f', 'g', 'h', 'k', 'l', 'm',
416 'n', 'p', 'r', 's', 't', 'v', 'z', 'x' };
417 u_int i
, j
= 0, rounds
, seed
= 1;
420 rounds
= (dgst_raw_len
/ 2) + 1;
421 retval
= xcalloc((rounds
* 6), sizeof(char));
423 for (i
= 0; i
< rounds
; i
++) {
424 u_int idx0
, idx1
, idx2
, idx3
, idx4
;
425 if ((i
+ 1 < rounds
) || (dgst_raw_len
% 2 != 0)) {
426 idx0
= (((((u_int
)(dgst_raw
[2 * i
])) >> 6) & 3) +
428 idx1
= (((u_int
)(dgst_raw
[2 * i
])) >> 2) & 15;
429 idx2
= ((((u_int
)(dgst_raw
[2 * i
])) & 3) +
431 retval
[j
++] = vowels
[idx0
];
432 retval
[j
++] = consonants
[idx1
];
433 retval
[j
++] = vowels
[idx2
];
434 if ((i
+ 1) < rounds
) {
435 idx3
= (((u_int
)(dgst_raw
[(2 * i
) + 1])) >> 4) & 15;
436 idx4
= (((u_int
)(dgst_raw
[(2 * i
) + 1]))) & 15;
437 retval
[j
++] = consonants
[idx3
];
439 retval
[j
++] = consonants
[idx4
];
441 ((((u_int
)(dgst_raw
[2 * i
])) * 7) +
442 ((u_int
)(dgst_raw
[(2 * i
) + 1])))) % 36;
448 retval
[j
++] = vowels
[idx0
];
449 retval
[j
++] = consonants
[idx1
];
450 retval
[j
++] = vowels
[idx2
];
459 * Draw an ASCII-Art representing the fingerprint so human brain can
460 * profit from its built-in pattern recognition ability.
461 * This technique is called "random art" and can be found in some
462 * scientific publications like this original paper:
464 * "Hash Visualization: a New Technique to improve Real-World Security",
465 * Perrig A. and Song D., 1999, International Workshop on Cryptographic
466 * Techniques and E-Commerce (CrypTEC '99)
467 * sparrow.ece.cmu.edu/~adrian/projects/validation/validation.pdf
469 * The subject came up in a talk by Dan Kaminsky, too.
471 * If you see the picture is different, the key is different.
472 * If the picture looks the same, you still know nothing.
474 * The algorithm used here is a worm crawling over a discrete plane,
475 * leaving a trace (augmenting the field) everywhere it goes.
476 * Movement is taken from dgst_raw 2bit-wise. Bumping into walls
477 * makes the respective movement vector be ignored for this turn.
478 * Graphs are not unambiguous, because circles in graphs can be
479 * walked in either direction.
483 * Field sizes for the random art. Have to be odd, so the starting point
484 * can be in the exact middle of the picture, and FLDBASE should be >=8 .
485 * Else pictures would be too dense, and drawing the frame would
486 * fail, too, because the key type would not fit in anymore.
489 #define FLDSIZE_Y (FLDBASE + 1)
490 #define FLDSIZE_X (FLDBASE * 2 + 1)
492 key_fingerprint_randomart(u_char
*dgst_raw
, u_int dgst_raw_len
, const Key
*k
)
495 * Chars to be used after each other every time the worm
496 * intersects with itself. Matter of taste.
498 char *augmentation_string
= " .o+=*BOX@%&#/^SE";
500 u_char field
[FLDSIZE_X
][FLDSIZE_Y
];
503 size_t len
= strlen(augmentation_string
) - 1;
505 retval
= xcalloc(1, (FLDSIZE_X
+ 3) * (FLDSIZE_Y
+ 2));
507 /* initialize field */
508 memset(field
, 0, FLDSIZE_X
* FLDSIZE_Y
* sizeof(char));
512 /* process raw key */
513 for (i
= 0; i
< dgst_raw_len
; i
++) {
515 /* each byte conveys four 2-bit move commands */
517 for (b
= 0; b
< 4; b
++) {
518 /* evaluate 2 bit, rest is shifted later */
519 x
+= (input
& 0x1) ? 1 : -1;
520 y
+= (input
& 0x2) ? 1 : -1;
522 /* assure we are still in bounds */
525 x
= MIN(x
, FLDSIZE_X
- 1);
526 y
= MIN(y
, FLDSIZE_Y
- 1);
528 /* augment the field */
529 if (field
[x
][y
] < len
- 2)
535 /* mark starting point and end point*/
536 field
[FLDSIZE_X
/ 2][FLDSIZE_Y
/ 2] = len
- 1;
540 snprintf(retval
, FLDSIZE_X
, "+--[%4s %4u]", key_type(k
), key_size(k
));
541 p
= strchr(retval
, '\0');
543 /* output upper border */
544 for (i
= p
- retval
- 1; i
< FLDSIZE_X
; i
++)
550 for (y
= 0; y
< FLDSIZE_Y
; y
++) {
552 for (x
= 0; x
< FLDSIZE_X
; x
++)
553 *p
++ = augmentation_string
[MIN(field
[x
][y
], len
)];
558 /* output lower border */
560 for (i
= 0; i
< FLDSIZE_X
; i
++)
568 key_fingerprint(Key
*k
, enum fp_type dgst_type
, enum fp_rep dgst_rep
)
574 dgst_raw
= key_fingerprint_raw(k
, dgst_type
, &dgst_raw_len
);
576 fatal("key_fingerprint: null from key_fingerprint_raw()");
579 retval
= key_fingerprint_hex(dgst_raw
, dgst_raw_len
);
581 case SSH_FP_BUBBLEBABBLE
:
582 retval
= key_fingerprint_bubblebabble(dgst_raw
, dgst_raw_len
);
584 case SSH_FP_RANDOMART
:
585 retval
= key_fingerprint_randomart(dgst_raw
, dgst_raw_len
, k
);
588 fatal("key_fingerprint: bad digest representation %d",
592 memset(dgst_raw
, 0, dgst_raw_len
);
598 * Reads a multiple-precision integer in decimal from the buffer, and advances
599 * the pointer. The integer must already be initialized. This function is
600 * permitted to modify the buffer. This leaves *cpp to point just beyond the
601 * last processed (and maybe modified) character. Note that this may modify
602 * the buffer containing the number.
605 read_bignum(char **cpp
, BIGNUM
* value
)
610 /* Skip any leading whitespace. */
611 for (; *cp
== ' ' || *cp
== '\t'; cp
++)
614 /* Check that it begins with a decimal digit. */
615 if (*cp
< '0' || *cp
> '9')
618 /* Save starting position. */
621 /* Move forward until all decimal digits skipped. */
622 for (; *cp
>= '0' && *cp
<= '9'; cp
++)
625 /* Save the old terminating character, and replace it by \0. */
629 /* Parse the number. */
630 if (BN_dec2bn(&value
, *cpp
) == 0)
633 /* Restore old terminating character. */
636 /* Move beyond the number and return success. */
642 write_bignum(FILE *f
, BIGNUM
*num
)
644 char *buf
= BN_bn2dec(num
);
646 error("write_bignum: BN_bn2dec() failed");
649 fprintf(f
, " %s", buf
);
654 /* returns 1 ok, -1 error */
656 key_read(Key
*ret
, char **cpp
)
664 #ifdef OPENSSL_HAS_ECC
672 /* Get number of bits. */
673 if (*cp
< '0' || *cp
> '9')
674 return -1; /* Bad bit count... */
675 for (bits
= 0; *cp
>= '0' && *cp
<= '9'; cp
++)
676 bits
= 10 * bits
+ *cp
- '0';
680 /* Get public exponent, public modulus. */
681 if (!read_bignum(cpp
, ret
->rsa
->e
))
683 if (!read_bignum(cpp
, ret
->rsa
->n
))
685 /* validate the claimed number of bits */
686 if ((u_int
)BN_num_bits(ret
->rsa
->n
) != bits
) {
687 verbose("key_read: claimed key size %d does not match "
688 "actual %d", bits
, BN_num_bits(ret
->rsa
->n
));
697 case KEY_DSA_CERT_V00
:
698 case KEY_RSA_CERT_V00
:
702 space
= strchr(cp
, ' ');
704 debug3("key_read: missing whitespace");
708 type
= key_type_from_name(cp
);
709 #ifdef OPENSSL_HAS_ECC
710 if (key_type_plain(type
) == KEY_ECDSA
&&
711 (curve_nid
= key_ecdsa_nid_from_name(cp
)) == -1) {
712 debug("key_read: invalid curve");
717 if (type
== KEY_UNSPEC
) {
718 debug3("key_read: missing keytype");
723 debug3("key_read: short string");
726 if (ret
->type
== KEY_UNSPEC
) {
728 } else if (ret
->type
!= type
) {
729 /* is a key, but different type */
730 debug3("key_read: type mismatch");
735 n
= uudecode(cp
, blob
, len
);
737 error("key_read: uudecode %s failed", cp
);
741 k
= key_from_blob(blob
, (u_int
)n
);
744 error("key_read: key_from_blob %s failed", cp
);
747 if (k
->type
!= type
) {
748 error("key_read: type mismatch: encoding error");
752 #ifdef OPENSSL_HAS_ECC
753 if (key_type_plain(type
) == KEY_ECDSA
&&
754 curve_nid
!= k
->ecdsa_nid
) {
755 error("key_read: type mismatch: EC curve mismatch");
761 if (key_is_cert(ret
)) {
762 if (!key_is_cert(k
)) {
763 error("key_read: loaded key is not a cert");
767 if (ret
->cert
!= NULL
)
768 cert_free(ret
->cert
);
772 if (key_type_plain(ret
->type
) == KEY_RSA
) {
773 if (ret
->rsa
!= NULL
)
778 RSA_print_fp(stderr
, ret
->rsa
, 8);
781 if (key_type_plain(ret
->type
) == KEY_DSA
) {
782 if (ret
->dsa
!= NULL
)
787 DSA_print_fp(stderr
, ret
->dsa
, 8);
790 #ifdef OPENSSL_HAS_ECC
791 if (key_type_plain(ret
->type
) == KEY_ECDSA
) {
792 if (ret
->ecdsa
!= NULL
)
793 EC_KEY_free(ret
->ecdsa
);
794 ret
->ecdsa
= k
->ecdsa
;
795 ret
->ecdsa_nid
= k
->ecdsa_nid
;
799 key_dump_ec_key(ret
->ecdsa
);
808 /* advance cp: skip whitespace and data */
809 while (*cp
== ' ' || *cp
== '\t')
811 while (*cp
!= '\0' && *cp
!= ' ' && *cp
!= '\t')
816 fatal("key_read: bad key type: %d", ret
->type
);
823 key_write(const Key
*key
, FILE *f
)
830 if (key_is_cert(key
)) {
831 if (key
->cert
== NULL
) {
832 error("%s: no cert data", __func__
);
835 if (buffer_len(&key
->cert
->certblob
) == 0) {
836 error("%s: no signed certificate blob", __func__
);
843 if (key
->rsa
== NULL
)
845 /* size of modulus 'n' */
846 bits
= BN_num_bits(key
->rsa
->n
);
847 fprintf(f
, "%u", bits
);
848 if (write_bignum(f
, key
->rsa
->e
) &&
849 write_bignum(f
, key
->rsa
->n
))
851 error("key_write: failed for RSA key");
854 case KEY_DSA_CERT_V00
:
856 if (key
->dsa
== NULL
)
859 #ifdef OPENSSL_HAS_ECC
862 if (key
->ecdsa
== NULL
)
867 case KEY_RSA_CERT_V00
:
869 if (key
->rsa
== NULL
)
876 key_to_blob(key
, &blob
, &len
);
878 n
= uuencode(blob
, len
, uu
, 2*len
);
880 fprintf(f
, "%s %s", key_ssh_name(key
), uu
);
890 key_type(const Key
*k
)
899 #ifdef OPENSSL_HAS_ECC
903 case KEY_RSA_CERT_V00
:
904 return "RSA-CERT-V00";
905 case KEY_DSA_CERT_V00
:
906 return "DSA-CERT-V00";
911 #ifdef OPENSSL_HAS_ECC
920 key_cert_type(const Key
*k
)
922 switch (k
->cert
->type
) {
923 case SSH2_CERT_TYPE_USER
:
925 case SSH2_CERT_TYPE_HOST
:
933 key_ssh_name_from_type_nid(int type
, int nid
)
940 case KEY_RSA_CERT_V00
:
941 return "ssh-rsa-cert-v00@openssh.com";
942 case KEY_DSA_CERT_V00
:
943 return "ssh-dss-cert-v00@openssh.com";
945 return "ssh-rsa-cert-v01@openssh.com";
947 return "ssh-dss-cert-v01@openssh.com";
948 #ifdef OPENSSL_HAS_ECC
951 case NID_X9_62_prime256v1
:
952 return "ecdsa-sha2-nistp256";
954 return "ecdsa-sha2-nistp384";
956 return "ecdsa-sha2-nistp521";
963 case NID_X9_62_prime256v1
:
964 return "ecdsa-sha2-nistp256-cert-v01@openssh.com";
966 return "ecdsa-sha2-nistp384-cert-v01@openssh.com";
968 return "ecdsa-sha2-nistp521-cert-v01@openssh.com";
973 #endif /* OPENSSL_HAS_ECC */
975 return "ssh-unknown";
979 key_ssh_name(const Key
*k
)
981 return key_ssh_name_from_type_nid(k
->type
, k
->ecdsa_nid
);
985 key_ssh_name_plain(const Key
*k
)
987 return key_ssh_name_from_type_nid(key_type_plain(k
->type
),
992 key_size(const Key
*k
)
997 case KEY_RSA_CERT_V00
:
999 return BN_num_bits(k
->rsa
->n
);
1001 case KEY_DSA_CERT_V00
:
1003 return BN_num_bits(k
->dsa
->p
);
1004 #ifdef OPENSSL_HAS_ECC
1006 case KEY_ECDSA_CERT
:
1007 return key_curve_nid_to_bits(k
->ecdsa_nid
);
1014 rsa_generate_private_key(u_int bits
)
1018 private = RSA_generate_key(bits
, RSA_F4
, NULL
, NULL
);
1019 if (private == NULL
)
1020 fatal("rsa_generate_private_key: key generation failed.");
1025 dsa_generate_private_key(u_int bits
)
1027 DSA
*private = DSA_generate_parameters(bits
, NULL
, 0, NULL
, NULL
, NULL
, NULL
);
1029 if (private == NULL
)
1030 fatal("dsa_generate_private_key: DSA_generate_parameters failed");
1031 if (!DSA_generate_key(private))
1032 fatal("dsa_generate_private_key: DSA_generate_key failed.");
1033 if (private == NULL
)
1034 fatal("dsa_generate_private_key: NULL.");
1039 key_ecdsa_bits_to_nid(int bits
)
1042 #ifdef OPENSSL_HAS_ECC
1044 return NID_X9_62_prime256v1
;
1046 return NID_secp384r1
;
1048 return NID_secp521r1
;
1055 #ifdef OPENSSL_HAS_ECC
1057 key_ecdsa_key_to_nid(EC_KEY
*k
)
1061 NID_X9_62_prime256v1
,
1069 const EC_GROUP
*g
= EC_KEY_get0_group(k
);
1072 * The group may be stored in a ASN.1 encoded private key in one of two
1073 * ways: as a "named group", which is reconstituted by ASN.1 object ID
1074 * or explicit group parameters encoded into the key blob. Only the
1075 * "named group" case sets the group NID for us, but we can figure
1076 * it out for the other case by comparing against all the groups that
1079 if ((nid
= EC_GROUP_get_curve_name(g
)) > 0)
1081 if ((bnctx
= BN_CTX_new()) == NULL
)
1082 fatal("%s: BN_CTX_new() failed", __func__
);
1083 for (i
= 0; nids
[i
] != -1; i
++) {
1084 if ((eg
= EC_GROUP_new_by_curve_name(nids
[i
])) == NULL
)
1085 fatal("%s: EC_GROUP_new_by_curve_name failed",
1087 if (EC_GROUP_cmp(g
, eg
, bnctx
) == 0)
1092 debug3("%s: nid = %d", __func__
, nids
[i
]);
1093 if (nids
[i
] != -1) {
1094 /* Use the group with the NID attached */
1095 EC_GROUP_set_asn1_flag(eg
, OPENSSL_EC_NAMED_CURVE
);
1096 if (EC_KEY_set_group(k
, eg
) != 1)
1097 fatal("%s: EC_KEY_set_group", __func__
);
1103 ecdsa_generate_private_key(u_int bits
, int *nid
)
1107 if ((*nid
= key_ecdsa_bits_to_nid(bits
)) == -1)
1108 fatal("%s: invalid key length", __func__
);
1109 if ((private = EC_KEY_new_by_curve_name(*nid
)) == NULL
)
1110 fatal("%s: EC_KEY_new_by_curve_name failed", __func__
);
1111 if (EC_KEY_generate_key(private) != 1)
1112 fatal("%s: EC_KEY_generate_key failed", __func__
);
1113 EC_KEY_set_asn1_flag(private, OPENSSL_EC_NAMED_CURVE
);
1116 #endif /* OPENSSL_HAS_ECC */
1119 key_generate(int type
, u_int bits
)
1121 Key
*k
= key_new(KEY_UNSPEC
);
1124 k
->dsa
= dsa_generate_private_key(bits
);
1126 #ifdef OPENSSL_HAS_ECC
1128 k
->ecdsa
= ecdsa_generate_private_key(bits
, &k
->ecdsa_nid
);
1133 k
->rsa
= rsa_generate_private_key(bits
);
1135 case KEY_RSA_CERT_V00
:
1136 case KEY_DSA_CERT_V00
:
1139 fatal("key_generate: cert keys cannot be generated directly");
1141 fatal("key_generate: unknown type %d", type
);
1148 key_cert_copy(const Key
*from_key
, struct Key
*to_key
)
1151 const struct KeyCert
*from
;
1154 if (to_key
->cert
!= NULL
) {
1155 cert_free(to_key
->cert
);
1156 to_key
->cert
= NULL
;
1159 if ((from
= from_key
->cert
) == NULL
)
1162 to
= to_key
->cert
= cert_new();
1164 buffer_append(&to
->certblob
, buffer_ptr(&from
->certblob
),
1165 buffer_len(&from
->certblob
));
1167 buffer_append(&to
->critical
,
1168 buffer_ptr(&from
->critical
), buffer_len(&from
->critical
));
1169 buffer_append(&to
->extensions
,
1170 buffer_ptr(&from
->extensions
), buffer_len(&from
->extensions
));
1172 to
->serial
= from
->serial
;
1173 to
->type
= from
->type
;
1174 to
->key_id
= from
->key_id
== NULL
? NULL
: xstrdup(from
->key_id
);
1175 to
->valid_after
= from
->valid_after
;
1176 to
->valid_before
= from
->valid_before
;
1177 to
->signature_key
= from
->signature_key
== NULL
?
1178 NULL
: key_from_private(from
->signature_key
);
1180 to
->nprincipals
= from
->nprincipals
;
1181 if (to
->nprincipals
> CERT_MAX_PRINCIPALS
)
1182 fatal("%s: nprincipals (%u) > CERT_MAX_PRINCIPALS (%u)",
1183 __func__
, to
->nprincipals
, CERT_MAX_PRINCIPALS
);
1184 if (to
->nprincipals
> 0) {
1185 to
->principals
= xcalloc(from
->nprincipals
,
1186 sizeof(*to
->principals
));
1187 for (i
= 0; i
< to
->nprincipals
; i
++)
1188 to
->principals
[i
] = xstrdup(from
->principals
[i
]);
1193 key_from_private(const Key
*k
)
1198 case KEY_DSA_CERT_V00
:
1200 n
= key_new(k
->type
);
1201 if ((BN_copy(n
->dsa
->p
, k
->dsa
->p
) == NULL
) ||
1202 (BN_copy(n
->dsa
->q
, k
->dsa
->q
) == NULL
) ||
1203 (BN_copy(n
->dsa
->g
, k
->dsa
->g
) == NULL
) ||
1204 (BN_copy(n
->dsa
->pub_key
, k
->dsa
->pub_key
) == NULL
))
1205 fatal("key_from_private: BN_copy failed");
1207 #ifdef OPENSSL_HAS_ECC
1209 case KEY_ECDSA_CERT
:
1210 n
= key_new(k
->type
);
1211 n
->ecdsa_nid
= k
->ecdsa_nid
;
1212 if ((n
->ecdsa
= EC_KEY_new_by_curve_name(k
->ecdsa_nid
)) == NULL
)
1213 fatal("%s: EC_KEY_new_by_curve_name failed", __func__
);
1214 if (EC_KEY_set_public_key(n
->ecdsa
,
1215 EC_KEY_get0_public_key(k
->ecdsa
)) != 1)
1216 fatal("%s: EC_KEY_set_public_key failed", __func__
);
1221 case KEY_RSA_CERT_V00
:
1223 n
= key_new(k
->type
);
1224 if ((BN_copy(n
->rsa
->n
, k
->rsa
->n
) == NULL
) ||
1225 (BN_copy(n
->rsa
->e
, k
->rsa
->e
) == NULL
))
1226 fatal("key_from_private: BN_copy failed");
1229 fatal("key_from_private: unknown type %d", k
->type
);
1233 key_cert_copy(k
, n
);
1238 key_type_from_name(char *name
)
1240 if (strcmp(name
, "rsa1") == 0) {
1242 } else if (strcmp(name
, "rsa") == 0) {
1244 } else if (strcmp(name
, "dsa") == 0) {
1246 } else if (strcmp(name
, "ssh-rsa") == 0) {
1248 } else if (strcmp(name
, "ssh-dss") == 0) {
1250 #ifdef OPENSSL_HAS_ECC
1251 } else if (strcmp(name
, "ecdsa") == 0 ||
1252 strcmp(name
, "ecdsa-sha2-nistp256") == 0 ||
1253 strcmp(name
, "ecdsa-sha2-nistp384") == 0 ||
1254 strcmp(name
, "ecdsa-sha2-nistp521") == 0) {
1257 } else if (strcmp(name
, "ssh-rsa-cert-v00@openssh.com") == 0) {
1258 return KEY_RSA_CERT_V00
;
1259 } else if (strcmp(name
, "ssh-dss-cert-v00@openssh.com") == 0) {
1260 return KEY_DSA_CERT_V00
;
1261 } else if (strcmp(name
, "ssh-rsa-cert-v01@openssh.com") == 0) {
1262 return KEY_RSA_CERT
;
1263 } else if (strcmp(name
, "ssh-dss-cert-v01@openssh.com") == 0) {
1264 return KEY_DSA_CERT
;
1265 #ifdef OPENSSL_HAS_ECC
1266 } else if (strcmp(name
, "ecdsa-sha2-nistp256-cert-v01@openssh.com") == 0 ||
1267 strcmp(name
, "ecdsa-sha2-nistp384-cert-v01@openssh.com") == 0 ||
1268 strcmp(name
, "ecdsa-sha2-nistp521-cert-v01@openssh.com") == 0) {
1269 return KEY_ECDSA_CERT
;
1273 debug2("key_type_from_name: unknown key type '%s'", name
);
1278 key_ecdsa_nid_from_name(const char *name
)
1280 #ifdef OPENSSL_HAS_ECC
1281 if (strcmp(name
, "ecdsa-sha2-nistp256") == 0 ||
1282 strcmp(name
, "ecdsa-sha2-nistp256-cert-v01@openssh.com") == 0)
1283 return NID_X9_62_prime256v1
;
1284 if (strcmp(name
, "ecdsa-sha2-nistp384") == 0 ||
1285 strcmp(name
, "ecdsa-sha2-nistp384-cert-v01@openssh.com") == 0)
1286 return NID_secp384r1
;
1287 if (strcmp(name
, "ecdsa-sha2-nistp521") == 0 ||
1288 strcmp(name
, "ecdsa-sha2-nistp521-cert-v01@openssh.com") == 0)
1289 return NID_secp521r1
;
1290 #endif /* OPENSSL_HAS_ECC */
1292 debug2("%s: unknown/non-ECDSA key type '%s'", __func__
, name
);
1297 key_names_valid2(const char *names
)
1301 if (names
== NULL
|| strcmp(names
, "") == 0)
1303 s
= cp
= xstrdup(names
);
1304 for ((p
= strsep(&cp
, ",")); p
&& *p
!= '\0';
1305 (p
= strsep(&cp
, ","))) {
1306 switch (key_type_from_name(p
)) {
1313 debug3("key names ok: [%s]", names
);
1319 cert_parse(Buffer
*b
, Key
*key
, const u_char
*blob
, u_int blen
)
1321 u_char
*principals
, *critical
, *exts
, *sig_key
, *sig
;
1322 u_int signed_len
, plen
, clen
, sklen
, slen
, kidlen
, elen
;
1326 int v00
= key
->type
== KEY_DSA_CERT_V00
||
1327 key
->type
== KEY_RSA_CERT_V00
;
1331 /* Copy the entire key blob for verification and later serialisation */
1332 buffer_append(&key
->cert
->certblob
, blob
, blen
);
1334 elen
= 0; /* Not touched for v00 certs */
1335 principals
= exts
= critical
= sig_key
= sig
= NULL
;
1336 if ((!v00
&& buffer_get_int64_ret(&key
->cert
->serial
, b
) != 0) ||
1337 buffer_get_int_ret(&key
->cert
->type
, b
) != 0 ||
1338 (key
->cert
->key_id
= buffer_get_cstring_ret(b
, &kidlen
)) == NULL
||
1339 (principals
= buffer_get_string_ret(b
, &plen
)) == NULL
||
1340 buffer_get_int64_ret(&key
->cert
->valid_after
, b
) != 0 ||
1341 buffer_get_int64_ret(&key
->cert
->valid_before
, b
) != 0 ||
1342 (critical
= buffer_get_string_ret(b
, &clen
)) == NULL
||
1343 (!v00
&& (exts
= buffer_get_string_ret(b
, &elen
)) == NULL
) ||
1344 (v00
&& buffer_get_string_ptr_ret(b
, NULL
) == NULL
) || /* nonce */
1345 buffer_get_string_ptr_ret(b
, NULL
) == NULL
|| /* reserved */
1346 (sig_key
= buffer_get_string_ret(b
, &sklen
)) == NULL
) {
1347 error("%s: parse error", __func__
);
1351 if (kidlen
!= strlen(key
->cert
->key_id
)) {
1352 error("%s: key ID contains \\0 character", __func__
);
1356 /* Signature is left in the buffer so we can calculate this length */
1357 signed_len
= buffer_len(&key
->cert
->certblob
) - buffer_len(b
);
1359 if ((sig
= buffer_get_string_ret(b
, &slen
)) == NULL
) {
1360 error("%s: parse error", __func__
);
1364 if (key
->cert
->type
!= SSH2_CERT_TYPE_USER
&&
1365 key
->cert
->type
!= SSH2_CERT_TYPE_HOST
) {
1366 error("Unknown certificate type %u", key
->cert
->type
);
1370 buffer_append(&tmp
, principals
, plen
);
1371 while (buffer_len(&tmp
) > 0) {
1372 if (key
->cert
->nprincipals
>= CERT_MAX_PRINCIPALS
) {
1373 error("%s: Too many principals", __func__
);
1376 if ((principal
= buffer_get_cstring_ret(&tmp
, &plen
)) == NULL
) {
1377 error("%s: Principals data invalid", __func__
);
1380 key
->cert
->principals
= xrealloc(key
->cert
->principals
,
1381 key
->cert
->nprincipals
+ 1, sizeof(*key
->cert
->principals
));
1382 key
->cert
->principals
[key
->cert
->nprincipals
++] = principal
;
1387 buffer_append(&key
->cert
->critical
, critical
, clen
);
1388 buffer_append(&tmp
, critical
, clen
);
1389 /* validate structure */
1390 while (buffer_len(&tmp
) != 0) {
1391 if (buffer_get_string_ptr_ret(&tmp
, NULL
) == NULL
||
1392 buffer_get_string_ptr_ret(&tmp
, NULL
) == NULL
) {
1393 error("%s: critical option data invalid", __func__
);
1399 buffer_append(&key
->cert
->extensions
, exts
, elen
);
1400 buffer_append(&tmp
, exts
, elen
);
1401 /* validate structure */
1402 while (buffer_len(&tmp
) != 0) {
1403 if (buffer_get_string_ptr_ret(&tmp
, NULL
) == NULL
||
1404 buffer_get_string_ptr_ret(&tmp
, NULL
) == NULL
) {
1405 error("%s: extension data invalid", __func__
);
1411 if ((key
->cert
->signature_key
= key_from_blob(sig_key
,
1413 error("%s: Signature key invalid", __func__
);
1416 if (key
->cert
->signature_key
->type
!= KEY_RSA
&&
1417 key
->cert
->signature_key
->type
!= KEY_DSA
&&
1418 key
->cert
->signature_key
->type
!= KEY_ECDSA
) {
1419 error("%s: Invalid signature key type %s (%d)", __func__
,
1420 key_type(key
->cert
->signature_key
),
1421 key
->cert
->signature_key
->type
);
1425 switch (key_verify(key
->cert
->signature_key
, sig
, slen
,
1426 buffer_ptr(&key
->cert
->certblob
), signed_len
)) {
1429 break; /* Good signature */
1431 error("%s: Invalid signature on certificate", __func__
);
1434 error("%s: Certificate signature verification failed",
1441 if (principals
!= NULL
)
1443 if (critical
!= NULL
)
1447 if (sig_key
!= NULL
)
1455 key_from_blob(const u_char
*blob
, u_int blen
)
1459 char *ktype
= NULL
, *curve
= NULL
;
1461 #ifdef OPENSSL_HAS_ECC
1467 dump_base64(stderr
, blob
, blen
);
1470 buffer_append(&b
, blob
, blen
);
1471 if ((ktype
= buffer_get_cstring_ret(&b
, NULL
)) == NULL
) {
1472 error("key_from_blob: can't read key type");
1476 type
= key_type_from_name(ktype
);
1477 #ifdef OPENSSL_HAS_ECC
1478 if (key_type_plain(type
) == KEY_ECDSA
)
1479 nid
= key_ecdsa_nid_from_name(ktype
);
1484 (void)buffer_get_string_ptr_ret(&b
, NULL
); /* Skip nonce */
1487 case KEY_RSA_CERT_V00
:
1488 key
= key_new(type
);
1489 if (buffer_get_bignum2_ret(&b
, key
->rsa
->e
) == -1 ||
1490 buffer_get_bignum2_ret(&b
, key
->rsa
->n
) == -1) {
1491 error("key_from_blob: can't read rsa key");
1498 RSA_print_fp(stderr
, key
->rsa
, 8);
1502 (void)buffer_get_string_ptr_ret(&b
, NULL
); /* Skip nonce */
1505 case KEY_DSA_CERT_V00
:
1506 key
= key_new(type
);
1507 if (buffer_get_bignum2_ret(&b
, key
->dsa
->p
) == -1 ||
1508 buffer_get_bignum2_ret(&b
, key
->dsa
->q
) == -1 ||
1509 buffer_get_bignum2_ret(&b
, key
->dsa
->g
) == -1 ||
1510 buffer_get_bignum2_ret(&b
, key
->dsa
->pub_key
) == -1) {
1511 error("key_from_blob: can't read dsa key");
1515 DSA_print_fp(stderr
, key
->dsa
, 8);
1518 #ifdef OPENSSL_HAS_ECC
1519 case KEY_ECDSA_CERT
:
1520 (void)buffer_get_string_ptr_ret(&b
, NULL
); /* Skip nonce */
1523 key
= key_new(type
);
1524 key
->ecdsa_nid
= nid
;
1525 if ((curve
= buffer_get_string_ret(&b
, NULL
)) == NULL
) {
1526 error("key_from_blob: can't read ecdsa curve");
1529 if (key
->ecdsa_nid
!= key_curve_name_to_nid(curve
)) {
1530 error("key_from_blob: ecdsa curve doesn't match type");
1533 if (key
->ecdsa
!= NULL
)
1534 EC_KEY_free(key
->ecdsa
);
1535 if ((key
->ecdsa
= EC_KEY_new_by_curve_name(key
->ecdsa_nid
))
1537 fatal("key_from_blob: EC_KEY_new_by_curve_name failed");
1538 if ((q
= EC_POINT_new(EC_KEY_get0_group(key
->ecdsa
))) == NULL
)
1539 fatal("key_from_blob: EC_POINT_new failed");
1540 if (buffer_get_ecpoint_ret(&b
, EC_KEY_get0_group(key
->ecdsa
),
1542 error("key_from_blob: can't read ecdsa key point");
1545 if (key_ec_validate_public(EC_KEY_get0_group(key
->ecdsa
),
1548 if (EC_KEY_set_public_key(key
->ecdsa
, q
) != 1)
1549 fatal("key_from_blob: EC_KEY_set_public_key failed");
1551 key_dump_ec_point(EC_KEY_get0_group(key
->ecdsa
), q
);
1554 #endif /* OPENSSL_HAS_ECC */
1556 key
= key_new(type
);
1559 error("key_from_blob: cannot handle type %s", ktype
);
1562 if (key_is_cert(key
) && cert_parse(&b
, key
, blob
, blen
) == -1) {
1563 error("key_from_blob: can't parse cert data");
1566 rlen
= buffer_len(&b
);
1567 if (key
!= NULL
&& rlen
!= 0)
1568 error("key_from_blob: remaining bytes in key blob %d", rlen
);
1574 #ifdef OPENSSL_HAS_ECC
1583 key_to_blob(const Key
*key
, u_char
**blobp
, u_int
*lenp
)
1589 error("key_to_blob: key == NULL");
1593 switch (key
->type
) {
1594 case KEY_DSA_CERT_V00
:
1595 case KEY_RSA_CERT_V00
:
1597 case KEY_ECDSA_CERT
:
1599 /* Use the existing blob */
1600 buffer_append(&b
, buffer_ptr(&key
->cert
->certblob
),
1601 buffer_len(&key
->cert
->certblob
));
1604 buffer_put_cstring(&b
, key_ssh_name(key
));
1605 buffer_put_bignum2(&b
, key
->dsa
->p
);
1606 buffer_put_bignum2(&b
, key
->dsa
->q
);
1607 buffer_put_bignum2(&b
, key
->dsa
->g
);
1608 buffer_put_bignum2(&b
, key
->dsa
->pub_key
);
1610 #ifdef OPENSSL_HAS_ECC
1612 buffer_put_cstring(&b
, key_ssh_name(key
));
1613 buffer_put_cstring(&b
, key_curve_nid_to_name(key
->ecdsa_nid
));
1614 buffer_put_ecpoint(&b
, EC_KEY_get0_group(key
->ecdsa
),
1615 EC_KEY_get0_public_key(key
->ecdsa
));
1619 buffer_put_cstring(&b
, key_ssh_name(key
));
1620 buffer_put_bignum2(&b
, key
->rsa
->e
);
1621 buffer_put_bignum2(&b
, key
->rsa
->n
);
1624 error("key_to_blob: unsupported key type %d", key
->type
);
1628 len
= buffer_len(&b
);
1631 if (blobp
!= NULL
) {
1632 *blobp
= xmalloc(len
);
1633 memcpy(*blobp
, buffer_ptr(&b
), len
);
1635 memset(buffer_ptr(&b
), 0, len
);
1643 u_char
**sigp
, u_int
*lenp
,
1644 const u_char
*data
, u_int datalen
)
1646 switch (key
->type
) {
1647 case KEY_DSA_CERT_V00
:
1650 return ssh_dss_sign(key
, sigp
, lenp
, data
, datalen
);
1651 #ifdef OPENSSL_HAS_ECC
1652 case KEY_ECDSA_CERT
:
1654 return ssh_ecdsa_sign(key
, sigp
, lenp
, data
, datalen
);
1656 case KEY_RSA_CERT_V00
:
1659 return ssh_rsa_sign(key
, sigp
, lenp
, data
, datalen
);
1661 error("key_sign: invalid key type %d", key
->type
);
1667 * key_verify returns 1 for a correct signature, 0 for an incorrect signature
1673 const u_char
*signature
, u_int signaturelen
,
1674 const u_char
*data
, u_int datalen
)
1676 if (signaturelen
== 0)
1679 switch (key
->type
) {
1680 case KEY_DSA_CERT_V00
:
1683 return ssh_dss_verify(key
, signature
, signaturelen
, data
, datalen
);
1684 #ifdef OPENSSL_HAS_ECC
1685 case KEY_ECDSA_CERT
:
1687 return ssh_ecdsa_verify(key
, signature
, signaturelen
, data
, datalen
);
1689 case KEY_RSA_CERT_V00
:
1692 return ssh_rsa_verify(key
, signature
, signaturelen
, data
, datalen
);
1694 error("key_verify: invalid key type %d", key
->type
);
1699 /* Converts a private to a public key */
1701 key_demote(const Key
*k
)
1705 pk
= xcalloc(1, sizeof(*pk
));
1707 pk
->flags
= k
->flags
;
1708 pk
->ecdsa_nid
= k
->ecdsa_nid
;
1714 case KEY_RSA_CERT_V00
:
1716 key_cert_copy(k
, pk
);
1720 if ((pk
->rsa
= RSA_new()) == NULL
)
1721 fatal("key_demote: RSA_new failed");
1722 if ((pk
->rsa
->e
= BN_dup(k
->rsa
->e
)) == NULL
)
1723 fatal("key_demote: BN_dup failed");
1724 if ((pk
->rsa
->n
= BN_dup(k
->rsa
->n
)) == NULL
)
1725 fatal("key_demote: BN_dup failed");
1727 case KEY_DSA_CERT_V00
:
1729 key_cert_copy(k
, pk
);
1732 if ((pk
->dsa
= DSA_new()) == NULL
)
1733 fatal("key_demote: DSA_new failed");
1734 if ((pk
->dsa
->p
= BN_dup(k
->dsa
->p
)) == NULL
)
1735 fatal("key_demote: BN_dup failed");
1736 if ((pk
->dsa
->q
= BN_dup(k
->dsa
->q
)) == NULL
)
1737 fatal("key_demote: BN_dup failed");
1738 if ((pk
->dsa
->g
= BN_dup(k
->dsa
->g
)) == NULL
)
1739 fatal("key_demote: BN_dup failed");
1740 if ((pk
->dsa
->pub_key
= BN_dup(k
->dsa
->pub_key
)) == NULL
)
1741 fatal("key_demote: BN_dup failed");
1743 #ifdef OPENSSL_HAS_ECC
1744 case KEY_ECDSA_CERT
:
1745 key_cert_copy(k
, pk
);
1748 if ((pk
->ecdsa
= EC_KEY_new_by_curve_name(pk
->ecdsa_nid
)) == NULL
)
1749 fatal("key_demote: EC_KEY_new_by_curve_name failed");
1750 if (EC_KEY_set_public_key(pk
->ecdsa
,
1751 EC_KEY_get0_public_key(k
->ecdsa
)) != 1)
1752 fatal("key_demote: EC_KEY_set_public_key failed");
1756 fatal("key_free: bad key type %d", k
->type
);
1764 key_is_cert(const Key
*k
)
1769 case KEY_RSA_CERT_V00
:
1770 case KEY_DSA_CERT_V00
:
1773 case KEY_ECDSA_CERT
:
1780 /* Return the cert-less equivalent to a certified key type */
1782 key_type_plain(int type
)
1785 case KEY_RSA_CERT_V00
:
1788 case KEY_DSA_CERT_V00
:
1791 case KEY_ECDSA_CERT
:
1798 /* Convert a KEY_RSA or KEY_DSA to their _CERT equivalent */
1800 key_to_certified(Key
*k
, int legacy
)
1804 k
->cert
= cert_new();
1805 k
->type
= legacy
? KEY_RSA_CERT_V00
: KEY_RSA_CERT
;
1808 k
->cert
= cert_new();
1809 k
->type
= legacy
? KEY_DSA_CERT_V00
: KEY_DSA_CERT
;
1812 k
->cert
= cert_new();
1813 k
->type
= KEY_ECDSA_CERT
;
1816 error("%s: key has incorrect type %s", __func__
, key_type(k
));
1821 /* Convert a KEY_RSA_CERT or KEY_DSA_CERT to their raw key equivalent */
1823 key_drop_cert(Key
*k
)
1826 case KEY_RSA_CERT_V00
:
1831 case KEY_DSA_CERT_V00
:
1836 case KEY_ECDSA_CERT
:
1838 k
->type
= KEY_ECDSA
;
1841 error("%s: key has incorrect type %s", __func__
, key_type(k
));
1847 * Sign a KEY_RSA_CERT, KEY_DSA_CERT or KEY_ECDSA_CERT, (re-)generating
1848 * the signed certblob
1851 key_certify(Key
*k
, Key
*ca
)
1854 u_char
*ca_blob
, *sig_blob
, nonce
[32];
1855 u_int i
, ca_len
, sig_len
;
1857 if (k
->cert
== NULL
) {
1858 error("%s: key lacks cert info", __func__
);
1862 if (!key_is_cert(k
)) {
1863 error("%s: certificate has unknown type %d", __func__
,
1868 if (ca
->type
!= KEY_RSA
&& ca
->type
!= KEY_DSA
&&
1869 ca
->type
!= KEY_ECDSA
) {
1870 error("%s: CA key has unsupported type %s", __func__
,
1875 key_to_blob(ca
, &ca_blob
, &ca_len
);
1877 buffer_clear(&k
->cert
->certblob
);
1878 buffer_put_cstring(&k
->cert
->certblob
, key_ssh_name(k
));
1880 /* -v01 certs put nonce first */
1881 if (!key_cert_is_legacy(k
)) {
1882 arc4random_buf(&nonce
, sizeof(nonce
));
1883 buffer_put_string(&k
->cert
->certblob
, nonce
, sizeof(nonce
));
1887 case KEY_DSA_CERT_V00
:
1889 buffer_put_bignum2(&k
->cert
->certblob
, k
->dsa
->p
);
1890 buffer_put_bignum2(&k
->cert
->certblob
, k
->dsa
->q
);
1891 buffer_put_bignum2(&k
->cert
->certblob
, k
->dsa
->g
);
1892 buffer_put_bignum2(&k
->cert
->certblob
, k
->dsa
->pub_key
);
1894 #ifdef OPENSSL_HAS_ECC
1895 case KEY_ECDSA_CERT
:
1896 buffer_put_cstring(&k
->cert
->certblob
,
1897 key_curve_nid_to_name(k
->ecdsa_nid
));
1898 buffer_put_ecpoint(&k
->cert
->certblob
,
1899 EC_KEY_get0_group(k
->ecdsa
),
1900 EC_KEY_get0_public_key(k
->ecdsa
));
1903 case KEY_RSA_CERT_V00
:
1905 buffer_put_bignum2(&k
->cert
->certblob
, k
->rsa
->e
);
1906 buffer_put_bignum2(&k
->cert
->certblob
, k
->rsa
->n
);
1909 error("%s: key has incorrect type %s", __func__
, key_type(k
));
1910 buffer_clear(&k
->cert
->certblob
);
1915 /* -v01 certs have a serial number next */
1916 if (!key_cert_is_legacy(k
))
1917 buffer_put_int64(&k
->cert
->certblob
, k
->cert
->serial
);
1919 buffer_put_int(&k
->cert
->certblob
, k
->cert
->type
);
1920 buffer_put_cstring(&k
->cert
->certblob
, k
->cert
->key_id
);
1922 buffer_init(&principals
);
1923 for (i
= 0; i
< k
->cert
->nprincipals
; i
++)
1924 buffer_put_cstring(&principals
, k
->cert
->principals
[i
]);
1925 buffer_put_string(&k
->cert
->certblob
, buffer_ptr(&principals
),
1926 buffer_len(&principals
));
1927 buffer_free(&principals
);
1929 buffer_put_int64(&k
->cert
->certblob
, k
->cert
->valid_after
);
1930 buffer_put_int64(&k
->cert
->certblob
, k
->cert
->valid_before
);
1931 buffer_put_string(&k
->cert
->certblob
,
1932 buffer_ptr(&k
->cert
->critical
), buffer_len(&k
->cert
->critical
));
1934 /* -v01 certs have non-critical options here */
1935 if (!key_cert_is_legacy(k
)) {
1936 buffer_put_string(&k
->cert
->certblob
,
1937 buffer_ptr(&k
->cert
->extensions
),
1938 buffer_len(&k
->cert
->extensions
));
1941 /* -v00 certs put the nonce at the end */
1942 if (key_cert_is_legacy(k
))
1943 buffer_put_string(&k
->cert
->certblob
, nonce
, sizeof(nonce
));
1945 buffer_put_string(&k
->cert
->certblob
, NULL
, 0); /* reserved */
1946 buffer_put_string(&k
->cert
->certblob
, ca_blob
, ca_len
);
1949 /* Sign the whole mess */
1950 if (key_sign(ca
, &sig_blob
, &sig_len
, buffer_ptr(&k
->cert
->certblob
),
1951 buffer_len(&k
->cert
->certblob
)) != 0) {
1952 error("%s: signature operation failed", __func__
);
1953 buffer_clear(&k
->cert
->certblob
);
1956 /* Append signature and we are done */
1957 buffer_put_string(&k
->cert
->certblob
, sig_blob
, sig_len
);
1964 key_cert_check_authority(const Key
*k
, int want_host
, int require_principal
,
1965 const char *name
, const char **reason
)
1967 u_int i
, principal_matches
;
1968 time_t now
= time(NULL
);
1971 if (k
->cert
->type
!= SSH2_CERT_TYPE_HOST
) {
1972 *reason
= "Certificate invalid: not a host certificate";
1976 if (k
->cert
->type
!= SSH2_CERT_TYPE_USER
) {
1977 *reason
= "Certificate invalid: not a user certificate";
1982 error("%s: system clock lies before epoch", __func__
);
1983 *reason
= "Certificate invalid: not yet valid";
1986 if ((u_int64_t
)now
< k
->cert
->valid_after
) {
1987 *reason
= "Certificate invalid: not yet valid";
1990 if ((u_int64_t
)now
>= k
->cert
->valid_before
) {
1991 *reason
= "Certificate invalid: expired";
1994 if (k
->cert
->nprincipals
== 0) {
1995 if (require_principal
) {
1996 *reason
= "Certificate lacks principal list";
1999 } else if (name
!= NULL
) {
2000 principal_matches
= 0;
2001 for (i
= 0; i
< k
->cert
->nprincipals
; i
++) {
2002 if (strcmp(name
, k
->cert
->principals
[i
]) == 0) {
2003 principal_matches
= 1;
2007 if (!principal_matches
) {
2008 *reason
= "Certificate invalid: name is not a listed "
2017 key_cert_is_legacy(Key
*k
)
2020 case KEY_DSA_CERT_V00
:
2021 case KEY_RSA_CERT_V00
:
2028 /* XXX: these are really begging for a table-driven approach */
2030 key_curve_name_to_nid(const char *name
)
2032 #ifdef OPENSSL_HAS_ECC
2033 if (strcmp(name
, "nistp256") == 0)
2034 return NID_X9_62_prime256v1
;
2035 else if (strcmp(name
, "nistp384") == 0)
2036 return NID_secp384r1
;
2037 else if (strcmp(name
, "nistp521") == 0)
2038 return NID_secp521r1
;
2041 debug("%s: unsupported EC curve name \"%.100s\"", __func__
, name
);
2046 key_curve_nid_to_bits(int nid
)
2049 #ifdef OPENSSL_HAS_ECC
2050 case NID_X9_62_prime256v1
:
2058 error("%s: unsupported EC curve nid %d", __func__
, nid
);
2064 key_curve_nid_to_name(int nid
)
2066 #ifdef OPENSSL_HAS_ECC
2067 if (nid
== NID_X9_62_prime256v1
)
2069 else if (nid
== NID_secp384r1
)
2071 else if (nid
== NID_secp521r1
)
2074 error("%s: unsupported EC curve nid %d", __func__
, nid
);
2078 #ifdef OPENSSL_HAS_ECC
2080 key_ec_nid_to_evpmd(int nid
)
2082 int kbits
= key_curve_nid_to_bits(nid
);
2085 fatal("%s: invalid nid %d", __func__
, nid
);
2086 /* RFC5656 section 6.2.1 */
2088 return EVP_sha256();
2089 else if (kbits
<= 384)
2090 return EVP_sha384();
2092 return EVP_sha512();
2096 key_ec_validate_public(const EC_GROUP
*group
, const EC_POINT
*public)
2099 EC_POINT
*nq
= NULL
;
2100 BIGNUM
*order
, *x
, *y
, *tmp
;
2103 if ((bnctx
= BN_CTX_new()) == NULL
)
2104 fatal("%s: BN_CTX_new failed", __func__
);
2105 BN_CTX_start(bnctx
);
2108 * We shouldn't ever hit this case because bignum_get_ecpoint()
2109 * refuses to load GF2m points.
2111 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group
)) !=
2112 NID_X9_62_prime_field
) {
2113 error("%s: group is not a prime field", __func__
);
2118 if (EC_POINT_is_at_infinity(group
, public)) {
2119 error("%s: received degenerate public key (infinity)",
2124 if ((x
= BN_CTX_get(bnctx
)) == NULL
||
2125 (y
= BN_CTX_get(bnctx
)) == NULL
||
2126 (order
= BN_CTX_get(bnctx
)) == NULL
||
2127 (tmp
= BN_CTX_get(bnctx
)) == NULL
)
2128 fatal("%s: BN_CTX_get failed", __func__
);
2130 /* log2(x) > log2(order)/2, log2(y) > log2(order)/2 */
2131 if (EC_GROUP_get_order(group
, order
, bnctx
) != 1)
2132 fatal("%s: EC_GROUP_get_order failed", __func__
);
2133 if (EC_POINT_get_affine_coordinates_GFp(group
, public,
2135 fatal("%s: EC_POINT_get_affine_coordinates_GFp", __func__
);
2136 if (BN_num_bits(x
) <= BN_num_bits(order
) / 2) {
2137 error("%s: public key x coordinate too small: "
2138 "bits(x) = %d, bits(order)/2 = %d", __func__
,
2139 BN_num_bits(x
), BN_num_bits(order
) / 2);
2142 if (BN_num_bits(y
) <= BN_num_bits(order
) / 2) {
2143 error("%s: public key y coordinate too small: "
2144 "bits(y) = %d, bits(order)/2 = %d", __func__
,
2145 BN_num_bits(x
), BN_num_bits(order
) / 2);
2149 /* nQ == infinity (n == order of subgroup) */
2150 if ((nq
= EC_POINT_new(group
)) == NULL
)
2151 fatal("%s: BN_CTX_tmp failed", __func__
);
2152 if (EC_POINT_mul(group
, nq
, NULL
, public, order
, bnctx
) != 1)
2153 fatal("%s: EC_GROUP_mul failed", __func__
);
2154 if (EC_POINT_is_at_infinity(group
, nq
) != 1) {
2155 error("%s: received degenerate public key (nQ != infinity)",
2160 /* x < order - 1, y < order - 1 */
2161 if (!BN_sub(tmp
, order
, BN_value_one()))
2162 fatal("%s: BN_sub failed", __func__
);
2163 if (BN_cmp(x
, tmp
) >= 0) {
2164 error("%s: public key x coordinate >= group order - 1",
2168 if (BN_cmp(y
, tmp
) >= 0) {
2169 error("%s: public key y coordinate >= group order - 1",
2181 key_ec_validate_private(const EC_KEY
*key
)
2184 BIGNUM
*order
, *tmp
;
2187 if ((bnctx
= BN_CTX_new()) == NULL
)
2188 fatal("%s: BN_CTX_new failed", __func__
);
2189 BN_CTX_start(bnctx
);
2191 if ((order
= BN_CTX_get(bnctx
)) == NULL
||
2192 (tmp
= BN_CTX_get(bnctx
)) == NULL
)
2193 fatal("%s: BN_CTX_get failed", __func__
);
2195 /* log2(private) > log2(order)/2 */
2196 if (EC_GROUP_get_order(EC_KEY_get0_group(key
), order
, bnctx
) != 1)
2197 fatal("%s: EC_GROUP_get_order failed", __func__
);
2198 if (BN_num_bits(EC_KEY_get0_private_key(key
)) <=
2199 BN_num_bits(order
) / 2) {
2200 error("%s: private key too small: "
2201 "bits(y) = %d, bits(order)/2 = %d", __func__
,
2202 BN_num_bits(EC_KEY_get0_private_key(key
)),
2203 BN_num_bits(order
) / 2);
2207 /* private < order - 1 */
2208 if (!BN_sub(tmp
, order
, BN_value_one()))
2209 fatal("%s: BN_sub failed", __func__
);
2210 if (BN_cmp(EC_KEY_get0_private_key(key
), tmp
) >= 0) {
2211 error("%s: private key >= group order - 1", __func__
);
2220 #if defined(DEBUG_KEXECDH) || defined(DEBUG_PK)
2222 key_dump_ec_point(const EC_GROUP
*group
, const EC_POINT
*point
)
2227 if (point
== NULL
) {
2228 fputs("point=(NULL)\n", stderr
);
2231 if ((bnctx
= BN_CTX_new()) == NULL
)
2232 fatal("%s: BN_CTX_new failed", __func__
);
2233 BN_CTX_start(bnctx
);
2234 if ((x
= BN_CTX_get(bnctx
)) == NULL
|| (y
= BN_CTX_get(bnctx
)) == NULL
)
2235 fatal("%s: BN_CTX_get failed", __func__
);
2236 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group
)) !=
2237 NID_X9_62_prime_field
)
2238 fatal("%s: group is not a prime field", __func__
);
2239 if (EC_POINT_get_affine_coordinates_GFp(group
, point
, x
, y
, bnctx
) != 1)
2240 fatal("%s: EC_POINT_get_affine_coordinates_GFp", __func__
);
2241 fputs("x=", stderr
);
2242 BN_print_fp(stderr
, x
);
2243 fputs("\ny=", stderr
);
2244 BN_print_fp(stderr
, y
);
2245 fputs("\n", stderr
);
2250 key_dump_ec_key(const EC_KEY
*key
)
2252 const BIGNUM
*exponent
;
2254 key_dump_ec_point(EC_KEY_get0_group(key
), EC_KEY_get0_public_key(key
));
2255 fputs("exponent=", stderr
);
2256 if ((exponent
= EC_KEY_get0_private_key(key
)) == NULL
)
2257 fputs("(NULL)", stderr
);
2259 BN_print_fp(stderr
, EC_KEY_get0_private_key(key
));
2260 fputs("\n", stderr
);
2262 #endif /* defined(DEBUG_KEXECDH) || defined(DEBUG_PK) */
2263 #endif /* OPENSSL_HAS_ECC */