Path: blob/main/crypto/heimdal/lib/wind/stringprep.c
34914 views
/*1* Copyright (c) 2004, 2006, 2008 Kungliga Tekniska Högskolan2* (Royal Institute of Technology, Stockholm, Sweden).3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8*9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11*12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* 3. Neither the name of the Institute nor the names of its contributors17* may be used to endorse or promote products derived from this software18* without specific prior written permission.19*20* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND21* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE23* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE24* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL25* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS26* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT28* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY29* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF30* SUCH DAMAGE.31*/3233#ifdef HAVE_CONFIG_H34#include <config.h>35#endif36#include "windlocl.h"37#include <stdlib.h>38#include <string.h>39#include <errno.h>4041/**42* Process a input UCS4 string according a string-prep profile.43*44* @param in input UCS4 string to process45* @param in_len length of the input string46* @param out output UCS4 string47* @param out_len length of the output string.48* @param flags stringprep profile.49*50* @return returns 0 on success, an wind error code otherwise51* @ingroup wind52*/5354int55wind_stringprep(const uint32_t *in, size_t in_len,56uint32_t *out, size_t *out_len,57wind_profile_flags flags)58{59size_t tmp_len = in_len * 3;60uint32_t *tmp;61int ret;62size_t olen;6364if (in_len == 0) {65*out_len = 0;66return 0;67}6869tmp = malloc(tmp_len * sizeof(uint32_t));70if (tmp == NULL)71return ENOMEM;7273ret = _wind_stringprep_map(in, in_len, tmp, &tmp_len, flags);74if (ret) {75free(tmp);76return ret;77}7879olen = *out_len;80ret = _wind_stringprep_normalize(tmp, tmp_len, tmp, &olen);81if (ret) {82free(tmp);83return ret;84}85ret = _wind_stringprep_prohibited(tmp, olen, flags);86if (ret) {87free(tmp);88return ret;89}90ret = _wind_stringprep_testbidi(tmp, olen, flags);91if (ret) {92free(tmp);93return ret;94}9596/* Insignificant Character Handling for ldap-prep */97if (flags & WIND_PROFILE_LDAP_CASE_EXACT_ATTRIBUTE) {98ret = _wind_ldap_case_exact_attribute(tmp, olen, out, out_len);99#if 0100} else if (flags & WIND_PROFILE_LDAP_CASE_EXACT_ASSERTION) {101} else if (flags & WIND_PROFILE_LDAP_NUMERIC) {102} else if (flags & WIND_PROFILE_LDAP_TELEPHONE) {103#endif104} else {105memcpy(out, tmp, sizeof(out[0]) * olen);106*out_len = olen;107}108free(tmp);109110return ret;111}112113static const struct {114const char *name;115wind_profile_flags flags;116} profiles[] = {117{ "nameprep", WIND_PROFILE_NAME },118{ "saslprep", WIND_PROFILE_SASL },119{ "ldapprep", WIND_PROFILE_LDAP }120};121122/**123* Try to find the profile given a name.124*125* @param name name of the profile.126* @param flags the resulting profile.127*128* @return returns 0 on success, an wind error code otherwise129* @ingroup wind130*/131132int133wind_profile(const char *name, wind_profile_flags *flags)134{135unsigned int i;136137for (i = 0; i < sizeof(profiles)/sizeof(profiles[0]); i++) {138if (strcasecmp(profiles[i].name, name) == 0) {139*flags = profiles[i].flags;140return 0;141}142}143return WIND_ERR_NO_PROFILE;144}145146147