1 /* printer.h --- Convert SCRAM token structures into strings.
2 * Copyright (C) 2009-2012 Simon Josefsson
4 * This file is part of GNU SASL Library.
6 * GNU SASL Library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public License
8 * as published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
11 * GNU SASL Library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with GNU SASL Library; if not, write to the Free
18 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
39 /* Get token validator. */
43 scram_escape (const char *str)
45 char *out = malloc (strlen (str) * 3 + 1);
75 /* Print SCRAM client-first token into newly allocated output string
76 OUT. Returns 0 on success, -1 on invalid token, and -2 on memory
79 scram_print_client_first (struct scram_client_first *cf, char **out)
81 char *username = NULL;
85 /* Below we assume fields are sensible, so first verify that to
87 if (!scram_valid_client_first (cf))
90 /* Escape username and authzid. */
92 username = scram_escape (cf->username);
98 authzid = scram_escape (cf->authzid);
103 n = asprintf (out, "%c%s%s,%s%s,n=%s,r=%s",
105 cf->cbflag == 'p' ? "=" : "",
106 cf->cbflag == 'p' ? cf->cbname : "",
108 authzid ? authzid : "", username, cf->client_nonce);
113 if (n <= 0 || *out == NULL)
119 /* Print SCRAM server-first token into newly allocated output string
120 OUT. Returns 0 on success, -1 on invalid token, and -2 on memory
121 allocation errors. */
123 scram_print_server_first (struct scram_server_first *sf, char **out)
127 /* Below we assume fields are sensible, so first verify that to
129 if (!scram_valid_server_first (sf))
132 n = asprintf (out, "r=%s,s=%s,i=%lu",
133 sf->nonce, sf->salt, (unsigned long) sf->iter);
134 if (n <= 0 || *out == NULL)
140 /* Print SCRAM client-final token into newly allocated output string
141 OUT. Returns 0 on success, -1 on invalid token, and -2 on memory
142 allocation errors. */
144 scram_print_client_final (struct scram_client_final *cl, char **out)
148 /* Below we assume fields are sensible, so first verify that to
150 if (!scram_valid_client_final (cl))
153 n = asprintf (out, "c=%s,r=%s,p=%s", cl->cbind, cl->nonce, cl->proof);
154 if (n <= 0 || *out == NULL)
160 /* Print SCRAM server-final token into newly allocated output string
161 OUT. Returns 0 on success, -1 on invalid token, and -2 on memory
162 allocation errors. */
164 scram_print_server_final (struct scram_server_final *sl, char **out)
168 /* Below we assume fields are sensible, so first verify that to
170 if (!scram_valid_server_final (sl))
173 n = asprintf (out, "v=%s", sl->verifier);
174 if (n <= 0 || *out == NULL)