o_str.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. * Copyright 2003-2021 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. ERR_raise(ERR_LIB_CRYPTO, 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. ERR_raise(ERR_LIB_CRYPTO, 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. ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_ILLEGAL_HEX_DIGIT);
  138. return 0;
  139. }
  140. cnt++;
  141. if (q != NULL) {
  142. if (cnt > buf_n) {
  143. ERR_raise(ERR_LIB_CRYPTO, 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, const char sep)
  158. {
  159. return hexstr2buf_sep(buf, buf_n, buflen, str, sep);
  160. }
  161. unsigned char *ossl_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);
  167. if (buf_n <= 1) {
  168. ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_HEX_STRING_TOO_SHORT);
  169. return NULL;
  170. }
  171. buf_n /= 2;
  172. if ((buf = OPENSSL_malloc(buf_n)) == NULL) {
  173. ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
  174. return NULL;
  175. }
  176. if (buflen != NULL)
  177. *buflen = 0;
  178. tmp_buflen = 0;
  179. if (hexstr2buf_sep(buf, buf_n, &tmp_buflen, str, sep)) {
  180. if (buflen != NULL)
  181. *buflen = (long)tmp_buflen;
  182. return buf;
  183. }
  184. OPENSSL_free(buf);
  185. return NULL;
  186. }
  187. unsigned char *OPENSSL_hexstr2buf(const char *str, long *buflen)
  188. {
  189. return ossl_hexstr2buf_sep(str, buflen, DEFAULT_SEPARATOR);
  190. }
  191. static int buf2hexstr_sep(char *str, size_t str_n, size_t *strlen,
  192. const unsigned char *buf, size_t buflen,
  193. const char sep)
  194. {
  195. static const char hexdig[] = "0123456789ABCDEF";
  196. const unsigned char *p;
  197. char *q;
  198. size_t i;
  199. int has_sep = (sep != CH_ZERO);
  200. size_t len = has_sep ? buflen * 3 : 1 + buflen * 2;
  201. if (strlen != NULL)
  202. *strlen = len;
  203. if (str == NULL)
  204. return 1;
  205. if (str_n < (unsigned long)len) {
  206. ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_SMALL_BUFFER);
  207. return 0;
  208. }
  209. q = str;
  210. for (i = 0, p = buf; i < buflen; i++, p++) {
  211. *q++ = hexdig[(*p >> 4) & 0xf];
  212. *q++ = hexdig[*p & 0xf];
  213. if (has_sep)
  214. *q++ = sep;
  215. }
  216. if (has_sep)
  217. --q;
  218. *q = CH_ZERO;
  219. #ifdef CHARSET_EBCDIC
  220. ebcdic2ascii(str, str, q - str - 1);
  221. #endif
  222. return 1;
  223. }
  224. int OPENSSL_buf2hexstr_ex(char *str, size_t str_n, size_t *strlen,
  225. const unsigned char *buf, size_t buflen,
  226. const char sep)
  227. {
  228. return buf2hexstr_sep(str, str_n, strlen, buf, buflen, sep);
  229. }
  230. char *ossl_buf2hexstr_sep(const unsigned char *buf, long buflen, char sep)
  231. {
  232. char *tmp;
  233. size_t tmp_n;
  234. if (buflen == 0)
  235. return OPENSSL_zalloc(1);
  236. tmp_n = (sep != CH_ZERO) ? buflen * 3 : 1 + buflen * 2;
  237. if ((tmp = OPENSSL_malloc(tmp_n)) == NULL) {
  238. ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
  239. return NULL;
  240. }
  241. if (buf2hexstr_sep(tmp, tmp_n, NULL, buf, buflen, sep))
  242. return tmp;
  243. OPENSSL_free(tmp);
  244. return NULL;
  245. }
  246. /*
  247. * Given a buffer of length 'len' return a OPENSSL_malloc'ed string with its
  248. * hex representation @@@ (Contents of buffer are always kept in ASCII, also
  249. * on EBCDIC machines)
  250. */
  251. char *OPENSSL_buf2hexstr(const unsigned char *buf, long buflen)
  252. {
  253. return ossl_buf2hexstr_sep(buf, buflen, ':');
  254. }
  255. int openssl_strerror_r(int errnum, char *buf, size_t buflen)
  256. {
  257. #if defined(_MSC_VER) && _MSC_VER>=1400 && !defined(_WIN32_WCE)
  258. return !strerror_s(buf, buflen, errnum);
  259. #elif defined(_GNU_SOURCE)
  260. char *err;
  261. /*
  262. * GNU strerror_r may not actually set buf.
  263. * It can return a pointer to some (immutable) static string in which case
  264. * buf is left unused.
  265. */
  266. err = strerror_r(errnum, buf, buflen);
  267. if (err == NULL || buflen == 0)
  268. return 0;
  269. /*
  270. * If err is statically allocated, err != buf and we need to copy the data.
  271. * If err points somewhere inside buf, OPENSSL_strlcpy can handle this,
  272. * since src and dest are not annotated with __restrict and the function
  273. * reads src byte for byte and writes to dest.
  274. * If err == buf we do not have to copy anything.
  275. */
  276. if (err != buf)
  277. OPENSSL_strlcpy(buf, err, buflen);
  278. return 1;
  279. #elif (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) || \
  280. (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 600)
  281. /*
  282. * We can use "real" strerror_r. The OpenSSL version differs in that it
  283. * gives 1 on success and 0 on failure for consistency with other OpenSSL
  284. * functions. Real strerror_r does it the other way around
  285. */
  286. return !strerror_r(errnum, buf, buflen);
  287. #else
  288. char *err;
  289. /* Fall back to non-thread safe strerror()...its all we can do */
  290. if (buflen < 2)
  291. return 0;
  292. err = strerror(errnum);
  293. /* Can this ever happen? */
  294. if (err == NULL)
  295. return 0;
  296. OPENSSL_strlcpy(buf, err, buflen);
  297. return 1;
  298. #endif
  299. }