+ return conf_get_str(conf, CONF_host);
+}
+
+#ifndef PLATFORM_HAS_SMEMCLR
+/*
+ * Securely wipe memory.
+ *
+ * The actual wiping is no different from what memset would do: the
+ * point of 'securely' is to try to be sure over-clever compilers
+ * won't optimise away memsets on variables that are about to be freed
+ * or go out of scope. See
+ * https://buildsecurityin.us-cert.gov/bsi-rules/home/g1/771-BSI.html
+ *
+ * Some platforms (e.g. Windows) may provide their own version of this
+ * function.
+ */
+void smemclr(void *b, size_t n) {
+ volatile char *vp;
+
+ if (b && n > 0) {
+ /*
+ * Zero out the memory.
+ */
+ memset(b, 0, n);
+
+ /*
+ * Perform a volatile access to the object, forcing the
+ * compiler to admit that the previous memset was important.
+ *
+ * This while loop should in practice run for zero iterations
+ * (since we know we just zeroed the object out), but in
+ * theory (as far as the compiler knows) it might range over
+ * the whole object. (If we had just written, say, '*vp =
+ * *vp;', a compiler could in principle have 'helpfully'
+ * optimised the memset into only zeroing out the first byte.
+ * This should be robust.)
+ */
+ vp = b;
+ while (*vp) vp++;
+ }
+}
+#endif
+
+/*
+ * Validate a manual host key specification (either entered in the
+ * GUI, or via -hostkey). If valid, we return TRUE, and update 'key'
+ * to contain a canonicalised version of the key string in 'key'
+ * (which is guaranteed to take up at most as much space as the
+ * original version), suitable for putting into the Conf. If not
+ * valid, we return FALSE.
+ */
+int validate_manual_hostkey(char *key)
+{
+ char *p, *q, *r, *s;
+
+ /*
+ * Step through the string word by word, looking for a word that's
+ * in one of the formats we like.
+ */
+ p = key;
+ while ((p += strspn(p, " \t"))[0]) {
+ q = p;
+ p += strcspn(p, " \t");
+ if (*p) *p++ = '\0';
+
+ /*
+ * Now q is our word.
+ */
+
+ if (strlen(q) == 16*3 - 1 &&
+ q[strspn(q, "0123456789abcdefABCDEF:")] == 0) {
+ /*
+ * Might be a key fingerprint. Check the colons are in the
+ * right places, and if so, return the same fingerprint
+ * canonicalised into lowercase.
+ */
+ int i;
+ for (i = 0; i < 16; i++)
+ if (q[3*i] == ':' || q[3*i+1] == ':')
+ goto not_fingerprint; /* sorry */
+ for (i = 0; i < 15; i++)
+ if (q[3*i+2] != ':')
+ goto not_fingerprint; /* sorry */
+ for (i = 0; i < 16*3 - 1; i++)
+ key[i] = tolower(q[i]);
+ key[16*3 - 1] = '\0';
+ return TRUE;
+ }
+ not_fingerprint:;
+
+ /*
+ * Before we check for a public-key blob, trim newlines out of
+ * the middle of the word, in case someone's managed to paste
+ * in a public-key blob _with_ them.
+ */
+ for (r = s = q; *r; r++)
+ if (*r != '\n' && *r != '\r')
+ *s++ = *r;
+ *s = '\0';
+
+ if (strlen(q) % 4 == 0 && strlen(q) > 2*4 &&
+ q[strspn(q, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+ "abcdefghijklmnopqrstuvwxyz+/=")] == 0) {
+ /*
+ * Might be a base64-encoded SSH-2 public key blob. Check
+ * that it starts with a sensible algorithm string. No
+ * canonicalisation is necessary for this string type.
+ *
+ * The algorithm string must be at most 64 characters long
+ * (RFC 4251 section 6).
+ */
+ unsigned char decoded[6];
+ unsigned alglen;
+ int minlen;
+ int len = 0;
+
+ len += base64_decode_atom(q, decoded+len);
+ if (len < 3)
+ goto not_ssh2_blob; /* sorry */
+ len += base64_decode_atom(q+4, decoded+len);
+ if (len < 4)
+ goto not_ssh2_blob; /* sorry */
+
+ alglen = GET_32BIT_MSB_FIRST(decoded);
+ if (alglen > 64)
+ goto not_ssh2_blob; /* sorry */
+
+ minlen = ((alglen + 4) + 2) / 3;
+ if (strlen(q) < minlen)
+ goto not_ssh2_blob; /* sorry */
+
+ strcpy(key, q);
+ return TRUE;
+ }
+ not_ssh2_blob:;
+ }
+
+ return FALSE;
+}
+
+int smemeq(const void *av, const void *bv, size_t len)
+{
+ const unsigned char *a = (const unsigned char *)av;
+ const unsigned char *b = (const unsigned char *)bv;
+ unsigned val = 0;
+
+ while (len-- > 0) {
+ val |= *a++ ^ *b++;
+ }
+ /* Now val is 0 iff we want to return 1, and in the range
+ * 0x01..0xFF iff we want to return 0. So subtracting from 0x100
+ * will clear bit 8 iff we want to return 0, and leave it set iff
+ * we want to return 1, so then we can just shift down. */
+ return (0x100 - val) >> 8;
+}
+
+int match_ssh_id(int stringlen, const void *string, const char *id)
+{
+ int idlen = strlen(id);
+ return (idlen == stringlen && !memcmp(string, id, idlen));
+}
+
+void *get_ssh_string(int *datalen, const void **data, int *stringlen)
+{
+ void *ret;
+ int len;
+
+ if (*datalen < 4)
+ return NULL;
+ len = GET_32BIT_MSB_FIRST((const unsigned char *)*data);
+ if (*datalen < len+4)
+ return NULL;
+ ret = (void *)((const char *)*data + 4);
+ *datalen -= len + 4;
+ *data = (const char *)*data + len + 4;
+ *stringlen = len;
+ return ret;
+}
+
+int get_ssh_uint32(int *datalen, const void **data, unsigned *ret)
+{
+ if (*datalen < 4)
+ return FALSE;
+ *ret = GET_32BIT_MSB_FIRST((const unsigned char *)*data);
+ *datalen -= 4;
+ *data = (const char *)*data + 4;
+ return TRUE;