o_str.c 8.2 KB

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