25#include "libssh/priv.h"
26#ifdef HAVE_OPENSSL_EC_H
27#include <openssl/ec.h>
29#ifdef HAVE_OPENSSL_ECDSA_H
30#include <openssl/ecdsa.h>
33#include <openssl/evp.h>
35#include "libssh/crypto.h"
39#define ED25519_SIG_LEN 64
41#include "libssh/ed25519.h"
44#define ED25519_KEY_LEN 32
46#define MAX_PUBKEY_SIZE 0x100000
47#define MAX_PRIVKEY_SIZE 0x400000
49#define RSA_MIN_KEY_SIZE 1024
50#define RSA_MIN_FIPS_KEY_SIZE 2048
51#define RSA_DEFAULT_KEY_SIZE 3072
53#define SSH_KEY_FLAG_EMPTY 0x0
54#define SSH_KEY_FLAG_PUBLIC 0x0001
55#define SSH_KEY_FLAG_PRIVATE 0x0002
56#define SSH_KEY_FLAG_PKCS11_URI 0x0004
60#define SSHSIG_VERSION 0x01
61#define SSHSIG_MAGIC_PREAMBLE "SSHSIG"
62#define SSHSIG_MAGIC_PREAMBLE_LEN (sizeof(SSHSIG_MAGIC_PREAMBLE) - 1)
63#define SSHSIG_BEGIN_SIGNATURE "-----BEGIN SSH SIGNATURE-----"
64#define SSHSIG_END_SIGNATURE "-----END SSH SIGNATURE-----"
65#define SSHSIG_LINE_LENGTH 76
67struct ssh_key_struct {
68 enum ssh_keytypes_e type;
72#if defined(HAVE_LIBGCRYPT)
75#elif defined(HAVE_LIBMBEDCRYPTO)
76 mbedtls_pk_context *pk;
77 mbedtls_ecdsa_context *ecdsa;
78#elif defined(HAVE_LIBCRYPTO)
84 uint8_t *ed25519_pubkey;
87 ed25519_pubkey *ed25519_pubkey;
88 ed25519_privkey *ed25519_privkey;
90 ssh_string sk_application;
92 enum ssh_keytypes_e cert_type;
96 ssh_string sk_key_handle;
97 ssh_string sk_reserved;
100 ssh_string sk_user_id;
103struct ssh_signature_struct {
104 enum ssh_keytypes_e type;
105 enum ssh_digest_e hash_type;
107#if defined(HAVE_LIBGCRYPT)
109 gcry_sexp_t ecdsa_sig;
110#elif defined(HAVE_LIBMBEDCRYPTO)
112 struct mbedtls_ecdsa_sig ecdsa_sig;
114#ifndef HAVE_LIBCRYPTO
115 ed25519_signature *ed25519_sig;
124typedef struct ssh_signature_struct *ssh_signature;
135 enum ssh_keytypes_e type);
139 enum ssh_keytypes_e type);
140enum ssh_digest_e ssh_key_hash_from_name(
const char *name);
142#define is_ecdsa_key_type(t) \
143 ((t) >= SSH_KEYTYPE_ECDSA_P256 && (t) <= SSH_KEYTYPE_ECDSA_P521)
145#define is_cert_type(kt)\
146 ((kt) == SSH_KEYTYPE_RSA_CERT01 ||\
147 (kt) == SSH_KEYTYPE_SK_ECDSA_CERT01 ||\
148 (kt) == SSH_KEYTYPE_SK_ED25519_CERT01 ||\
149 ((kt) >= SSH_KEYTYPE_ECDSA_P256_CERT01 &&\
150 (kt) <= SSH_KEYTYPE_ED25519_CERT01))
152#define is_sk_key_type(kt) \
153 ((kt) == SSH_KEYTYPE_SK_ECDSA || (kt) == SSH_KEYTYPE_SK_ED25519 || \
154 (kt) == SSH_KEYTYPE_SK_ECDSA_CERT01 || \
155 (kt) == SSH_KEYTYPE_SK_ED25519_CERT01)
158ssh_signature ssh_signature_new(
void);
159void ssh_signature_free(ssh_signature sign);
160#define SSH_SIGNATURE_FREE(x) \
161 do { ssh_signature_free(x); x = NULL; } while(0)
163int ssh_pki_export_signature_blob(
const ssh_signature sign,
164 ssh_string *sign_blob);
165int ssh_pki_import_signature_blob(
const ssh_string sig_blob,
166 const ssh_key pubkey,
167 ssh_signature *psig);
168int ssh_pki_signature_verify(ssh_session session,
171 const unsigned char *digest,
175int ssh_pki_export_pubkey_blob(
const ssh_key key,
177int ssh_pki_import_pubkey_blob(
const ssh_string key_blob,
180int ssh_pki_import_cert_blob(
const ssh_string cert_blob,
184int ssh_pki_export_privkey_blob(
const ssh_key key,
189ssh_string ssh_pki_do_sign(ssh_session session, ssh_buffer sigbuf,
190 const ssh_key privatekey,
enum ssh_digest_e hash_type);
191ssh_string ssh_pki_do_sign_agent(ssh_session session,
192 struct ssh_buffer_struct *buf,
193 const ssh_key pubkey);
194ssh_string ssh_srv_pki_do_sign_sessionid(ssh_session session,
195 const ssh_key privkey,
196 const enum ssh_digest_e digest);
199ssh_public_key ssh_pki_convert_key_to_publickey(
const ssh_key key);
200ssh_private_key ssh_pki_convert_key_to_privatekey(
const ssh_key key);
206int ssh_key_size(ssh_key key);
209#ifdef WITH_PKCS11_URI
210bool ssh_pki_is_uri(
const char *filename);
211char *ssh_pki_export_pub_uri_from_priv_uri(
const char *priv_uri);
enum ssh_digest_e ssh_key_type_to_hash(ssh_session session, enum ssh_keytypes_e type)
Convert a key type to a hash type. This is usually unambiguous for all the key types,...
Definition pki.c:628
enum ssh_keytypes_e ssh_key_type_plain(enum ssh_keytypes_e type)
Get the public key type corresponding to a certificate type.
Definition pki.c:781
enum ssh_keytypes_e ssh_key_type_from_signature_name(const char *name)
Convert a ssh key algorithm name to a ssh key algorithm type.
Definition pki.c:710
int ssh_key_algorithm_allowed(ssh_session session, const char *type)
Checks the given key against the configured allowed public key algorithm types.
Definition pki.c:494
void ssh_key_clean(ssh_key key)
clean up the key and deallocate all existing keys
Definition pki.c:221
const char * ssh_key_get_signature_algorithm(ssh_session session, enum ssh_keytypes_e type)
Gets signature algorithm name to be used with the given key type.
Definition pki.c:678
bool ssh_key_size_allowed(ssh_session session, ssh_key key)
Check the given key is acceptable in regards to the key size policy specified by the configuration.
Definition pki.c:548