~ubuntu-branches/debian/sid/postfix/sid

« back to all changes in this revision

Viewing changes to src/tls/tls_mgr.c

  • Committer: Package Import Robot
  • Author(s): LaMont Jones, LaMont Jones, localization folks
  • Date: 2014-02-11 07:44:30 UTC
  • mfrom: (1.1.41)
  • Revision ID: package-import@ubuntu.com-20140211074430-91tdwgjriazawdz4
Tags: 2.11.0-1
[LaMont Jones]

* New upstream release: 2.11.0

[localization folks]

* l10n: Updated German translations.  Closes: #734893 (Helge Kreutzmann)

Show diffs side-by-side

added added

removed removed

Lines of Context:
10
10
/*      VSTRING *buf;
11
11
/*      int     len;
12
12
/*
13
 
/*      int     tls_mgr_policy(cache_type, cachable)
 
13
/*      int     tls_mgr_policy(cache_type, cachable, timeout)
14
14
/*      const char *cache_type;
15
15
/*      int     *cachable;
 
16
/*      int     *timeout;
16
17
/*
17
18
/*      int     tls_mgr_update(cache_type, cache_id, buf, len)
18
19
/*      const char *cache_type;
28
29
/*      int     tls_mgr_delete(cache_type, cache_id)
29
30
/*      const char *cache_type;
30
31
/*      const char *cache_id;
 
32
/*
 
33
/*      TLS_TICKET_KEY *tls_mgr_key(keyname, timeout)
 
34
/*      unsigned char *keyname;
 
35
/*      int     timeout;
31
36
/* DESCRIPTION
32
37
/*      These routines communicate with the tlsmgr(8) server for
33
38
/*      entropy and session cache management. Since these are
48
53
/*      tls_mgr_delete() removes specified session from
49
54
/*      the specified session cache.
50
55
/*
51
 
/*      Arguments
 
56
/*      tls_mgr_key() is used to retrieve the current TLS session ticket
 
57
/*      encryption or decryption keys.
 
58
/*
 
59
/*      Arguments:
52
60
/* .IP cache_type
53
61
/*      One of TLS_MGR_SCACHE_SMTPD, TLS_MGR_SCACHE_SMTP or
54
62
/*      TLS_MGR_SCACHE_LMTP.
55
63
/* .IP cachable
56
64
/*      Pointer to int, set non-zero if the requested cache_type
57
65
/*      is enabled.
 
66
/* .IP timeout
 
67
/*      Pointer to int, returns the cache entry timeout.
58
68
/* .IP cache_id
59
69
/*      The session cache lookup key.
60
70
/* .IP buf
61
71
/*      The result or input buffer.
62
72
/* .IP len
63
73
/*      The length of the input buffer, or the amount of data requested.
 
74
/* .IP keyname
 
75
/*      Is null when requesting the current encryption keys.  Otherwise,
 
76
/*      keyname is a pointer to an array of TLS_TICKET_NAMELEN unsigned
 
77
/*      chars (not NUL terminated) that is an identifier for a key
 
78
/*      previously used to encrypt a session ticket.  When encrypting
 
79
/*      a null result indicates that session tickets are not supported, when
 
80
/*      decrypting it indicates that no matching keys were found.
 
81
/* .IP timeout
 
82
/*      The encryption key timeout.  Once a key has been active for this many
 
83
/*      seconds it is retired and used only for decrypting previously issued
 
84
/*      session tickets for another timeout seconds, and is then destroyed.
 
85
/*      The timeout must not be longer than half the SSL session lifetime.
64
86
/* DIAGNOSTICS
65
87
/*      All client functions return one of the following status codes:
66
88
/* .IP TLS_MGR_STAT_OK
104
126
#include <vstring.h>
105
127
#include <attr.h>
106
128
#include <attr_clnt.h>
 
129
#include <mymalloc.h>
 
130
#include <stringops.h>
107
131
 
108
132
/* Global library. */
109
133
 
110
134
#include <mail_params.h>
111
135
#include <mail_proto.h>
 
136
 
 
137
/* TLS library. */
112
138
#include <tls_mgr.h>
113
139
 
114
140
/* Application-specific. */
115
141
 
 
142
#define STR(x) vstring_str(x)
 
143
#define LEN(x) VSTRING_LEN(x)
 
144
 
116
145
static ATTR_CLNT *tls_mgr;
117
146
 
118
147
/* tls_mgr_open - create client handle */
119
148
 
120
149
static void tls_mgr_open(void)
121
150
{
 
151
    char   *service;
122
152
 
123
153
    /*
124
154
     * Sanity check.
130
160
     * Use whatever IPC is preferred for internal use: UNIX-domain sockets or
131
161
     * Solaris streams.
132
162
     */
133
 
#ifndef VAR_TLS_MGR_SERVICE
134
 
    tls_mgr = attr_clnt_create("local:" TLS_MGR_CLASS "/" TLS_MGR_SERVICE,
135
 
                               var_ipc_timeout, var_ipc_idle_limit,
136
 
                               var_ipc_ttl_limit);
137
 
#else
138
 
    tls_mgr = attr_clnt_create(var_tlsmgr_service, var_ipc_timeout,
 
163
    service = concatenate("local:" TLS_MGR_CLASS "/", var_tls_mgr_service,
 
164
                          (char *) 0);
 
165
    tls_mgr = attr_clnt_create(service, var_ipc_timeout,
139
166
                               var_ipc_idle_limit, var_ipc_ttl_limit);
140
 
#endif
 
167
    myfree(service);
 
168
 
141
169
    attr_clnt_control(tls_mgr,
142
170
                      ATTR_CLNT_CTL_PROTO, attr_vprint, attr_vscan,
143
171
                      ATTR_CLNT_CTL_END);
173
201
 
174
202
/* tls_mgr_policy - request caching policy */
175
203
 
176
 
int     tls_mgr_policy(const char *cache_type, int *cachable)
 
204
int     tls_mgr_policy(const char *cache_type, int *cachable, int *timeout)
177
205
{
178
206
    int     status;
179
207
 
194
222
                          ATTR_FLAG_MISSING,    /* Reply attributes */
195
223
                          ATTR_TYPE_INT, TLS_MGR_ATTR_STATUS, &status,
196
224
                          ATTR_TYPE_INT, TLS_MGR_ATTR_CACHABLE, cachable,
197
 
                          ATTR_TYPE_END) != 2)
 
225
                          ATTR_TYPE_INT, TLS_MGR_ATTR_SESSTOUT, timeout,
 
226
                          ATTR_TYPE_END) != 3)
198
227
        status = TLS_MGR_STAT_FAIL;
199
228
    return (status);
200
229
}
287
316
    return (status);
288
317
}
289
318
 
 
319
/* request_scache_key - ask tlsmgr(8) for matching key */
 
320
 
 
321
static TLS_TICKET_KEY *request_scache_key(unsigned char *keyname)
 
322
{
 
323
    TLS_TICKET_KEY tmp;
 
324
    static VSTRING *keybuf;
 
325
    char   *name;
 
326
    size_t  len;
 
327
    int     status;
 
328
 
 
329
    /*
 
330
     * Create the tlsmgr client handle.
 
331
     */
 
332
    if (tls_mgr == 0)
 
333
        tls_mgr_open();
 
334
 
 
335
    if (keybuf == 0)
 
336
        keybuf = vstring_alloc(sizeof(tmp));
 
337
 
 
338
    /* In tlsmgr requests we encode null key names as empty strings. */
 
339
    name = keyname ? (char *) keyname : "";
 
340
    len = keyname ? TLS_TICKET_NAMELEN : 0;
 
341
 
 
342
    /*
 
343
     * Send the request and receive the reply.
 
344
     */
 
345
    if (attr_clnt_request(tls_mgr,
 
346
                          ATTR_FLAG_NONE,       /* Request */
 
347
                        ATTR_TYPE_STR, TLS_MGR_ATTR_REQ, TLS_MGR_REQ_TKTKEY,
 
348
                          ATTR_TYPE_DATA, TLS_MGR_ATTR_KEYNAME, len, name,
 
349
                          ATTR_TYPE_END,
 
350
                          ATTR_FLAG_MISSING,    /* Reply */
 
351
                          ATTR_TYPE_INT, TLS_MGR_ATTR_STATUS, &status,
 
352
                          ATTR_TYPE_DATA, TLS_MGR_ATTR_KEYBUF, keybuf,
 
353
                          ATTR_TYPE_END) != 2
 
354
        || status != TLS_MGR_STAT_OK
 
355
        || LEN(keybuf) != sizeof(tmp))
 
356
        return (0);
 
357
 
 
358
    memcpy((char *) &tmp, STR(keybuf), sizeof(tmp));
 
359
    return (tls_scache_key_rotate(&tmp));
 
360
}
 
361
 
 
362
/* tls_mgr_key - session ticket key lookup, local cache, then tlsmgr(8) */
 
363
 
 
364
TLS_TICKET_KEY *tls_mgr_key(unsigned char *keyname, int timeout)
 
365
{
 
366
    TLS_TICKET_KEY *key = 0;
 
367
    time_t  now = time((time_t *) 0);
 
368
 
 
369
    /* A zero timeout disables session tickets. */
 
370
    if (timeout <= 0)
 
371
        return (0);
 
372
 
 
373
    if ((key = tls_scache_key(keyname, now, timeout)) == 0)
 
374
        key = request_scache_key(keyname);
 
375
    return (key);
 
376
}
 
377
 
290
378
#ifdef TEST
291
379
 
292
380
/* System library. */
306
394
 
307
395
/* Application-specific. */
308
396
 
309
 
#define STR(x) vstring_str(x)
310
 
#define LEN(x) VSTRING_LEN(x)
311
 
 
312
397
int     main(int unused_ac, char **av)
313
398
{
314
399
    VSTRING *inbuf = vstring_alloc(10);
331
416
            argv_free(argv);
332
417
            continue;
333
418
        }
334
 
 
335
419
#define COMMAND(argv, str, len) \
336
420
    (strcasecmp(argv->argv[0], str) == 0 && argv->argc == len)
337
421
 
338
422
        if (COMMAND(argv, "policy", 2)) {
339
423
            int     cachable;
 
424
            int     timeout;
340
425
 
341
 
            status = tls_mgr_policy(argv->argv[1], &cachable);
342
 
            vstream_printf("status=%d cachable=%d\n", status, cachable);
 
426
            status = tls_mgr_policy(argv->argv[1], &cachable, &timeout);
 
427
            vstream_printf("status=%d cachable=%d timeout=%d\n",
 
428
                           status, cachable, timeout);
343
429
        } else if (COMMAND(argv, "seed", 2)) {
344
430
            VSTRING *buf = vstring_alloc(10);
345
431
            VSTRING *hex = vstring_alloc(10);