o_str.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. * Copyright 2003-2022 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include "internal/e_os.h"
  10. #include <string.h>
  11. #include <limits.h>
  12. #include <openssl/crypto.h>
  13. #include "crypto/ctype.h"
  14. #include "internal/cryptlib.h"
  15. #include "internal/thread_once.h"
  16. #define DEFAULT_SEPARATOR ':'
  17. #define CH_ZERO '\0'
  18. char *CRYPTO_strdup(const char *str, const char* file, int line)
  19. {
  20. char *ret;
  21. if (str == NULL)
  22. return NULL;
  23. ret = CRYPTO_malloc(strlen(str) + 1, file, line);
  24. if (ret != NULL)
  25. strcpy(ret, str);
  26. return ret;
  27. }
  28. char *CRYPTO_strndup(const char *str, size_t s, const char* file, int line)
  29. {
  30. size_t maxlen;
  31. char *ret;
  32. if (str == NULL)
  33. return NULL;
  34. maxlen = OPENSSL_strnlen(str, s);
  35. ret = CRYPTO_malloc(maxlen + 1, file, line);
  36. if (ret) {
  37. memcpy(ret, str, maxlen);
  38. ret[maxlen] = CH_ZERO;
  39. }
  40. return ret;
  41. }
  42. void *CRYPTO_memdup(const void *data, size_t siz, const char* file, int line)
  43. {
  44. void *ret;
  45. if (data == NULL || siz >= INT_MAX)
  46. return NULL;
  47. ret = CRYPTO_malloc(siz, file, line);
  48. if (ret == NULL)
  49. return NULL;
  50. return memcpy(ret, data, siz);
  51. }
  52. size_t OPENSSL_strnlen(const char *str, size_t maxlen)
  53. {
  54. const char *p;
  55. for (p = str; maxlen-- != 0 && *p != CH_ZERO; ++p) ;
  56. return p - str;
  57. }
  58. size_t OPENSSL_strlcpy(char *dst, const char *src, size_t size)
  59. {
  60. size_t l = 0;
  61. for (; size > 1 && *src; size--) {
  62. *dst++ = *src++;
  63. l++;
  64. }
  65. if (size)
  66. *dst = CH_ZERO;
  67. return l + strlen(src);
  68. }
  69. size_t OPENSSL_strlcat(char *dst, const char *src, size_t size)
  70. {
  71. size_t l = 0;
  72. for (; size > 0 && *dst; size--, dst++)
  73. l++;
  74. return l + OPENSSL_strlcpy(dst, src, size);
  75. }
  76. int OPENSSL_hexchar2int(unsigned char c)
  77. {
  78. #ifdef CHARSET_EBCDIC
  79. c = os_toebcdic[c];
  80. #endif
  81. switch (c) {
  82. case '0':
  83. return 0;
  84. case '1':
  85. return 1;
  86. case '2':
  87. return 2;
  88. case '3':
  89. return 3;
  90. case '4':
  91. return 4;
  92. case '5':
  93. return 5;
  94. case '6':
  95. return 6;
  96. case '7':
  97. return 7;
  98. case '8':
  99. return 8;
  100. case '9':
  101. return 9;
  102. case 'a': case 'A':
  103. return 0x0A;
  104. case 'b': case 'B':
  105. return 0x0B;
  106. case 'c': case 'C':
  107. return 0x0C;
  108. case 'd': case 'D':
  109. return 0x0D;
  110. case 'e': case 'E':
  111. return 0x0E;
  112. case 'f': case 'F':
  113. return 0x0F;
  114. }
  115. return -1;
  116. }
  117. static int hexstr2buf_sep(unsigned char *buf, size_t buf_n, size_t *buflen,
  118. const char *str, const char sep)
  119. {
  120. unsigned char *q;
  121. unsigned char ch, cl;
  122. int chi, cli;
  123. const unsigned char *p;
  124. size_t cnt;
  125. for (p = (const unsigned char *)str, q = buf, cnt = 0; *p; ) {
  126. ch = *p++;
  127. /* A separator of CH_ZERO means there is no separator */
  128. if (ch == sep && sep != CH_ZERO)
  129. continue;
  130. cl = *p++;
  131. if (!cl) {
  132. ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_ODD_NUMBER_OF_DIGITS);
  133. return 0;
  134. }
  135. cli = OPENSSL_hexchar2int(cl);
  136. chi = OPENSSL_hexchar2int(ch);
  137. if (cli < 0 || chi < 0) {
  138. ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_ILLEGAL_HEX_DIGIT);
  139. return 0;
  140. }
  141. cnt++;
  142. if (q != NULL) {
  143. if (cnt > buf_n) {
  144. ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_SMALL_BUFFER);
  145. return 0;
  146. }
  147. *q++ = (unsigned char)((chi << 4) | cli);
  148. }
  149. }
  150. if (buflen != NULL)
  151. *buflen = cnt;
  152. return 1;
  153. }
  154. /*
  155. * Given a string of hex digits convert to a buffer
  156. */
  157. int OPENSSL_hexstr2buf_ex(unsigned char *buf, size_t buf_n, size_t *buflen,
  158. const char *str, const char sep)
  159. {
  160. return hexstr2buf_sep(buf, buf_n, buflen, str, sep);
  161. }
  162. unsigned char *ossl_hexstr2buf_sep(const char *str, long *buflen,
  163. const char sep)
  164. {
  165. unsigned char *buf;
  166. size_t buf_n, tmp_buflen;
  167. buf_n = strlen(str);
  168. if (buf_n <= 1) {
  169. ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_HEX_STRING_TOO_SHORT);
  170. return NULL;
  171. }
  172. buf_n /= 2;
  173. if ((buf = OPENSSL_malloc(buf_n)) == NULL)
  174. return NULL;
  175. if (buflen != NULL)
  176. *buflen = 0;
  177. tmp_buflen = 0;
  178. if (hexstr2buf_sep(buf, buf_n, &tmp_buflen, str, sep)) {
  179. if (buflen != NULL)
  180. *buflen = (long)tmp_buflen;
  181. return buf;
  182. }
  183. OPENSSL_free(buf);
  184. return NULL;
  185. }
  186. unsigned char *OPENSSL_hexstr2buf(const char *str, long *buflen)
  187. {
  188. return ossl_hexstr2buf_sep(str, buflen, DEFAULT_SEPARATOR);
  189. }
  190. static int buf2hexstr_sep(char *str, size_t str_n, size_t *strlength,
  191. const unsigned char *buf, size_t buflen,
  192. const char sep)
  193. {
  194. static const char hexdig[] = "0123456789ABCDEF";
  195. const unsigned char *p;
  196. char *q;
  197. size_t i;
  198. int has_sep = (sep != CH_ZERO);
  199. size_t len = has_sep ? buflen * 3 : 1 + buflen * 2;
  200. if (strlength != NULL)
  201. *strlength = len;
  202. if (str == NULL)
  203. return 1;
  204. if (str_n < (unsigned long)len) {
  205. ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_SMALL_BUFFER);
  206. return 0;
  207. }
  208. q = str;
  209. for (i = 0, p = buf; i < buflen; i++, p++) {
  210. *q++ = hexdig[(*p >> 4) & 0xf];
  211. *q++ = hexdig[*p & 0xf];
  212. if (has_sep)
  213. *q++ = sep;
  214. }
  215. if (has_sep)
  216. --q;
  217. *q = CH_ZERO;
  218. #ifdef CHARSET_EBCDIC
  219. ebcdic2ascii(str, str, q - str - 1);
  220. #endif
  221. return 1;
  222. }
  223. int OPENSSL_buf2hexstr_ex(char *str, size_t str_n, size_t *strlength,
  224. const unsigned char *buf, size_t buflen,
  225. const char sep)
  226. {
  227. return buf2hexstr_sep(str, str_n, strlength, buf, buflen, sep);
  228. }
  229. char *ossl_buf2hexstr_sep(const unsigned char *buf, long buflen, char sep)
  230. {
  231. char *tmp;
  232. size_t tmp_n;
  233. if (buflen == 0)
  234. return OPENSSL_zalloc(1);
  235. tmp_n = (sep != CH_ZERO) ? buflen * 3 : 1 + buflen * 2;
  236. if ((tmp = OPENSSL_malloc(tmp_n)) == NULL)
  237. return NULL;
  238. if (buf2hexstr_sep(tmp, tmp_n, NULL, buf, buflen, sep))
  239. return tmp;
  240. OPENSSL_free(tmp);
  241. return NULL;
  242. }
  243. /*
  244. * Given a buffer of length 'buflen' return a OPENSSL_malloc'ed string with
  245. * its hex representation @@@ (Contents of buffer are always kept in ASCII,
  246. * also on EBCDIC machines)
  247. */
  248. char *OPENSSL_buf2hexstr(const unsigned char *buf, long buflen)
  249. {
  250. return ossl_buf2hexstr_sep(buf, buflen, DEFAULT_SEPARATOR);
  251. }
  252. int openssl_strerror_r(int errnum, char *buf, size_t buflen)
  253. {
  254. #if defined(_MSC_VER) && _MSC_VER>=1400 && !defined(_WIN32_WCE)
  255. return !strerror_s(buf, buflen, errnum);
  256. #elif defined(_GNU_SOURCE)
  257. char *err;
  258. /*
  259. * GNU strerror_r may not actually set buf.
  260. * It can return a pointer to some (immutable) static string in which case
  261. * buf is left unused.
  262. */
  263. err = strerror_r(errnum, buf, buflen);
  264. if (err == NULL || buflen == 0)
  265. return 0;
  266. /*
  267. * If err is statically allocated, err != buf and we need to copy the data.
  268. * If err points somewhere inside buf, OPENSSL_strlcpy can handle this,
  269. * since src and dest are not annotated with __restrict and the function
  270. * reads src byte for byte and writes to dest.
  271. * If err == buf we do not have to copy anything.
  272. */
  273. if (err != buf)
  274. OPENSSL_strlcpy(buf, err, buflen);
  275. return 1;
  276. #elif (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) || \
  277. (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 600)
  278. /*
  279. * We can use "real" strerror_r. The OpenSSL version differs in that it
  280. * gives 1 on success and 0 on failure for consistency with other OpenSSL
  281. * functions. Real strerror_r does it the other way around
  282. */
  283. return !strerror_r(errnum, buf, buflen);
  284. #else
  285. char *err;
  286. /* Fall back to non-thread safe strerror()...its all we can do */
  287. if (buflen < 2)
  288. return 0;
  289. err = strerror(errnum);
  290. /* Can this ever happen? */
  291. if (err == NULL)
  292. return 0;
  293. OPENSSL_strlcpy(buf, err, buflen);
  294. return 1;
  295. #endif
  296. }
  297. int OPENSSL_strcasecmp(const char *s1, const char *s2)
  298. {
  299. int t;
  300. while ((t = ossl_tolower(*s1) - ossl_tolower(*s2++)) == 0)
  301. if (*s1++ == '\0')
  302. return 0;
  303. return t;
  304. }
  305. int OPENSSL_strncasecmp(const char *s1, const char *s2, size_t n)
  306. {
  307. int t;
  308. size_t i;
  309. for (i = 0; i < n; i++)
  310. if ((t = ossl_tolower(*s1) - ossl_tolower(*s2++)) != 0)
  311. return t;
  312. else if (*s1++ == '\0')
  313. return 0;
  314. return 0;
  315. }