home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / lib / libmisc / miscinit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  5.0 KB  |  203 lines

  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. #include "sec.h"
  20. #include "ssl.h"
  21. #ifdef SOURCE_KIT
  22. #include "sslimpl.h"
  23. #else /* SOURCE_KIT */
  24. #include "impl.h"
  25. #endif /* SOURCE_KIT */
  26. #include "nspr.h"
  27. #ifdef SOURCE_KIT
  28. #ifdef FORTEZZA
  29. /* Sigh for FortezzaGlobalinit() */
  30. #include "fortezza.h"
  31. #endif /* FORTEZZA */
  32. #endif /* SOURCE_KIT */
  33.  
  34. extern int XP_ERRNO_EWOULDBLOCK;
  35.  
  36. static void *
  37. null_hash_new_context(void)
  38. {
  39.     return NULL;
  40. }
  41.  
  42. static void *
  43. null_hash_clone_context(void *v)
  44. {
  45.     XP_ASSERT(v == NULL);
  46.     return NULL;
  47. }
  48.  
  49. static void
  50. null_hash_begin(void *v)
  51. {
  52. }
  53.  
  54. static void
  55. null_hash_update(void *v, unsigned char *input, unsigned int length)
  56. {
  57. }
  58.  
  59. static void
  60. null_hash_end(void *v, unsigned char *output, unsigned int *outLen,
  61.           unsigned int maxOut)
  62. {
  63.     *outLen = 0;
  64. }
  65.  
  66. static void
  67. null_hash_destroy_context(void *v, DSBool b)
  68. {
  69.     XP_ASSERT(v == NULL);
  70. }
  71.  
  72. SECHashObject SECHashObjects[] = {
  73.   { 0,
  74.     (void * (*)(void)) null_hash_new_context,
  75.     (void * (*)(void *)) null_hash_clone_context,
  76.     (void (*)(void *, DSBool)) null_hash_destroy_context,
  77.     (void (*)(void *)) null_hash_begin,
  78.     (void (*)(void *, unsigned char *, unsigned int)) null_hash_update,
  79.     (void (*)(void *, unsigned char *, unsigned int *,
  80.           unsigned int)) null_hash_end
  81.   },
  82.   { MD2_LENGTH,
  83.     (void * (*)(void)) MD2_NewContext,
  84.     (void * (*)(void *)) MD2_CloneContext,
  85.     (void (*)(void *, DSBool)) MD2_DestroyContext,
  86.     (void (*)(void *)) MD2_Begin,
  87.     (void (*)(void *, unsigned char *, unsigned int)) MD2_Update,
  88.     (void (*)(void *, unsigned char *, unsigned int *, unsigned int)) MD2_End
  89.   },
  90.   { MD5_LENGTH,
  91.     (void * (*)(void)) MD5_NewContext,
  92.     (void * (*)(void *)) MD5_CloneContext,
  93.     (void (*)(void *, DSBool)) MD5_DestroyContext,
  94.     (void (*)(void *)) MD5_Begin,
  95.     (void (*)(void *, unsigned char *, unsigned int)) MD5_Update,
  96.     (void (*)(void *, unsigned char *, unsigned int *, unsigned int)) MD5_End
  97.   },
  98.   { SHA1_LENGTH,
  99.     (void * (*)(void)) SHA1_NewContext,
  100.     (void * (*)(void *)) SHA1_CloneContext,
  101.     (void (*)(void *, DSBool)) SHA1_DestroyContext,
  102.     (void (*)(void *)) SHA1_Begin,
  103.     (void (*)(void *, unsigned char *, unsigned int)) SHA1_Update,
  104.     (void (*)(void *, unsigned char *, unsigned int *, unsigned int)) SHA1_End
  105.   },
  106. };
  107.  
  108. static int sec_inited = 0;
  109. void SEC_Init(void)
  110. {
  111.     /* PR_Init() must be called before SEC_Init() */
  112. #if !defined(SERVER_BUILD)
  113.     XP_ASSERT(PR_Initialized() == PR_TRUE);
  114. #endif
  115.     if (sec_inited)
  116.     return;
  117.  
  118.     SEC_RNGInit();
  119. #ifdef SOURCE_KIT
  120.     SSL_InitHashLock();
  121.     SSL3_Init();
  122. #ifdef FORTEZZA
  123.     FortezzaGlobalInit();
  124. #endif /* FORTEZZA */
  125. #endif /* SOURCE_KIT */
  126.     sec_inited = 1;
  127. }
  128.  
  129. #ifdef SOURCE_KIT
  130. #ifdef NSPR20
  131.  
  132. static PRIntervalTime sec_io_timeout = PR_INTERVAL_NO_TIMEOUT;
  133.  
  134. void SEC_Set_IO_Timeout(PRUint32 timeout)
  135. {
  136.     sec_io_timeout = PR_SecondsToInterval(timeout);
  137. }
  138.  
  139. static void SEC_Map_Error()
  140. {
  141.     int err, block;
  142.  
  143.     err = _PR_MD_GET_SOCKET_ERROR();
  144.     block = ((err == _MD_EWOULDBLOCK) || (err == _MD_EAGAIN) ||
  145.              (sec_io_timeout == PR_INTERVAL_NO_WAIT && err == _MD_ETIMEDOUT));
  146.     if (block)
  147.     err = XP_ERRNO_EWOULDBLOCK;
  148.     XP_SetError(err);
  149. }
  150.  
  151. int SEC_Send(int s, const void *buf, int len, int flags)
  152. {
  153.     int rv;
  154.  
  155. #if defined(__sun) && defined(SYSV)
  156.     rv = _PR_MD_SEND(s, buf, len, flags, sec_io_timeout);
  157. #else
  158.     rv = _PR_MD_SEND(s, buf, len, 0, sec_io_timeout);
  159. #endif
  160.     if (rv < 0)
  161.     SEC_Map_Error();
  162.     return rv;
  163. }
  164.  
  165. int SEC_Recv(int s, void *buf, int len, int flags)
  166. {
  167.     int rv;
  168.  
  169.     rv = _PR_MD_RECV(s, buf, len, flags, sec_io_timeout);
  170.     if (rv < 0)
  171.     SEC_Map_Error();
  172.     return rv;
  173. }
  174.  
  175. #else /* NSPR20 */
  176.  
  177. int SEC_Send(int s, const void *buf, int len, int flags)
  178. {
  179.     int rv;
  180.     /* Brutal workaround for bug in Solaris 2.4 send routine */
  181. #if defined(__sun) && defined(SYSV)
  182.     rv = XP_SOCK_SEND(s, buf, len, flags);
  183. #else
  184.     rv = XP_SOCK_WRITE(s, buf, len);
  185. #endif
  186.     if (rv < 0)
  187.     XP_SetError(XP_SOCK_ERRNO);
  188.     return rv;
  189. }
  190.  
  191. int SEC_Recv(int s, void *buf, int len, int flags)
  192. {
  193.     int rv;
  194.  
  195.     rv = XP_SOCK_RECV(s, buf, len, flags);
  196.     if (rv < 0)
  197.     XP_SetError(XP_SOCK_ERRNO);
  198.     return rv;
  199. }
  200.  
  201. #endif /* NSPR20 */
  202. #endif /* SOURCE_KIT */
  203.