~ubuntu-branches/ubuntu/trusty/libesmtp/trusty-proposed

« back to all changes in this revision

Viewing changes to crammd5/client-crammd5.c

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy T. Bouse
  • Date: 2002-03-06 08:37:48 UTC
  • Revision ID: james.westby@ubuntu.com-20020306083748-ihmt32mddsslvg5i
Tags: upstream-0.8.11
ImportĀ upstreamĀ versionĀ 0.8.11

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  This file is part of libESMTP, a library for submission of RFC 2822
 
3
 *  formatted electronic mail messages using the SMTP protocol described
 
4
 *  in RFC 2821.
 
5
 *
 
6
 *  Copyright (C) 2001,2002  Brian Stafford  <brian@stafford.uklinux.net>
 
7
 *
 
8
 *  This library is free software; you can redistribute it and/or
 
9
 *  modify it under the terms of the GNU Lesser General Public
 
10
 *  License as published by the Free Software Foundation; either
 
11
 *  version 2.1 of the License, or (at your option) any later version.
 
12
 *
 
13
 *  This library is distributed in the hope that it will be useful,
 
14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
16
 *  Lesser General Public License for more details.
 
17
 *
 
18
 *  You should have received a copy of the GNU Lesser General Public
 
19
 *  License along with this library; if not, write to the Free Software
 
20
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
21
 */
 
22
#ifdef HAVE_CONFIG_H
 
23
#include <config.h>
 
24
#endif
 
25
 
 
26
#include <assert.h>
 
27
 
 
28
#include <string.h>
 
29
#include <stdlib.h>
 
30
 
 
31
#include <sys/types.h>
 
32
#include "hmacmd5.h"
 
33
#include "auth-client.h"
 
34
#include "auth-plugin.h"
 
35
 
 
36
#define NELT(x)         (sizeof x / sizeof x[0])
 
37
 
 
38
static int crammd5_init (void *pctx);
 
39
static void crammd5_destroy (void *ctx);
 
40
static const char *crammd5_response (void *ctx,
 
41
                                     const char *challenge, int *len,
 
42
                                     auth_interact_t interact, void *arg);
 
43
 
 
44
const struct auth_client_plugin sasl_client =
 
45
  {
 
46
  /* Plugin information */
 
47
    "CRAM-MD5",
 
48
    "Challenge-Response Authentication Mechanism (RFC 2195)",
 
49
  /* Plugin instance */
 
50
    crammd5_init,
 
51
    crammd5_destroy,
 
52
  /* Authentication */
 
53
    crammd5_response,
 
54
    0,
 
55
  /* Security Layer */
 
56
    0,
 
57
    NULL,
 
58
    NULL,
 
59
  };
 
60
 
 
61
static const struct auth_client_request client_request[] =
 
62
  {
 
63
    { "user",           AUTH_CLEARTEXT | AUTH_USER,     "User Name",    0, },
 
64
    { "passphrase",     AUTH_PASS,                      "Pass Phrase",  0, },
 
65
  };
 
66
 
 
67
struct crammd5_context
 
68
  {
 
69
    int state;
 
70
    char *response;
 
71
    int response_len;
 
72
  };
 
73
 
 
74
static int 
 
75
crammd5_init (void *pctx)
 
76
{
 
77
  struct crammd5_context *context;
 
78
 
 
79
  context = malloc (sizeof (struct crammd5_context));
 
80
  memset (context, 0, sizeof (struct crammd5_context));
 
81
 
 
82
  *(void **) pctx = context;
 
83
  return 1;
 
84
}
 
85
 
 
86
static void
 
87
crammd5_destroy (void *ctx)
 
88
{
 
89
  struct crammd5_context *context = ctx;
 
90
 
 
91
  if (context->response != NULL)
 
92
    {
 
93
      memset (context->response, 0, context->response_len);
 
94
      free (context->response);
 
95
    }
 
96
  free (context);
 
97
}
 
98
 
 
99
static const char *
 
100
crammd5_response (void *ctx, const char *challenge, int *len,
 
101
                  auth_interact_t interact, void *arg)
 
102
{
 
103
  struct crammd5_context *context = ctx;
 
104
  char *result[NELT (client_request)];
 
105
  unsigned char digest[16];
 
106
  char *p, *response;
 
107
  int response_len;
 
108
  size_t i;
 
109
  static const char hex[] = "0123456789abcdef";
 
110
 
 
111
  switch (context->state)
 
112
    {
 
113
    case 0:     /* No initial response */
 
114
      context->state = 1;
 
115
      *len = 0;
 
116
      return NULL;
 
117
 
 
118
    case 1:     /* Digest the challenge and compute a response. */
 
119
      if (!(*interact) (client_request, result, NELT (client_request), arg))
 
120
        break;
 
121
      hmac_md5 (challenge, *len, result[1], strlen (result[1]), digest);
 
122
      response_len = strlen (result[0]) + 1 + 2 * sizeof digest;
 
123
      response = malloc (response_len);
 
124
      strcpy (response, result[0]);
 
125
      strcat (response, " ");
 
126
      p = strchr (response, '\0');
 
127
      for (i = 0; i < sizeof digest; i++)
 
128
        {
 
129
          *p++ = hex[(digest[i] >> 4) & 0x0F];
 
130
          *p++ = hex[digest[i] & 0x0F];
 
131
        }
 
132
      /* Note no \0 termination */
 
133
      context->state = -1;
 
134
      context->response = response;
 
135
      context->response_len = response_len;
 
136
      *len = response_len;
 
137
      return response;
 
138
    }
 
139
  *len = 0;
 
140
  return NULL;
 
141
}
 
142