1*27a1722dSdjm /* $OpenBSD: ssh_api.h,v 1.2 2018/04/10 00:10:49 djm Exp $ */ 2d9c3c4c1Smarkus /* 3d9c3c4c1Smarkus * Copyright (c) 2012 Markus Friedl. All rights reserved. 4d9c3c4c1Smarkus * 5d9c3c4c1Smarkus * Permission to use, copy, modify, and distribute this software for any 6d9c3c4c1Smarkus * purpose with or without fee is hereby granted, provided that the above 7d9c3c4c1Smarkus * copyright notice and this permission notice appear in all copies. 8d9c3c4c1Smarkus * 9d9c3c4c1Smarkus * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10d9c3c4c1Smarkus * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11d9c3c4c1Smarkus * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12d9c3c4c1Smarkus * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13d9c3c4c1Smarkus * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14d9c3c4c1Smarkus * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15d9c3c4c1Smarkus * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16d9c3c4c1Smarkus */ 17d9c3c4c1Smarkus 18d9c3c4c1Smarkus #ifndef API_H 19d9c3c4c1Smarkus #define API_H 20d9c3c4c1Smarkus 21d9c3c4c1Smarkus #include <sys/queue.h> 22d9c3c4c1Smarkus #include <sys/types.h> 23d9c3c4c1Smarkus #include <signal.h> 24d9c3c4c1Smarkus 25d9c3c4c1Smarkus #include "cipher.h" 26d9c3c4c1Smarkus #include "sshkey.h" 27d9c3c4c1Smarkus #include "kex.h" 28d9c3c4c1Smarkus #include "ssh.h" 29d9c3c4c1Smarkus #include "ssh2.h" 30d9c3c4c1Smarkus #include "packet.h" 31d9c3c4c1Smarkus 32d9c3c4c1Smarkus struct kex_params { 33d9c3c4c1Smarkus char *proposal[PROPOSAL_MAX]; 34d9c3c4c1Smarkus }; 35d9c3c4c1Smarkus 36d9c3c4c1Smarkus /* public SSH API functions */ 37d9c3c4c1Smarkus 38d9c3c4c1Smarkus /* 39d9c3c4c1Smarkus * ssh_init() create a ssh connection object with given (optional) 40d9c3c4c1Smarkus * key exchange parameters. 41d9c3c4c1Smarkus */ 42d9c3c4c1Smarkus int ssh_init(struct ssh **, int is_server, struct kex_params *kex_params); 43d9c3c4c1Smarkus 44d9c3c4c1Smarkus /* 45d9c3c4c1Smarkus * release ssh connection state. 46d9c3c4c1Smarkus */ 47d9c3c4c1Smarkus void ssh_free(struct ssh *); 48d9c3c4c1Smarkus 49d9c3c4c1Smarkus /* 50d9c3c4c1Smarkus * attach application specific data to the connection state 51d9c3c4c1Smarkus */ 52d9c3c4c1Smarkus void ssh_set_app_data(struct ssh *, void *); 53d9c3c4c1Smarkus void *ssh_get_app_data(struct ssh *); 54d9c3c4c1Smarkus 55d9c3c4c1Smarkus /* 56d9c3c4c1Smarkus * ssh_add_hostkey() registers a private/public hostkey for an ssh 57d9c3c4c1Smarkus * connection. 58d9c3c4c1Smarkus * ssh_add_hostkey() needs to be called before a key exchange is 59d9c3c4c1Smarkus * initiated with ssh_packet_next(). 60d9c3c4c1Smarkus * private hostkeys are required if we need to act as a server. 61d9c3c4c1Smarkus * public hostkeys are used to verify the servers hostkey. 62d9c3c4c1Smarkus */ 63d9c3c4c1Smarkus int ssh_add_hostkey(struct ssh *ssh, struct sshkey *key); 64d9c3c4c1Smarkus 65d9c3c4c1Smarkus /* 66d9c3c4c1Smarkus * ssh_set_verify_host_key_callback() registers a callback function 67d9c3c4c1Smarkus * which should be called instead of the default verification. The 68d9c3c4c1Smarkus * function given must return 0 if the hostkey is ok, -1 if the 69d9c3c4c1Smarkus * verification has failed. 70d9c3c4c1Smarkus */ 71d9c3c4c1Smarkus int ssh_set_verify_host_key_callback(struct ssh *ssh, 72d9c3c4c1Smarkus int (*cb)(struct sshkey *, struct ssh *)); 73d9c3c4c1Smarkus 74d9c3c4c1Smarkus /* 75d9c3c4c1Smarkus * ssh_packet_next() advances to the next input packet and returns 76d9c3c4c1Smarkus * the packet type in typep. 77d9c3c4c1Smarkus * ssh_packet_next() works by processing an input byte-stream, 78d9c3c4c1Smarkus * decrypting the received data and hiding the key-exchange from 79d9c3c4c1Smarkus * the caller. 80d9c3c4c1Smarkus * ssh_packet_next() sets typep if there is no new packet available. 81d9c3c4c1Smarkus * in this case the caller must fill the input byte-stream by passing 82d9c3c4c1Smarkus * the data received over network to ssh_input_append(). 83*27a1722dSdjm * additionally, the caller needs to send the resulting output 84d9c3c4c1Smarkus * byte-stream back over the network. otherwise the key exchange 85d9c3c4c1Smarkus * would not proceed. the output byte-stream is accessed through 86d9c3c4c1Smarkus * ssh_output_ptr(). 87d9c3c4c1Smarkus */ 88d9c3c4c1Smarkus int ssh_packet_next(struct ssh *ssh, u_char *typep); 89d9c3c4c1Smarkus 90d9c3c4c1Smarkus /* 91d9c3c4c1Smarkus * ssh_packet_payload() returns a pointer to the raw payload data of 92d9c3c4c1Smarkus * the current input packet and the length of this payload. 93d9c3c4c1Smarkus * the payload is accessible until ssh_packet_next() is called again. 94d9c3c4c1Smarkus */ 95d9c3c4c1Smarkus const u_char *ssh_packet_payload(struct ssh *ssh, size_t *lenp); 96d9c3c4c1Smarkus 97d9c3c4c1Smarkus /* 98d9c3c4c1Smarkus * ssh_packet_put() creates an encrypted packet with the given type 99d9c3c4c1Smarkus * and payload. 100d9c3c4c1Smarkus * the encrypted packet is appended to the output byte-stream. 101d9c3c4c1Smarkus */ 102d9c3c4c1Smarkus int ssh_packet_put(struct ssh *ssh, int type, const u_char *data, 103d9c3c4c1Smarkus size_t len); 104d9c3c4c1Smarkus 105d9c3c4c1Smarkus /* 106d9c3c4c1Smarkus * ssh_input_space() checks if 'len' bytes can be appended to the 107d9c3c4c1Smarkus * input byte-stream. 108d9c3c4c1Smarkus */ 109d9c3c4c1Smarkus int ssh_input_space(struct ssh *ssh, size_t len); 110d9c3c4c1Smarkus 111d9c3c4c1Smarkus /* 112d9c3c4c1Smarkus * ssh_input_append() appends data to the input byte-stream. 113d9c3c4c1Smarkus */ 114d9c3c4c1Smarkus int ssh_input_append(struct ssh *ssh, const u_char *data, size_t len); 115d9c3c4c1Smarkus 116d9c3c4c1Smarkus /* 117d9c3c4c1Smarkus * ssh_output_space() checks if 'len' bytes can be appended to the 118d9c3c4c1Smarkus * output byte-stream. XXX 119d9c3c4c1Smarkus */ 120d9c3c4c1Smarkus int ssh_output_space(struct ssh *ssh, size_t len); 121d9c3c4c1Smarkus 122d9c3c4c1Smarkus /* 123d9c3c4c1Smarkus * ssh_output_ptr() retrieves both a pointer and the length of the 124d9c3c4c1Smarkus * current output byte-stream. the bytes need to be sent over the 125d9c3c4c1Smarkus * network. the number of bytes that have been successfully sent can 126d9c3c4c1Smarkus * be removed from the output byte-stream with ssh_output_consume(). 127d9c3c4c1Smarkus */ 128d9c3c4c1Smarkus const u_char *ssh_output_ptr(struct ssh *ssh, size_t *len); 129d9c3c4c1Smarkus 130d9c3c4c1Smarkus /* 131d9c3c4c1Smarkus * ssh_output_consume() removes the given number of bytes from 132d9c3c4c1Smarkus * the output byte-stream. 133d9c3c4c1Smarkus */ 134d9c3c4c1Smarkus int ssh_output_consume(struct ssh *ssh, size_t len); 135d9c3c4c1Smarkus 136d9c3c4c1Smarkus #endif 137