secport.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. /*
  5. * secport.h - portability interfaces for security libraries
  6. */
  7. #ifndef _SECPORT_H_
  8. #define _SECPORT_H_
  9. #include "utilrename.h"
  10. #include "prlink.h"
  11. /*
  12. * define XP_WIN, XP_BEOS, or XP_UNIX, in case they are not defined
  13. * by anyone else
  14. */
  15. #ifdef _WINDOWS
  16. #ifndef XP_WIN
  17. #define XP_WIN
  18. #endif
  19. #if defined(_WIN32) || defined(WIN32)
  20. #ifndef XP_WIN32
  21. #define XP_WIN32
  22. #endif
  23. #endif
  24. #endif
  25. #ifdef __BEOS__
  26. #ifndef XP_BEOS
  27. #define XP_BEOS
  28. #endif
  29. #endif
  30. #ifdef unix
  31. #ifndef XP_UNIX
  32. #define XP_UNIX
  33. #endif
  34. #endif
  35. #include <sys/types.h>
  36. #include <ctype.h>
  37. #include <string.h>
  38. #include <stddef.h>
  39. #include <stdlib.h>
  40. #include <stdint.h>
  41. #include "prtypes.h"
  42. #include "prlog.h" /* for PR_ASSERT */
  43. #include "plarena.h"
  44. #include "plstr.h"
  45. /*
  46. * HACK for NSS 2.8 to allow Admin to compile without source changes.
  47. */
  48. #ifndef SEC_BEGIN_PROTOS
  49. #include "seccomon.h"
  50. #endif
  51. /*
  52. * The PORT_*Arena* function signatures mostly involve PLArenaPool* arguments.
  53. * But this is misleading! It's not actually safe to use vanilla PLArenaPools
  54. * with them. There are two "subclasses" of PLArenaPool that should be used
  55. * instead.
  56. *
  57. * - PORTArenaPool (defined in secport.c): this "subclass" is always
  58. * heap-allocated and uses a (heap-allocated) lock to protect all accesses.
  59. * Use PORT_NewArena() and PORT_FreeArena() to create and destroy
  60. * PORTArenaPools.
  61. *
  62. * - PORTCheapArenaPool (defined here): this "subclass" can be stack-allocated
  63. * and does not use a lock to protect accesses. This makes it cheaper but
  64. * less general. It is best used for arena pools that (a) are hot, (b) have
  65. * lifetimes bounded within a single function, and (c) don't need locking.
  66. * Use PORT_InitCheapArena() and PORT_DestroyCheapArena() to initialize and
  67. * finalize PORTCheapArenaPools.
  68. *
  69. * All the other PORT_Arena* functions will operate safely with either
  70. * subclass.
  71. */
  72. typedef struct PORTCheapArenaPool_str {
  73. PLArenaPool arena;
  74. PRUint32 magic; /* This is used to distinguish the two subclasses. */
  75. } PORTCheapArenaPool;
  76. SEC_BEGIN_PROTOS
  77. extern void *PORT_Alloc(size_t len);
  78. extern void *PORT_Realloc(void *old, size_t len);
  79. extern void *PORT_ZAlloc(size_t len);
  80. extern void *PORT_ZAllocAligned(size_t bytes, size_t alignment, void **mem);
  81. extern void *PORT_ZAllocAlignedOffset(size_t bytes, size_t alignment,
  82. size_t offset);
  83. extern void PORT_Free(void *ptr);
  84. extern void PORT_ZFree(void *ptr, size_t len);
  85. extern char *PORT_Strdup(const char *s);
  86. extern void PORT_SetError(int value);
  87. extern int PORT_GetError(void);
  88. /* These functions are for use with PORTArenaPools. */
  89. extern PLArenaPool *PORT_NewArena(unsigned long chunksize);
  90. extern void PORT_FreeArena(PLArenaPool *arena, PRBool zero);
  91. /* These functions are for use with PORTCheapArenaPools. */
  92. extern void PORT_InitCheapArena(PORTCheapArenaPool *arena,
  93. unsigned long chunksize);
  94. extern void PORT_DestroyCheapArena(PORTCheapArenaPool *arena);
  95. /* These functions work with both kinds of arena pool. */
  96. extern void *PORT_ArenaAlloc(PLArenaPool *arena, size_t size);
  97. extern void *PORT_ArenaZAlloc(PLArenaPool *arena, size_t size);
  98. extern void *PORT_ArenaGrow(PLArenaPool *arena, void *ptr,
  99. size_t oldsize, size_t newsize);
  100. extern void *PORT_ArenaMark(PLArenaPool *arena);
  101. extern void PORT_ArenaRelease(PLArenaPool *arena, void *mark);
  102. extern void PORT_ArenaZRelease(PLArenaPool *arena, void *mark);
  103. extern void PORT_ArenaUnmark(PLArenaPool *arena, void *mark);
  104. extern char *PORT_ArenaStrdup(PLArenaPool *arena, const char *str);
  105. SEC_END_PROTOS
  106. #define PORT_Assert PR_ASSERT
  107. /* This is a variation of PORT_Assert where the arguments will be always
  108. * used either in Debug or not. But, in optimized mode the result will be
  109. * ignored. See more details in Bug 1588015. */
  110. #define PORT_AssertArg PR_ASSERT_ARG
  111. /* macros to handle endian based byte conversion */
  112. #define PORT_GET_BYTE_BE(value, offset, len) \
  113. ((unsigned char)(((len) - (offset)-1) >= sizeof(value) ? 0 : (((value) >> (((len) - (offset)-1) * PR_BITS_PER_BYTE)) & 0xff)))
  114. #define PORT_GET_BYTE_LE(value, offset, len) \
  115. ((unsigned char)((offset) > sizeof(value) ? 0 : (((value) >> ((offset)*PR_BITS_PER_BYTE)) & 0xff)))
  116. /* This runs a function that should return SECSuccess.
  117. * Intended for NSS internal use only.
  118. * The return value is asserted in a debug build, otherwise it is ignored.
  119. * This is no substitute for proper error handling. It is OK only if you
  120. * have ensured that the function cannot fail by other means such as checking
  121. * prerequisites. In that case this can be used as a safeguard against
  122. * unexpected changes in a function.
  123. */
  124. #ifdef DEBUG
  125. #define PORT_CheckSuccess(f) PR_ASSERT((f) == SECSuccess)
  126. #else
  127. #define PORT_CheckSuccess(f) (f)
  128. #endif
  129. #define PORT_ZNew(type) (type *)PORT_ZAlloc(sizeof(type))
  130. #define PORT_ZNewAligned(type, alignment, mem) \
  131. (type *)PORT_ZAllocAlignedOffset(sizeof(type), alignment, offsetof(type, mem))
  132. #define PORT_New(type) (type *)PORT_Alloc(sizeof(type))
  133. #define PORT_ArenaNew(poolp, type) \
  134. (type *)PORT_ArenaAlloc(poolp, sizeof(type))
  135. #define PORT_ArenaZNew(poolp, type) \
  136. (type *)PORT_ArenaZAlloc(poolp, sizeof(type))
  137. #define PORT_NewArray(type, num) \
  138. (type *)PORT_Alloc(sizeof(type) * (num))
  139. #define PORT_ZNewArray(type, num) \
  140. (type *)PORT_ZAlloc(sizeof(type) * (num))
  141. #define PORT_ArenaNewArray(poolp, type, num) \
  142. (type *)PORT_ArenaAlloc(poolp, sizeof(type) * (num))
  143. #define PORT_ArenaZNewArray(poolp, type, num) \
  144. (type *)PORT_ArenaZAlloc(poolp, sizeof(type) * (num))
  145. /* Please, keep these defines sorted alphabetically. Thanks! */
  146. #define PORT_Atoi(buff) (int)strtol(buff, NULL, 10)
  147. /* Returns a UTF-8 encoded constant error string for err.
  148. * Returns NULL if initialization of the error tables fails
  149. * due to insufficient memory.
  150. *
  151. * This string must not be modified by the application.
  152. */
  153. #define PORT_ErrorToString(err) PR_ErrorToString((err), PR_LANGUAGE_I_DEFAULT)
  154. #define PORT_ErrorToName PR_ErrorToName
  155. #define PORT_Memcmp memcmp
  156. #define PORT_Memcpy memcpy
  157. #ifndef SUNOS4
  158. #define PORT_Memmove memmove
  159. #else /*SUNOS4*/
  160. #define PORT_Memmove(s, ct, n) bcopy((ct), (s), (n))
  161. #endif /*SUNOS4*/
  162. #define PORT_Memset memset
  163. #define PORT_Strcasecmp PL_strcasecmp
  164. #define PORT_Strcat strcat
  165. #define PORT_Strchr strchr
  166. #define PORT_Strrchr strrchr
  167. #define PORT_Strcmp strcmp
  168. #define PORT_Strcpy strcpy
  169. #define PORT_Strlen(s) strlen(s)
  170. #define PORT_Strncasecmp PL_strncasecmp
  171. #define PORT_Strncat strncat
  172. #define PORT_Strncmp strncmp
  173. #define PORT_Strncpy strncpy
  174. #define PORT_Strpbrk strpbrk
  175. #define PORT_Strstr strstr
  176. #define PORT_Strtok strtok
  177. #define PORT_Tolower tolower
  178. typedef PRBool(PR_CALLBACK *PORTCharConversionWSwapFunc)(PRBool toUnicode,
  179. unsigned char *inBuf, unsigned int inBufLen,
  180. unsigned char *outBuf, unsigned int maxOutBufLen,
  181. unsigned int *outBufLen, PRBool swapBytes);
  182. typedef PRBool(PR_CALLBACK *PORTCharConversionFunc)(PRBool toUnicode,
  183. unsigned char *inBuf, unsigned int inBufLen,
  184. unsigned char *outBuf, unsigned int maxOutBufLen,
  185. unsigned int *outBufLen);
  186. SEC_BEGIN_PROTOS
  187. void PORT_SetUCS4_UTF8ConversionFunction(PORTCharConversionFunc convFunc);
  188. void PORT_SetUCS2_ASCIIConversionFunction(PORTCharConversionWSwapFunc convFunc);
  189. PRBool PORT_UCS4_UTF8Conversion(PRBool toUnicode, unsigned char *inBuf,
  190. unsigned int inBufLen, unsigned char *outBuf,
  191. unsigned int maxOutBufLen, unsigned int *outBufLen);
  192. PRBool PORT_UCS2_ASCIIConversion(PRBool toUnicode, unsigned char *inBuf,
  193. unsigned int inBufLen, unsigned char *outBuf,
  194. unsigned int maxOutBufLen, unsigned int *outBufLen,
  195. PRBool swapBytes);
  196. void PORT_SetUCS2_UTF8ConversionFunction(PORTCharConversionFunc convFunc);
  197. PRBool PORT_UCS2_UTF8Conversion(PRBool toUnicode, unsigned char *inBuf,
  198. unsigned int inBufLen, unsigned char *outBuf,
  199. unsigned int maxOutBufLen, unsigned int *outBufLen);
  200. /* One-way conversion from ISO-8859-1 to UTF-8 */
  201. PRBool PORT_ISO88591_UTF8Conversion(const unsigned char *inBuf,
  202. unsigned int inBufLen, unsigned char *outBuf,
  203. unsigned int maxOutBufLen, unsigned int *outBufLen);
  204. extern PRBool
  205. sec_port_ucs4_utf8_conversion_function(
  206. PRBool toUnicode,
  207. unsigned char *inBuf,
  208. unsigned int inBufLen,
  209. unsigned char *outBuf,
  210. unsigned int maxOutBufLen,
  211. unsigned int *outBufLen);
  212. extern PRBool
  213. sec_port_ucs2_utf8_conversion_function(
  214. PRBool toUnicode,
  215. unsigned char *inBuf,
  216. unsigned int inBufLen,
  217. unsigned char *outBuf,
  218. unsigned int maxOutBufLen,
  219. unsigned int *outBufLen);
  220. /* One-way conversion from ISO-8859-1 to UTF-8 */
  221. extern PRBool
  222. sec_port_iso88591_utf8_conversion_function(
  223. const unsigned char *inBuf,
  224. unsigned int inBufLen,
  225. unsigned char *outBuf,
  226. unsigned int maxOutBufLen,
  227. unsigned int *outBufLen);
  228. extern int NSS_PutEnv(const char *envVarName, const char *envValue);
  229. extern int NSS_SecureMemcmp(const void *a, const void *b, size_t n);
  230. extern unsigned int NSS_SecureMemcmpZero(const void *mem, size_t n);
  231. /*
  232. * Load a shared library called "newShLibName" in the same directory as
  233. * a shared library that is already loaded, called existingShLibName.
  234. * A pointer to a static function in that shared library,
  235. * staticShLibFunc, is required.
  236. *
  237. * existingShLibName:
  238. * The file name of the shared library that shall be used as the
  239. * "reference library". The loader will attempt to load the requested
  240. * library from the same directory as the reference library.
  241. *
  242. * staticShLibFunc:
  243. * Pointer to a static function in the "reference library".
  244. *
  245. * newShLibName:
  246. * The simple file name of the new shared library to be loaded.
  247. *
  248. * We use PR_GetLibraryFilePathname to get the pathname of the loaded
  249. * shared lib that contains this function, and then do a
  250. * PR_LoadLibraryWithFlags with an absolute pathname for the shared
  251. * library to be loaded.
  252. *
  253. * On Windows, the "alternate search path" strategy is employed, if available.
  254. * On Unix, if existingShLibName is a symbolic link, and no link exists for the
  255. * new library, the original link will be resolved, and the new library loaded
  256. * from the resolved location.
  257. *
  258. * If the new shared library is not found in the same location as the reference
  259. * library, it will then be loaded from the normal system library path.
  260. */
  261. PRLibrary *
  262. PORT_LoadLibraryFromOrigin(const char *existingShLibName,
  263. PRFuncPtr staticShLibFunc,
  264. const char *newShLibName);
  265. SEC_END_PROTOS
  266. /*
  267. * Constant time macros
  268. */
  269. /* These macros use the fact that arithmetic shift shifts-in the sign bit.
  270. * However, this is not ensured by the C standard so you may need to replace
  271. * them with something else for odd compilers. These macros work for object
  272. * sizes up to 32 bits. The inequalities will produce incorrect results if
  273. * abs(a-b) >= PR_UINT32_MAX/2. This can be a voided if unsigned values stay
  274. * within the range 0-PRUINT32_MAX/2 and signed values stay within the range
  275. * -PRINT32_MAX/2-PRINT32_MAX/2. If these are insufficient, we can fix
  276. * this by either expanding the PORT_CT_DUPLICATE_MSB_TO_ALL to PRUint64
  277. * or by creating the following new macros for inequality:
  278. *
  279. * PORT_CT_OVERFLOW prevents the overflow condition by handling the case
  280. * where the high bits in a and b are different specially. Basically if
  281. * the high bit in a and b differs we can just
  282. * copy the high bit of one of the parameters to determine the result as
  283. * follows:
  284. * GxU if a has the high bit on, a>b, so d=a
  285. * LxU if b has the high bit on, a<b, so d=b
  286. * GxS if b has the high bit on, it's negative a>b so d=b
  287. * LxS if a has the high bit on, it's negative a<b so d=a
  288. * where PORT_CT_xxU() macros do unsigned compares and PORT_CT_xxS() do signed
  289. * compares.
  290. *
  291. * #define PORT_CT_OVERFLOW(a,b,c,d) \
  292. * PORT_CT_SEL(PORT_CT_DUPLICATE_MSB_TO_ALL((a)^(b)), \
  293. * (PORT_CT_DUPLICATE_MSB_TO_ALL(d)),c)
  294. * #define PORT_CT_GTU(a,b) PORT_CT_OVERFLOW(a,b,PORT_CT_GT(a,b),a)
  295. * #define PORT_CT_LTU(a,b) PORT_CT_OVERFLOW(a,b,PORT_CT_LT(a,b),b)
  296. * #define PORT_CT_GEU(a,b) PORT_CT_OVERFLOW(a,b,PORT_CT_GE(a,b),a)
  297. * #define PORT_CT_LEU(a,b) PORT_CT_OVERFLOW(a,b,PORT_CT_LE(a,b),b)
  298. * #define PORT_CT_GTS(a,b) PORT_CT_OVERFLOW(a,b,PORT_CT_GT(a,b),b)
  299. * #define PORT_CT_LTS(a,b) PORT_CT_OVERFLOW(a,b,PORT_CT_LT(a,b),a)
  300. * #define PORT_CT_GES(a,b) PORT_CT_OVERFLOW(a,b,PORT_CT_GE(a,b),b)
  301. * #define PORT_CT_LES(a,b) PORT_CT_OVERFLOW(a,b,PORT_CT_LE(a,b),a)
  302. *
  303. *
  304. * */
  305. /* Constant-time helper macro that copies the MSB of x to all other bits. */
  306. #define PORT_CT_DUPLICATE_MSB_TO_ALL(x) ((PRUint32)((PRInt32)(x) >> (sizeof(PRInt32) * 8 - 1)))
  307. /* Constant-time helper macro that selects l or r depending on all-1 or all-0
  308. * mask m */
  309. #define PORT_CT_SEL(m, l, r) (((m) & (l)) | (~(m) & (r)))
  310. /* Constant-time helper macro that returns all-1s if x is not 0; and all-0s
  311. * otherwise. */
  312. #define PORT_CT_NOT_ZERO(x) (PORT_CT_DUPLICATE_MSB_TO_ALL(((x) | (0 - (x)))))
  313. /* Constant-time helper macro that returns all-1s if x is 0; and all-0s
  314. * otherwise. */
  315. #define PORT_CT_ZERO(x) (~PORT_CT_DUPLICATE_MSB_TO_ALL(((x) | (0 - (x)))))
  316. /* Constant-time helper macro for equalities and inequalities.
  317. * returns all-1's for true and all-0's for false */
  318. #define PORT_CT_EQ(a, b) PORT_CT_ZERO(((a) - (b)))
  319. #define PORT_CT_NE(a, b) PORT_CT_NOT_ZERO(((a) - (b)))
  320. #define PORT_CT_GT(a, b) PORT_CT_DUPLICATE_MSB_TO_ALL((b) - (a))
  321. #define PORT_CT_LT(a, b) PORT_CT_DUPLICATE_MSB_TO_ALL((a) - (b))
  322. #define PORT_CT_GE(a, b) (~PORT_CT_LT(a, b))
  323. #define PORT_CT_LE(a, b) (~PORT_CT_GT(a, b))
  324. #define PORT_CT_TRUE (~0)
  325. #define PORT_CT_FALSE 0
  326. #endif /* _SECPORT_H_ */