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