o_str.c 8.9 KB

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