ssl.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. /**
  2. * \file ssl.h
  3. *
  4. * \brief SSL/TLS functions.
  5. *
  6. * Copyright (C) 2006-2010, Brainspark B.V.
  7. *
  8. * This file is part of PolarSSL (http://www.polarssl.org)
  9. * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
  10. *
  11. * All rights reserved.
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License along
  24. * with this program; if not, write to the Free Software Foundation, Inc.,
  25. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  26. */
  27. #ifndef POLARSSL_SSL_H
  28. #define POLARSSL_SSL_H
  29. #include <time.h>
  30. #include "polarssl/net.h"
  31. #include "polarssl/dhm.h"
  32. #include "polarssl/rsa.h"
  33. #include "polarssl/md5.h"
  34. #include "polarssl/sha1.h"
  35. #include "polarssl/x509.h"
  36. #include "config.h"
  37. #if defined(POLARSSL_PKCS11_C)
  38. #include "polarssl/pkcs11.h"
  39. #endif
  40. #if defined(_MSC_VER) && !defined(inline)
  41. #define inline _inline
  42. #else
  43. #if defined(__ARMCC_VERSION) && !defined(inline)
  44. #define inline __inline
  45. #endif /* __ARMCC_VERSION */
  46. #endif /*_MSC_VER */
  47. /*
  48. * SSL Error codes
  49. */
  50. #define POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE -0x7080 /**< The requested feature is not available. */
  51. #define POLARSSL_ERR_SSL_BAD_INPUT_DATA -0x7100 /**< Bad input parameters to function. */
  52. #define POLARSSL_ERR_SSL_INVALID_MAC -0x7180 /**< Verification of the message MAC failed. */
  53. #define POLARSSL_ERR_SSL_INVALID_RECORD -0x7200 /**< An invalid SSL record was received. */
  54. #define POLARSSL_ERR_SSL_CONN_EOF -0x7280 /**< The connection indicated an EOF. */
  55. #define POLARSSL_ERR_SSL_UNKNOWN_CIPHER -0x7300 /**< An unknown cipher was received. */
  56. #define POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN -0x7380 /**< The server has no ciphersuites in common with the client. */
  57. #define POLARSSL_ERR_SSL_NO_SESSION_FOUND -0x7400 /**< No session to recover was found. */
  58. #define POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE -0x7480 /**< No client certification received from the client, but required by the authentication mode. */
  59. #define POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE -0x7500 /**< Our own certificate(s) is/are too large to send in an SSL message.*/
  60. #define POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED -0x7580 /**< The own certificate is not set, but needed by the server. */
  61. #define POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED -0x7600 /**< The own private key is not set, but needed. */
  62. #define POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED -0x7680 /**< No CA Chain is set, but required to operate. */
  63. #define POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE -0x7700 /**< An unexpected message was received from our peer. */
  64. #define POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE -0x7780 /**< A fatal alert message was received from our peer. */
  65. #define POLARSSL_ERR_SSL_PEER_VERIFY_FAILED -0x7800 /**< Verification of our peer failed. */
  66. #define POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY -0x7880 /**< The peer notified us that the connection is going to be closed. */
  67. #define POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO -0x7900 /**< Processing of the ClientHello handshake message failed. */
  68. #define POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO -0x7980 /**< Processing of the ServerHello handshake message failed. */
  69. #define POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE -0x7A00 /**< Processing of the Certificate handshake message failed. */
  70. #define POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST -0x7A80 /**< Processing of the CertificateRequest handshake message failed. */
  71. #define POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE -0x7B00 /**< Processing of the ServerKeyExchange handshake message failed. */
  72. #define POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO_DONE -0x7B80 /**< Processing of the ServerHelloDone handshake message failed. */
  73. #define POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE -0x7C00 /**< Processing of the ClientKeyExchange handshake message failed. */
  74. #define POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_DHM_RP -0x7C80 /**< Processing of the ClientKeyExchange handshake message failed in DHM Read Public. */
  75. #define POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_DHM_CS -0x7D00 /**< Processing of the ClientKeyExchange handshake message failed in DHM Calculate Secret. */
  76. #define POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY -0x7D80 /**< Processing of the CertificateVerify handshake message failed. */
  77. #define POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC -0x7E00 /**< Processing of the ChangeCipherSpec handshake message failed. */
  78. #define POLARSSL_ERR_SSL_BAD_HS_FINISHED -0x7E80 /**< Processing of the Finished handshake message failed. */
  79. /*
  80. * Various constants
  81. */
  82. #define SSL_MAJOR_VERSION_3 3
  83. #define SSL_MINOR_VERSION_0 0 /*!< SSL v3.0 */
  84. #define SSL_MINOR_VERSION_1 1 /*!< TLS v1.0 */
  85. #define SSL_MINOR_VERSION_2 2 /*!< TLS v1.1 */
  86. #define SSL_IS_CLIENT 0
  87. #define SSL_IS_SERVER 1
  88. #define SSL_COMPRESS_NULL 0
  89. #define SSL_VERIFY_NONE 0
  90. #define SSL_VERIFY_OPTIONAL 1
  91. #define SSL_VERIFY_REQUIRED 2
  92. //#define SSL_MAX_CONTENT_LEN 16384
  93. #define SSL_MAX_CONTENT_LEN 2024
  94. /*
  95. * Allow an extra 512 bytes for the record header
  96. * and encryption overhead (counter + MAC + padding).
  97. */
  98. #define SSL_BUFFER_LEN (SSL_MAX_CONTENT_LEN + 512)
  99. /*
  100. * Supported ciphersuites
  101. */
  102. #define SSL_RSA_RC4_128_MD5 0x04
  103. #define SSL_RSA_RC4_128_SHA 0x05
  104. #define SSL_RSA_DES_168_SHA 0x0A
  105. #define SSL_EDH_RSA_DES_168_SHA 0x16
  106. #define SSL_RSA_AES_128_SHA 0x2F
  107. #define SSL_EDH_RSA_AES_128_SHA 0x33
  108. #define SSL_RSA_AES_256_SHA 0x35
  109. #define SSL_EDH_RSA_AES_256_SHA 0x39
  110. #define SSL_RSA_CAMELLIA_128_SHA 0x41
  111. #define SSL_EDH_RSA_CAMELLIA_128_SHA 0x45
  112. #define SSL_RSA_CAMELLIA_256_SHA 0x84
  113. #define SSL_EDH_RSA_CAMELLIA_256_SHA 0x88
  114. /*
  115. * Message, alert and handshake types
  116. */
  117. #define SSL_MSG_CHANGE_CIPHER_SPEC 20
  118. #define SSL_MSG_ALERT 21
  119. #define SSL_MSG_HANDSHAKE 22
  120. #define SSL_MSG_APPLICATION_DATA 23
  121. #define SSL_ALERT_LEVEL_WARNING 1
  122. #define SSL_ALERT_LEVEL_FATAL 2
  123. #define SSL_ALERT_MSG_CLOSE_NOTIFY 0
  124. #define SSL_ALERT_MSG_UNEXPECTED_MESSAGE 10
  125. #define SSL_ALERT_MSG_BAD_RECORD_MAC 20
  126. #define SSL_ALERT_MSG_DECRYPTION_FAILED 21
  127. #define SSL_ALERT_MSG_RECORD_OVERFLOW 22
  128. #define SSL_ALERT_MSG_DECOMPRESSION_FAILURE 30
  129. #define SSL_ALERT_MSG_HANDSHAKE_FAILURE 40
  130. #define SSL_ALERT_MSG_NO_CERT 41
  131. #define SSL_ALERT_MSG_BAD_CERT 42
  132. #define SSL_ALERT_MSG_UNSUPPORTED_CERT 43
  133. #define SSL_ALERT_MSG_CERT_REVOKED 44
  134. #define SSL_ALERT_MSG_CERT_EXPIRED 45
  135. #define SSL_ALERT_MSG_CERT_UNKNOWN 46
  136. #define SSL_ALERT_MSG_ILLEGAL_PARAMETER 47
  137. #define SSL_ALERT_MSG_UNKNOWN_CA 48
  138. #define SSL_ALERT_MSG_ACCESS_DENIED 49
  139. #define SSL_ALERT_MSG_DECODE_ERROR 50
  140. #define SSL_ALERT_MSG_DECRYPT_ERROR 51
  141. #define SSL_ALERT_MSG_EXPORT_RESTRICTION 60
  142. #define SSL_ALERT_MSG_PROTOCOL_VERSION 70
  143. #define SSL_ALERT_MSG_INSUFFICIENT_SECURITY 71
  144. #define SSL_ALERT_MSG_INTERNAL_ERROR 80
  145. #define SSL_ALERT_MSG_USER_CANCELED 90
  146. #define SSL_ALERT_MSG_NO_RENEGOTIATION 100
  147. #define SSL_HS_HELLO_REQUEST 0
  148. #define SSL_HS_CLIENT_HELLO 1
  149. #define SSL_HS_SERVER_HELLO 2
  150. #define SSL_HS_CERTIFICATE 11
  151. #define SSL_HS_SERVER_KEY_EXCHANGE 12
  152. #define SSL_HS_CERTIFICATE_REQUEST 13
  153. #define SSL_HS_SERVER_HELLO_DONE 14
  154. #define SSL_HS_CERTIFICATE_VERIFY 15
  155. #define SSL_HS_CLIENT_KEY_EXCHANGE 16
  156. #define SSL_HS_FINISHED 20
  157. /*
  158. * TLS extensions
  159. */
  160. #define TLS_EXT_SERVERNAME 0
  161. #define TLS_EXT_SERVERNAME_HOSTNAME 0
  162. /*
  163. * SSL state machine
  164. */
  165. typedef enum
  166. {
  167. SSL_HELLO_REQUEST,
  168. SSL_CLIENT_HELLO,
  169. SSL_SERVER_HELLO,
  170. SSL_SERVER_CERTIFICATE,
  171. SSL_SERVER_KEY_EXCHANGE,
  172. SSL_CERTIFICATE_REQUEST,
  173. SSL_SERVER_HELLO_DONE,
  174. SSL_CLIENT_CERTIFICATE,
  175. SSL_CLIENT_KEY_EXCHANGE,
  176. SSL_CERTIFICATE_VERIFY,
  177. SSL_CLIENT_CHANGE_CIPHER_SPEC,
  178. SSL_CLIENT_FINISHED,
  179. SSL_SERVER_CHANGE_CIPHER_SPEC,
  180. SSL_SERVER_FINISHED,
  181. SSL_FLUSH_BUFFERS,
  182. SSL_HANDSHAKE_OVER
  183. }
  184. ssl_states;
  185. typedef struct _ssl_session ssl_session;
  186. typedef struct _ssl_context ssl_context;
  187. /*
  188. * This structure is used for session resuming.
  189. */
  190. struct _ssl_session
  191. {
  192. time_t start; /*!< starting time */
  193. int ciphersuite; /*!< chosen ciphersuite */
  194. size_t length; /*!< session id length */
  195. unsigned char id[32]; /*!< session identifier */
  196. unsigned char master[48]; /*!< the master secret */
  197. ssl_session *next; /*!< next session entry */
  198. };
  199. struct _ssl_context
  200. {
  201. /*
  202. * Miscellaneous
  203. */
  204. int state; /*!< SSL handshake: current state */
  205. int major_ver; /*!< equal to SSL_MAJOR_VERSION_3 */
  206. int minor_ver; /*!< either 0 (SSL3) or 1 (TLS1.0) */
  207. int max_major_ver; /*!< max. major version from client */
  208. int max_minor_ver; /*!< max. minor version from client */
  209. /*
  210. * Callbacks (RNG, debug, I/O, verification)
  211. */
  212. int (*f_rng)(void *);
  213. void (*f_dbg)(void *, int, const char *);
  214. int (*f_recv)(void *, unsigned char *, size_t);
  215. int (*f_send)(void *, const unsigned char *, size_t);
  216. int (*f_vrfy)(void *, x509_cert *, int, int);
  217. void *p_rng; /*!< context for the RNG function */
  218. void *p_dbg; /*!< context for the debug function */
  219. void *p_recv; /*!< context for reading operations */
  220. void *p_send; /*!< context for writing operations */
  221. void *p_vrfy; /*!< context for verification */
  222. /*
  223. * Session layer
  224. */
  225. int resume; /*!< session resuming flag */
  226. int timeout; /*!< sess. expiration time */
  227. ssl_session *session; /*!< current session data */
  228. int (*s_get)(ssl_context *); /*!< (server) get callback */
  229. int (*s_set)(ssl_context *); /*!< (server) set callback */
  230. /*
  231. * Record layer (incoming data)
  232. */
  233. unsigned char *in_ctr; /*!< 64-bit incoming message counter */
  234. unsigned char *in_hdr; /*!< 5-byte record header (in_ctr+8) */
  235. unsigned char *in_msg; /*!< the message contents (in_hdr+5) */
  236. unsigned char *in_offt; /*!< read offset in application data */
  237. int in_msgtype; /*!< record header: message type */
  238. size_t in_msglen; /*!< record header: message length */
  239. size_t in_left; /*!< amount of data read so far */
  240. size_t in_hslen; /*!< current handshake message length */
  241. int nb_zero; /*!< # of 0-length encrypted messages */
  242. /*
  243. * Record layer (outgoing data)
  244. */
  245. unsigned char *out_ctr; /*!< 64-bit outgoing message counter */
  246. unsigned char *out_hdr; /*!< 5-byte record header (out_ctr+8) */
  247. unsigned char *out_msg; /*!< the message contents (out_hdr+5) */
  248. int out_msgtype; /*!< record header: message type */
  249. size_t out_msglen; /*!< record header: message length */
  250. size_t out_left; /*!< amount of data not yet written */
  251. /*
  252. * PKI layer
  253. */
  254. rsa_context *rsa_key; /*!< own RSA private key */
  255. #if defined(POLARSSL_PKCS11_C)
  256. pkcs11_context *pkcs11_key; /*!< own PKCS#11 RSA private key */
  257. #endif
  258. x509_cert *own_cert; /*!< own X.509 certificate */
  259. x509_cert *ca_chain; /*!< own trusted CA chain */
  260. x509_crl *ca_crl; /*!< trusted CA CRLs */
  261. x509_cert *peer_cert; /*!< peer X.509 cert chain */
  262. const char *peer_cn; /*!< expected peer CN */
  263. int endpoint; /*!< 0: client, 1: server */
  264. int authmode; /*!< verification mode */
  265. int client_auth; /*!< flag for client auth. */
  266. int verify_result; /*!< verification result */
  267. /*
  268. * Crypto layer
  269. */
  270. dhm_context dhm_ctx; /*!< DHM key exchange */
  271. md5_context fin_md5; /*!< Finished MD5 checksum */
  272. sha1_context fin_sha1; /*!< Finished SHA-1 checksum */
  273. int do_crypt; /*!< en(de)cryption flag */
  274. int *ciphersuites; /*!< allowed ciphersuites */
  275. size_t pmslen; /*!< premaster length */
  276. unsigned int keylen; /*!< symmetric key length */
  277. size_t minlen; /*!< min. ciphertext length */
  278. size_t ivlen; /*!< IV length */
  279. size_t maclen; /*!< MAC length */
  280. unsigned char randbytes[64]; /*!< random bytes */
  281. unsigned char premaster[256]; /*!< premaster secret */
  282. unsigned char iv_enc[16]; /*!< IV (encryption) */
  283. unsigned char iv_dec[16]; /*!< IV (decryption) */
  284. unsigned char mac_enc[32]; /*!< MAC (encryption) */
  285. unsigned char mac_dec[32]; /*!< MAC (decryption) */
  286. unsigned long ctx_enc[128]; /*!< encryption context */
  287. unsigned long ctx_dec[128]; /*!< decryption context */
  288. /*
  289. * TLS extensions
  290. */
  291. unsigned char *hostname;
  292. size_t hostname_len;
  293. };
  294. #ifdef __cplusplus
  295. extern "C" {
  296. #endif
  297. extern int ssl_default_ciphersuites[];
  298. /**
  299. * \brief Returns the list of ciphersuites supported by the SSL/TLS module.
  300. *
  301. * \return a statically allocated array of ciphersuites, the last
  302. * entry is 0.
  303. */
  304. static inline const int *ssl_list_ciphersuites( void )
  305. {
  306. return ssl_default_ciphersuites;
  307. }
  308. /**
  309. * \brief Return the name of the ciphersuite associated with the given
  310. * ID
  311. *
  312. * \param ciphersuite_id SSL ciphersuite ID
  313. *
  314. * \return a string containing the ciphersuite name
  315. */
  316. const char *ssl_get_ciphersuite_name( const int ciphersuite_id );
  317. /**
  318. * \brief Return the ID of the ciphersuite associated with the given
  319. * name
  320. *
  321. * \param ciphersuite_name SSL ciphersuite name
  322. *
  323. * \return the ID with the ciphersuite or 0 if not found
  324. */
  325. int ssl_get_ciphersuite_id( const char *ciphersuite_name );
  326. /**
  327. * \brief Initialize an SSL context
  328. *
  329. * \param ssl SSL context
  330. *
  331. * \return 0 if successful, or 1 if memory allocation failed
  332. */
  333. int ssl_init( ssl_context *ssl );
  334. /**
  335. * \brief Set the current endpoint type
  336. *
  337. * \param ssl SSL context
  338. * \param endpoint must be SSL_IS_CLIENT or SSL_IS_SERVER
  339. */
  340. void ssl_set_endpoint( ssl_context *ssl, int endpoint );
  341. /**
  342. * \brief Set the certificate verification mode
  343. *
  344. * \param ssl SSL context
  345. * \param authmode can be:
  346. *
  347. * SSL_VERIFY_NONE: peer certificate is not checked (default),
  348. * this is insecure and SHOULD be avoided.
  349. *
  350. * SSL_VERIFY_OPTIONAL: peer certificate is checked, however the
  351. * handshake continues even if verification failed;
  352. * ssl_get_verify_result() can be called after the
  353. * handshake is complete.
  354. *
  355. * SSL_VERIFY_REQUIRED: peer *must* present a valid certificate,
  356. * handshake is aborted if verification failed.
  357. */
  358. void ssl_set_authmode( ssl_context *ssl, int authmode );
  359. /**
  360. * \brief Set the verification callback (Optional).
  361. *
  362. * If set, the verification callback is called once for every
  363. * certificate in the chain. The verification function has the
  364. * following parameter: (void *parameter, x509_cert certificate,
  365. * int certifcate_depth, int preverify_ok). It should
  366. * return 0 on SUCCESS.
  367. *
  368. * \param ssl SSL context
  369. * \param f_vrfy verification function
  370. * \param p_vrfy verification parameter
  371. */
  372. void ssl_set_verify( ssl_context *ssl,
  373. int (*f_vrfy)(void *, x509_cert *, int, int),
  374. void *p_vrfy );
  375. /**
  376. * \brief Set the random number generator callback
  377. *
  378. * \param ssl SSL context
  379. * \param f_rng RNG function
  380. * \param p_rng RNG parameter
  381. */
  382. void ssl_set_rng( ssl_context *ssl,
  383. int (*f_rng)(void *),
  384. void *p_rng );
  385. /**
  386. * \brief Set the debug callback
  387. *
  388. * \param ssl SSL context
  389. * \param f_dbg debug function
  390. * \param p_dbg debug parameter
  391. */
  392. void ssl_set_dbg( ssl_context *ssl,
  393. void (*f_dbg)(void *, int, const char *),
  394. void *p_dbg );
  395. /**
  396. * \brief Set the underlying BIO read and write callbacks
  397. *
  398. * \param ssl SSL context
  399. * \param f_recv read callback
  400. * \param p_recv read parameter
  401. * \param f_send write callback
  402. * \param p_send write parameter
  403. */
  404. void ssl_set_bio( ssl_context *ssl,
  405. int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
  406. int (*f_send)(void *, const unsigned char *, size_t), void *p_send );
  407. /**
  408. * \brief Set the session callbacks (server-side only)
  409. *
  410. * \param ssl SSL context
  411. * \param s_get session get callback
  412. * \param s_set session set callback
  413. */
  414. void ssl_set_scb( ssl_context *ssl,
  415. int (*s_get)(ssl_context *),
  416. int (*s_set)(ssl_context *) );
  417. /**
  418. * \brief Set the session resuming flag, timeout and data
  419. *
  420. * \param ssl SSL context
  421. * \param resume if 0 (default), the session will not be resumed
  422. * \param timeout session timeout in seconds, or 0 (no timeout)
  423. * \param session session context
  424. */
  425. void ssl_set_session( ssl_context *ssl, int resume, int timeout,
  426. ssl_session *session );
  427. /**
  428. * \brief Set the list of allowed ciphersuites
  429. *
  430. * \param ssl SSL context
  431. * \param ciphersuites 0-terminated list of allowed ciphersuites
  432. */
  433. void ssl_set_ciphersuites( ssl_context *ssl, int *ciphersuites );
  434. /**
  435. * \brief Set the data required to verify peer certificate
  436. *
  437. * \param ssl SSL context
  438. * \param ca_chain trusted CA chain
  439. * \param ca_crl trusted CA CRLs
  440. * \param peer_cn expected peer CommonName (or NULL)
  441. *
  442. * \note TODO: add two more parameters: depth and crl
  443. */
  444. void ssl_set_ca_chain( ssl_context *ssl, x509_cert *ca_chain,
  445. x509_crl *ca_crl, const char *peer_cn );
  446. /**
  447. * \brief Set own certificate and private key
  448. *
  449. * \param ssl SSL context
  450. * \param own_cert own public certificate
  451. * \param rsa_key own private RSA key
  452. */
  453. void ssl_set_own_cert( ssl_context *ssl, x509_cert *own_cert,
  454. rsa_context *rsa_key );
  455. #if defined(POLARSSL_PKCS11_C)
  456. /**
  457. * \brief Set own certificate and PKCS#11 private key
  458. *
  459. * \param ssl SSL context
  460. * \param own_cert own public certificate
  461. * \param pkcs11_key own PKCS#11 RSA key
  462. */
  463. void ssl_set_own_cert_pkcs11( ssl_context *ssl, x509_cert *own_cert,
  464. pkcs11_context *pkcs11_key );
  465. #endif
  466. /**
  467. * \brief Set the Diffie-Hellman public P and G values,
  468. * read as hexadecimal strings (server-side only)
  469. *
  470. * \param ssl SSL context
  471. * \param dhm_P Diffie-Hellman-Merkle modulus
  472. * \param dhm_G Diffie-Hellman-Merkle generator
  473. *
  474. * \return 0 if successful
  475. */
  476. int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G );
  477. /**
  478. * \brief Set the Diffie-Hellman public P and G values,
  479. * read from existing context (server-side only)
  480. *
  481. * \param ssl SSL context
  482. * \param dhm_ctx Diffie-Hellman-Merkle context
  483. *
  484. * \return 0 if successful
  485. */
  486. int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx );
  487. /**
  488. * \brief Set hostname for ServerName TLS Extension
  489. *
  490. *
  491. * \param ssl SSL context
  492. * \param hostname the server hostname
  493. *
  494. * \return 0 if successful
  495. */
  496. int ssl_set_hostname( ssl_context *ssl, const char *hostname );
  497. /**
  498. * \brief Return the number of data bytes available to read
  499. *
  500. * \param ssl SSL context
  501. *
  502. * \return how many bytes are available in the read buffer
  503. */
  504. size_t ssl_get_bytes_avail( const ssl_context *ssl );
  505. /**
  506. * \brief Return the result of the certificate verification
  507. *
  508. * \param ssl SSL context
  509. *
  510. * \return 0 if successful, or a combination of:
  511. * BADCERT_EXPIRED
  512. * BADCERT_REVOKED
  513. * BADCERT_CN_MISMATCH
  514. * BADCERT_NOT_TRUSTED
  515. */
  516. int ssl_get_verify_result( const ssl_context *ssl );
  517. /**
  518. * \brief Return the name of the current ciphersuite
  519. *
  520. * \param ssl SSL context
  521. *
  522. * \return a string containing the ciphersuite name
  523. */
  524. const char *ssl_get_ciphersuite( const ssl_context *ssl );
  525. /**
  526. * \brief Return the current SSL version (SSLv3/TLSv1/etc)
  527. *
  528. * \param ssl SSL context
  529. *
  530. * \return a string containing the SSL version
  531. */
  532. const char *ssl_get_version( const ssl_context *ssl );
  533. /**
  534. * \brief Perform the SSL handshake
  535. *
  536. * \param ssl SSL context
  537. *
  538. * \return 0 if successful, POLARSSL_ERR_NET_WANT_READ,
  539. * POLARSSL_ERR_NET_WANT_WRITE, or a specific SSL error code.
  540. */
  541. int ssl_handshake( ssl_context *ssl );
  542. /**
  543. * \brief Read at most 'len' application data bytes
  544. *
  545. * \param ssl SSL context
  546. * \param buf buffer that will hold the data
  547. * \param len how many bytes must be read
  548. *
  549. * \return This function returns the number of bytes read, 0 for EOF,
  550. * or a negative error code.
  551. */
  552. int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len );
  553. /**
  554. * \brief Write exactly 'len' application data bytes
  555. *
  556. * \param ssl SSL context
  557. * \param buf buffer holding the data
  558. * \param len how many bytes must be written
  559. *
  560. * \return This function returns the number of bytes written,
  561. * or a negative error code.
  562. *
  563. * \note When this function returns POLARSSL_ERR_NET_WANT_WRITE,
  564. * it must be called later with the *same* arguments,
  565. * until it returns a positive value.
  566. */
  567. int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len );
  568. /**
  569. * \brief Notify the peer that the connection is being closed
  570. *
  571. * \param ssl SSL context
  572. */
  573. int ssl_close_notify( ssl_context *ssl );
  574. /**
  575. * \brief Free an SSL context
  576. *
  577. * \param ssl SSL context
  578. */
  579. void ssl_free( ssl_context *ssl );
  580. /*
  581. * Internal functions (do not call directly)
  582. */
  583. int ssl_handshake_client( ssl_context *ssl );
  584. int ssl_handshake_server( ssl_context *ssl );
  585. int ssl_derive_keys( ssl_context *ssl );
  586. void ssl_calc_verify( ssl_context *ssl, unsigned char hash[36] );
  587. int ssl_read_record( ssl_context *ssl );
  588. /**
  589. * \return 0 if successful, POLARSSL_ERR_SSL_CONN_EOF on EOF or
  590. * another negative error code.
  591. */
  592. int ssl_fetch_input( ssl_context *ssl, size_t nb_want );
  593. int ssl_write_record( ssl_context *ssl );
  594. int ssl_flush_output( ssl_context *ssl );
  595. int ssl_parse_certificate( ssl_context *ssl );
  596. int ssl_write_certificate( ssl_context *ssl );
  597. int ssl_parse_change_cipher_spec( ssl_context *ssl );
  598. int ssl_write_change_cipher_spec( ssl_context *ssl );
  599. int ssl_parse_finished( ssl_context *ssl );
  600. int ssl_write_finished( ssl_context *ssl );
  601. #ifdef __cplusplus
  602. }
  603. #endif
  604. #endif /* ssl.h */