xref: /minix3/crypto/external/bsd/heimdal/dist/lib/wind/stringprep.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: stringprep.c,v 1.1.1.2 2014/04/24 12:45:56 pettai Exp $	*/
2ebfedea0SLionel Sambuc 
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc  * Copyright (c) 2004, 2006, 2008 Kungliga Tekniska Högskolan
5ebfedea0SLionel Sambuc  * (Royal Institute of Technology, Stockholm, Sweden).
6ebfedea0SLionel Sambuc  * All rights reserved.
7ebfedea0SLionel Sambuc  *
8ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
9ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
10ebfedea0SLionel Sambuc  * are met:
11ebfedea0SLionel Sambuc  *
12ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
13ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
14ebfedea0SLionel Sambuc  *
15ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
16ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
17ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
18ebfedea0SLionel Sambuc  *
19ebfedea0SLionel Sambuc  * 3. Neither the name of the Institute nor the names of its contributors
20ebfedea0SLionel Sambuc  *    may be used to endorse or promote products derived from this software
21ebfedea0SLionel Sambuc  *    without specific prior written permission.
22ebfedea0SLionel Sambuc  *
23ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24ebfedea0SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ebfedea0SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27ebfedea0SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28ebfedea0SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29ebfedea0SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31ebfedea0SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32ebfedea0SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33ebfedea0SLionel Sambuc  * SUCH DAMAGE.
34ebfedea0SLionel Sambuc  */
35ebfedea0SLionel Sambuc 
36ebfedea0SLionel Sambuc #ifdef HAVE_CONFIG_H
37ebfedea0SLionel Sambuc #include <config.h>
38ebfedea0SLionel Sambuc #endif
39ebfedea0SLionel Sambuc #include "windlocl.h"
40ebfedea0SLionel Sambuc #include <stdlib.h>
41ebfedea0SLionel Sambuc #include <string.h>
42ebfedea0SLionel Sambuc #include <errno.h>
43ebfedea0SLionel Sambuc 
44ebfedea0SLionel Sambuc /**
45ebfedea0SLionel Sambuc  * Process a input UCS4 string according a string-prep profile.
46ebfedea0SLionel Sambuc  *
47ebfedea0SLionel Sambuc  * @param in input UCS4 string to process
48ebfedea0SLionel Sambuc  * @param in_len length of the input string
49ebfedea0SLionel Sambuc  * @param out output UCS4 string
50ebfedea0SLionel Sambuc  * @param out_len length of the output string.
51ebfedea0SLionel Sambuc  * @param flags stringprep profile.
52ebfedea0SLionel Sambuc  *
53ebfedea0SLionel Sambuc  * @return returns 0 on success, an wind error code otherwise
54ebfedea0SLionel Sambuc  * @ingroup wind
55ebfedea0SLionel Sambuc  */
56ebfedea0SLionel Sambuc 
57ebfedea0SLionel Sambuc int
wind_stringprep(const uint32_t * in,size_t in_len,uint32_t * out,size_t * out_len,wind_profile_flags flags)58ebfedea0SLionel Sambuc wind_stringprep(const uint32_t *in, size_t in_len,
59ebfedea0SLionel Sambuc 		uint32_t *out, size_t *out_len,
60ebfedea0SLionel Sambuc 		wind_profile_flags flags)
61ebfedea0SLionel Sambuc {
62ebfedea0SLionel Sambuc     size_t tmp_len = in_len * 3;
63ebfedea0SLionel Sambuc     uint32_t *tmp;
64ebfedea0SLionel Sambuc     int ret;
65ebfedea0SLionel Sambuc     size_t olen;
66ebfedea0SLionel Sambuc 
67ebfedea0SLionel Sambuc     if (in_len == 0) {
68ebfedea0SLionel Sambuc 	*out_len = 0;
69ebfedea0SLionel Sambuc 	return 0;
70ebfedea0SLionel Sambuc     }
71ebfedea0SLionel Sambuc 
72ebfedea0SLionel Sambuc     tmp = malloc(tmp_len * sizeof(uint32_t));
73ebfedea0SLionel Sambuc     if (tmp == NULL)
74ebfedea0SLionel Sambuc 	return ENOMEM;
75ebfedea0SLionel Sambuc 
76ebfedea0SLionel Sambuc     ret = _wind_stringprep_map(in, in_len, tmp, &tmp_len, flags);
77ebfedea0SLionel Sambuc     if (ret) {
78ebfedea0SLionel Sambuc 	free(tmp);
79ebfedea0SLionel Sambuc 	return ret;
80ebfedea0SLionel Sambuc     }
81ebfedea0SLionel Sambuc 
82ebfedea0SLionel Sambuc     olen = *out_len;
83ebfedea0SLionel Sambuc     ret = _wind_stringprep_normalize(tmp, tmp_len, tmp, &olen);
84ebfedea0SLionel Sambuc     if (ret) {
85ebfedea0SLionel Sambuc 	free(tmp);
86ebfedea0SLionel Sambuc 	return ret;
87ebfedea0SLionel Sambuc     }
88ebfedea0SLionel Sambuc     ret = _wind_stringprep_prohibited(tmp, olen, flags);
89ebfedea0SLionel Sambuc     if (ret) {
90ebfedea0SLionel Sambuc 	free(tmp);
91ebfedea0SLionel Sambuc 	return ret;
92ebfedea0SLionel Sambuc     }
93ebfedea0SLionel Sambuc     ret = _wind_stringprep_testbidi(tmp, olen, flags);
94ebfedea0SLionel Sambuc     if (ret) {
95ebfedea0SLionel Sambuc 	free(tmp);
96ebfedea0SLionel Sambuc 	return ret;
97ebfedea0SLionel Sambuc     }
98ebfedea0SLionel Sambuc 
99ebfedea0SLionel Sambuc     /* Insignificant Character Handling for ldap-prep */
100ebfedea0SLionel Sambuc     if (flags & WIND_PROFILE_LDAP_CASE_EXACT_ATTRIBUTE) {
101ebfedea0SLionel Sambuc 	ret = _wind_ldap_case_exact_attribute(tmp, olen, out, out_len);
102ebfedea0SLionel Sambuc #if 0
103ebfedea0SLionel Sambuc     } else if (flags & WIND_PROFILE_LDAP_CASE_EXACT_ASSERTION) {
104ebfedea0SLionel Sambuc     } else if (flags & WIND_PROFILE_LDAP_NUMERIC) {
105ebfedea0SLionel Sambuc     } else if (flags & WIND_PROFILE_LDAP_TELEPHONE) {
106ebfedea0SLionel Sambuc #endif
107ebfedea0SLionel Sambuc     } else {
108ebfedea0SLionel Sambuc 	memcpy(out, tmp, sizeof(out[0]) * olen);
109ebfedea0SLionel Sambuc 	*out_len = olen;
110ebfedea0SLionel Sambuc     }
111ebfedea0SLionel Sambuc     free(tmp);
112ebfedea0SLionel Sambuc 
113ebfedea0SLionel Sambuc     return ret;
114ebfedea0SLionel Sambuc }
115ebfedea0SLionel Sambuc 
116*0a6a1f1dSLionel Sambuc static const struct {
117ebfedea0SLionel Sambuc     const char *name;
118ebfedea0SLionel Sambuc     wind_profile_flags flags;
119ebfedea0SLionel Sambuc } profiles[] = {
120ebfedea0SLionel Sambuc     { "nameprep", WIND_PROFILE_NAME },
121ebfedea0SLionel Sambuc     { "saslprep", WIND_PROFILE_SASL },
122ebfedea0SLionel Sambuc     { "ldapprep", WIND_PROFILE_LDAP }
123ebfedea0SLionel Sambuc };
124ebfedea0SLionel Sambuc 
125ebfedea0SLionel Sambuc /**
126ebfedea0SLionel Sambuc  * Try to find the profile given a name.
127ebfedea0SLionel Sambuc  *
128ebfedea0SLionel Sambuc  * @param name name of the profile.
129ebfedea0SLionel Sambuc  * @param flags the resulting profile.
130ebfedea0SLionel Sambuc  *
131ebfedea0SLionel Sambuc  * @return returns 0 on success, an wind error code otherwise
132ebfedea0SLionel Sambuc  * @ingroup wind
133ebfedea0SLionel Sambuc  */
134ebfedea0SLionel Sambuc 
135ebfedea0SLionel Sambuc int
wind_profile(const char * name,wind_profile_flags * flags)136ebfedea0SLionel Sambuc wind_profile(const char *name, wind_profile_flags *flags)
137ebfedea0SLionel Sambuc {
138ebfedea0SLionel Sambuc     unsigned int i;
139ebfedea0SLionel Sambuc 
140ebfedea0SLionel Sambuc     for (i = 0; i < sizeof(profiles)/sizeof(profiles[0]); i++) {
141ebfedea0SLionel Sambuc 	if (strcasecmp(profiles[i].name, name) == 0) {
142ebfedea0SLionel Sambuc 	    *flags = profiles[i].flags;
143ebfedea0SLionel Sambuc 	    return 0;
144ebfedea0SLionel Sambuc 	}
145ebfedea0SLionel Sambuc     }
146ebfedea0SLionel Sambuc     return WIND_ERR_NO_PROFILE;
147ebfedea0SLionel Sambuc }
148