a_strex.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. /*
  2. * Copyright 2000-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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 <stdio.h>
  10. #include <string.h>
  11. #include "internal/cryptlib.h"
  12. #include "internal/asn1_int.h"
  13. #include <openssl/crypto.h>
  14. #include <openssl/x509.h>
  15. #include <openssl/asn1.h>
  16. #include "charmap.h"
  17. /*
  18. * ASN1_STRING_print_ex() and X509_NAME_print_ex(). Enhanced string and name
  19. * printing routines handling multibyte characters, RFC2253 and a host of
  20. * other options.
  21. */
  22. #define CHARTYPE_BS_ESC (ASN1_STRFLGS_ESC_2253 | CHARTYPE_FIRST_ESC_2253 | CHARTYPE_LAST_ESC_2253)
  23. #define ESC_FLAGS (ASN1_STRFLGS_ESC_2253 | \
  24. ASN1_STRFLGS_ESC_2254 | \
  25. ASN1_STRFLGS_ESC_QUOTE | \
  26. ASN1_STRFLGS_ESC_CTRL | \
  27. ASN1_STRFLGS_ESC_MSB)
  28. /*
  29. * Three IO functions for sending data to memory, a BIO and and a FILE
  30. * pointer.
  31. */
  32. static int send_bio_chars(void *arg, const void *buf, int len)
  33. {
  34. if (!arg)
  35. return 1;
  36. if (BIO_write(arg, buf, len) != len)
  37. return 0;
  38. return 1;
  39. }
  40. #ifndef OPENSSL_NO_STDIO
  41. static int send_fp_chars(void *arg, const void *buf, int len)
  42. {
  43. if (!arg)
  44. return 1;
  45. if (fwrite(buf, 1, len, arg) != (unsigned int)len)
  46. return 0;
  47. return 1;
  48. }
  49. #endif
  50. typedef int char_io (void *arg, const void *buf, int len);
  51. /*
  52. * This function handles display of strings, one character at a time. It is
  53. * passed an unsigned long for each character because it could come from 2 or
  54. * even 4 byte forms.
  55. */
  56. static int do_esc_char(unsigned long c, unsigned short flags, char *do_quotes,
  57. char_io *io_ch, void *arg)
  58. {
  59. unsigned short chflgs;
  60. unsigned char chtmp;
  61. char tmphex[HEX_SIZE(long) + 3];
  62. if (c > 0xffffffffL)
  63. return -1;
  64. if (c > 0xffff) {
  65. BIO_snprintf(tmphex, sizeof(tmphex), "\\W%08lX", c);
  66. if (!io_ch(arg, tmphex, 10))
  67. return -1;
  68. return 10;
  69. }
  70. if (c > 0xff) {
  71. BIO_snprintf(tmphex, sizeof(tmphex), "\\U%04lX", c);
  72. if (!io_ch(arg, tmphex, 6))
  73. return -1;
  74. return 6;
  75. }
  76. chtmp = (unsigned char)c;
  77. if (chtmp > 0x7f)
  78. chflgs = flags & ASN1_STRFLGS_ESC_MSB;
  79. else
  80. chflgs = char_type[chtmp] & flags;
  81. if (chflgs & CHARTYPE_BS_ESC) {
  82. /* If we don't escape with quotes, signal we need quotes */
  83. if (chflgs & ASN1_STRFLGS_ESC_QUOTE) {
  84. if (do_quotes)
  85. *do_quotes = 1;
  86. if (!io_ch(arg, &chtmp, 1))
  87. return -1;
  88. return 1;
  89. }
  90. if (!io_ch(arg, "\\", 1))
  91. return -1;
  92. if (!io_ch(arg, &chtmp, 1))
  93. return -1;
  94. return 2;
  95. }
  96. if (chflgs & (ASN1_STRFLGS_ESC_CTRL
  97. | ASN1_STRFLGS_ESC_MSB
  98. | ASN1_STRFLGS_ESC_2254)) {
  99. BIO_snprintf(tmphex, 11, "\\%02X", chtmp);
  100. if (!io_ch(arg, tmphex, 3))
  101. return -1;
  102. return 3;
  103. }
  104. /*
  105. * If we get this far and do any escaping at all must escape the escape
  106. * character itself: backslash.
  107. */
  108. if (chtmp == '\\' && (flags & ESC_FLAGS)) {
  109. if (!io_ch(arg, "\\\\", 2))
  110. return -1;
  111. return 2;
  112. }
  113. if (!io_ch(arg, &chtmp, 1))
  114. return -1;
  115. return 1;
  116. }
  117. #define BUF_TYPE_WIDTH_MASK 0x7
  118. #define BUF_TYPE_CONVUTF8 0x8
  119. /*
  120. * This function sends each character in a buffer to do_esc_char(). It
  121. * interprets the content formats and converts to or from UTF8 as
  122. * appropriate.
  123. */
  124. static int do_buf(unsigned char *buf, int buflen,
  125. int type, unsigned short flags, char *quotes, char_io *io_ch,
  126. void *arg)
  127. {
  128. int i, outlen, len;
  129. unsigned short orflags;
  130. unsigned char *p, *q;
  131. unsigned long c;
  132. p = buf;
  133. q = buf + buflen;
  134. outlen = 0;
  135. while (p != q) {
  136. if (p == buf && flags & ASN1_STRFLGS_ESC_2253)
  137. orflags = CHARTYPE_FIRST_ESC_2253;
  138. else
  139. orflags = 0;
  140. switch (type & BUF_TYPE_WIDTH_MASK) {
  141. case 4:
  142. c = ((unsigned long)*p++) << 24;
  143. c |= ((unsigned long)*p++) << 16;
  144. c |= ((unsigned long)*p++) << 8;
  145. c |= *p++;
  146. break;
  147. case 2:
  148. c = ((unsigned long)*p++) << 8;
  149. c |= *p++;
  150. break;
  151. case 1:
  152. c = *p++;
  153. break;
  154. case 0:
  155. i = UTF8_getc(p, buflen, &c);
  156. if (i < 0)
  157. return -1; /* Invalid UTF8String */
  158. p += i;
  159. break;
  160. default:
  161. return -1; /* invalid width */
  162. }
  163. if (p == q && flags & ASN1_STRFLGS_ESC_2253)
  164. orflags = CHARTYPE_LAST_ESC_2253;
  165. if (type & BUF_TYPE_CONVUTF8) {
  166. unsigned char utfbuf[6];
  167. int utflen;
  168. utflen = UTF8_putc(utfbuf, sizeof(utfbuf), c);
  169. for (i = 0; i < utflen; i++) {
  170. /*
  171. * We don't need to worry about setting orflags correctly
  172. * because if utflen==1 its value will be correct anyway
  173. * otherwise each character will be > 0x7f and so the
  174. * character will never be escaped on first and last.
  175. */
  176. len = do_esc_char(utfbuf[i], flags | orflags, quotes,
  177. io_ch, arg);
  178. if (len < 0)
  179. return -1;
  180. outlen += len;
  181. }
  182. } else {
  183. len = do_esc_char(c, flags | orflags, quotes,
  184. io_ch, arg);
  185. if (len < 0)
  186. return -1;
  187. outlen += len;
  188. }
  189. }
  190. return outlen;
  191. }
  192. /* This function hex dumps a buffer of characters */
  193. static int do_hex_dump(char_io *io_ch, void *arg, unsigned char *buf,
  194. int buflen)
  195. {
  196. static const char hexdig[] = "0123456789ABCDEF";
  197. unsigned char *p, *q;
  198. char hextmp[2];
  199. if (arg) {
  200. p = buf;
  201. q = buf + buflen;
  202. while (p != q) {
  203. hextmp[0] = hexdig[*p >> 4];
  204. hextmp[1] = hexdig[*p & 0xf];
  205. if (!io_ch(arg, hextmp, 2))
  206. return -1;
  207. p++;
  208. }
  209. }
  210. return buflen << 1;
  211. }
  212. /*
  213. * "dump" a string. This is done when the type is unknown, or the flags
  214. * request it. We can either dump the content octets or the entire DER
  215. * encoding. This uses the RFC2253 #01234 format.
  216. */
  217. static int do_dump(unsigned long lflags, char_io *io_ch, void *arg,
  218. const ASN1_STRING *str)
  219. {
  220. /*
  221. * Placing the ASN1_STRING in a temp ASN1_TYPE allows the DER encoding to
  222. * readily obtained
  223. */
  224. ASN1_TYPE t;
  225. unsigned char *der_buf, *p;
  226. int outlen, der_len;
  227. if (!io_ch(arg, "#", 1))
  228. return -1;
  229. /* If we don't dump DER encoding just dump content octets */
  230. if (!(lflags & ASN1_STRFLGS_DUMP_DER)) {
  231. outlen = do_hex_dump(io_ch, arg, str->data, str->length);
  232. if (outlen < 0)
  233. return -1;
  234. return outlen + 1;
  235. }
  236. t.type = str->type;
  237. t.value.ptr = (char *)str;
  238. der_len = i2d_ASN1_TYPE(&t, NULL);
  239. if ((der_buf = OPENSSL_malloc(der_len)) == NULL) {
  240. ASN1err(ASN1_F_DO_DUMP, ERR_R_MALLOC_FAILURE);
  241. return -1;
  242. }
  243. p = der_buf;
  244. i2d_ASN1_TYPE(&t, &p);
  245. outlen = do_hex_dump(io_ch, arg, der_buf, der_len);
  246. OPENSSL_free(der_buf);
  247. if (outlen < 0)
  248. return -1;
  249. return outlen + 1;
  250. }
  251. /*
  252. * Lookup table to convert tags to character widths, 0 = UTF8 encoded, -1 is
  253. * used for non string types otherwise it is the number of bytes per
  254. * character
  255. */
  256. static const signed char tag2nbyte[] = {
  257. -1, -1, -1, -1, -1, /* 0-4 */
  258. -1, -1, -1, -1, -1, /* 5-9 */
  259. -1, -1, /* 10-11 */
  260. 0, /* 12 V_ASN1_UTF8STRING */
  261. -1, -1, -1, -1, -1, /* 13-17 */
  262. 1, /* 18 V_ASN1_NUMERICSTRING */
  263. 1, /* 19 V_ASN1_PRINTABLESTRING */
  264. 1, /* 20 V_ASN1_T61STRING */
  265. -1, /* 21 */
  266. 1, /* 22 V_ASN1_IA5STRING */
  267. 1, /* 23 V_ASN1_UTCTIME */
  268. 1, /* 24 V_ASN1_GENERALIZEDTIME */
  269. -1, /* 25 */
  270. 1, /* 26 V_ASN1_ISO64STRING */
  271. -1, /* 27 */
  272. 4, /* 28 V_ASN1_UNIVERSALSTRING */
  273. -1, /* 29 */
  274. 2 /* 30 V_ASN1_BMPSTRING */
  275. };
  276. /*
  277. * This is the main function, print out an ASN1_STRING taking note of various
  278. * escape and display options. Returns number of characters written or -1 if
  279. * an error occurred.
  280. */
  281. static int do_print_ex(char_io *io_ch, void *arg, unsigned long lflags,
  282. const ASN1_STRING *str)
  283. {
  284. int outlen, len;
  285. int type;
  286. char quotes;
  287. unsigned short flags;
  288. quotes = 0;
  289. /* Keep a copy of escape flags */
  290. flags = (unsigned short)(lflags & ESC_FLAGS);
  291. type = str->type;
  292. outlen = 0;
  293. if (lflags & ASN1_STRFLGS_SHOW_TYPE) {
  294. const char *tagname;
  295. tagname = ASN1_tag2str(type);
  296. outlen += strlen(tagname);
  297. if (!io_ch(arg, tagname, outlen) || !io_ch(arg, ":", 1))
  298. return -1;
  299. outlen++;
  300. }
  301. /* Decide what to do with type, either dump content or display it */
  302. /* Dump everything */
  303. if (lflags & ASN1_STRFLGS_DUMP_ALL)
  304. type = -1;
  305. /* Ignore the string type */
  306. else if (lflags & ASN1_STRFLGS_IGNORE_TYPE)
  307. type = 1;
  308. else {
  309. /* Else determine width based on type */
  310. if ((type > 0) && (type < 31))
  311. type = tag2nbyte[type];
  312. else
  313. type = -1;
  314. if ((type == -1) && !(lflags & ASN1_STRFLGS_DUMP_UNKNOWN))
  315. type = 1;
  316. }
  317. if (type == -1) {
  318. len = do_dump(lflags, io_ch, arg, str);
  319. if (len < 0)
  320. return -1;
  321. outlen += len;
  322. return outlen;
  323. }
  324. if (lflags & ASN1_STRFLGS_UTF8_CONVERT) {
  325. /*
  326. * Note: if string is UTF8 and we want to convert to UTF8 then we
  327. * just interpret it as 1 byte per character to avoid converting
  328. * twice.
  329. */
  330. if (!type)
  331. type = 1;
  332. else
  333. type |= BUF_TYPE_CONVUTF8;
  334. }
  335. len = do_buf(str->data, str->length, type, flags, &quotes, io_ch, NULL);
  336. if (len < 0)
  337. return -1;
  338. outlen += len;
  339. if (quotes)
  340. outlen += 2;
  341. if (!arg)
  342. return outlen;
  343. if (quotes && !io_ch(arg, "\"", 1))
  344. return -1;
  345. if (do_buf(str->data, str->length, type, flags, NULL, io_ch, arg) < 0)
  346. return -1;
  347. if (quotes && !io_ch(arg, "\"", 1))
  348. return -1;
  349. return outlen;
  350. }
  351. /* Used for line indenting: print 'indent' spaces */
  352. static int do_indent(char_io *io_ch, void *arg, int indent)
  353. {
  354. int i;
  355. for (i = 0; i < indent; i++)
  356. if (!io_ch(arg, " ", 1))
  357. return 0;
  358. return 1;
  359. }
  360. #define FN_WIDTH_LN 25
  361. #define FN_WIDTH_SN 10
  362. static int do_name_ex(char_io *io_ch, void *arg, const X509_NAME *n,
  363. int indent, unsigned long flags)
  364. {
  365. int i, prev = -1, orflags, cnt;
  366. int fn_opt, fn_nid;
  367. ASN1_OBJECT *fn;
  368. const ASN1_STRING *val;
  369. const X509_NAME_ENTRY *ent;
  370. char objtmp[80];
  371. const char *objbuf;
  372. int outlen, len;
  373. char *sep_dn, *sep_mv, *sep_eq;
  374. int sep_dn_len, sep_mv_len, sep_eq_len;
  375. if (indent < 0)
  376. indent = 0;
  377. outlen = indent;
  378. if (!do_indent(io_ch, arg, indent))
  379. return -1;
  380. switch (flags & XN_FLAG_SEP_MASK) {
  381. case XN_FLAG_SEP_MULTILINE:
  382. sep_dn = "\n";
  383. sep_dn_len = 1;
  384. sep_mv = " + ";
  385. sep_mv_len = 3;
  386. break;
  387. case XN_FLAG_SEP_COMMA_PLUS:
  388. sep_dn = ",";
  389. sep_dn_len = 1;
  390. sep_mv = "+";
  391. sep_mv_len = 1;
  392. indent = 0;
  393. break;
  394. case XN_FLAG_SEP_CPLUS_SPC:
  395. sep_dn = ", ";
  396. sep_dn_len = 2;
  397. sep_mv = " + ";
  398. sep_mv_len = 3;
  399. indent = 0;
  400. break;
  401. case XN_FLAG_SEP_SPLUS_SPC:
  402. sep_dn = "; ";
  403. sep_dn_len = 2;
  404. sep_mv = " + ";
  405. sep_mv_len = 3;
  406. indent = 0;
  407. break;
  408. default:
  409. return -1;
  410. }
  411. if (flags & XN_FLAG_SPC_EQ) {
  412. sep_eq = " = ";
  413. sep_eq_len = 3;
  414. } else {
  415. sep_eq = "=";
  416. sep_eq_len = 1;
  417. }
  418. fn_opt = flags & XN_FLAG_FN_MASK;
  419. cnt = X509_NAME_entry_count(n);
  420. for (i = 0; i < cnt; i++) {
  421. if (flags & XN_FLAG_DN_REV)
  422. ent = X509_NAME_get_entry(n, cnt - i - 1);
  423. else
  424. ent = X509_NAME_get_entry(n, i);
  425. if (prev != -1) {
  426. if (prev == X509_NAME_ENTRY_set(ent)) {
  427. if (!io_ch(arg, sep_mv, sep_mv_len))
  428. return -1;
  429. outlen += sep_mv_len;
  430. } else {
  431. if (!io_ch(arg, sep_dn, sep_dn_len))
  432. return -1;
  433. outlen += sep_dn_len;
  434. if (!do_indent(io_ch, arg, indent))
  435. return -1;
  436. outlen += indent;
  437. }
  438. }
  439. prev = X509_NAME_ENTRY_set(ent);
  440. fn = X509_NAME_ENTRY_get_object(ent);
  441. val = X509_NAME_ENTRY_get_data(ent);
  442. fn_nid = OBJ_obj2nid(fn);
  443. if (fn_opt != XN_FLAG_FN_NONE) {
  444. int objlen, fld_len;
  445. if ((fn_opt == XN_FLAG_FN_OID) || (fn_nid == NID_undef)) {
  446. OBJ_obj2txt(objtmp, sizeof(objtmp), fn, 1);
  447. fld_len = 0; /* XXX: what should this be? */
  448. objbuf = objtmp;
  449. } else {
  450. if (fn_opt == XN_FLAG_FN_SN) {
  451. fld_len = FN_WIDTH_SN;
  452. objbuf = OBJ_nid2sn(fn_nid);
  453. } else if (fn_opt == XN_FLAG_FN_LN) {
  454. fld_len = FN_WIDTH_LN;
  455. objbuf = OBJ_nid2ln(fn_nid);
  456. } else {
  457. fld_len = 0; /* XXX: what should this be? */
  458. objbuf = "";
  459. }
  460. }
  461. objlen = strlen(objbuf);
  462. if (!io_ch(arg, objbuf, objlen))
  463. return -1;
  464. if ((objlen < fld_len) && (flags & XN_FLAG_FN_ALIGN)) {
  465. if (!do_indent(io_ch, arg, fld_len - objlen))
  466. return -1;
  467. outlen += fld_len - objlen;
  468. }
  469. if (!io_ch(arg, sep_eq, sep_eq_len))
  470. return -1;
  471. outlen += objlen + sep_eq_len;
  472. }
  473. /*
  474. * If the field name is unknown then fix up the DER dump flag. We
  475. * might want to limit this further so it will DER dump on anything
  476. * other than a few 'standard' fields.
  477. */
  478. if ((fn_nid == NID_undef) && (flags & XN_FLAG_DUMP_UNKNOWN_FIELDS))
  479. orflags = ASN1_STRFLGS_DUMP_ALL;
  480. else
  481. orflags = 0;
  482. len = do_print_ex(io_ch, arg, flags | orflags, val);
  483. if (len < 0)
  484. return -1;
  485. outlen += len;
  486. }
  487. return outlen;
  488. }
  489. /* Wrappers round the main functions */
  490. int X509_NAME_print_ex(BIO *out, const X509_NAME *nm, int indent,
  491. unsigned long flags)
  492. {
  493. if (flags == XN_FLAG_COMPAT)
  494. return X509_NAME_print(out, nm, indent);
  495. return do_name_ex(send_bio_chars, out, nm, indent, flags);
  496. }
  497. #ifndef OPENSSL_NO_STDIO
  498. int X509_NAME_print_ex_fp(FILE *fp, const X509_NAME *nm, int indent,
  499. unsigned long flags)
  500. {
  501. if (flags == XN_FLAG_COMPAT) {
  502. BIO *btmp;
  503. int ret;
  504. btmp = BIO_new_fp(fp, BIO_NOCLOSE);
  505. if (!btmp)
  506. return -1;
  507. ret = X509_NAME_print(btmp, nm, indent);
  508. BIO_free(btmp);
  509. return ret;
  510. }
  511. return do_name_ex(send_fp_chars, fp, nm, indent, flags);
  512. }
  513. #endif
  514. int ASN1_STRING_print_ex(BIO *out, const ASN1_STRING *str, unsigned long flags)
  515. {
  516. return do_print_ex(send_bio_chars, out, flags, str);
  517. }
  518. #ifndef OPENSSL_NO_STDIO
  519. int ASN1_STRING_print_ex_fp(FILE *fp, const ASN1_STRING *str, unsigned long flags)
  520. {
  521. return do_print_ex(send_fp_chars, fp, flags, str);
  522. }
  523. #endif
  524. /*
  525. * Utility function: convert any string type to UTF8, returns number of bytes
  526. * in output string or a negative error code
  527. */
  528. int ASN1_STRING_to_UTF8(unsigned char **out, const ASN1_STRING *in)
  529. {
  530. ASN1_STRING stmp, *str = &stmp;
  531. int mbflag, type, ret;
  532. if (!in)
  533. return -1;
  534. type = in->type;
  535. if ((type < 0) || (type > 30))
  536. return -1;
  537. mbflag = tag2nbyte[type];
  538. if (mbflag == -1)
  539. return -1;
  540. mbflag |= MBSTRING_FLAG;
  541. stmp.data = NULL;
  542. stmp.length = 0;
  543. stmp.flags = 0;
  544. ret =
  545. ASN1_mbstring_copy(&str, in->data, in->length, mbflag,
  546. B_ASN1_UTF8STRING);
  547. if (ret < 0)
  548. return ret;
  549. *out = stmp.data;
  550. return stmp.length;
  551. }
  552. /* Return 1 if host is a valid hostname and 0 otherwise */
  553. int asn1_valid_host(const ASN1_STRING *host)
  554. {
  555. int hostlen = host->length;
  556. const unsigned char *hostptr = host->data;
  557. int type = host->type;
  558. int i;
  559. signed char width = -1;
  560. unsigned short chflags = 0, prevchflags;
  561. if (type > 0 && type < 31)
  562. width = tag2nbyte[type];
  563. if (width == -1 || hostlen == 0)
  564. return 0;
  565. /* Treat UTF8String as width 1 as any MSB set is invalid */
  566. if (width == 0)
  567. width = 1;
  568. for (i = 0 ; i < hostlen; i+= width) {
  569. prevchflags = chflags;
  570. /* Value must be <= 0x7F: check upper bytes are all zeroes */
  571. if (width == 4) {
  572. if (*hostptr++ != 0 || *hostptr++ != 0 || *hostptr++ != 0)
  573. return 0;
  574. } else if (width == 2) {
  575. if (*hostptr++ != 0)
  576. return 0;
  577. }
  578. if (*hostptr > 0x7f)
  579. return 0;
  580. chflags = char_type[*hostptr++];
  581. if (!(chflags & (CHARTYPE_HOST_ANY | CHARTYPE_HOST_WILD))) {
  582. /* Nothing else allowed at start or end of string */
  583. if (i == 0 || i == hostlen - 1)
  584. return 0;
  585. /* Otherwise invalid if not dot or hyphen */
  586. if (!(chflags & (CHARTYPE_HOST_DOT | CHARTYPE_HOST_HYPHEN)))
  587. return 0;
  588. /*
  589. * If previous is dot or hyphen then illegal unless both
  590. * are hyphens: as .- -. .. are all illegal
  591. */
  592. if (prevchflags & (CHARTYPE_HOST_DOT | CHARTYPE_HOST_HYPHEN)
  593. && ((prevchflags & CHARTYPE_HOST_DOT)
  594. || (chflags & CHARTYPE_HOST_DOT)))
  595. return 0;
  596. }
  597. }
  598. return 1;
  599. }