diff options
author | Nikos Mavrogiannopoulos <nmav@redhat.com> | 2016-11-10 16:43:23 +0100 |
---|---|---|
committer | Nikos Mavrogiannopoulos <nmav@redhat.com> | 2016-11-11 13:38:37 +0100 |
commit | c2fa842bdcb88d0f7a16be9e49cc87b340cb68e2 (patch) | |
tree | 824bfcd9cd5f464699ed2b4983cfded8b726c039 /lib/str-unicode.c | |
parent | 6d5c80e2bf7efbc4e99588b8d8ec4f383d34375f (diff) | |
download | gnutls-tmp-uninorm-everywhere.tar.gz |
x509: when writing fields known to be in UTF-8 form convert to NFCtmp-uninorm-everywhere
This ensures that we always write proper UTF-8 data, uniquely encoded.
Diffstat (limited to 'lib/str-unicode.c')
-rw-r--r-- | lib/str-unicode.c | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/lib/str-unicode.c b/lib/str-unicode.c index 7bda8cab07..20e9ca10bd 100644 --- a/lib/str-unicode.c +++ b/lib/str-unicode.c @@ -103,4 +103,50 @@ uint8_t *_gnutls_normalize_u8_password(const uint8_t *password) return NULL; } +uint8_t *_gnutls_normalize_u8_nfc(const uint8_t *str, size_t str_size) +{ + size_t nrm_size = 0; + size_t out_size = 0; + uint8_t *out = NULL; + uint8_t *nrm = NULL; + + if (str_size == 0) { + return (uint8_t*)strdup(""); + } + + /* check for invalid UTF-8 */ + if (u8_check(str, str_size) != NULL) { + gnutls_assert(); + return NULL; + } + + /* normalize to NFC */ + nrm = u8_normalize(UNINORM_NFC, str, str_size, NULL, &nrm_size); + if (nrm == NULL) { + gnutls_assert(); + goto fail; + } + + out_size = nrm_size; + + /* copy to output with null terminator */ + out = gnutls_malloc(out_size+1); + if (out == NULL) { + gnutls_assert(); + goto fail; + } + + memcpy(out, nrm, out_size); + out[out_size] = 0; + + gnutls_free(nrm); + + return out; + + fail: + gnutls_free(out); + gnutls_free(nrm); + return NULL; +} + #endif |