This manual was last updated 11 April 2008 for version 0.2.26 of GNU SASL.
Copyright © 2002, 2003, 2004, 2005, 2006, 2007, 2008 Simon Josefsson.
Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled “GNU Free Documentation License”.
Appendices
Indices
GNU SASL is an implementation of the Simple Authentication and Security Layer framework and a few common SASL mechanisms. SASL is used by network servers (e.g., IMAP, SMTP) to request authentication from clients, and in clients to authenticate against servers.
GNU SASL consists of a library (`libgsasl'), a command line utility (`gsasl') to access the library from the shell, and a manual. The library includes support for the framework (with authentication functions and application data privacy and integrity functions) and at least partial support for the CRAM-MD5, EXTERNAL, GSSAPI, ANONYMOUS, PLAIN, SECURID, DIGEST-MD5, LOGIN, and NTLM mechanisms.
The library is easily ported because it does not do network communication by itself, but rather leaves it up to the calling application. The library is flexible with regards to the authorization infrastructure used, as it utilize a callback into the application to decide whether a user is authorized or not.
GNU SASL is developed for the GNU/Linux system, but runs on over 20 platforms including most major Unix platforms and Windows, and many kind of devices including iPAQ handhelds and S/390 mainframes.
GNU SASL is written in pure ANSI C89 to be portable to embedded and otherwise limited platforms. The entire library, with full support for ANONYMOUS, EXTERNAL, PLAIN, LOGIN and CRAM-MD5, and the front-end that support client and server mode, and the IMAP and SMTP protocols, fits in under 60kb on an Intel x86 platform, without any modifications to the code. (This figure was accurate as of version 0.0.13.)
The library is licensed under the GNU Lesser General Public License version 2.1. The command-line application (src/), examples (examples/), self-test suite (tests/) are licensed under the GNU General Public License license version 3.0. The documentation (doc/) is licensed under the GNU Free Documentation License version 1.2.
Illustration 1.1: Logical overview showing how applications use authentication mechanisms through an abstract interface.
This manual documents the GNU SASL Library programming interface. All functions and data types provided by the library are explained.
The reader is assumed to possess basic familiarity with SASL and network programming in C or C++.
This manual can be used in several ways. If read from the beginning to the end, it gives a good introduction into the library and how it can be used in an application. Forward references are included where necessary. Later on, the manual can be used as a reference manual to get just the information needed about any particular interface of the library. Experienced programmers might want to start looking at the examples at the end of the manual, and then only read up those parts of the interface which are unclear.
GNU SASL might have a couple of advantages over other libraries doing a similar job.
Note that the library do not implement any policy to decide whether a certain user is “authenticated” or “authorized” or not. Rather, it uses a callback into the application to answer these questions.
This section describes SASL from a protocol point of view.
The Simple Authentication and Security Layer (SASL) is a method for adding authentication support to connection-based protocols. A protocol includes a command for identifying and authenticating a user to a server and for optionally negotiating a security layer for subsequent protocol interactions.
The command has a required argument identifying a SASL mechanism. SASL mechanisms are named by strings, from 1 to 20 characters in length, consisting of upper-case letters, digits, hyphens, and/or underscores.
If a server supports the requested mechanism, it initiates an authentication protocol exchange. This consists of a series of server challenges and client responses that are specific to the requested mechanism. The challenges and responses are defined by the mechanisms as binary tokens of arbitrary length. The protocol's profile then specifies how these binary tokens are then encoded for transfer over the connection.
After receiving the authentication command or any client response, a server may issue a challenge, indicate failure, or indicate completion. The protocol's profile specifies how the server indicates which of the above it is doing.
After receiving a challenge, a client may issue a response or abort the exchange. The protocol's profile specifies how the client indicates which of the above it is doing.
During the authentication protocol exchange, the mechanism performs authentication, transmits an authorization identity (frequently known as a userid) from the client to server, and negotiates the use of a mechanism-specific security layer. If the use of a security layer is agreed upon, then the mechanism must also define or negotiate the maximum cipher-text buffer size that each side is able to receive.
The transmitted authorization identity may be different than the identity in the client's authentication credentials. This permits agents such as proxy servers to authenticate using their own credentials, yet request the access privileges of the identity for which they are proxying. With any mechanism, transmitting an authorization identity of the empty string directs the server to derive an authorization identity from the client's authentication credentials.
If use of a security layer is negotiated, it is applied to all subsequent data sent over the connection. The security layer takes effect immediately following the last response of the authentication exchange for data sent by the client and the completion indication for data sent by the server. Once the security layer is in effect, the protocol stream is processed by the security layer into buffers of cipher-text. Each buffer is transferred over the connection as a stream of octets prepended with a four octet field in network byte order that represents the length of the following buffer. The length of the cipher-text buffer must be no larger than the maximum size that was defined or negotiated by the other side.
The GNU SASL library does not have any required external dependencies, but some optional features are enabled if you have a specific external library.
The command-line interface to GNU SASL requires a POSIX or Windows platform for network connectivity. The command-line tool can make use of GnuTLS (http://josefsson.org/gnutls/) to support the STARTTLS modes of IMAP and SMTP, but GnuTLS is not required.
Note that the library does not need a POSIX platform or network connectivity.
GNU SASL has at some point in time been tested on the following platforms.
alphaev67-unknown-linux-gnu, alphaev6-unknown-linux-gnu,
arm-unknown-linux-gnu, hppa-unknown-linux-gnu,
hppa64-unknown-linux-gnu, i686-pc-linux-gnu,
ia64-unknown-linux-gnu, m68k-unknown-linux-gnu,
mips-unknown-linux-gnu, mipsel-unknown-linux-gnu,
powerpc-unknown-linux-gnu, s390-ibm-linux-gnu,
sparc-unknown-linux-gnu.
armv4l-unknown-linux-gnu.
alphaev67-dec-osf5.1,
alphaev68-dec-osf5.1.
alphaev6-unknown-linux-gnu,
alphaev67-unknown-linux-gnu.
ia64-unknown-linux-gnu.
alphaev6-unknown-linux-gnu,
alphaev67-unknown-linux-gnu, ia64-unknown-linux-gnu.
i686-pc-linux-gnu.
i686-pc-linux-gnu.
i686-pc-linux-gnu.
i686-pc-linux-gnu.
mips-sgi-irix6.5.
rs6000-ibm-aix4.3.2.0.
i686-pc-cygwin.
ia64-hp-hpux11.22,
hppa2.0w-hp-hpux11.11.
sparc-sun-solaris2.8.
sparc-sun-solaris2.9.
alpha-unknown-netbsd1.6,
i386-unknown-netbsdelf1.6.
alpha-unknown-openbsd3.1,
i386-unknown-openbsd3.1.
alpha-unknown-freebsd4.7,
i386-unknown-freebsd4.7.
m68k-uclinux-elf.
If you port GNU SASL to a new platform, please report it to the author so this list can be updated.
A mailing list where users may help each other exists, and you can reach it by sending e-mail to help-gsasl@gnu.org. Archives of the mailing list discussions, and an interface to manage subscriptions, is available through the World Wide Web at http://lists.gnu.org/mailman/listinfo/help-gsasl.
Commercial support is available for users of GNU SASL. The kind of support that can be purchased may include:
If you are interested, please write to:
Simon Josefsson Datakonsult Hagagatan 24 113 47 Stockholm Sweden E-mail: simon@josefsson.org
If your company provide support related to GNU SASL and would like to be mentioned here, contact the author (see Bug Reports).
The package can be downloaded from several places, including:
http://josefsson.org/gsasl/releases/
The latest version is stored in a file, e.g., ‘gsasl-0.2.26.tar.gz’ where the ‘0.2.26’ value is the highest version number in the directory.
The package is then extracted, configured and built like many other packages that use Autoconf. For detailed information on configuring and building it, refer to the INSTALL file that is part of the distribution archive.
Here is an example terminal session that download, configure, build and install the package. You will need a few basic tools, such as ‘sh’, ‘make’ and ‘cc’.
$ wget -q http://josefsson.org/gsasl/releases/gsasl-0.2.26.tar.gz
$ tar xfz gsasl-0.2.26.tar.gz
$ cd gsasl-0.2.26/
$ ./configure
...
$ make
...
$ make install
...
After that gsasl should be properly installed and ready for use.
A few configure options may be relevant, summarized in the
table.
--disable-client--disable-server--disable-server, the function will return an error code.
--disable-obsolete--disable-anonymous--disable-external--disable-plain--disable-login--disable-securid--disable-ntlm--disable-cram-md5--disable-digest-md5--disable-gssapi--enable-kerberos_v5--without-stringprepFor the complete list, refer to the output from configure
--help.
There are two ways to build GNU SASL on Windows: via MinGW or via Visual Studio C++.
With MinGW, you can build a GNU SASL DLL and use it from other applications. After installing MinGW (http://mingw.org/) follow the generic installation instructions (see Downloading and Installing). The DLL is installed by default.
For information on how to use the DLL in other applications, see: http://www.mingw.org/mingwfaq.shtml#faq-msvcdll.
You can build GNU SASL as a native Visual Studio C++ project. This allows you to build the code for other platforms that VS supports, such as Windows Mobile. You need Visual Studio 2005 or later.
First download and unpack the archive as described in the generic
installation instructions (see Downloading and Installing). Don't
run ./configure. Instead, start Visual Studio and open the
project file lib/win32/libgsasl.sln inside the GNU SASL
directory. You should be able to build the project using VS.
Output libraries will be written into the lib/win32/lib (or
lib/win32/lib/debug for Debug versions) folder.
Warning! Unless you build GNU SASL linked with libgcrypt, GNU SASL
uses the Windows function CryptGenRandom for generating
cryptographic random data. The function is known to have some
security weaknesses. See http://eprint.iacr.org/2007/419 for
more information.
If you think you have found a bug in GNU SASL, please investigate it and report it.
Please make an effort to produce a self-contained report, with something definite that can be tested or debugged. Vague queries or piecemeal messages are difficult to act on and don't help the development effort.
If your bug report is good, we will do our best to help you to get a corrected version of the software; if the bug report is poor, we won't do anything about it (apart from asking you to send better bug reports).
If you think something in this manual is unclear, or downright incorrect, or if the language needs to be improved, please also send a note.
Send your bug report to:
If you want to submit a patch for inclusion – from solve a typo you discovered, up to adding support for a new feature – you should submit it as a bug report (see Bug Reports). There are some things that you can do to increase the chances for it to be included in the official package.
Unless your patch is very small (say, under 10 lines) we require that you assign the copyright of your work to the Free Software Foundation. This is to protect the freedom of the project. If you have not already signed papers, we will send you the necessary information when you submit your contribution.
For contributions that doesn't consist of actual programming code, the only guidelines are common sense. Use it.
For code contributions, a number of style guides will help you:
If you normally code using another coding standard, there is no problem, but you should use ‘indent’ to reformat the code (see GNU Indent) before submitting your work.
To use GNU SASL, you have to perform some changes to your sources and the build system. The necessary changes are small and explained in the following sections. At the end of this chapter, it is described how the library is initialized, and how the requirements of the library are verified.
A faster way to find out how to adapt your application for use with GNU SASL may be to look at the examples at the end of this manual (see Examples).
All interfaces (data types and functions) of the library are defined in the header file `gsasl.h'. You must include this in all programs using the library, either directly or through some other header file, like this:
#include <gsasl.h>
The name space is gsasl_* for function names, Gsasl* for
data types and GSASL_* for other symbols. In addition the same
name prefixes with one prepended underscore are reserved for internal
use and should never be used by an application.
The library must be initialized before it can be used. The library is
initialized by calling gsasl_init (see Global Functions).
The resources allocated by the initialization process can be released
if the application no longer has a need to call `Libgsasl' functions,
this is done by calling gsasl_done. For example:
int
main (int argc, char *argv[])
{
Gsasl *ctx = NULL;
int rc;
...
rc = gsasl_init (&ctx);
if (rc != GSASL_OK)
{
printf ("SASL initialization failure (%d): %s\n",
rc, gsasl_strerror (rc));
return 1;
}
...
In order to make error messages from gsasl_strerror be
translated (see Top) the application must
set the current locale using setlocale before calling
gsasl_init. For example:
int
main (int argc, char *argv[])
{
Gsasl *ctx = NULL;
int rc;
...
setlocale (LC_ALL, "");
...
rc = gsasl_init (&ctx);
if (rc != GSASL_OK)
{
printf (gettext ("SASL initialization failure (%d): %s\n"),
rc, gsasl_strerror (rc));
return 1;
}
...
In order to take advantage of the secure memory features in Libgcrypt1, you need to initialize secure memory in your application, and for some platforms even make your application setuid root. See the Libgcrypt documentation for more information. Example code to initialize secure memory in your code:
#include <gcrypt.h>
...
int
main (int argc, char *argv[])
{
Gsasl *ctx = NULL;
int rc;
...
/* Check version of libgcrypt. */
if (!gcry_check_version (GCRYPT_VERSION))
die ("version mismatch\n");
/* Allocate a pool of 16k secure memory. This also drops priviliges
on some systems. */
gcry_control (GCRYCTL_INIT_SECMEM, 16384, 0);
/* Tell Libgcrypt that initialization has completed. */
gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
...
rc = gsasl_init (&ctx);
if (rc != GSASL_OK)
{
printf ("SASL initialization failure (%d): %s\n",
rc, gsasl_strerror (rc));
return 1;
}
...
If you do not do this, keying material will not be allocated in secure memory (which for most application is not the biggest secure problem anyway). Note that the GNU SASL Library has not been audited to make sure it only ever stores passwords or keys in secure memory.
It is often desirable to check that the version of the library used is indeed one which fits all requirements. Even with binary compatibility new features may have been introduced but due to problem with the dynamic linker an old version is actually used. So you may want to check that the version is okay right after program startup.
req_version: version string to compare with, or NULL.
Check library version.
See
GSASL_VERSIONfor a suitablereq_versionstring.Return value: Check that the the version of the library is at minimum the one given as a string in
req_versionand return the actual version string of the library; returnNULLif the condition is not met. IfNULLis passed to this function no check is done and only the version string is returned.
The normal way to use the function is to put something similar to the
following early in your main:
if (!gsasl_check_version (GSASL_VERSION))
{
printf ("gsasl_check_version failed:\n"
"Header file incompatible with shared library.\n");
exit(1);
}
If you want to compile a source file including the `gsasl.h' header file, you must make sure that the compiler can find it in the directory hierarchy. This is accomplished by adding the path to the directory in which the header file is located to the compilers include file search path (via the -I option).
However, the path to the include file is determined at the time the source is configured. To solve this problem, the library uses the external package pkg-config that knows the path to the include file and other configuration options. The options that need to be added to the compiler invocation at compile time are output by the --cflags option to pkg-config libgsasl. The following example shows how it can be used at the command line:
gcc -c foo.c `pkg-config libgsasl --cflags`
Adding the output of ‘pkg-config libgsasl --cflags’ to the compilers command line will ensure that the compiler can find the `gsasl.h' header file.
A similar problem occurs when linking the program with the library. Again, the compiler has to find the library files. For this to work, the path to the library files has to be added to the library search path (via the -L option). For this, the option --libs to pkg-config libgsasl can be used. For convenience, this option also outputs all other options that are required to link the program with the `libgsasl' libarary (for instance, the ‘-lidn’ option). The example shows how to link foo.o with the `libgsasl' library to a program foo.
gcc -o foo foo.o `pkg-config libgsasl --libs`
Of course you can also combine both examples to a single command by specifying both options to pkg-config:
gcc -o foo foo.c `pkg-config libgsasl --cflags --libs`
If you work on a project that uses Autoconf (see GNU Autoconf) to help find installed libraries, the suggestions in the previous section are not the entire story. There are a few methods to detect and incorporate the GNU SASL Library into your Autoconf based package. The preferred approach, is to use Libtool in your project, and use the normal Autoconf header file and library tests.
If your audience is a typical GNU/Linux desktop, you can often assume they have the ‘pkg-config’ tool installed, in which you can use its Autoconf M4 macro to find and set up your package for use with Libgsasl. The following illustrate this scenario.
AC_ARG_ENABLE(gsasl,
AC_HELP_STRING([--disable-gsasl], [don't use GNU SASL]),
gsasl=$enableval)
if test "$gsal" != "no" ; then
PKG_CHECK_MODULES(GSASL, libgsasl >= 0.2.26,
[gsasl=yes],
[gsasl=no])
if test "$gsasl" != "yes" ; then
sal=no
AC_MSG_WARN([Cannot find GNU SASL, disabling])
else
gsasl=yes
AC_DEFINE(USE_GSASL, 1, [Define to 1 if you want GNU SASL.])
fi
fi
AC_MSG_CHECKING([if GNU SASL should be used])
AC_MSG_RESULT($gsasl)
If your package uses Libtool(see GNU Libtool), you can use the normal Autoconf tests to find Libgsasl and rely on the Libtool dependency tracking to include the proper dependency libraries (e.g., Libidn). The following illustrate this scenario.
AC_CHECK_HEADER(gsasl.h,
AC_CHECK_LIB(gsasl, gsasl_check_version,
[gsasl=yes AC_SUBST(GSASL_LIBS, -lgsasl)],
gsasl=no),
gsasl=no)
AC_ARG_ENABLE(gsasl,
AC_HELP_STRING([--disable-gsasl], [don't use GNU SASL]),
gsasl=$enableval)
if test "$gsasl" != "no" ; then
AC_DEFINE(USE_SASL, 1, [Define to 1 if you want GNU SASL.])
else
AC_MSG_WARN([Cannot find GNU SASL, diabling])
fi
AC_MSG_CHECKING([if GNU SASL should be used])
AC_MSG_RESULT($gsasl)
Your application's use of the library can be roughly modeled into the following steps: initialize the library, optionally specify the callback, perform the authentication, and finally clean up. The following image illustrate this.

The third step may look the most complex, but for a simple client it will actually not involve any code. If your application need to handle several concurrent clients, or if it is a server that need to serve many clients simultaneous, things do get a bit more complicated.
For illustration, we will write a simple client. Writing a server
would be similar, the only difference is that, later on, instead of
supplying username or passwords, you need to decide whether someone
should be allowed to log in or not. The code for what we have
discussed so far make up our main function in our client
(see Example 1):
int main (int argc, char *argv[])
{
Gsasl *ctx = NULL;
int rc;
if ((rc = gsasl_init (&ctx)) != GSASL_OK)
{
printf ("Cannot initialize libgsasl (%d): %s",
rc, gsasl_strerror (rc));
return 1;
}
client (ctx);
gsasl_done (ctx);
return 0;
}
Here, the call to the function client correspond to the third
step in the image above.
For a more complicated application, that have several clients running
simultaneous, instead of simply calling client, it may have
created new threads for each session, and call client within
each thread. The library is thread safe.
An actual authentication session is more complicated than what we have seen so far. The steps that make up it are: decide which mechanism to use, start the session, optionally specify the callback, optionally set any properties, perform the authentication loop, and clean up. Naturally, your application will start to talk its own protocol (e.g., SMTP or IMAP) after these steps have concluded.
The authentication loop is based on sending tokens (typically short messages encoded in base 64) back and forth between the client and server. It continue until authentication succeeds or there is an error. The format of the data to transfer, the number of iterations in the loop, and other details are specified by each mechanism. The goal of the library is to isolate your application from the details of all different mechanisms.
Note that the library do not send data to the server itself, but return it in an buffer. You must send it to the server yourself, according to an application protocol profile. For example, the SASL application protocol profile for SMTP is described in RFC 2554.
The following image illustrate the steps we have been talking about.

We will now show the implementation of the client function used
before.
void client (Gsasl *ctx)
{
Gsasl_session *session;
const char *mech = "PLAIN";
int rc;
/* Create new authentication session. */
if ((rc = gsasl_client_start (ctx, mech, &session)) != GSASL_OK)
{
printf ("Cannot initialize client (%d): %s\n",
rc, gsasl_strerror (rc));
return;
}
/* Set username and password in session handle. This info will be
lost when this session is deallocated below. */
gsasl_property_set (session, GSASL_AUTHID, "jas");
gsasl_property_set (session, GSASL_PASSWORD, "secret");
/* Do it. */
client_authenticate (ctx, session);
/* Cleanup. */
gsasl_finish (session);
}
This function is responsible for deciding which mechanism to use. In
this case, the ‘PLAIN’ mechanism is hard coded, but you will see
later how this can be made more flexible. The function create a new
session, store the username and password in the session handle, then
call another function client_authenticate to handle the
authentication loop, and end by cleaning up. Let's continue with the
implementation of client_authenticate.
void client_authenticate (Gsasl * ctx, Gsasl_session * session)
{
char buf[BUFSIZ] = "";
char *p;
int rc;
/* This loop mimic a protocol where the server get to send data
first. */
do
{
printf ("Input base64 encoded data from server:\n");
fgets (buf, sizeof (buf) - 1, stdin);
if (buf[strlen (buf) - 1] == '\n')
buf[strlen (buf) - 1] = '\0';
rc = gsasl_step64 (session, buf, &p);
if (rc == GSASL_NEEDS_MORE || rc == GSASL_OK)
{
printf ("Output:\n%s\n", p);
free (p);
}
}
while (rc == GSASL_NEEDS_MORE);
printf ("\n");
if (rc != GSASL_OK)
{
printf ("Authentication error (%d): %s\n",
rc, gsasl_strerror (rc));
return;
}
/* The client is done. Here you would typically check if the
server let the client in. If not, you could try again. */
printf ("If server accepted us, we're done.\n");
}
This last function need to be discussed in some detail. First, you should be aware that there are two versions of this function, that differ in a subtle way. The version above (see Example 2) is used for application profiles where the server send data first. For some mechanisms, this may waste a roundtrip, because the server need input from the client to proceed. Therefor, today the recommended approach is to permit client to send data first (see Example 1). Which version you should use depend on which application protocol you are implementing.
Further, you should realize that it is bad programming style to use a
fixed size buffer. On GNU systems, you may use the getline
functions instead of fgets. However, in practice, there are
few mechanisms that use very large tokens. In typical configurations,
the mechanism with the largest tokens (GSSAPI) can use at least 500
bytes. A fixed buffer size of 8192 bytes may thus be sufficient for
now. But don't say I didn't warn you, when a future mechanism doesn't
work in your application, because of a fixed size buffer.
The gsasl_step64 (and of course also gasl_step) return
two non-error return codes. GSASL_OK is used for success,
indicating that the library consider the authentication finished.
That may include a successful server authentication, depending on the
mechanism. You must not let the client continue to the application
protocol part unless you receive GSASL_OK from these functions.
In particular, don't be fooled into believing authentication were
successful if the server reply “OK” but these function has failed
with an error. The server may have been hacked, and could be tricking
you into sending confidential data, without having successfully
authenticated the server.
The non-error return code GSASL_NEEDS_MORE is used to signal to
your application that you should send the output token to the peer,
and wait for a new token, and do another iteration. If the server
conclude the authentication process, with no data, you should call
gsasl_step64 (or gsasl_step) specifying a zero-length
token.
If the functions (gsasl_step and gsasl_step64) return
any non-error code, the content of the output buffer is undefined.
Otherwise, it is the callers responsibility to deallocate the buffer,
by calling free. Note that in some situations, where the
buffer is empty, NULL is returned as the buffer value. You
should treat this as an empty buffer.
Our earlier code was hard coded to use a specific mechanism. This is
rarely a good idea. Instead, it is recommended to select the best
mechanism available from the list of mechanism supported by the
server. Note that without TLS or similar, the list may have been
maliciously altered, by an attacker. This means that you should abort
if you cannot find any mechanism that exceeds your minimum security
level. There is a function gsasl_client_suggest_mechanism
(see Global Functions) that will try to pick the “best”
available mechanism from a list of mechanisms. Our simple interactive
example client (see Example 3) include the following function to
decide which mechanism to use. Note that the code doesn't blindly use
what is returned from gsasl_client_suggest_mechanism, but
rather let some logic (in this case the user, through an interactive
query) decide which mechanism is acceptable.
const char *client_mechanism (Gsasl *ctx)
{
static char mech[GSASL_MAX_MECHANISM_SIZE + 1] = "";
char mechlist[BUFSIZ] = "";
const char *suggestion;
printf ("Enter list of mechanism that server support, separate by SPC:\n");
fgets (mechlist, sizeof (mechlist) - 1, stdin);
suggestion = gsasl_client_suggest_mechanism (ctx, mechlist);
if (suggestion)
printf ("Library suggest use of `%s'.\n", suggestion);
printf ("Enter mechanism to use:\n");
fgets (mech, sizeof (mech) - 1, stdin);
mech[strlen (mech) - 1] = '\0';
return mech;
}
When running this example code, it might look like in the following output.
Enter list of mechanism that server support, separate by SPC:
CRAM-MD5 DIGEST-MD5 GSSAPI FOO BAR
Library suggest use of `GSSAPI'.
Enter mechanism to use:
CRAM-MD5
Input base64 encoded data from server:
Zm5vcmQ=
Output:
amFzIDkyY2U1NWE5MTM2ZTY4NzEyMTUyZTFjYmFmNjVkZjgx
If server accepted us, we're done.
Our earlier code specified the username and password before the authentication loop, as in:
gsasl_property_set (ctx, GSASL_AUTHID, "jas");
gsasl_property_set (ctx, GSASL_PASSWORD, "secret");
This may work for simple mechanisms, that only ever need an username and a password. But some mechanism require more information, such as an authorization identity, a special PIN or passcode, a realm, a hostname, a service name, or an anonymous identifier. Querying the user for all that information, without knowing exactly which of it is really needed will result in a poor user interface. The user should not have to input private information, if it isn't required.
The approach is a bad idea for another reason. What if the server abort the authentication process? Then your application have already queried the user for a username and password. It would be better if you only asked the user for this information, annoying to input, when it is known to be needed.
A better approach to this problem is to use a callback. Then the mechanism may query your application whenever it need some information, like the username and password. It will only do this at the precise step in the authentication when the information is actually needed. Further, if the user abort, e.g., a password prompt, the mechanism is directly informed of this (because it invoked the callback), and could recover somehow.
Our final example (see Example 4) specify a callback function,
inside main as below.
/* Set the callback handler for the library. */
gsasl_callback_set (ctx, callback);
The function itself is implemented as follows.
int callback (Gsasl * ctx, Gsasl_session * sctx, Gsasl_property prop)
{
char buf[BUFSIZ] = "";
int rc = GSASL_NO_CALLBACK;
/* Get user info from user. */
printf ("Callback invoked, for property %d.\n", prop);
switch (prop)
{
case GSASL_PASSCODE:
printf ("Enter passcode:\n");
fgets (buf, sizeof (buf) - 1, stdin);
buf[strlen (buf) - 1] = '\0';
gsasl_property_set (sctx, GSASL_PASSCODE, buf);
rc = GSASL_OK;
break;
case GSASL_AUTHID:
printf ("Enter username:\n");
fgets (buf, sizeof (buf) - 1, stdin);
buf[strlen (buf) - 1] = '\0';
gsasl_property_set (sctx, GSASL_AUTHID, buf);
rc = GSASL_OK;
break;
default:
printf ("Unknown property! Don't worry.\n");
break;
}
return rc;
}
Again, it is bad style to use a fixed size buffer. Mmm'kay.
Which properties you should handle is up to you. If you don't know
how to respond to a certain property, simply return
GSASL_NO_CALLBACK. The basic properties to support are
authentication identity (GSASL_AUTHID), authorization identity
(GSASL_AUTHZID), and password (GSASL_PASSWORD). See
See Properties, for the list of all properties, and what your
callback should (ideally) do for them, and which properties each
mechanism require in order to work.
Properties with associated data:
GSASL_AUTHID
The authentication identity.
GSASL_AUTHZID
The authorization identity.
GSASL_PASSWORD
The password of the authentication identity.
GSASL_ANONYMOUS_TOKEN
The anonymous token. This is typically the email address of the user.
GSASL_SERVICE
The registered GSSAPI service name of the application service, e.g. “imap”. While the names are registered for GSSAPI, other mechanisms such as DIGEST-MD5 may also use this.
GSASL_HOSTNAME
Should be the local host name of the machine.
GSASL_GSSAPI_DISPLAY_NAME
Contain the GSSAPI “display name”, set by the server GSSAPI
mechanism. Typically you retrieve this property in your callback,
when invoked for GSASL_VALIDATE_GSSAPI.
GSASL_REALM
The name of the authentication domain. This is used by several mechanisms, including DIGEST-MD5, GSS-API, KERBEROS_V5 and NTLM.
GSASL_PASSCODE
The SecurID passcode.
GSASL_PIN
The SecurID personal identification number (PIN).
GSASL_SUGGESTED_PIN
A SecurID personal identification number (PIN) suggested by the server.
Abstract properties, used to trigger the callback, typically used in servers to validate client credentials:
GSASL_VALIDATE_SIMPLE
You may retrieve GSASL_AUTHID, GSASL_AUTHZID and GSASL_PASSWORD and use them to make an authentication and authorization decision.
GSASL_VALIDATE_EXTERNAL
Used by EXTERNAL mechanism on the server side to validate the client. The GSASL_AUTHID will contain the authorization identity of the client.
GSASL_VALIDATE_ANONYMOUS
Used by ANONYMOUS mechanism on the server side to validate the client. The GSASL_ANONYMOUS_TOKEN will contain token that identity the client.
GSASL_VALIDATE_GSSAPI
Used by the GSSAPI mechanism on the server side, to validate the client. You may retrieve the authorization identity from GSASL_AUTHZID and the GSS-API display name from GSASL_GSSAPI_DISPLAY_NAME.
GSASL_VALIDATE_SECURID
Used by SECURID mechanism on the server side to validate client. The GSASL_AUTHID, GSASL_AUTHZID, GSASL_PASSCODE, and GSASL_PIN will be set. It can return GSASL_SECURID_SERVER_NEED_ADDITIONAL_PASSCODE to ask the client to supply another passcode, and GSASL_SECURID_SERVER_NEED_NEW_PIN to require the client to supply a new PIN code.
Different SASL mechanisms have different requirements on the application using it. To handle these differences the library can use a callback function into your application in several different ways. Some mechanisms, such as ‘PLAIN’, are simple to explain and use. The client callback query the user for a username and password. The server callback hand the username and password into any local policy deciding authentication system (such as /etc/passwd via PAM).
Mechanism such as ‘CRAM-MD5’ and ‘DIGEST-MD5’ uses hashed passwords. The client callback behaviour is the same as for PLAIN. However, the server do not receive the plain text password over the network but rather a hash of it. Existing policy deciding systems like PAM cannot handle this, so the server callback for these mechanisms are more complicated.
Further, mechanisms like GSSAPI (Kerberos 5) assume a specific authentication system. In theory this means that the SASL library would not need to interact with the application, but rather call this specific authentication system directly. However, some callbacks are supported anyway, to modify the behaviour of how the specific authentication system is used (i.e., to handle “super-user” login as some other user).
Some mechanisms, like ‘EXTERNAL’ and ‘ANONYMOUS’ are entirely dependent on callbacks.
The EXTERNAL mechanism is used to authenticate a user to a server based on out-of-band authentication. EXTERNAL is typically used over TLS authenticated channels. Note that in the server, you need to make sure that TLS actually authenticated the client successfully. It is normally not sufficient that TLS is used, since they also support anonymous modes.
In the client, this mechanism is always enabled, and will send the
GSASL_AUTHZID property as the authorization name to the server,
if the property is set. If the property is not set, the empty
authorization name is sent. You need not implement a callback.
In the server, this mechanism will invoke the
GSASL_VALIDATE_EXTERNAL callback to decide whether the client
is authenticated and authorized to log in. Your callback can retrieve
the GSASL_AUTHZID property to inspect the requested
authorization name from the client.
The ANONYMOUS mechanism is used to “authenticate” clients to anonymous services; or rather, just indicate that the client wishes to use the service anonymously. The client sends a token, usually her email address, which serve the purpose of some trace information suitable for log files. The token is not permitted to be empty.
In the client, this mechanism is always enabled, and will send the
GSASL_ANONYMOUS_TOKEN property as the trace information to the
server.
In the server, this mechanism will invoke the
GSASL_VALIDATE_ANONYMOUS callback to decide whether the client
should be permitted to log in. Your callback can retrieve the
GSASL_ANONYMOUS_TOKEN property to, for example, save it in a
log file. The token is normally not used to decide whether the client
should be permitted to log in or not.
The PLAIN mechanism uses username and password to authenticate users. Two user names are relevant. The first, the authentication identity, indicate the credential holder, i.e., whom the provided password belongs to. The second, the authorization identity, is typically empty, to indicate that the user requests to log on to the server as herself. However, if the authorization identity is not empty, the server should decide whether the authenticated user may log on as the authorization identity. Normally, only “super-user” accounts such as ‘admin’ or similar should be allowed this.
In the client, this mechanism is always enabled, and require the
GSASL_AUTHID and GSASL_PASSWORD properties. If set,
GSASL_AUTHZID will also be used.
In the server, the mechanism is always enabled. Two approaches to authenticate and authorize the client is provided.
In the first approach, the server side of the mechanism will invoke
the GSASL_VALIDATE_SIMPLE callback property to decide whether
the client should be accepted or not. The callback may inspect the
GSASL_AUTHID, GSASL_AUTHID, and GSASL_PASSWORD
properties. These properties values will be normalized.
If the first approach fails (because, e.g., your callback return
‘GSASL_NO_CALLBACK’ to signal that it does not implement
GSASL_VALIDATE_SIMPLE) the mechanism will continue to query the
application for a password, via the GSASL_PASSWORD property.
Your callback may use the GSASL_AUTHID and GSASL_AUTHZID
properties to select the proper password. The password is then
normalized and compared to the client credential.
Which approach to use? If your database store hashed passwords, you have no option, but must use the first approach. If passwords in your user database are stored in prepared (SASLprep) form, the first approach will be faster. If you do not have prepared passwords available, you can use the second approach to make sure the password is prepared properly before comparison.
The LOGIN mechanism is a non-standard mechanism, and is similar to the PLAIN mechanism except that LOGIN lack the support for authorization identities. Always use PLAIN instead of LOGIN in new applications.
The callback behaviour is the same as for PLAIN, except that
GSASL_AUTHZID is not used nor required, and that the server do
not normalize the password using SASLprep.
See Use of SASLprep in LOGIN, for a proposed clarification of the interpretation of a hypothetical LOGIN specification.
The CRAM-MD5 is a widely used, but officially deprecated (apparently in favor of DIGEST-MD5), challenge-response mechanism that transfer hashed passwords instead of clear text passwords. For insecure channels (e.g., when TLS is not used), it is safer than PLAIN. The CRAM-MD5 mechanism do not support authorization identities; making the relationship between CRAM-MD5 and DIGEST-MD5 similar to the relationship between LOGIN and PLAIN.
The disadvantage with hashed passwords is that the server cannot use normal authentication infrastructures such as PAM, because the server must have access to the correct password in order to validate an authentication attempt.
In the client, this mechanism is always enabled, and require the
GSASL_AUTHID and GSASL_PASSWORD properties.
In the server, the mechanism will invoke the GSASL_PASSWORD
callback, which may use the GSASL_AUTHID property to determine
which users' password should be used. The GSASL_AUTHID will be
in normalized form. The server will then normalize the returned
password, and compare the client response with the computed correct
response, and accept the user accordingly.
See Use of SASLprep in CRAM-MD5, for a clarification on the interpretation of the CRAM-MD5 specification that this implementation rely on.
The DIGEST-MD5 mechanism is based on repeated hashing using MD5, which after the MD5 break may be argued to be weaker than HMAC-MD5, but supports more features. For example, authorization identities and data integrity and privacy protection are supported. Like CRAM-MD5, only a hashed password is transfered. Consequently, DIGEST-MD5 need access to the correct password (although it may be hashed, another improvement compared to CRAM-MD5) to verify the client response. Alas, this make it impossible to use, e.g., PAM on the server side.
In the client, this mechanism is always enabled, and require the
GSASL_AUTHID, GSASL_PASSWORD, GSASL_SERVICE, and
GSASL_HOSTNAME properties. If set, GSASL_AUTHZID and
GSASL_REALM will also be used.
In the server, the mechanism will invoke the GSASL_PASSWORD
callback, which may use the GSASL_AUTHID, GSASL_AUTHZID
and GSASL_REALM properties to determine which users' password
should be used. The server will then compare the client response with
a computed correct response, and accept the user accordingly.
Currently only the authentication quality of service is implemented. In other words, payload integrity or privacy protection are not supported. Consequently, there are no properties for the maximum buffer size, quality of protection, and cipher fields.
The NTLM is a non-standard mechanism. Do not use it in new applications, and do not expect it to be secure. Currently only the client side is supported.
In the client, this mechanism is always enabled, and require the
GSASL_AUTHID and GSASL_PASSWORD properties. It will set
the ‘domain’ field in the NTLM request to the value of
GSASL_REALM. Some servers reportedly need non-empty but
arbitrary values in that field.
The SECURID mechanism uses authentication and authorization identity together with a passcode from a hardware token to authenticate users.
In the client, this mechanism is always enabled, and require the
GSASL_AUTHID and GSASL_PASSCODE properties. If set,
GSASL_AUTHZID will also be used. If the server requests it,
the GSASL_PIN property is also required, and its callback may
inspect the GSASL_SUGGESTED_PIN property to discover a
server-provided PIN to use.
In the server, this mechanism will invoke the
GSASL_VALIDATE_SECURID callback. The callback may inspect the
GSASL_AUTHID, GSASL_AUTHZID, and GSASL_PASSCODE
properties. The callback can return
GSASL_SECURID_SERVER_NEED_ADDITIONAL_PASSCODE to ask for
another additional passcode from the client. The callback can return
GSASL_SECURID_SERVER_NEED_NEW_PIN to ask for a new PIN code
from the client, in which case it may also set the
GSASL_SUGGESTED_PIN property to indicate a recommended new PIN.
If the callbacks has invoked again, after having returned
GSASL_SECURID_SERVER_NEED_NEW_PIN, it may also inspect the
GSASL_PIN property, in addition to the other properties, to
find out the client selected PIN code.
GSS-API is a framework, similar to SASL, for authentication. The GSSAPI mechanism only support the Kerberos 5 GSS-API mechanism, though. (A new SASL mechanism to support non-Kerberos 5 GSS-API mechanisms may be supported in the future.)
In the client, the mechanism is enabled only if the user has acquired
credentials (i.e., a ticket granting ticket), and require the
GSASL_AUTHID, GSASL_SERVICE, and GSASL_HOSTNAME
properties.
In the server, the mechanism require the GSASL_SERVICE, and
GSASL_HOSTNAME properties, and will invoke the
GSASL_VALIDATE_GSSAPI callback in order to validate the user.
The callback may inspect the GSASL_AUTHZID and
GSASL_GSSAPI_DISPLAY_NAME properties to decide whether to
authorize the user. Note that authentication is performed by the
GSS-API library.
XXX: explain more about quality of service, maximum buffer size, etc.
The KERBEROS_V5 is an experimental mechanism, the protocol specification is available on the GNU SASL homepage. It can operate in three modes, non-infrastructure mode, infrastructure mode and proxied infrastructure mode. Currently only non-infrastructure mode is supported.
In the non-infrastructure mode, it works as a superset of most features provided by PLAIN, CRAM-MD5, DIGEST-MD5 and GSSAPI while at the same time building on what is believed to be proven technology (the RFC 1510 network security system). In the non-infrastructure mode, the client must specify (via callbacks) the name of the user, and optionally the server name and realm. The server must be able to retrieve passwords given the name of the user.
In the infrastructure mode (proxied or otherwise), it allows clients and servers to authenticate via SASL in an RFC 1510 environment, using a trusted third party, a “Key Distribution Central”. In the normal mode, clients aquire tickets out of band and then invokes a one roundtrip AP-REQ and AP-REP exchange. In the proxied mode, which can be used by clients without IP addresses or without connectivity to the KDC (e.g., when the KDC is IPv4 and the client is IPV6-only), the client uses the server to proxy ticket requests and finishes with the AP-REQ/AP-REP exchange. In infrastructure mode (proxied or otherwise), the client nor server need to implement any callbacks (this will likely change later, to allow a server to authorize users, similar to the GSSAPI callback).
XXX: update when implementation has matured
ctx: pointer to libgsasl handle.
This functions initializes libgsasl. The handle pointed to by ctx is valid for use with other libgsasl functions iff this function is successful. It also register all builtin SASL mechanisms, using
gsasl_register().Return value: GSASL_OK iff successful, otherwise
GSASL_MALLOC_ERROR.
ctx: libgsasl handle.
This function destroys a libgsasl handle. The handle must not be used with other libgsasl functions after this call.
ctx: libgsasl handle.
out: newly allocated output character array.
Return a newly allocated string containing SASL names, separated by space, of mechanisms supported by the libgsasl client.
outis allocated by this function, and it is the responsibility of caller to deallocate it.Return value: Returns
GSASL_OKif successful, or error code.
ctx: libgsasl handle.
out: newly allocated output character array.
Return a newly allocated string containing SASL names, separated by space, of mechanisms supported by the libgsasl server.
outis allocated by this function, and it is the responsibility of caller to deallocate it.Return value: Returns
GSASL_OKif successful, or error code.
ctx: libgsasl handle.
name: name of SASL mechanism.
Decide whether there is client-side support for a specified mechanism.
Return value: Returns 1 if the libgsasl client supports the named mechanism, otherwise 0.
ctx: libgsasl handle.
name: name of SASL mechanism.
Decide whether there is server-side support for a specified mechanism.
Return value: Returns 1 if the libgsasl server supports the named mechanism, otherwise 0.
ctx: libgsasl handle.
mechlist: input character array with SASL mechanism names, separated by invalid characters (e.g. SPC).
Given a list of mechanisms, suggest which to use.
Return value: Returns name of "best" SASL mechanism supported by the libgsasl client which is present in the input string.
ctx: pointer to libgsasl handle.
mech: plugin structure with information about plugin.
This function initialize given mechanism, and if successful, add it to the list of plugins that is used by the library.
Return value:
GSASL_OKiff successful, otherwiseGSASL_MALLOC_ERROR.Since: 0.2.0
The callback is used by mechanisms to retrieve information, such as
username and password, from the application. In a server, the
callback is used to decide whether a user is permitted to log in or
not. You tell the library of your callback function by calling
gsasl_callback_set.
Since your callback may need to access to data from other parts of
your application, there are hooks to store and retrieve application
specific pointers. This avoid the use of global variables in your
application, which wouldn't be thread safe. You store a pointer to
some information (opaque from the point of view of the library) by
calling gsasl_callback_hook_set and can later retrieve this
data in your callback by calling gsasl_callback_hook_get.
ctx: handle received from
gsasl_init().cb: pointer to function implemented by application.
Store the pointer to the application provided callback in the library handle. The callback will be used, via
gsasl_callback(), by mechanisms to discover various parameters (such as username and passwords). The callback function will be called with a Gsasl_property value indicating the requested behaviour. For example, forGSASL_ANONYMOUS_TOKEN, the function is expected to invoke gsasl_property_set(CTX,GSASL_ANONYMOUS_TOKEN, "token") where "token" is the anonymous token the application wishes the SASL mechanism to use. See the manual for the meaning of all parameters.Since: 0.2.0
ctx: handle received from
gsasl_init(), may beNULLto derive it fromsctx.sctx: session handle.
prop: enumerated value of Gsasl_property type.
Invoke the application callback. The
propvalue indicate what the callback is expected to do. For example, forGSASL_ANONYMOUS_TOKEN, the function is expected to invoke gsasl_property_set(SCTX,GSASL_ANONYMOUS_TOKEN, "token") where "token" is the anonymous token the application wishes the SASL mechanism to use. See the manual for the meaning of all parameters.Note that if no callback has been set by the application, but the obsolete callback interface has been used, this function will translate the old callback interface into the new. This interface should be sufficient to invoke all callbacks, both new and old.
Return value: Returns whatever the application callback return, or
GSASL_NO_CALLBACKif no application was known.Since: 0.2.0
ctx: libgsasl handle.
hook: opaque pointer to application specific data.
Store application specific data in the libgsasl handle.
The application data can be later (for instance, inside a callback) be retrieved by calling
gsasl_callback_hook_get(). This is normally used by the application to maintain a global state between the main program and callbacks.Since: 0.2.0
ctx: libgsasl handle.
Retrieve application specific data from libgsasl handle.
The application data is set using
gsasl_callback_hook_set(). This is normally used by the application to maintain a global state between the main program and callbacks.Return value: Returns the application specific data, or
NULL.Since: 0.2.0
sctx: libgsasl session handle.
hook: opaque pointer to application specific data.
Store application specific data in the libgsasl session handle.
The application data can be later (for instance, inside a callback) be retrieved by calling
gsasl_session_hook_get(). This is normally used by the application to maintain a per-session state between the main program and callbacks.Since: 0.2.14
sctx: libgsasl session handle.
Retrieve application specific data from libgsasl session handle.
The application data is set using
gsasl_callback_hook_set(). This is normally used by the application to maintain a per-session state between the main program and callbacks.Return value: Returns the application specific data, or
NULL.Since: 0.2.14
sctx: session handle.
prop: enumerated value of Gsasl_property type, indicating the type of data in
data.data: zero terminated character string to store.
Make a copy of
dataand store it in the session handle for the indicated propertyprop.You can immediately deallocate
dataafter calling this function, without affecting the data stored in the session handle.Since: 0.2.0
sctx: session handle.
prop: enumerated value of Gsasl_property type, indicating the type of data in
data.data: character string to store.
len: length of character string to store.
Make a copy of
lensizeddataand store a zero terminated version of it in the session handle for the indicated propertyprop.You can immediately deallocate
dataafter calling this function, without affecting the data stored in the session handle.Except for the length indicator, this function is identical to gsasl_property_set.
Since: 0.2.0
sctx: session handle.
prop: enumerated value of Gsasl_property type, indicating the type of data in
data.Retrieve the data stored in the session handle for given property
prop.The pointer is to live data, and must not be deallocated or modified in any way.
This function will not invoke the application callback.
Return value: Return property value, if known, or
NULLif no value known.Since: 0.2.0
sctx: session handle.
prop: enumerated value of Gsasl_property type, indicating the type of data in
data.Retrieve the data stored in the session handle for given property
prop, possibly invoking the application callback to get the value.The pointer is to live data, and must not be deallocated or modified in any way.
This function will invoke the application callback, using
gsasl_callback(), when a property value is not known.If no value is known, and no callback is specified or if the callback fail to return data, and if any obsolete callback functions has been set by the application, this function will try to call these obsolete callbacks, and store the returned data as the corresponding property. This behaviour of this function will be removed when the obsolete callback interfaces are removed.
Return value: Return data for property, or
NULLif no value known.Since: 0.2.0
ctx: libgsasl handle.
mech: name of SASL mechanism.
sctx: pointer to client handle.
This functions initiates a client SASL authentication. This function must be called before any other gsasl_client_*() function is called.
Return value: Returns
GSASL_OKif successful, or error code.
ctx: libgsasl handle.
mech: name of SASL mechanism.
sctx: pointer to server handle.
This functions initiates a server SASL authentication. This function must be called before any other gsasl_server_*() function is called.
Return value: Returns
GSASL_OKif successful, or error code.
sctx: libgsasl session handle.
input: input byte array.
input_len: size of input byte array.
output: newly allocated output byte array.
output_len: pointer to output variable with size of output byte array.
Perform one step of SASL authentication. This reads data from the other end (from
inputandinput_len), processes it (potentially invoking callbacks to the application), and writes data to server (into newly allocated variableoutputandoutput_lenthat indicate the length ofoutput).The contents of the
outputbuffer is unspecified if this functions returns anything other thanGSASL_OKorGSASL_NEEDS_MORE. If this function returnGSASL_OKorGSASL_NEEDS_MORE, however, theoutputbuffer is allocated by this function, and it is the responsibility of caller to deallocate it by calling free (output).Return value: Returns
GSASL_OKif authenticated terminated successfully,GSASL_NEEDS_MOREif more data is needed, or error code.
sctx: libgsasl client handle.
b64input: input base64 encoded byte array.
b64output: newly allocated output base64 encoded byte array.
This is a simple wrapper around
gsasl_step()that base64 decodes the input and base64 encodes the output.The contents of the
b64outputbuffer is unspecified if this functions returns anything other thanGSASL_OKorGSASL_NEEDS_MORE. If this function returnGSASL_OKorGSASL_NEEDS_MORE, however, theb64outputbuffer is allocated by this function, and it is the responsibility of caller to deallocate it by calling free (b64output).Return value: Returns
GSASL_OKif authenticated terminated successfully,GSASL_NEEDS_MOREif more data is needed, or error code.
sctx: libgsasl session handle.
Destroy a libgsasl client or server handle. The handle must not be used with other libgsasl functions after this call.
sctx: libgsasl session handle.
input: input byte array.
input_len: size of input byte array.
output: newly allocated output byte array.
output_len: size of output byte array.
Encode data according to negotiated SASL mechanism. This might mean that data is integrity or privacy protected.
The
outputbuffer is allocated by this function, and it is the responsibility of caller to deallocate it by calling free(output).Return value: Returns
GSASL_OKif encoding was successful, otherwise an error code.
sctx: libgsasl session handle.
input: input byte array.
input_len: size of input byte array.
output: newly allocated output byte array.
output_len: size of output byte array.
Decode data according to negotiated SASL mechanism. This might mean that data is integrity or privacy protected.
The
outputbuffer is allocated by this function, and it is the responsibility of caller to deallocate it by calling free(output).Return value: Returns
GSASL_OKif encoding was successful, otherwise an error code.
in: a UTF-8 encoded string.
flags: any SASLprep flag, e.g.,
GSASL_ALLOW_UNASSIGNED.out: on exit, contains newly allocated output string.
stringpreprc: if non-NULL, will hold precise stringprep return code.
Prepare string using SASLprep. On success, the
outvariable must be deallocated by the caller.Return value: Returns
GSASL_OKon success, orGSASL_SASLPREP_ERRORon error.Since: 0.2.3
in: input byte array
inlen: size of input byte array
out: pointer to newly allocated output byte array
outlen: pointer to size of newly allocated output byte array
Encode data as base64. The string is zero terminated, and
outlenholds the length excluding the terminating zero. Theoutbuffer must be deallocated by the caller.Return value: Returns
GSASL_OKon success, orGSASL_MALLOC_ERRORif input was too large or memory allocation fail.Since: 0.2.2
in: input byte array
inlen: size of input byte array
out: pointer to newly allocated output byte array
outlen: pointer to size of newly allocated output byte array
Decode Base64 data. The
outbuffer must be deallocated by the caller.Return value: Returns
GSASL_OKon success,GSASL_BASE64_ERRORif input was invalid, andGSASL_MALLOC_ERRORon memory allocation errors.Since: 0.2.2
filename: filename of file containing passwords.
username: username string.
key: newly allocated output character array.
Retrieve password for user from specified file. The buffer
keycontain the password if this function is successful. The caller is responsible for deallocating it.The file should be on the UoW "MD5 Based Authentication" format, which means it is in text format with comments denoted by # first on the line, with user entries looking as "usernameTABpassword". This function removes CR and LF at the end of lines before processing. TAB, CR, and LF denote ASCII values 9, 13, and 10, respectively.
Return value: Return
GSASL_OKif output buffer contains the password,GSASL_AUTHENTICATION_ERRORif the user could not be found, or other error code.
data: output array to be filled with unpredictable random data.
datalen: size of output array.
Store unpredictable data of given size in the provided buffer.
Return value: Returns
GSASL_OKiff successful.
data: output array to be filled with strong random data.
datalen: size of output array.
Store cryptographically strong random data of given size in the provided buffer.
Return value: Returns
GSASL_OKiff successful.
in: input character array of data to hash.
inlen: length of input character array of data to hash.
Compute hash of data using MD5. The
outbuffer must be deallocated by the caller.Return value: Returns
GSASL_OKiff successful.
key: input character array with key to use.
keylen: length of input character array with key to use.
in: input character array of data to hash.
inlen: length of input character array of data to hash.
Compute keyed checksum of data using HMAC-MD5. The
outhashbuffer must be deallocated by the caller.Return value: Returns
GSASL_OKiff successful.
ptr: memory pointer
Invoke free(
ptr) to de-allocate memory pointer. Typically used on strings allocated by other libgsasl functions.This is useful on Windows where libgsasl is linked to one CRT and the application is linked to another CRT. Then malloc/free will not use the same heap. This happens if you build libgsasl using mingw32 and the application with Visual Studio.
Since: 0.2.19
Most functions in the GNU SASL Library are returning an error if they fail. For this reason, the application should always catch the error condition and take appropriate measures, for example by releasing the resources and passing the error up to the caller, or by displaying a descriptive message to the user and cancelling the operation.
Some error values do not indicate a system error or an error in the operation, but the result of an operation that failed properly.
Errors are returned as an int. Except for the OK case an
application should always use the constants instead of their numeric
value. Applications are encouraged to use the constants even for OK
as it improves readability. Possible values are:
GSASL_OK0 so you may use it in boolean constructs.
GSASL_NEEDS_MOREGSASL_UNKNOWN_MECHANISMGSASL_MECHANISM_CALLED_TOO_MANY_TIMESGSASL_MALLOC_ERRORGSASL_BASE64_ERRORGSASL_CRYPTO_ERRORGSASL_GSSAPI_RELEASE_BUFFER_ERRORGSASL_GSSAPI_IMPORT_NAME_ERRORGSASL_GSSAPI_INIT_SEC_CONTEXT_ERRORGSASL_GSSAPI_ACCEPT_SEC_CONTEXT_ERRORGSASL_GSSAPI_UNWRAP_ERRORGSASL_GSSAPI_WRAP_ERRORGSASL_GSSAPI_ACQUIRE_CRED_ERRORGSASL_GSSAPI_DISPLAY_NAME_ERRORGSASL_GSSAPI_UNSUPPORTED_PROTECTION_ERRORGSASL_MECHANISM_PARSE_ERRORGSASL_AUTHENTICATION_ERRORGSASL_INTEGRITY_ERRORGSASL_NO_CLIENT_CODEGSASL_NO_SERVER_CODEGSASL_NO_CALLBACKGSASL_NO_ANONYMOUS_TOKENGSASL_NO_AUTHIDGSASL_NO_AUTHZIDGSASL_NO_PASSWORDGSASL_NO_PASSCODEGSASL_NO_PINGSASL_NO_SERVICEGSASL_NO_HOSTNAMEGSASL_SASLPREP_ERRORGSASL_TOO_SMALL_BUFFERGSASL_FOPEN_ERRORGSASL_FCLOSE_ERRORGSASL_CANNOT_GET_CTXGSASL_NEED_CLIENT_ANONYMOUS_CALLBACKGSASL_NEED_CLIENT_PASSWORD_CALLBACKGSASL_NEED_CLIENT_PASSCODE_CALLBACKGSASL_NEED_CLIENT_PIN_CALLBACKGSASL_NEED_CLIENT_AUTHORIZATION_ID_CALLBACKGSASL_NEED_CLIENT_AUTHENTICATION_ID_CALLBACKGSASL_NEED_CLIENT_SERVICE_CALLBACKGSASL_NEED_SERVER_VALIDATE_CALLBACKGSASL_NEED_SERVER_CRAM_MD5_CALLBACKGSASL_NEED_SERVER_DIGEST_MD5_CALLBACKGSASL_NEED_SERVER_ANONYMOUS_CALLBACKGSASL_NEED_SERVER_EXTERNAL_CALLBACKGSASL_NEED_SERVER_REALM_CALLBACKGSASL_NEED_SERVER_SECURID_CALLBACKGSASL_NEED_SERVER_SERVICE_CALLBACKGSASL_NEED_SERVER_GSSAPI_CALLBACKGSASL_NEED_SERVER_RETRIEVE_CALLBACKGSASL_UNICODE_NORMALIZATION_ERRORGSASL_NO_MORE_REALMSGSASL_INVALID_HANDLEerr: libgsasl error code
Convert return code to human readable string.
Return value: Returns a pointer to a statically allocated string containing a description of the error with the error value
err. This string can be used to output a diagnostic message to the user.
This chapter contains example code which illustrate how the GNU SASL Library can be used when writing your own application.