libssh  0.7.3
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 #define ssh_pki_log(...) \
47  _ssh_log(SSH_LOG_FUNCTIONS, __func__, __VA_ARGS__)
48 void _ssh_pki_log(const char *function,
49  const char *format, ...) PRINTF_ATTRIBUTE(2, 3);
50 
51 int pki_key_ecdsa_nid_from_name(const char *name);
52 const char *pki_key_ecdsa_nid_to_name(int nid);
53 
54 /* SSH Key Functions */
55 ssh_key pki_key_dup(const ssh_key key, int demote);
56 int pki_key_generate_rsa(ssh_key key, int parameter);
57 int pki_key_generate_dss(ssh_key key, int parameter);
58 int pki_key_generate_ecdsa(ssh_key key, int parameter);
59 int pki_key_generate_ed25519(ssh_key key);
60 
61 int pki_key_compare(const ssh_key k1,
62  const ssh_key k2,
63  enum ssh_keycmp_e what);
64 
65 /* SSH Private Key Functions */
66 enum ssh_keytypes_e pki_privatekey_type_from_string(const char *privkey);
67 ssh_key pki_private_key_from_base64(const char *b64_key,
68  const char *passphrase,
69  ssh_auth_callback auth_fn,
70  void *auth_data);
71 
72 ssh_string pki_private_key_to_pem(const ssh_key key,
73  const char *passphrase,
74  ssh_auth_callback auth_fn,
75  void *auth_data);
76 
77 /* SSH Public Key Functions */
78 int pki_pubkey_build_dss(ssh_key key,
79  ssh_string p,
80  ssh_string q,
81  ssh_string g,
82  ssh_string pubkey);
83 int pki_pubkey_build_rsa(ssh_key key,
84  ssh_string e,
85  ssh_string n);
86 int pki_pubkey_build_ecdsa(ssh_key key, int nid, ssh_string e);
87 ssh_string pki_publickey_to_blob(const ssh_key key);
88 int pki_export_pubkey_rsa1(const ssh_key key,
89  const char *host,
90  char *rsa1,
91  size_t rsa1_len);
92 
93 /* SSH Signature Functions */
94 ssh_string pki_signature_to_blob(const ssh_signature sign);
95 ssh_signature pki_signature_from_blob(const ssh_key pubkey,
96  const ssh_string sig_blob,
97  enum ssh_keytypes_e type);
98 int pki_signature_verify(ssh_session session,
99  const ssh_signature sig,
100  const ssh_key key,
101  const unsigned char *hash,
102  size_t hlen);
103 
104 /* SSH Signing Functions */
105 ssh_signature pki_do_sign(const ssh_key privkey,
106  const unsigned char *hash,
107  size_t hlen);
108 ssh_signature pki_do_sign_sessionid(const ssh_key key,
109  const unsigned char *hash,
110  size_t hlen);
111 int pki_ed25519_sign(const ssh_key privkey, ssh_signature sig,
112  const unsigned char *hash, size_t hlen);
113 int pki_ed25519_verify(const ssh_key pubkey, ssh_signature sig,
114  const unsigned char *hash, size_t hlen);
115 int pki_ed25519_key_cmp(const ssh_key k1,
116  const ssh_key k2,
117  enum ssh_keycmp_e what);
118 int pki_ed25519_key_dup(ssh_key new, const ssh_key key);
119 int pki_ed25519_public_key_to_blob(ssh_buffer buffer, ssh_key key);
120 ssh_string pki_ed25519_sig_to_blob(ssh_signature sig);
121 int pki_ed25519_sig_from_blob(ssh_signature sig, ssh_string sig_blob);
122 
123 /* PKI Container OpenSSH */
124 ssh_key ssh_pki_openssh_privkey_import(const char *text_key,
125  const char *passphrase, ssh_auth_callback auth_fn, void *auth_data);
126 ssh_string ssh_pki_openssh_privkey_export(const ssh_key privkey,
127  const char *passphrase, ssh_auth_callback auth_fn, void *auth_data);
128 
129 #endif /* PKI_PRIV_H_ */