libssh  0.10.90
The SSH library
Loading...
Searching...
No Matches
pki.h
1/*
2 * This file is part of the SSH Library
3 *
4 * Copyright (c) 2010 by Aris Adamantiadis
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#ifndef PKI_H_
22#define PKI_H_
23
24#include <stdint.h>
25#include "libssh/priv.h"
26#ifdef HAVE_OPENSSL_EC_H
27#include <openssl/ec.h>
28#endif
29#ifdef HAVE_OPENSSL_ECDSA_H
30#include <openssl/ecdsa.h>
31#endif
32#ifdef HAVE_LIBCRYPTO
33#include <openssl/evp.h>
34#endif
35#include "libssh/crypto.h"
36#ifdef HAVE_LIBCRYPTO
37/* If using OpenSSL implementation, define the signature length which would be
38 * defined in libssh/ed25519.h otherwise */
39#define ED25519_SIG_LEN 64
40#else
41#include "libssh/ed25519.h"
42#endif
43/* This definition is used for both OpenSSL and internal implementations */
44#define ED25519_KEY_LEN 32
45
46#define MAX_PUBKEY_SIZE 0x100000 /* 1M */
47#define MAX_PRIVKEY_SIZE 0x400000 /* 4M */
48
49#define SSH_KEY_FLAG_EMPTY 0x0
50#define SSH_KEY_FLAG_PUBLIC 0x0001
51#define SSH_KEY_FLAG_PRIVATE 0x0002
52#define SSH_KEY_FLAG_PKCS11_URI 0x0004
53
54struct ssh_key_struct {
55 enum ssh_keytypes_e type;
56 int flags;
57 const char *type_c; /* Don't free it ! it is static */
58 int ecdsa_nid;
59#if defined(HAVE_LIBGCRYPT)
60 gcry_sexp_t rsa;
61 gcry_sexp_t ecdsa;
62#elif defined(HAVE_LIBMBEDCRYPTO)
63 mbedtls_pk_context *rsa;
64 mbedtls_ecdsa_context *ecdsa;
65#elif defined(HAVE_LIBCRYPTO)
66 /* This holds either ENGINE key for PKCS#11 support or just key in
67 * high-level format */
68 EVP_PKEY *key;
69 uint8_t *ed25519_pubkey;
70 uint8_t *ed25519_privkey;
71#endif /* HAVE_LIBGCRYPT */
72#ifndef HAVE_LIBCRYPTO
73 ed25519_pubkey *ed25519_pubkey;
74 ed25519_privkey *ed25519_privkey;
75#endif /* HAVE_LIBCRYPTO */
76 ssh_string sk_application;
77 ssh_buffer cert;
78 enum ssh_keytypes_e cert_type;
79};
80
81struct ssh_signature_struct {
82 enum ssh_keytypes_e type;
83 enum ssh_digest_e hash_type;
84 const char *type_c;
85#if defined(HAVE_LIBGCRYPT)
86 gcry_sexp_t rsa_sig;
87 gcry_sexp_t ecdsa_sig;
88#elif defined(HAVE_LIBMBEDCRYPTO)
89 ssh_string rsa_sig;
90 struct mbedtls_ecdsa_sig ecdsa_sig;
91#endif /* HAVE_LIBGCRYPT */
92#ifndef HAVE_LIBCRYPTO
93 ed25519_signature *ed25519_sig;
94#endif /* HAVE_LIBGCRYPT */
95 ssh_string raw_sig;
96
97 /* Security Key specific additions */
98 uint8_t sk_flags;
99 uint32_t sk_counter;
100};
101
102typedef struct ssh_signature_struct *ssh_signature;
103
104#ifdef __cplusplus
105extern "C" {
106#endif
107
108/* SSH Key Functions */
109void ssh_key_clean (ssh_key key);
110
111const char *
112ssh_key_get_signature_algorithm(ssh_session session,
113 enum ssh_keytypes_e type);
114enum ssh_keytypes_e ssh_key_type_from_signature_name(const char *name);
115enum ssh_keytypes_e ssh_key_type_plain(enum ssh_keytypes_e type);
116enum ssh_digest_e ssh_key_type_to_hash(ssh_session session,
117 enum ssh_keytypes_e type);
118enum ssh_digest_e ssh_key_hash_from_name(const char *name);
119
120#define is_ecdsa_key_type(t) \
121 ((t) >= SSH_KEYTYPE_ECDSA_P256 && (t) <= SSH_KEYTYPE_ECDSA_P521)
122
123#define is_cert_type(kt)\
124 ((kt) == SSH_KEYTYPE_RSA_CERT01 ||\
125 (kt) == SSH_KEYTYPE_SK_ECDSA_CERT01 ||\
126 (kt) == SSH_KEYTYPE_SK_ED25519_CERT01 ||\
127 ((kt) >= SSH_KEYTYPE_ECDSA_P256_CERT01 &&\
128 (kt) <= SSH_KEYTYPE_ED25519_CERT01))
129
130/* SSH Signature Functions */
131ssh_signature ssh_signature_new(void);
132void ssh_signature_free(ssh_signature sign);
133#define SSH_SIGNATURE_FREE(x) \
134 do { ssh_signature_free(x); x = NULL; } while(0)
135
136int ssh_pki_export_signature_blob(const ssh_signature sign,
137 ssh_string *sign_blob);
138int ssh_pki_import_signature_blob(const ssh_string sig_blob,
139 const ssh_key pubkey,
140 ssh_signature *psig);
141int ssh_pki_signature_verify(ssh_session session,
142 ssh_signature sig,
143 const ssh_key key,
144 const unsigned char *digest,
145 size_t dlen);
146
147/* SSH Public Key Functions */
148int ssh_pki_export_pubkey_blob(const ssh_key key,
149 ssh_string *pblob);
150int ssh_pki_import_pubkey_blob(const ssh_string key_blob,
151 ssh_key *pkey);
152
153int ssh_pki_import_cert_blob(const ssh_string cert_blob,
154 ssh_key *pkey);
155
156/* SSH Private Key Functions */
157int ssh_pki_export_privkey_blob(const ssh_key key,
158 ssh_string *pblob);
159
160
161/* SSH Signing Functions */
162ssh_string ssh_pki_do_sign(ssh_session session, ssh_buffer sigbuf,
163 const ssh_key privatekey, enum ssh_digest_e hash_type);
164ssh_string ssh_pki_do_sign_agent(ssh_session session,
165 struct ssh_buffer_struct *buf,
166 const ssh_key pubkey);
167ssh_string ssh_srv_pki_do_sign_sessionid(ssh_session session,
168 const ssh_key privkey,
169 const enum ssh_digest_e digest);
170
171/* Temporary functions, to be removed after migration to ssh_key */
172ssh_public_key ssh_pki_convert_key_to_publickey(const ssh_key key);
173ssh_private_key ssh_pki_convert_key_to_privatekey(const ssh_key key);
174
175int ssh_key_algorithm_allowed(ssh_session session, const char *type);
176bool ssh_key_size_allowed(ssh_session session, ssh_key key);
177
178/* Return the key size in bits */
179int ssh_key_size(ssh_key key);
180
181/* PKCS11 URI function to check if filename is a path or a PKCS11 URI */
182#ifdef WITH_PKCS11_URI
183bool ssh_pki_is_uri(const char *filename);
184char *ssh_pki_export_pub_uri_from_priv_uri(const char *priv_uri);
185#endif /* WITH_PKCS11_URI */
186
187#ifdef __cplusplus
188}
189#endif
190
191#endif /* PKI_H_ */
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:424
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:595
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:524
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:345
void ssh_key_clean(ssh_key key)
clean up the key and deallocate all existing keys
Definition pki.c:139
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:492
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:399