libssh  0.8.4
The SSH library
pki_priv.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_PRIV_H_
22 #define PKI_PRIV_H_
23 
24 #include "libssh/pki.h"
25 
26 /* defined in bcrypt_pbkdf.c */
27 int bcrypt_pbkdf(const char *pass,
28  size_t passlen,
29  const uint8_t *salt,
30  size_t saltlen,
31  uint8_t *key,
32  size_t keylen,
33  unsigned int rounds);
34 
35 #define RSA_HEADER_BEGIN "-----BEGIN RSA PRIVATE KEY-----"
36 #define RSA_HEADER_END "-----END RSA PRIVATE KEY-----"
37 #define DSA_HEADER_BEGIN "-----BEGIN DSA PRIVATE KEY-----"
38 #define DSA_HEADER_END "-----END DSA PRIVATE KEY-----"
39 #define ECDSA_HEADER_BEGIN "-----BEGIN EC PRIVATE KEY-----"
40 #define ECDSA_HEADER_END "-----END EC PRIVATE KEY-----"
41 #define OPENSSH_HEADER_BEGIN "-----BEGIN OPENSSH PRIVATE KEY-----"
42 #define OPENSSH_HEADER_END "-----END OPENSSH PRIVATE KEY-----"
43 /* Magic defined in OpenSSH/PROTOCOL.key */
44 #define OPENSSH_AUTH_MAGIC "openssh-key-v1"
45 
46 int pki_key_ecdsa_nid_from_name(const char *name);
47 const char *pki_key_ecdsa_nid_to_name(int nid);
48 const char *ssh_key_signature_to_char(enum ssh_keytypes_e type,
49  enum ssh_digest_e hash_type);
50 enum ssh_digest_e ssh_key_type_to_hash(ssh_session session,
51  enum ssh_keytypes_e type);
52 
53 /* SSH Key Functions */
54 ssh_key pki_key_dup(const ssh_key key, int demote);
55 int pki_key_generate_rsa(ssh_key key, int parameter);
56 int pki_key_generate_dss(ssh_key key, int parameter);
57 int pki_key_generate_ecdsa(ssh_key key, int parameter);
58 int pki_key_generate_ed25519(ssh_key key);
59 
60 int pki_key_compare(const ssh_key k1,
61  const ssh_key k2,
62  enum ssh_keycmp_e what);
63 
64 /* SSH Private Key Functions */
65 enum ssh_keytypes_e pki_privatekey_type_from_string(const char *privkey);
66 ssh_key pki_private_key_from_base64(const char *b64_key,
67  const char *passphrase,
68  ssh_auth_callback auth_fn,
69  void *auth_data);
70 
71 ssh_string pki_private_key_to_pem(const ssh_key key,
72  const char *passphrase,
73  ssh_auth_callback auth_fn,
74  void *auth_data);
75 int pki_import_privkey_buffer(enum ssh_keytypes_e type,
76  ssh_buffer buffer,
77  ssh_key *pkey);
78 
79 /* SSH Public Key Functions */
80 int pki_pubkey_build_dss(ssh_key key,
81  ssh_string p,
82  ssh_string q,
83  ssh_string g,
84  ssh_string pubkey);
85 int pki_pubkey_build_rsa(ssh_key key,
86  ssh_string e,
87  ssh_string n);
88 int pki_pubkey_build_ecdsa(ssh_key key, int nid, ssh_string e);
89 ssh_string pki_publickey_to_blob(const ssh_key key);
90 
91 /* SSH Private Key Functions */
92 int pki_privkey_build_dss(ssh_key key,
93  ssh_string p,
94  ssh_string q,
95  ssh_string g,
96  ssh_string pubkey,
97  ssh_string privkey);
98 int pki_privkey_build_rsa(ssh_key key,
99  ssh_string n,
100  ssh_string e,
101  ssh_string d,
102  ssh_string iqmp,
103  ssh_string p,
104  ssh_string q);
105 int pki_privkey_build_ecdsa(ssh_key key,
106  int nid,
107  ssh_string e,
108  ssh_string exp);
109 ssh_string pki_publickey_to_blob(const ssh_key key);
110 
111 /* SSH Signature Functions */
112 ssh_string pki_signature_to_blob(const ssh_signature sign);
113 ssh_signature pki_signature_from_blob(const ssh_key pubkey,
114  const ssh_string sig_blob,
115  enum ssh_keytypes_e type,
116  enum ssh_digest_e hash_type);
117 int pki_signature_verify(ssh_session session,
118  const ssh_signature sig,
119  const ssh_key key,
120  const unsigned char *hash,
121  size_t hlen);
122 
123 /* SSH Signing Functions */
124 #define pki_do_sign(key, hash, hlen) \
125  pki_do_sign_hash(key, hash, hlen, SSH_DIGEST_AUTO)
126 ssh_signature pki_do_sign_hash(const ssh_key privkey,
127  const unsigned char *hash,
128  size_t hlen,
129  enum ssh_digest_e hash_type);
130 #define pki_do_sign_sessionid(key, hash, hlen) \
131  pki_do_sign_sessionid_hash(key, hash, hlen, SSH_DIGEST_AUTO)
132 ssh_signature pki_do_sign_sessionid_hash(const ssh_key key,
133  const unsigned char *hash,
134  size_t hlen,
135  enum ssh_digest_e hash_type);
136 int pki_ed25519_sign(const ssh_key privkey, ssh_signature sig,
137  const unsigned char *hash, size_t hlen);
138 int pki_ed25519_verify(const ssh_key pubkey, ssh_signature sig,
139  const unsigned char *hash, size_t hlen);
140 int pki_ed25519_key_cmp(const ssh_key k1,
141  const ssh_key k2,
142  enum ssh_keycmp_e what);
143 int pki_ed25519_key_dup(ssh_key new, const ssh_key key);
144 int pki_ed25519_public_key_to_blob(ssh_buffer buffer, ssh_key key);
145 ssh_string pki_ed25519_sig_to_blob(ssh_signature sig);
146 int pki_ed25519_sig_from_blob(ssh_signature sig, ssh_string sig_blob);
147 int pki_privkey_build_ed25519(ssh_key key,
148  ssh_string pubkey,
149  ssh_string privkey);
150 
151 /* PKI Container OpenSSH */
152 ssh_key ssh_pki_openssh_pubkey_import(const char *text_key);
153 ssh_key ssh_pki_openssh_privkey_import(const char *text_key,
154  const char *passphrase, ssh_auth_callback auth_fn, void *auth_data);
155 ssh_string ssh_pki_openssh_privkey_export(const ssh_key privkey,
156  const char *passphrase, ssh_auth_callback auth_fn, void *auth_data);
157 
158 #endif /* PKI_PRIV_H_ */
Definition: pki.h:42
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, unless the SHA2 extension (RFC 8332) is negotiated during key exchange.
Definition: pki.c:306
Definition: session.h:102
const char * ssh_key_signature_to_char(enum ssh_keytypes_e type, enum ssh_digest_e hash_type)
Convert a signature type to a string.
Definition: pki.c:204
Definition: string.h:29
Definition: pki.h:70
Definition: buffer.c:47