SSLCTX.java 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * Copyright (c) 2007, Cameron Rich
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * * Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. * * Neither the name of the axTLS project nor the names of its contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  22. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  25. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  26. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  27. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. /*
  31. * A wrapper around the unmanaged interface to give a semi-decent Java API
  32. */
  33. package axTLSj;
  34. import java.net.*;
  35. /**
  36. * @class SSLCTX
  37. * @ingroup java_api
  38. * @brief A base object for SSLServer/SSLClient.
  39. */
  40. public class SSLCTX
  41. {
  42. /**
  43. * A reference to the real client/server context.
  44. */
  45. protected int m_ctx;
  46. /**
  47. * @brief Establish a new client/server context.
  48. *
  49. * This function is called before any client/server SSL connections are
  50. * made. If multiple threads are used, then each thread will have its
  51. * own SSLCTX context. Any number of connections may be made with a single
  52. * context.
  53. *
  54. * Each new connection will use the this context's private key and
  55. * certificate chain. If a different certificate chain is required, then a
  56. * different context needs to be be used.
  57. *
  58. * @param options [in] Any particular options. At present the options
  59. * supported are:
  60. * - SSL_SERVER_VERIFY_LATER (client only): Don't stop a handshake if the
  61. * server authentication fails. The certificate can be authenticated later
  62. * with a call to verifyCert().
  63. * - SSL_CLIENT_AUTHENTICATION (server only): Enforce client authentication
  64. * i.e. each handshake will include a "certificate request" message from
  65. * the server.
  66. * - SSL_DISPLAY_BYTES (full mode build only): Display the byte sequences
  67. * during the handshake.
  68. * - SSL_DISPLAY_STATES (full mode build only): Display the state changes
  69. * during the handshake.
  70. * - SSL_DISPLAY_CERTS (full mode build only): Display the certificates that
  71. * are passed during a handshake.
  72. * - SSL_DISPLAY_RSA (full mode build only): Display the RSA key details
  73. * that are passed during a handshake.
  74. *
  75. * @param num_sessions [in] The number of sessions to be used for session
  76. * caching. If this value is 0, then there is no session caching.
  77. *
  78. * If this option is null, then the default internal private key/
  79. * certificate pair is used (if CONFIG_SSL_USE_DEFAULT_KEY is set).
  80. *
  81. * The resources used by this object are automatically freed.
  82. * @return A client/server context.
  83. */
  84. protected SSLCTX(int options, int num_sessions)
  85. {
  86. m_ctx = axtlsj.ssl_ctx_new(options, num_sessions);
  87. }
  88. /**
  89. * @brief Remove a client/server context.
  90. *
  91. * Frees any used resources used by this context. Each connection will be
  92. * sent a "Close Notify" alert (if possible).
  93. */
  94. public void dispose()
  95. {
  96. axtlsj.ssl_ctx_free(m_ctx);
  97. }
  98. /**
  99. * @brief Read the SSL data stream.
  100. * @param ssl [in] An SSL object reference.
  101. * @param rh [out] After a successful read, the decrypted data can be
  102. * retrieved with rh.getData(). It will be null otherwise.
  103. * @return The number of decrypted bytes:
  104. * - if > 0, then the handshaking is complete and we are returning the
  105. * number of decrypted bytes.
  106. * - SSL_OK if the handshaking stage is successful (but not yet complete).
  107. * - < 0 if an error.
  108. * @see ssl.h for the error code list.
  109. * @note Use rh before doing any successive ssl calls.
  110. */
  111. public int read(SSL ssl, SSLReadHolder rh)
  112. {
  113. return axtlsj.ssl_read(ssl.m_ssl, rh);
  114. }
  115. /**
  116. * @brief Write to the SSL data stream.
  117. * @param ssl [in] An SSL obect reference.
  118. * @param out_data [in] The data to be written
  119. * @return The number of bytes sent, or if < 0 if an error.
  120. * @see ssl.h for the error code list.
  121. */
  122. public int write(SSL ssl, byte[] out_data)
  123. {
  124. return axtlsj.ssl_write(ssl.m_ssl, out_data, out_data.length);
  125. }
  126. /**
  127. * @brief Write to the SSL data stream.
  128. * @param ssl [in] An SSL obect reference.
  129. * @param out_data [in] The data to be written
  130. * @param out_len [in] The number of bytes to be written
  131. * @return The number of bytes sent, or if < 0 if an error.
  132. * @see ssl.h for the error code list.
  133. */
  134. public int write(SSL ssl, byte[] out_data, int out_len)
  135. {
  136. return axtlsj.ssl_write(ssl.m_ssl, out_data, out_len);
  137. }
  138. /**
  139. * @brief Find an ssl object based on a Socket reference.
  140. *
  141. * Goes through the list of SSL objects maintained in a client/server
  142. * context to look for a socket match.
  143. * @param s [in] A reference to a <A HREF="http://java.sun.com/j2se/1.4.2/docs/api">Socket</A> object.
  144. * @return A reference to the SSL object. Returns null if the object
  145. * could not be found.
  146. */
  147. public SSL find(Socket s)
  148. {
  149. int client_fd = axtlsj.getFd(s);
  150. return new SSL(axtlsj.ssl_find(m_ctx, client_fd));
  151. }
  152. /**
  153. * @brief Authenticate a received certificate.
  154. *
  155. * This call is usually made by a client after a handshake is complete
  156. * and the context is in SSL_SERVER_VERIFY_LATER mode.
  157. * @param ssl [in] An SSL object reference.
  158. * @return SSL_OK if the certificate is verified.
  159. */
  160. public int verifyCert(SSL ssl)
  161. {
  162. return axtlsj.ssl_verify_cert(ssl.m_ssl);
  163. }
  164. /**
  165. * @brief Force the client to perform its handshake again.
  166. *
  167. * For a client this involves sending another "client hello" message.
  168. * For the server is means sending a "hello request" message.
  169. *
  170. * This is a blocking call on the client (until the handshake completes).
  171. * @param ssl [in] An SSL object reference.
  172. * @return SSL_OK if renegotiation instantiation was ok
  173. */
  174. public int renegotiate(SSL ssl)
  175. {
  176. return axtlsj.ssl_renegotiate(ssl.m_ssl);
  177. }
  178. /**
  179. * @brief Load a file into memory that is in binary DER or ASCII PEM format.
  180. *
  181. * These are temporary objects that are used to load private keys,
  182. * certificates etc into memory.
  183. * @param obj_type [in] The format of the file. Can be one of:
  184. * - SSL_OBJ_X509_CERT (no password required)
  185. * - SSL_OBJ_X509_CACERT (no password required)
  186. * - SSL_OBJ_RSA_KEY (AES128/AES256 PEM encryption supported)
  187. * - SSL_OBJ_P8 (RC4-128 encrypted data supported)
  188. * - SSL_OBJ_P12 (RC4-128 encrypted data supported)
  189. *
  190. * PEM files are automatically detected (if supported).
  191. * @param filename [in] The location of a file in DER/PEM format.
  192. * @param password [in] The password used. Can be null if not required.
  193. * @return SSL_OK if all ok
  194. */
  195. public int objLoad(int obj_type, String filename, String password)
  196. {
  197. return axtlsj.ssl_obj_load(m_ctx, obj_type, filename, password);
  198. }
  199. /**
  200. * @brief Transfer binary data into the object loader.
  201. *
  202. * These are temporary objects that are used to load private keys,
  203. * certificates etc into memory.
  204. * @param obj_type [in] The format of the memory data.
  205. * @param data [in] The binary data to be loaded.
  206. * @param len [in] The amount of data to be loaded.
  207. * @param password [in] The password used. Can be null if not required.
  208. * @return SSL_OK if all ok
  209. */
  210. public int objLoad(int obj_type, byte[] data, int len, String password)
  211. {
  212. return axtlsj.ssl_obj_memory_load(m_ctx, obj_type, data, len, password);
  213. }
  214. }