static int get_id(const char *url, uint8_t *bin, size_t *bin_size, unsigned cert) { int ret; unsigned url_size = strlen(url); const char *p = url, *p2; if (cert != 0) { if (url_size < sizeof(WIN_URL) || strncmp(url, WIN_URL, WIN_URL_SIZE) != 0) return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); } else { if (url_size < sizeof(WIN_URL) || strncmp(url, WIN_URL, WIN_URL_SIZE) != 0) return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); } p += sizeof(WIN_URL) - 1; p = strstr(p, "id="); if (p == NULL) return gnutls_assert_val(GNUTLS_E_PARSING_ERROR); p += 3; p2 = strchr(p, ';'); if (p2 == NULL) { url_size = strlen(p); } else { url_size = (p2-p); } ret = _gnutls_hex2bin(p, url_size, bin, bin_size); if (ret < 0) return ret; return 0; }
/** * gnutls_hex_decode - decode hex encoded data * @hex_data: contain the encoded data * @result: the place where decoded data will be copied * @result_size: holds the size of the result * * This function will decode the given encoded data, using the hex encoding * used by PSK password files. * * Note that hex_data should be null terminated. * * Returns: %GNUTLS_E_SHORT_MEMORY_BUFFER if the buffer given is not * long enough, or 0 on success. **/ int gnutls_hex_decode (const gnutls_datum_t * hex_data, char *result, size_t * result_size) { int ret; ret = _gnutls_hex2bin (hex_data->data, hex_data->size, (opaque *) result, result_size); if (ret < 0) return ret; return 0; }
/* this function parses passwd.psk file. Format is: * string(username):hex(passwd) */ static int pwd_put_values (gnutls_datum_t * psk, char *str) { char *p; int len, ret; size_t size; p = strchr (str, ':'); if (p == NULL) { gnutls_assert (); return GNUTLS_E_SRP_PWD_PARSING_ERROR; } *p = '\0'; p++; /* skip username */ /* read the key */ len = strlen (p); if (p[len - 1] == '\n' || p[len - 1] == ' ') len--; size = psk->size = len / 2; psk->data = gnutls_malloc (size); if (psk->data == NULL) { gnutls_assert (); return GNUTLS_E_MEMORY_ERROR; } ret = _gnutls_hex2bin ((opaque *) p, len, psk->data, &size); psk->size = (unsigned int) size; if (ret < 0) { gnutls_assert (); return ret; } return 0; }
static int get_keyid (gnutls_openpgp_keyid_t keyid, const char *str) { size_t keyid_size = sizeof (keyid); if (strlen (str) != 16) { _gnutls_debug_log ("The OpenPGP subkey ID has to be 16 hexadecimal characters.\n"); return GNUTLS_E_INVALID_REQUEST; } if (_gnutls_hex2bin (str, strlen (str), keyid, &keyid_size) < 0) { _gnutls_debug_log ("Error converting hex string: %s.\n", str); return GNUTLS_E_INVALID_REQUEST; } return 0; }
static int decode_tpmkey_url(const char *url, struct tpmkey_url_st *s) { char *p; size_t size; int ret; unsigned int i, j; if (strstr(url, "tpmkey:") == NULL) return gnutls_assert_val(GNUTLS_E_PARSING_ERROR); memset(s, 0, sizeof(*s)); p = strstr(url, "file="); if (p != NULL) { p += sizeof("file=") - 1; size = strlen(p); s->filename = gnutls_malloc(size + 1); if (s->filename == NULL) return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR); ret = unescape_string(s->filename, p, &size, ';'); if (ret < 0) { gnutls_assert(); goto cleanup; } s->filename[size] = 0; } else if ((p = strstr(url, "uuid=")) != NULL) { char tmp_uuid[33]; uint8_t raw_uuid[16]; p += sizeof("uuid=") - 1; size = strlen(p); for (j = i = 0; i < size; i++) { if (j == sizeof(tmp_uuid) - 1) { break; } if (c_isalnum(p[i])) tmp_uuid[j++] = p[i]; } tmp_uuid[j] = 0; size = sizeof(raw_uuid); ret = _gnutls_hex2bin(tmp_uuid, strlen(tmp_uuid), raw_uuid, &size); if (ret < 0) { gnutls_assert(); goto cleanup; } memcpy(&s->uuid.ulTimeLow, raw_uuid, 4); memcpy(&s->uuid.usTimeMid, &raw_uuid[4], 2); memcpy(&s->uuid.usTimeHigh, &raw_uuid[6], 2); s->uuid.bClockSeqHigh = raw_uuid[8]; s->uuid.bClockSeqLow = raw_uuid[9]; memcpy(&s->uuid.rgbNode, &raw_uuid[10], 6); s->uuid_set = 1; } else { return gnutls_assert_val(GNUTLS_E_PARSING_ERROR); } if ((p = strstr(url, "storage=user")) != NULL) s->storage = TSS_PS_TYPE_USER; else s->storage = TSS_PS_TYPE_SYSTEM; return 0; cleanup: clear_tpmkey_url(s); return ret; }
/** * gnutls_hex2bin: * @hex_data: string with data in hex format * @hex_size: size of hex data * @bin_data: output array with binary data * @bin_size: when calling *@bin_size should hold size of @bin_data, * on return will hold actual size of @bin_data. * * Convert a buffer with hex data to binary data. * * Returns: %GNUTLS_E_SUCCESS on success, otherwise an error. * * Since: 2.4.0 **/ int gnutls_hex2bin (const char *hex_data, size_t hex_size, char *bin_data, size_t * bin_size) { return _gnutls_hex2bin (hex_data, (int) hex_size, bin_data, bin_size); }