sslvfy.xml 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <refentry id="refsslvfy"> <!-- -*- xml-mode -*- -->
  2. <refmeta>
  3. <refentrytitle>ne_ssl_set_verify</refentrytitle>
  4. <manvolnum>3</manvolnum>
  5. </refmeta>
  6. <refnamediv>
  7. <refname id="ne_ssl_set_verify">ne_ssl_set_verify</refname>
  8. <refpurpose>register an SSL certificate verification callback</refpurpose>
  9. </refnamediv>
  10. <refsynopsisdiv>
  11. <funcsynopsis>
  12. <funcsynopsisinfo>#include &lt;ne_session.h&gt;</funcsynopsisinfo>
  13. <!-- hard to put data type declarations here -->
  14. <funcprototype>
  15. <funcdef>typedef int <function>ne_ssl_verify_fn</function></funcdef>
  16. <paramdef>void *<parameter>userdata</parameter></paramdef>
  17. <paramdef>int <parameter>failures</parameter></paramdef>
  18. <paramdef>const ne_ssl_certificate *<parameter>cert</parameter></paramdef>
  19. </funcprototype>
  20. <funcprototype>
  21. <funcdef>void <function>ne_ssl_set_verify</function></funcdef>
  22. <paramdef>ne_session *<parameter>session</parameter></paramdef>
  23. <paramdef>ne_ssl_verify_fn <parameter>verify_fn</parameter></paramdef>
  24. <paramdef>void *<parameter>userdata</parameter></paramdef>
  25. </funcprototype>
  26. </funcsynopsis>
  27. </refsynopsisdiv>
  28. <refsect1>
  29. <title>Description</title>
  30. <para>To enable manual SSL certificate verification, a
  31. callback can be registered using
  32. <function>ne_ssl_set_verify</function>. If such a callback is not
  33. registered, when a connection is established to an SSL server which
  34. does not present a certificate signed by a trusted CA (see <xref
  35. linkend="ne_ssl_trust_cert"/>), or if the certificate presented is invalid in
  36. some way, the connection will fail.</para>
  37. <para>When the callback is invoked, the
  38. <parameter>failures</parameter> parameter gives a bitmask indicating
  39. in what way the automatic certificate verification failed. The value
  40. is equal to the bit-wise OR of one or more of the following
  41. constants (and is guaranteed to be non-zero):</para>
  42. <variablelist>
  43. <varlistentry><term><constant>NE_SSL_NOTYETVALID</constant></term>
  44. <listitem>
  45. <simpara>The certificate is not yet valid.</simpara>
  46. </listitem>
  47. </varlistentry>
  48. <varlistentry><term><constant>NE_SSL_EXPIRED</constant></term>
  49. <listitem>
  50. <simpara>The certificate has expired.</simpara>
  51. </listitem>
  52. </varlistentry>
  53. <varlistentry><term><constant>NE_SSL_IDMISMATCH</constant></term>
  54. <listitem>
  55. <simpara>The hostname used for the session does not match
  56. the hostname to which the certificate was issued.</simpara>
  57. </listitem>
  58. </varlistentry>
  59. <varlistentry><term><constant>NE_SSL_UNTRUSTED</constant></term>
  60. <listitem>
  61. <simpara>The Certificate Authority which signed the certificate
  62. is not trusted.</simpara>
  63. </listitem>
  64. </varlistentry>
  65. </variablelist>
  66. <para>Note that if either of the
  67. <constant>NE_SSL_IDMISMATCH</constant> or
  68. <constant>NE_SSL_UNTRUSTED</constant> failures is given, the
  69. connection may have been intercepted by a third party, and
  70. must not be presumed to be <quote>secure</quote>.</para>
  71. <para>The <parameter>cert</parameter> parameter passed to the
  72. callback represents the certificate which was presented by the server.
  73. If the server presented a chain of certificates, the chain can be
  74. accessed using <xref linkend="ne_ssl_cert_signedby"/>. The
  75. <parameter>cert</parameter> object given is not valid after the
  76. callback returns.</para>
  77. </refsect1>
  78. <refsect1>
  79. <title>Return value</title>
  80. <para>The verification callback must return zero to indicate
  81. that the certificate should be trusted; and non-zero otherwise (in
  82. which case, the connection will fail).</para>
  83. </refsect1>
  84. <refsect1>
  85. <title>Examples</title>
  86. <para>The following code implements an example verification
  87. callback, using the <function>dump_cert</function> function
  88. from <xref linkend="ne_ssl_cert_subject"/> to display
  89. certification information. Notice that the hostname of the
  90. server used for the session is passed as the
  91. <parameter>userdata</parameter> parameter to the
  92. callback.</para>
  93. <programlisting>
  94. static int
  95. my_verify(void *userdata, int failures, const ne_ssl_certificate *cert)
  96. {
  97. const char *hostname = userdata;
  98. dump_cert(cert);
  99. puts("Certificate verification failed - the connection may have been "
  100. "intercepted by a third party!");
  101. if (failures &amp; NE_SSL_IDMISMATCH) {
  102. const char *id = ne_ssl_cert_identity(cert);
  103. if (id)
  104. printf("Server certificate was issued to '%s' not '%s'.\n",
  105. id, hostname);
  106. else
  107. printf("The certificate was not issued for '%s'\n", hostname);
  108. }
  109. if (failures &amp; NE_SSL_UNTRUSTED)
  110. puts("The certificate is not signed by a trusted Certificate Authority.");
  111. /* ... check for validity failures ... */
  112. if (prompt_user())
  113. return 1; /* fail verification */
  114. else
  115. return 0; /* trust the certificate anyway */
  116. }
  117. int
  118. main(...)
  119. {
  120. ne_session *sess = ne_session_create("https", "some.host.name", 443);
  121. ne_ssl_set_verify(sess, my_verify, "some.host.name");
  122. ...
  123. }</programlisting>
  124. </refsect1>
  125. <refsect1>
  126. <title>See also</title>
  127. <para><xref linkend="ne_ssl_trust_cert"/>, <xref
  128. linkend="ne_ssl_readable_dname"/>, <xref linkend="ne_ssl_cert_subject"/></para>
  129. </refsect1>
  130. </refentry>