SSL.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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.io.*;
  35. import java.util.*;
  36. /**
  37. * @defgroup java_api Java API.
  38. *
  39. * Ensure that the appropriate dispose() methods are called when finished with
  40. * various objects - otherwise memory leaks will result.
  41. */
  42. /**
  43. * @class SSL
  44. * @ingroup java_api
  45. * @brief A representation of an SSL connection.
  46. *
  47. */
  48. public class SSL
  49. {
  50. public int m_ssl; /**< A pointer to the real SSL type */
  51. /**
  52. * @brief Store the reference to an SSL context.
  53. * @param ip [in] A reference to an SSL object.
  54. */
  55. public SSL(int ip)
  56. {
  57. m_ssl = ip;
  58. }
  59. /**
  60. * @brief Free any used resources on this connection.
  61. *
  62. * A "Close Notify" message is sent on this connection (if possible). It
  63. * is up to the application to close the socket.
  64. */
  65. public void dispose()
  66. {
  67. axtlsj.ssl_free(m_ssl);
  68. }
  69. /**
  70. * @brief Return the result of a handshake.
  71. * @return SSL_OK if the handshake is complete and ok.
  72. * @see ssl.h for the error code list.
  73. */
  74. public int handshakeStatus()
  75. {
  76. return axtlsj.ssl_handshake_status(m_ssl);
  77. }
  78. /**
  79. * @brief Return the SSL cipher id.
  80. * @return The cipher id which is one of:
  81. * - SSL_AES128_SHA (0x2f)
  82. * - SSL_AES256_SHA (0x35)
  83. * - SSL_RC4_128_SHA (0x05)
  84. * - SSL_RC4_128_MD5 (0x04)
  85. */
  86. public byte getCipherId()
  87. {
  88. return axtlsj.ssl_get_cipher_id(m_ssl);
  89. }
  90. /**
  91. * @brief Get the session id for a handshake.
  92. *
  93. * This will be a 32 byte sequence and is available after the first
  94. * handshaking messages are sent.
  95. * @return The session id as a 32 byte sequence.
  96. * @note A SSLv23 handshake may have only 16 valid bytes.
  97. */
  98. public byte[] getSessionId()
  99. {
  100. return axtlsj.ssl_get_session_id(m_ssl);
  101. }
  102. /**
  103. * @brief Retrieve an X.509 distinguished name component.
  104. *
  105. * When a handshake is complete and a certificate has been exchanged,
  106. * then the details of the remote certificate can be retrieved.
  107. *
  108. * This will usually be used by a client to check that the server's common
  109. * name matches the URL.
  110. *
  111. * A full handshake needs to occur for this call to work.
  112. *
  113. * @param component [in] one of:
  114. * - SSL_X509_CERT_COMMON_NAME
  115. * - SSL_X509_CERT_ORGANIZATION
  116. * - SSL_X509_CERT_ORGANIZATIONAL_NAME
  117. * - SSL_X509_CA_CERT_COMMON_NAME
  118. * - SSL_X509_CA_CERT_ORGANIZATION
  119. * - SSL_X509_CA_CERT_ORGANIZATIONAL_NAME
  120. * @return The appropriate string (or null if not defined)
  121. */
  122. public String getCertificateDN(int component)
  123. {
  124. return axtlsj.ssl_get_cert_dn(m_ssl, component);
  125. }
  126. }