File: keys.c

package info (click to toggle)
proftpd-mod-sftp-ldap 0.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 508 kB
  • sloc: sh: 3,020; ansic: 996; makefile: 107
file content (496 lines) | stat: -rw-r--r-- 13,138 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
/*
 * ProFTPD: mod_sftp_ldap keys
 * Copyright (c) 2010-2016 TJ Saunders
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
 *
 * As a special exemption, the respective copyright holders give permission
 * to link this program with OpenSSL, and distribute the resulting
 * executable, without including the source code for OpenSSL in the source
 * distribution.
 */

#include "keys.h"

#define SFTP_LDAP_BUFSZ				1024

#define SFTP_LDAP_KEY_BEGIN_MARKER_LEN		31
#define SFTP_LDAP_KEY_END_MARKER_LEN		29

/* Given a blob of bytes retrieved from a single row, read that blob as if
 * it were text, line by line.
 */
static char *get_line(pool *p, char **blob, size_t *bloblen) {
  char linebuf[SFTP_LDAP_BUFSZ], *line = "", *data;
  size_t datalen;

  data = *blob;
  datalen = *bloblen;

  if (data == NULL ||
      datalen == 0) {
    errno = EOF;
    return NULL;
  }

  while (data != NULL && datalen > 0) {
    char *ptr;
    size_t delimlen, linelen;
    int have_line_continuation = FALSE;

    pr_signals_handle();

    if (datalen <= 2) {
      line = pstrcat(p, line, data, NULL);

      *blob = NULL;
      *bloblen = 0;

      return line;
    }

    /* Find the CRLF markers in the data. */
    ptr = strstr(data, "\r\n");
    if (ptr != NULL) {
      delimlen = 1;

    } else {
      ptr = strstr(data, "\n");
      if (ptr != NULL) {
        delimlen = 0;
      }
    }

    if (ptr == NULL) {
      /* Just return the rest of the data. */
      line = pstrcat(p, line, data, NULL);

      *blob = NULL;
      *bloblen = 0;

      return line;
    }

    linelen = (ptr - data + 1);

    if (linelen == 1) {
      data += (delimlen + 1);
      datalen -= (delimlen + 1);

      continue;
    }

    /* Watch out for lines larger than our buffer. */
    if (linelen > sizeof(linebuf)) {
      (void) pr_log_writefile(sftp_logfd, MOD_SFTP_LDAP_VERSION,
        "line of key data (%lu bytes) exceeds buffer size, truncating; "
        "this WILL cause authentication failures", (unsigned long) linelen);
      linelen = sizeof(linebuf);
    }

    memcpy(linebuf, data, linelen);
    linebuf[linelen-1] = '\0';

    data += (linelen + delimlen);
    datalen -= (linelen + delimlen);

    /* Check for continued lines. */
    if (linelen >= 2 &&
        linebuf[linelen-2] == '\\') {
      linebuf[linelen-2] = '\0';
      have_line_continuation = TRUE;
    }

    line = pstrcat(p, line, linebuf, NULL);
    linelen = strlen(line);

    if (have_line_continuation) {
      continue;
    }

    ptr = strchr(line, ':');
    if (ptr != NULL) {
      unsigned int header_taglen, header_valuelen;

      /* We have a header.  Make sure the header tag is not longer than
       * the specified length of 64 bytes, and that the header value is
       * not longer than 1024 bytes.
       */
      header_taglen = ptr - line;
      if (header_taglen > 64) {
        (void) pr_log_writefile(sftp_logfd, MOD_SFTP_LDAP_VERSION,
          "header tag too long (%u) in retrieved LDAP data", header_taglen);
        errno = EINVAL;
        return NULL;
      }

      /* Header value starts at 2 after the ':' (one for the mandatory
       * space character.
       */
      header_valuelen = linelen - (header_taglen + 2);
      if (header_valuelen > 1024) {
        (void) pr_log_writefile(sftp_logfd, MOD_SFTP_LDAP_VERSION,
          "header value too long (%u) in retrieved LDAP data", header_valuelen);
        errno = EINVAL;
        return NULL;
      }
    }

    *blob = data;
    *bloblen = datalen;

    return line;
  }

  return NULL;
}

int sftp_ldap_keys_parse_raw(pool *p, char **blob, size_t *bloblen,
    unsigned char **key_data, uint32_t *key_datalen) {
  char chunk[SFTP_LDAP_BUFSZ], *data = NULL;
  BIO *bio = NULL, *b64 = NULL, *bmem = NULL;
  int chunklen, res;
  long datalen = 0;
  char *ptr;

  if (p == NULL ||
      blob == NULL ||
      bloblen == NULL ||
      key_data == NULL ||
      key_datalen == NULL) {
    errno = EINVAL;
    return -1;
  }

  /* First we need to check for any leading/trailing SSH-isms in the key, i.e.
   * an "ssh-rsa", "ssh-dss", or "ecdsa-sha2-nistp*" prefix.  Fortunately
   * the prefix, and the trailing suffix, are separated from the key material
   * with spaces.
   */
  ptr = strchr(*blob, ' ');
  if (ptr != NULL) {
    size_t prefix_len;

    /* Advance the blob pointer past the prefix. */
    prefix_len = (ptr + 1) - *blob;
    (*bloblen) -= prefix_len;
    (*blob) += prefix_len;
  }

  ptr = strrchr(*blob, ' ');
  if (ptr != NULL) {
    size_t suffix_len;

    /* "Trim" off the suffix by truncating the bloblen appropriately. */
    suffix_len = strlen(ptr);
    (*bloblen) -= suffix_len;
  }

  bio = BIO_new(BIO_s_mem());

  if (BIO_write(bio, (void *) *blob, *bloblen) < 0) {
    (void) pr_log_writefile(sftp_logfd, MOD_SFTP_LDAP_VERSION,
      "error buffering base64 data: %s", sftp_crypto_get_errors());
  }

  /* Add a base64 filter BIO, and read the data out, thus base64-decoding
   * the key.  Write the decoded data into another memory BIO.
   */
  b64 = BIO_new(BIO_f_base64());
  BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
  bio = BIO_push(b64, bio);

  bmem = BIO_new(BIO_s_mem());

  memset(chunk, '\0', sizeof(chunk));
  chunklen = BIO_read(bio, (void *) chunk, sizeof(chunk));

  if (chunklen < 0 &&
      !BIO_should_retry(bio)) {
    (void) pr_log_writefile(sftp_logfd, MOD_SFTP_LDAP_VERSION,
      "unable to base64-decode data from LDAP directory: %s",
      sftp_crypto_get_errors());
    BIO_free_all(bio);
    BIO_free_all(bmem);

    errno = EPERM;
    return -1;
  }

  while (chunklen > 0) {
    pr_signals_handle();

    if (BIO_write(bmem, (void *) chunk, chunklen) < 0) {
      (void) pr_log_writefile(sftp_logfd, MOD_SFTP_LDAP_VERSION,
        "error writing to memory BIO: %s", sftp_crypto_get_errors());
      BIO_free_all(bio);
      BIO_free_all(bmem);

      errno = EPERM;
      return -1;
    }

    memset(chunk, '\0', sizeof(chunk));
    chunklen = BIO_read(bio, (void *) chunk, sizeof(chunk));
  }

  datalen = BIO_get_mem_data(bmem, &data);

  if (data != NULL &&
      datalen > 0) {
    *key_datalen = datalen;
    *key_data = palloc(p, datalen + 1);
    (*key_data)[datalen] = '\0';
    memcpy(*key_data, data, datalen);
    res = 0;

  } else {
    (void) pr_log_writefile(sftp_logfd, MOD_SFTP_LDAP_VERSION,
      "error base64-decoding key data from LDAP directory");
    errno = ENOENT;
    res = -1;
  }

  BIO_free_all(bio);
  bio = NULL;

  BIO_free_all(bmem);
  return res;
}

/* Note: When iterating backwards, be sure to ignore/skip over any potential
 * end marker.
 */
static void find_key_start(char **line, size_t *linelen) {
  register unsigned int i;

  /* Do NOT examine the end marker. */
  if (*linelen >= SFTP_LDAP_KEY_END_MARKER_LEN) {
    size_t datalen;

    datalen = *linelen - SFTP_LDAP_KEY_END_MARKER_LEN;
    if (strncmp(*line + datalen, SFTP_SSH2_PUBKEY_END_MARKER, SFTP_LDAP_KEY_END_MARKER_LEN) == 0) {
      i = datalen;

    } else {
      i = *linelen;
    }

  } else {
    i = *linelen;
  }

  while (i-- > 0) {
    char ch;

    pr_signals_handle();

    ch = (*line)[i];

    if (('a' <= ch && ch <= 'z') ||
        ('A' <= ch && ch <= 'Z') ||
        ('0' <= ch && ch <= '9') ||
        ch == '+' ||
        ch == '/' ||
        ch == '=') {
      continue;
    }

    /* The start of the line, then, is the next character AFTER this one. */
    *line = &((*line)[i+1]);
    *linelen = *linelen - i - 1;
    break;
  }
}

int sftp_ldap_keys_parse_rfc4716(pool *p, char **blob, size_t *bloblen,
    unsigned char **key_data, uint32_t *key_datalen, pr_table_t *headers) {
  char *line;
  BIO *bio = NULL;
  int had_key_data = FALSE, res = -1;

  if (p == NULL ||
      blob == NULL ||
      bloblen == NULL ||
      key_data == NULL ||
      key_datalen == NULL) {
    errno = EINVAL;
    return -1;
  }

  line = get_line(p, blob, bloblen);
  while (line == NULL &&
         errno == EINVAL) {
    pr_signals_handle();
    line = get_line(p, blob, bloblen);
  }

  if (line == NULL) {
    return -1;
  }

  while (line != NULL) {
    size_t linelen;

    pr_signals_handle();

    linelen = strlen(line);

    if (bio == NULL &&
        strncmp(line, SFTP_SSH2_PUBKEY_BEGIN_MARKER, SFTP_LDAP_KEY_BEGIN_MARKER_LEN) == 0) {
      bio = BIO_new(BIO_s_mem());
      linelen -= SFTP_LDAP_KEY_BEGIN_MARKER_LEN;
      line += SFTP_LDAP_KEY_BEGIN_MARKER_LEN;

      had_key_data = TRUE;
    }

    if (bio != NULL &&
        linelen >= SFTP_LDAP_KEY_END_MARKER_LEN &&
        strncmp(line, SFTP_SSH2_PUBKEY_END_MARKER, SFTP_LDAP_KEY_END_MARKER_LEN) == 0) {
      char chunk[SFTP_LDAP_BUFSZ], *data = NULL;
      BIO *b64 = NULL, *bmem = NULL;
      int chunklen;
      long datalen = 0;

      /* Add a base64 filter BIO, and read the data out, thus base64-decoding
       * the key.  Write the decoded data into another memory BIO.
       */
      b64 = BIO_new(BIO_f_base64());
      BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
      bio = BIO_push(b64, bio);

      bmem = BIO_new(BIO_s_mem());

      memset(chunk, '\0', sizeof(chunk));
      chunklen = BIO_read(bio, (void *) chunk, sizeof(chunk));

      if (chunklen < 0 &&
          !BIO_should_retry(bio)) {
        (void) pr_log_writefile(sftp_logfd, MOD_SFTP_LDAP_VERSION,
          "unable to base64-decode RFC4716 key data from database: %s",
        sftp_crypto_get_errors());
        BIO_free_all(bio);
        BIO_free_all(bmem);

        errno = EPERM;
        return -1;
      }

      while (chunklen > 0) {
        pr_signals_handle();

        if (BIO_write(bmem, (void *) chunk, chunklen) < 0) {
          (void) pr_log_writefile(sftp_logfd, MOD_SFTP_LDAP_VERSION,
            "error writing to memory BIO: %s", sftp_crypto_get_errors());
          BIO_free_all(bio);
          BIO_free_all(bmem);

          errno = EPERM;
          return -1;
        }

        memset(chunk, '\0', sizeof(chunk));
        chunklen = BIO_read(bio, (void *) chunk, sizeof(chunk));
      }

      datalen = BIO_get_mem_data(bmem, &data);

      if (data != NULL &&
          datalen > 0) {
        *key_datalen = datalen;
        *key_data = palloc(p, datalen + 1);
        (*key_data)[datalen] = '\0';
        memcpy(*key_data, data, datalen);
        res = 0;

      } else {
        (void) pr_log_writefile(sftp_logfd, MOD_SFTP_LDAP_VERSION,
          "error base64-decoding RFC4716 key data from LDAP directory");
        errno = ENOENT;
        res = -1;
      }

      BIO_free_all(bio);
      bio = NULL;

      BIO_free_all(bmem);
      break;
    }

    if (bio != NULL &&
        linelen > 0) {
      size_t chunklen;

      /* Watch out for any headers in the key data; we cannot handle those
       * here.  However, if we simply bail, it makes things difficult for the
       * admin wishing to use the keys, as user authentication will fail.
       *
       * Thus we try some heuristics to avoid/skip the headers.  The key data
       * in which we are interested is base64-encoded.  We thus iterate from
       * the end of the line, looking for any non-base64 characters.  If found,
       * we ignore them.
       */
      find_key_start(&line, &linelen);

      chunklen = linelen;

      /* Do NOT consume the end marker. */
      if (linelen >= SFTP_LDAP_KEY_END_MARKER_LEN) {
        size_t datalen;

        datalen = linelen - SFTP_LDAP_KEY_END_MARKER_LEN;
        if (strncmp(line + datalen, SFTP_SSH2_PUBKEY_END_MARKER, SFTP_LDAP_KEY_END_MARKER_LEN) == 0) {
          /* Truncate the data to be written to the bio to not include the
           * end marker.
           */
          chunklen -= SFTP_LDAP_KEY_END_MARKER_LEN;
        }
      }

      if (chunklen > 0) {
        if (BIO_write(bio, line, chunklen) < 0) {
          (void) pr_log_writefile(sftp_logfd, MOD_SFTP_LDAP_VERSION,
            "error buffering base64 data: %s", sftp_crypto_get_errors());
        }

        linelen -= chunklen;
        line += chunklen;
      }

    } else {
      /* Consider this line consumed. */
      linelen = 0;
    }

    /* Get the next line, but only if we're done with this one. */
    if (linelen == 0) {
      line = get_line(p, blob, bloblen);
      while (line == NULL &&
             errno == EINVAL) {
        pr_signals_handle();
        line = get_line(p, blob, bloblen);
      }
    }
  }

  /* Provide a default errno value if necessary. */
  if (had_key_data == FALSE &&
      res < 0) {
    errno = ENOENT;
  }

  return res;
}