key.h revision 240075
1240075Sdes/* $OpenBSD: key.h,v 1.34 2012/05/23 03:28:28 djm Exp $ */ 276259Sgreen 365668Skris/* 492555Sdes * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved. 565668Skris * 665668Skris * Redistribution and use in source and binary forms, with or without 765668Skris * modification, are permitted provided that the following conditions 865668Skris * are met: 965668Skris * 1. Redistributions of source code must retain the above copyright 1065668Skris * notice, this list of conditions and the following disclaimer. 1165668Skris * 2. Redistributions in binary form must reproduce the above copyright 1265668Skris * notice, this list of conditions and the following disclaimer in the 1365668Skris * documentation and/or other materials provided with the distribution. 1465668Skris * 1565668Skris * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 1665668Skris * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1765668Skris * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 1865668Skris * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 1965668Skris * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 2065668Skris * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2165668Skris * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2265668Skris * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2365668Skris * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2465668Skris * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2565668Skris */ 2658582Skris#ifndef KEY_H 2758582Skris#define KEY_H 2858582Skris 29204917Sdes#include "buffer.h" 3076259Sgreen#include <openssl/rsa.h> 3176259Sgreen#include <openssl/dsa.h> 32221420Sdes#ifdef OPENSSL_HAS_ECC 33221420Sdes#include <openssl/ec.h> 34221420Sdes#endif 3576259Sgreen 3658582Skristypedef struct Key Key; 3758582Skrisenum types { 3876259Sgreen KEY_RSA1, 3958582Skris KEY_RSA, 4058582Skris KEY_DSA, 41221420Sdes KEY_ECDSA, 42204917Sdes KEY_RSA_CERT, 43204917Sdes KEY_DSA_CERT, 44221420Sdes KEY_ECDSA_CERT, 45215116Sdes KEY_RSA_CERT_V00, 46215116Sdes KEY_DSA_CERT_V00, 4776259Sgreen KEY_UNSPEC 4858582Skris}; 4976259Sgreenenum fp_type { 5076259Sgreen SSH_FP_SHA1, 51240075Sdes SSH_FP_MD5, 52240075Sdes SSH_FP_SHA256 5376259Sgreen}; 5476259Sgreenenum fp_rep { 5576259Sgreen SSH_FP_HEX, 56181111Sdes SSH_FP_BUBBLEBABBLE, 57181111Sdes SSH_FP_RANDOMART 5876259Sgreen}; 5992555Sdes 6092555Sdes/* key is stored in external hardware */ 6192555Sdes#define KEY_FLAG_EXT 0x0001 6292555Sdes 63204917Sdes#define CERT_MAX_PRINCIPALS 256 64204917Sdesstruct KeyCert { 65204917Sdes Buffer certblob; /* Kept around for use on wire */ 66204917Sdes u_int type; /* SSH2_CERT_TYPE_USER or SSH2_CERT_TYPE_HOST */ 67215116Sdes u_int64_t serial; 68204917Sdes char *key_id; 69204917Sdes u_int nprincipals; 70204917Sdes char **principals; 71204917Sdes u_int64_t valid_after, valid_before; 72215116Sdes Buffer critical; 73215116Sdes Buffer extensions; 74204917Sdes Key *signature_key; 75204917Sdes}; 76204917Sdes 7758582Skrisstruct Key { 7892555Sdes int type; 7992555Sdes int flags; 8058582Skris RSA *rsa; 8158582Skris DSA *dsa; 82221420Sdes int ecdsa_nid; /* NID of curve */ 83221420Sdes#ifdef OPENSSL_HAS_ECC 84221420Sdes EC_KEY *ecdsa; 85221420Sdes#else 86221420Sdes void *ecdsa; 87221420Sdes#endif 88204917Sdes struct KeyCert *cert; 8958582Skris}; 9058582Skris 91126274SdesKey *key_new(int); 92204917Sdesvoid key_add_private(Key *); 93126274SdesKey *key_new_private(int); 94126274Sdesvoid key_free(Key *); 95126274SdesKey *key_demote(const Key *); 96204917Sdesint key_equal_public(const Key *, const Key *); 97126274Sdesint key_equal(const Key *, const Key *); 98204917Sdeschar *key_fingerprint(Key *, enum fp_type, enum fp_rep); 99204917Sdesu_char *key_fingerprint_raw(Key *, enum fp_type, u_int *); 100126274Sdesconst char *key_type(const Key *); 101207319Sdesconst char *key_cert_type(const Key *); 102126274Sdesint key_write(const Key *, FILE *); 103126274Sdesint key_read(Key *, char **); 104126274Sdesu_int key_size(const Key *); 10558582Skris 10692555SdesKey *key_generate(int, u_int); 107126274SdesKey *key_from_private(const Key *); 10892555Sdesint key_type_from_name(char *); 109204917Sdesint key_is_cert(const Key *); 110204917Sdesint key_type_plain(int); 111215116Sdesint key_to_certified(Key *, int); 112204917Sdesint key_drop_cert(Key *); 113204917Sdesint key_certify(Key *, Key *); 114204917Sdesvoid key_cert_copy(const Key *, struct Key *); 115204917Sdesint key_cert_check_authority(const Key *, int, int, const char *, 116204917Sdes const char **); 117215116Sdesint key_cert_is_legacy(Key *); 11876259Sgreen 119221420Sdesint key_ecdsa_nid_from_name(const char *); 120221420Sdesint key_curve_name_to_nid(const char *); 121221420Sdesconst char * key_curve_nid_to_name(int); 122221420Sdesu_int key_curve_nid_to_bits(int); 123221420Sdesint key_ecdsa_bits_to_nid(int); 124221420Sdes#ifdef OPENSSL_HAS_ECC 125221420Sdesint key_ecdsa_key_to_nid(EC_KEY *); 126221420Sdesconst EVP_MD * key_ec_nid_to_evpmd(int nid); 127221420Sdesint key_ec_validate_public(const EC_GROUP *, const EC_POINT *); 128221420Sdesint key_ec_validate_private(const EC_KEY *); 129221420Sdes#endif 130221420Sdes 131126274SdesKey *key_from_blob(const u_char *, u_int); 132126274Sdesint key_to_blob(const Key *, u_char **, u_int *); 133126274Sdesconst char *key_ssh_name(const Key *); 134221420Sdesconst char *key_ssh_name_plain(const Key *); 135126274Sdesint key_names_valid2(const char *); 13676259Sgreen 137126274Sdesint key_sign(const Key *, u_char **, u_int *, const u_char *, u_int); 138126274Sdesint key_verify(const Key *, const u_char *, u_int, const u_char *, u_int); 13976259Sgreen 140126274Sdesint ssh_dss_sign(const Key *, u_char **, u_int *, const u_char *, u_int); 141126274Sdesint ssh_dss_verify(const Key *, const u_char *, u_int, const u_char *, u_int); 142221420Sdesint ssh_ecdsa_sign(const Key *, u_char **, u_int *, const u_char *, u_int); 143221420Sdesint ssh_ecdsa_verify(const Key *, const u_char *, u_int, const u_char *, u_int); 144126274Sdesint ssh_rsa_sign(const Key *, u_char **, u_int *, const u_char *, u_int); 145126274Sdesint ssh_rsa_verify(const Key *, const u_char *, u_int, const u_char *, u_int); 146113908Sdes 147221420Sdes#if defined(OPENSSL_HAS_ECC) && (defined(DEBUG_KEXECDH) || defined(DEBUG_PK)) 148221420Sdesvoid key_dump_ec_point(const EC_GROUP *, const EC_POINT *); 149221420Sdesvoid key_dump_ec_key(const EC_KEY *); 15058582Skris#endif 151221420Sdes 152221420Sdes#endif 153