Browse code

Add fix for cyrus-sasl memory leak

Change-Id: I57577a661b099a64ab7b4f44bf72979fa72f4804
Reviewed-on: http://photon-jenkins.eng.vmware.com:8082/4016
Reviewed-by: Anish Swaminathan <anishs@vmware.com>
Tested-by: Anish Swaminathan <anishs@vmware.com>

suezzelur authored on 2017/10/11 08:46:50
Showing 2 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,1134 @@
0
+diff -rupN cyrus-sasl-2.1.26/plugins/srp.c cyrus-sasl-2.1.26-new/plugins/srp.c
1
+--- cyrus-sasl-2.1.26/plugins/srp.c	2012-10-12 07:05:48.000000000 -0700
2
+@@ -1,10 +1,9 @@
3
+ /* SRP SASL plugin
4
+  * Ken Murchison
5
+  * Tim Martin  3/17/00
6
+- * $Id: srp.c,v 1.59 2010/11/30 11:41:47 mel Exp $
7
+  */
8
+ /* 
9
+- * Copyright (c) 1998-2003 Carnegie Mellon University.  All rights reserved.
10
++ * Copyright (c) 1998-2016 Carnegie Mellon University.  All rights reserved.
11
+  *
12
+  * Redistribution and use in source and binary forms, with or without
13
+  * modification, are permitted provided that the following conditions
14
+@@ -22,12 +21,13 @@
15
+  *    endorse or promote products derived from this software without
16
+  *    prior written permission. For permission or any other legal
17
+  *    details, please contact  
18
+- *      Office of Technology Transfer
19
+  *      Carnegie Mellon University
20
+- *      5000 Forbes Avenue
21
+- *      Pittsburgh, PA  15213-3890
22
+- *      (412) 268-4387, fax: (412) 268-7395
23
+- *      tech-transfer@andrew.cmu.edu
24
++ *      Center for Technology Transfer and Enterprise Creation
25
++ *      4615 Forbes Avenue
26
++ *      Suite 302
27
++ *      Pittsburgh, PA  15213
28
++ *      (412) 268-7393, fax: (412) 268-7395
29
++ *      innovation@andrew.cmu.edu
30
+  *
31
+  * 4. Redistributions of any form whatsoever must retain the following
32
+  *    acknowledgment:
33
+@@ -101,8 +101,6 @@ typedef unsigned short uint32;
34
+ 
35
+ /*****************************  Common Section  *****************************/
36
+ 
37
+-static const char plugin_id[] = "$Id: srp.c,v 1.59 2010/11/30 11:41:47 mel Exp $";
38
+-
39
+ /* Size limit of cipher block size */
40
+ #define SRP_MAXBLOCKSIZE 16
41
+ /* Size limit of SRP buffer */
42
+@@ -216,22 +214,22 @@ typedef struct srp_options_s {
43
+ typedef struct context {
44
+     int state;
45
+     
46
+-    BIGNUM N;			/* safe prime modulus */
47
+-    BIGNUM g;			/* generator */
48
++    BIGNUM *N;			/* safe prime modulus */
49
++    BIGNUM *g;			/* generator */
50
+     
51
+-    BIGNUM v;			/* password verifier */
52
++    BIGNUM *v;			/* password verifier */
53
+     
54
+-    BIGNUM b;			/* server private key */
55
+-    BIGNUM B;			/* server public key */
56
++    BIGNUM *b;			/* server private key */
57
++    BIGNUM *B;			/* server public key */
58
+     
59
+-    BIGNUM a;			/* client private key */
60
+-    BIGNUM A;			/* client public key */
61
++    BIGNUM *a;			/* client private key */
62
++    BIGNUM *A;			/* client public key */
63
+     
64
+-    char K[EVP_MAX_MD_SIZE];	/* shared context key */
65
+-    int Klen;
66
++    unsigned char K[EVP_MAX_MD_SIZE];	/* shared context key */
67
++    unsigned int Klen;
68
+     
69
+-    char M1[EVP_MAX_MD_SIZE];	/* client evidence */
70
+-    int M1len;
71
++    unsigned char M1[EVP_MAX_MD_SIZE];	/* client evidence */
72
++    unsigned int M1len;
73
+     
74
+     char *authid;		/* authentication id (server) */
75
+     char *userid;		/* authorization id (server) */
76
+@@ -242,7 +240,7 @@ typedef struct context {
77
+     char *server_options;
78
+     
79
+     srp_options_t client_opts;	/* cache between client steps */
80
+-    char cIV[SRP_MAXBLOCKSIZE];	/* cache between client steps */
81
++    unsigned char cIV[SRP_MAXBLOCKSIZE];	/* cache between client steps */
82
+     
83
+     char *salt;			/* password salt */
84
+     int saltlen;
85
+@@ -259,12 +257,12 @@ typedef struct context {
86
+     /* Layer foo */
87
+     unsigned layer;		/* bitmask of enabled layers */
88
+     const EVP_MD *hmac_md;	/* HMAC for integrity */
89
+-    HMAC_CTX hmac_send_ctx;
90
+-    HMAC_CTX hmac_recv_ctx;
91
++    HMAC_CTX *hmac_send_ctx;
92
++    HMAC_CTX *hmac_recv_ctx;
93
+ 
94
+     const EVP_CIPHER *cipher;	/* cipher for confidentiality */
95
+-    EVP_CIPHER_CTX cipher_enc_ctx;
96
+-    EVP_CIPHER_CTX cipher_dec_ctx;
97
++    EVP_CIPHER_CTX *cipher_enc_ctx;
98
++    EVP_CIPHER_CTX *cipher_dec_ctx;
99
+     
100
+     /* replay detection sequence numbers */
101
+     int seqnum_out;
102
+@@ -279,6 +277,52 @@ typedef struct context {
103
+     
104
+ } context_t;
105
+ 
106
++static void *OPENSSL_zalloc(size_t num)
107
++{
108
++    void *ret = OPENSSL_malloc(num);
109
++
110
++    if (ret != NULL)
111
++        memset(ret, 0, num);
112
++    return ret;
113
++}
114
++
115
++HMAC_CTX *HMAC_CTX_new(void)
116
++{
117
++    HMAC_CTX *ctx = OPENSSL_malloc(sizeof(*ctx));
118
++    if (ctx != NULL) {
119
++        if (!HMAC_CTX_reset(ctx)) {
120
++            HMAC_CTX_free(ctx);
121
++            return NULL;
122
++        }
123
++    }
124
++    return ctx;
125
++}
126
++
127
++void HMAC_CTX_free(HMAC_CTX *ctx)
128
++{
129
++    if (ctx != NULL) {
130
++        HMAC_CTX_cleanup(ctx);
131
++        OPENSSL_free(ctx);
132
++    }
133
++}
134
++
135
++int HMAC_CTX_reset(HMAC_CTX *ctx)
136
++{
137
++    HMAC_CTX_init(ctx);
138
++    return 1;
139
++}
140
++
141
++void EVP_MD_CTX_free(EVP_MD_CTX *ctx)
142
++{
143
++        EVP_MD_CTX_cleanup(ctx);
144
++            OPENSSL_free(ctx);
145
++}
146
++
147
++EVP_MD_CTX *EVP_MD_CTX_new(void)
148
++{
149
++    return OPENSSL_zalloc(sizeof(EVP_MD_CTX));
150
++}
151
++
152
+ static int srp_encode(void *context,
153
+ 		      const struct iovec *invec,
154
+ 		      unsigned numiov,
155
+@@ -317,12 +361,12 @@ static int srp_encode(void *context,
156
+ 	inputlen = invec[i].iov_len;
157
+     
158
+ 	if (text->layer & BIT_CONFIDENTIALITY) {
159
+-	    unsigned enclen;
160
++	    int enclen;
161
+ 
162
+ 	    /* encrypt the data into the output buffer */
163
+-	    EVP_EncryptUpdate(&text->cipher_enc_ctx,
164
+-			      text->encode_buf + *outputlen, &enclen,
165
+-			      input, inputlen);
166
++	    EVP_EncryptUpdate(text->cipher_enc_ctx,
167
++			      (unsigned char *) text->encode_buf + *outputlen,
168
++                              &enclen, (unsigned char *) input, inputlen);
169
+ 	    *outputlen += enclen;
170
+ 
171
+ 	    /* switch the input to the encrypted data */
172
+@@ -337,11 +381,12 @@ static int srp_encode(void *context,
173
+     }
174
+     
175
+     if (text->layer & BIT_CONFIDENTIALITY) {
176
+-	unsigned enclen;
177
++	int enclen;
178
+ 
179
+ 	/* encrypt the last block of data into the output buffer */
180
+-	EVP_EncryptFinal(&text->cipher_enc_ctx,
181
+-			 text->encode_buf + *outputlen, &enclen);
182
++	EVP_EncryptFinal(text->cipher_enc_ctx,
183
++			 (unsigned char *) text->encode_buf + *outputlen,
184
++                         &enclen);
185
+ 	*outputlen += enclen;
186
+     }
187
+ 
188
+@@ -349,18 +394,20 @@ static int srp_encode(void *context,
189
+ 	unsigned hashlen;
190
+ 
191
+ 	/* hash the content */
192
+-	HMAC_Update(&text->hmac_send_ctx, text->encode_buf+4, *outputlen-4);
193
++	HMAC_Update(text->hmac_send_ctx,
194
++                    (unsigned char *) text->encode_buf+4, *outputlen-4);
195
+ 	
196
+ 	if (text->layer & BIT_REPLAY_DETECTION) {
197
+ 	    /* hash the sequence number */
198
+ 	    tmpnum = htonl(text->seqnum_out);
199
+-	    HMAC_Update(&text->hmac_send_ctx, (char *) &tmpnum, 4);
200
++	    HMAC_Update(text->hmac_send_ctx, (unsigned char *) &tmpnum, 4);
201
+ 	    
202
+ 	    text->seqnum_out++;
203
+ 	}
204
+ 
205
+ 	/* append the HMAC into the output buffer */
206
+-	HMAC_Final(&text->hmac_send_ctx, text->encode_buf + *outputlen,
207
++	HMAC_Final(text->hmac_send_ctx,
208
++                   (unsigned char *) text->encode_buf + *outputlen,
209
+ 		   &hashlen);
210
+ 	*outputlen += hashlen;
211
+     }
212
+@@ -387,8 +434,8 @@ static int srp_decode_packet(void *conte
213
+ 
214
+     if (text->layer & BIT_INTEGRITY) {
215
+ 	const char *hash;
216
+-	char myhash[EVP_MAX_MD_SIZE];
217
+-	unsigned hashlen, myhashlen, i;
218
++	unsigned char myhash[EVP_MAX_MD_SIZE];
219
++	unsigned hashlen;
220
+ 	unsigned long tmpnum;
221
+ 
222
+ 	hashlen = EVP_MD_size(text->hmac_md);
223
+@@ -405,25 +452,23 @@ static int srp_decode_packet(void *conte
224
+ 	hash = input + inputlen;
225
+ 
226
+ 	/* create our own hash from the input */
227
+-	HMAC_Update(&text->hmac_recv_ctx, input, inputlen);
228
++	HMAC_Update(text->hmac_recv_ctx, (unsigned char *) input, inputlen);
229
+ 	    
230
+ 	if (text->layer & BIT_REPLAY_DETECTION) {
231
+ 	    /* hash the sequence number */
232
+ 	    tmpnum = htonl(text->seqnum_in);
233
+-	    HMAC_Update(&text->hmac_recv_ctx, (char *) &tmpnum, 4);
234
++	    HMAC_Update(text->hmac_recv_ctx, (unsigned char *) &tmpnum, 4);
235
+ 		
236
+ 	    text->seqnum_in++;
237
+ 	}
238
+ 	    
239
+-	HMAC_Final(&text->hmac_recv_ctx, myhash, &myhashlen);
240
++	HMAC_Final(text->hmac_recv_ctx, myhash, &hashlen);
241
+ 
242
+ 	/* compare hashes */
243
+-	for (i = 0; i < hashlen; i++) {
244
+-	    if ((myhashlen != hashlen) || (myhash[i] != hash[i])) {
245
+-		SETERROR(text->utils, "Hash is incorrect\n");
246
+-		return SASL_BADMAC;
247
+-	    }
248
+-	}
249
++        if (memcmp(hash, myhash, hashlen)) {
250
++            SETERROR(text->utils, "Hash is incorrect\n");
251
++            return SASL_BADMAC;
252
++        }
253
+     }
254
+ 	
255
+     ret = _plug_buf_alloc(text->utils, &(text->decode_pkt_buf),
256
+@@ -432,16 +477,17 @@ static int srp_decode_packet(void *conte
257
+     if (ret != SASL_OK) return ret;
258
+ 	
259
+     if (text->layer & BIT_CONFIDENTIALITY) {
260
+-	unsigned declen;
261
++	int declen;
262
+ 
263
+ 	/* decrypt the data into the output buffer */
264
+-	EVP_DecryptUpdate(&text->cipher_dec_ctx,
265
+-			  text->decode_pkt_buf, &declen,
266
+-			  (char *) input, inputlen);
267
++	EVP_DecryptUpdate(text->cipher_dec_ctx,
268
++			  (unsigned char *) text->decode_pkt_buf, &declen,
269
++			  (unsigned char *) input, inputlen);
270
+ 	*outputlen = declen;
271
+ 	    
272
+-	EVP_DecryptFinal(&text->cipher_dec_ctx,
273
+-			 text->decode_pkt_buf + declen, &declen);
274
++	EVP_DecryptFinal(text->cipher_dec_ctx,
275
++			 (unsigned char *) text->decode_pkt_buf + declen,
276
++                         &declen);
277
+ 	*outputlen += declen;
278
+     } else {
279
+ 	/* copy the raw input to the output */
280
+@@ -474,7 +520,8 @@ static int srp_decode(void *context,
281
+ /*
282
+  * Convert a big integer to it's byte representation
283
+  */
284
+-static int BigIntToBytes(BIGNUM *num, char *out, int maxoutlen, int *outlen)
285
++static int BigIntToBytes(BIGNUM *num, unsigned char *out, int maxoutlen,
286
++                         unsigned int *outlen)
287
+ {
288
+     int len;
289
+     
290
+@@ -504,12 +551,12 @@ static int BigIntCmpWord(BIGNUM *a, BN_U
291
+ /*
292
+  * Generate a random big integer.
293
+  */
294
+-static void GetRandBigInt(BIGNUM *out)
295
++static void GetRandBigInt(BIGNUM **out)
296
+ {
297
+-    BN_init(out);
298
++    *out = BN_new();
299
+     
300
+     /* xxx likely should use sasl random funcs */
301
+-    BN_rand(out, SRP_MAXBLOCKSIZE*8, 0, 0);
302
++    BN_rand(*out, SRP_MAXBLOCKSIZE*8, 0, 0);
303
+ }
304
+ 
305
+ #define MAX_BUFFER_LEN 2147483643
306
+@@ -624,7 +671,8 @@ static int MakeBuffer(const sasl_utils_t
307
+ 	case 'm':
308
+ 	    /* MPI */
309
+ 	    mpi = va_arg(ap, BIGNUM *);
310
+-	    r = BigIntToBytes(mpi, out+2, BN_num_bytes(mpi), &len);
311
++	    r = BigIntToBytes(mpi, (unsigned char *) out+2,
312
++                              BN_num_bytes(mpi), (unsigned *) &len);
313
+ 	    if (r) goto done;
314
+ 	    ns = htons(len);
315
+ 	    memcpy(out, &ns, 2);	/* add 2 byte len (network order) */
316
+@@ -695,7 +743,7 @@ static int UnBuffer(const sasl_utils_t *
317
+     va_list ap;
318
+     char *p;
319
+     int r = SASL_OK, noalloc;
320
+-    BIGNUM *mpi;
321
++    BIGNUM **mpi;
322
+     char **os, **str;
323
+     uint32 *u;
324
+     unsigned short ns;
325
+@@ -757,9 +805,12 @@ static int UnBuffer(const sasl_utils_t *
326
+ 		goto done;
327
+ 	    }
328
+ 	    
329
+-	    mpi = va_arg(ap, BIGNUM *);
330
+-	    BN_init(mpi);
331
+-	    BN_bin2bn(buf, len, mpi);
332
++	    mpi = va_arg(ap, BIGNUM **);
333
++            if (mpi) {
334
++                if (!*mpi) *mpi = BN_new();
335
++                else BN_clear(*mpi);
336
++                BN_bin2bn((unsigned char *) buf, len, *mpi);
337
++            }
338
+ 	    break;
339
+ 
340
+ 	case 'o':
341
+@@ -883,16 +934,17 @@ static int UnBuffer(const sasl_utils_t *
342
+ /*
343
+  * Apply the hash function to the data specifed by the fmt string.
344
+  */
345
+-static int MakeHash(const EVP_MD *md, unsigned char hash[], int *hashlen,
346
++static int MakeHash(const EVP_MD *md,
347
++                    unsigned char hash[], unsigned int *hashlen,
348
+ 		    const char *fmt, ...)
349
+ {
350
+     va_list ap;
351
+     char *p, buf[4096], *in;
352
+-    int inlen;
353
+-    EVP_MD_CTX mdctx;
354
++    unsigned int inlen;
355
++    EVP_MD_CTX *mdctx = EVP_MD_CTX_new();
356
+     int r = 0, hflag;
357
+ 
358
+-    EVP_DigestInit(&mdctx, md);
359
++    EVP_DigestInit(mdctx, md);
360
+ 
361
+     va_start(ap, fmt);
362
+     for (p = (char *) fmt; *p; p++) {
363
+@@ -910,7 +962,7 @@ static int MakeHash(const EVP_MD *md, un
364
+ 		BIGNUM *mval = va_arg(ap, BIGNUM *);
365
+ 
366
+ 		in = buf;
367
+-		r = BigIntToBytes(mval, buf, sizeof(buf)-1, &inlen);
368
++		r = BigIntToBytes(mval, (unsigned char *) buf, sizeof(buf)-1, &inlen);
369
+ 		if (r) goto done;
370
+ 		break;
371
+ 	    }
372
+@@ -947,47 +999,52 @@ static int MakeHash(const EVP_MD *md, un
373
+ 
374
+ 	if (hflag) {
375
+ 	    /* hash data separately before adding to current hash */
376
+-	    EVP_MD_CTX tmpctx;
377
++	    EVP_MD_CTX *tmpctx = EVP_MD_CTX_new();
378
+ 
379
+-	    EVP_DigestInit(&tmpctx, md);
380
+-	    EVP_DigestUpdate(&tmpctx, in, inlen);
381
+-	    EVP_DigestFinal(&tmpctx, buf, &inlen);
382
++	    EVP_DigestInit(tmpctx, md);
383
++	    EVP_DigestUpdate(tmpctx, in, inlen);
384
++	    EVP_DigestFinal(tmpctx, (unsigned char *) buf, &inlen);
385
++            EVP_MD_CTX_free(tmpctx);
386
+ 	    in = buf;
387
+ 	}
388
+ 
389
+-	EVP_DigestUpdate(&mdctx, in, inlen);
390
++	EVP_DigestUpdate(mdctx, in, inlen);
391
+     }
392
+   done:
393
+     va_end(ap);
394
+ 
395
+-    EVP_DigestFinal(&mdctx, hash, hashlen);
396
++    EVP_DigestFinal(mdctx, hash, hashlen);
397
++    EVP_MD_CTX_free(mdctx);
398
+ 
399
+     return r;
400
+ }
401
+ 
402
+ static int CalculateX(context_t *text, const char *salt, int saltlen, 
403
+-		      const char *user, const char *pass, int passlen, 
404
+-		      BIGNUM *x)
405
++		      const char *user, const unsigned char *pass, int passlen, 
406
++		      BIGNUM **x)
407
+ {
408
+-    char hash[EVP_MAX_MD_SIZE];
409
+-    int hashlen;
410
++    unsigned char hash[EVP_MAX_MD_SIZE];
411
++    unsigned int hashlen;
412
+     
413
+     /* x = H(salt | H(user | ':' | pass)) */
414
+     MakeHash(text->md, hash, &hashlen, "%s:%o", user, passlen, pass);
415
+     MakeHash(text->md, hash, &hashlen, "%o%o", saltlen, salt, hashlen, hash);
416
+     
417
+-    BN_init(x);
418
+-    BN_bin2bn(hash, hashlen, x);
419
++    *x = BN_new();
420
++    BN_bin2bn(hash, hashlen, *x);
421
+     
422
+     return SASL_OK;
423
+ }
424
+ 
425
+ static int CalculateM1(context_t *text, BIGNUM *N, BIGNUM *g,
426
+ 		       char *U, char *salt, int saltlen,
427
+-		       BIGNUM *A, BIGNUM *B, char *K, int Klen,
428
+-		       char *I, char *L, char *M1, int *M1len)
429
++		       BIGNUM *A, BIGNUM *B,
430
++                       unsigned char *K, unsigned int Klen,
431
++		       char *I, char *L,
432
++                       unsigned char *M1, unsigned int *M1len)
433
+ {
434
+-    int r, i, len;
435
++    int r;
436
++    unsigned int i, len;
437
+     unsigned char Nhash[EVP_MAX_MD_SIZE];
438
+     unsigned char ghash[EVP_MAX_MD_SIZE];
439
+     unsigned char Ng[EVP_MAX_MD_SIZE];
440
+@@ -1010,9 +1067,10 @@ static int CalculateM1(context_t *text,
441
+ }
442
+ 
443
+ static int CalculateM2(context_t *text, BIGNUM *A,
444
+-		       char *M1, int M1len, char *K, int Klen,
445
++		       unsigned char *M1, unsigned int M1len,
446
++                       unsigned char *K, unsigned int Klen,
447
+ 		       char *I, char *o, char *sid, uint32 ttl,
448
+-		       char *M2, int *M2len)
449
++		       unsigned char *M2, unsigned int *M2len)
450
+ {
451
+     int r;
452
+     
453
+@@ -1386,7 +1444,8 @@ static int SetMDA(srp_options_t *opts, c
454
+  * Setup the selected security layer.
455
+  */
456
+ static int LayerInit(srp_options_t *opts, context_t *text,
457
+-		     sasl_out_params_t *oparams, char *enc_IV, char *dec_IV,
458
++		     sasl_out_params_t *oparams,
459
++                     unsigned char *enc_IV, unsigned char *dec_IV,
460
+ 		     unsigned maxbufsize)
461
+ {
462
+     layer_option_t *opt;
463
+@@ -1431,8 +1490,10 @@ static int LayerInit(srp_options_t *opts
464
+ 
465
+ 	/* Initialize the HMACs */
466
+ 	text->hmac_md = EVP_get_digestbyname(opt->evp_name);
467
+-	HMAC_Init(&text->hmac_send_ctx, text->K, text->Klen, text->hmac_md);
468
+-	HMAC_Init(&text->hmac_recv_ctx, text->K, text->Klen, text->hmac_md);
469
++        text->hmac_send_ctx = HMAC_CTX_new();
470
++	HMAC_Init_ex(text->hmac_send_ctx, text->K, text->Klen, text->hmac_md, NULL);
471
++        text->hmac_recv_ctx = HMAC_CTX_new();
472
++	HMAC_Init_ex(text->hmac_recv_ctx, text->K, text->Klen, text->hmac_md, NULL);
473
+ 	
474
+ 	/* account for HMAC */
475
+ 	oparams->maxoutbuf -= EVP_MD_size(text->hmac_md);
476
+@@ -1456,11 +1517,13 @@ static int LayerInit(srp_options_t *opts
477
+ 	/* Initialize the ciphers */
478
+ 	text->cipher = EVP_get_cipherbyname(opt->evp_name);
479
+ 
480
+-	EVP_CIPHER_CTX_init(&text->cipher_enc_ctx);
481
+-	EVP_EncryptInit(&text->cipher_enc_ctx, text->cipher, text->K, enc_IV);
482
+-
483
+-	EVP_CIPHER_CTX_init(&text->cipher_dec_ctx);
484
+-	EVP_DecryptInit(&text->cipher_dec_ctx, text->cipher, text->K, dec_IV);
485
++        text->cipher_enc_ctx = EVP_CIPHER_CTX_new();
486
++	EVP_CIPHER_CTX_init(text->cipher_enc_ctx);
487
++	EVP_EncryptInit(text->cipher_enc_ctx, text->cipher, text->K, enc_IV);
488
++
489
++        text->cipher_dec_ctx = EVP_CIPHER_CTX_new();
490
++	EVP_CIPHER_CTX_init(text->cipher_dec_ctx);
491
++	EVP_DecryptInit(text->cipher_dec_ctx, text->cipher, text->K, dec_IV);
492
+     }
493
+     
494
+     return SASL_OK;
495
+@@ -1469,13 +1532,13 @@ static int LayerInit(srp_options_t *opts
496
+ static void LayerCleanup(context_t *text)
497
+ {
498
+     if (text->layer & BIT_INTEGRITY) {
499
+-	HMAC_cleanup(&text->hmac_send_ctx);
500
+-	HMAC_cleanup(&text->hmac_recv_ctx);
501
++	HMAC_CTX_free(text->hmac_send_ctx);
502
++	HMAC_CTX_free(text->hmac_recv_ctx);
503
+     }
504
+ 
505
+     if (text->layer & BIT_CONFIDENTIALITY) {
506
+-	EVP_CIPHER_CTX_cleanup(&text->cipher_enc_ctx);
507
+-	EVP_CIPHER_CTX_cleanup(&text->cipher_dec_ctx);
508
++	EVP_CIPHER_CTX_free(text->cipher_enc_ctx);
509
++	EVP_CIPHER_CTX_free(text->cipher_dec_ctx);
510
+     }
511
+ }
512
+     
513
+@@ -1490,13 +1553,13 @@ static void srp_common_mech_dispose(void
514
+     
515
+     if (!text) return;
516
+     
517
+-    BN_clear_free(&text->N);
518
+-    BN_clear_free(&text->g);
519
+-    BN_clear_free(&text->v);
520
+-    BN_clear_free(&text->b);
521
+-    BN_clear_free(&text->B);
522
+-    BN_clear_free(&text->a);
523
+-    BN_clear_free(&text->A);
524
++    BN_clear_free(text->N);
525
++    BN_clear_free(text->g);
526
++    BN_clear_free(text->v);
527
++    BN_clear_free(text->b);
528
++    BN_clear_free(text->B);
529
++    BN_clear_free(text->a);
530
++    BN_clear_free(text->A);
531
+     
532
+     if (text->authid)		utils->free(text->authid);
533
+     if (text->userid)		utils->free(text->userid);
534
+@@ -1534,16 +1597,16 @@ srp_common_mech_free(void *global_contex
535
+  *
536
+  * All arithmetic is done modulo N
537
+  */
538
+-static int generate_N_and_g(BIGNUM *N, BIGNUM *g)
539
++static int generate_N_and_g(BIGNUM **N, BIGNUM **g)
540
+ {
541
+     int result;
542
+-    
543
+-    BN_init(N);
544
+-    result = BN_hex2bn(&N, Ng_tab[NUM_Ng-1].N);
545
++
546
++    *N = BN_new();
547
++    result = BN_hex2bn(N, Ng_tab[NUM_Ng-1].N);
548
+     if (!result) return SASL_FAIL;
549
+     
550
+-    BN_init(g);
551
+-    BN_set_word(g, Ng_tab[NUM_Ng-1].g);
552
++    *g = BN_new();
553
++    BN_set_word(*g, Ng_tab[NUM_Ng-1].g);
554
+     
555
+     return SASL_OK;
556
+ }
557
+@@ -1551,10 +1614,10 @@ static int generate_N_and_g(BIGNUM *N, B
558
+ static int CalculateV(context_t *text,
559
+ 		      BIGNUM *N, BIGNUM *g,
560
+ 		      const char *user,
561
+-		      const char *pass, unsigned passlen,
562
+-		      BIGNUM *v, char **salt, int *saltlen)
563
++		      const unsigned char *pass, unsigned passlen,
564
++		      BIGNUM **v, char **salt, int *saltlen)
565
+ {
566
+-    BIGNUM x;
567
++    BIGNUM *x = NULL;
568
+     BN_CTX *ctx = BN_CTX_new();
569
+     int r;
570
+     
571
+@@ -1572,40 +1635,41 @@ static int CalculateV(context_t *text,
572
+     }
573
+     
574
+     /* v = g^x % N */
575
+-    BN_init(v);
576
+-    BN_mod_exp(v, g, &x, N, ctx);
577
++    *v = BN_new();
578
++    BN_mod_exp(*v, g, x, N, ctx);
579
+     
580
+     BN_CTX_free(ctx);
581
+-    BN_clear_free(&x);
582
++    BN_clear_free(x);
583
+     
584
+     return r;   
585
+ }
586
+ 
587
+ static int CalculateB(context_t *text  __attribute__((unused)),
588
+-		      BIGNUM *v, BIGNUM *N, BIGNUM *g, BIGNUM *b, BIGNUM *B)
589
++		      BIGNUM *v, BIGNUM *N, BIGNUM *g, BIGNUM **b, BIGNUM **B)
590
+ {
591
+-    BIGNUM v3;
592
++    BIGNUM *v3 = BN_new();
593
+     BN_CTX *ctx = BN_CTX_new();
594
+     
595
+     /* Generate b */
596
+     GetRandBigInt(b);
597
+ 	
598
+     /* Per [SRP]: make sure b > log[g](N) -- g is always 2 */
599
+-    BN_add_word(b, BN_num_bits(N));
600
++    BN_add_word(*b, BN_num_bits(N));
601
+ 	
602
+     /* B = (3v + g^b) % N */
603
+-    BN_init(&v3);
604
+-    BN_set_word(&v3, 3);
605
+-    BN_mod_mul(&v3, &v3, v, N, ctx);
606
+-    BN_init(B);
607
+-    BN_mod_exp(B, g, b, N, ctx);
608
++    BN_set_word(v3, 3);
609
++    BN_mod_mul(v3, v3, v, N, ctx);
610
++
611
++    *B = BN_new();
612
++    BN_mod_exp(*B, g, *b, N, ctx);
613
+ #if OPENSSL_VERSION_NUMBER >= 0x00907000L
614
+-    BN_mod_add(B, B, &v3, N, ctx);
615
++    BN_mod_add(*B, *B, v3, N, ctx);
616
+ #else
617
+-    BN_add(B, B, &v3);
618
+-    BN_mod(B, B, N, ctx);
619
++    BN_add(*B, *B, v3);
620
++    BN_mod(*B, *B, N, ctx);
621
+ #endif
622
+ 
623
++    BN_clear_free(v3);
624
+     BN_CTX_free(ctx);
625
+     
626
+     return SASL_OK;
627
+@@ -1613,13 +1677,13 @@ static int CalculateB(context_t *text  _
628
+ 	
629
+ static int ServerCalculateK(context_t *text, BIGNUM *v,
630
+ 			    BIGNUM *N, BIGNUM *A, BIGNUM *b, BIGNUM *B,
631
+-			    char *K, int *Klen)
632
++			    unsigned char *K, unsigned int *Klen)
633
+ {
634
+     unsigned char hash[EVP_MAX_MD_SIZE];
635
+-    int hashlen;
636
+-    BIGNUM u;
637
+-    BIGNUM base;
638
+-    BIGNUM S;
639
++    unsigned int hashlen;
640
++    BIGNUM *u = BN_new();
641
++    BIGNUM *base = BN_new();
642
++    BIGNUM *S = BN_new();
643
+     BN_CTX *ctx = BN_CTX_new();
644
+     int r;
645
+     
646
+@@ -1627,50 +1691,47 @@ static int ServerCalculateK(context_t *t
647
+     r = MakeHash(text->md, hash, &hashlen, "%m%m", A, B);
648
+     if (r) return r;
649
+ 	
650
+-    BN_init(&u);
651
+-    BN_bin2bn(hash, hashlen, &u);
652
++    BN_bin2bn(hash, hashlen, u);
653
+ 	
654
+     /* S = (Av^u) ^ b % N */
655
+-    BN_init(&base);
656
+-    BN_mod_exp(&base, v, &u, N, ctx);
657
+-    BN_mod_mul(&base, &base, A, N, ctx);
658
++    BN_mod_exp(base, v, u, N, ctx);
659
++    BN_mod_mul(base, base, A, N, ctx);
660
+     
661
+-    BN_init(&S);
662
+-    BN_mod_exp(&S, &base, b, N, ctx);
663
++    BN_mod_exp(S, base, b, N, ctx);
664
+     
665
+     /* per Tom Wu: make sure Av^u != 1 (mod N) */
666
+-    if (BN_is_one(&base)) {
667
++    if (BN_is_one(base)) {
668
+ 	SETERROR(text->utils, "Unsafe SRP value for 'Av^u'\n");
669
+ 	r = SASL_BADPROT;
670
+ 	goto err;
671
+     }
672
+     
673
+     /* per Tom Wu: make sure Av^u != -1 (mod N) */
674
+-    BN_add_word(&base, 1);
675
+-    if (BN_cmp(&S, N) == 0) {
676
++    BN_add_word(base, 1);
677
++    if (BN_cmp(S, N) == 0) {
678
+ 	SETERROR(text->utils, "Unsafe SRP value for 'Av^u'\n");
679
+ 	r = SASL_BADPROT;
680
+ 	goto err;
681
+     }
682
+     
683
+     /* K = H(S) */
684
+-    r = MakeHash(text->md, K, Klen, "%m", &S);
685
++    r = MakeHash(text->md, K, Klen, "%m", S);
686
+     if (r) goto err;
687
+     
688
+     r = SASL_OK;
689
+     
690
+   err:
691
+     BN_CTX_free(ctx);
692
+-    BN_clear_free(&u);
693
+-    BN_clear_free(&base);
694
+-    BN_clear_free(&S);
695
++    BN_clear_free(u);
696
++    BN_clear_free(base);
697
++    BN_clear_free(S);
698
+     
699
+     return r;
700
+ }
701
+ 
702
+ static int ParseUserSecret(const sasl_utils_t *utils,
703
+ 			   char *secret, size_t seclen,
704
+-			   char **mda, BIGNUM *v, char **salt, int *saltlen)
705
++			   char **mda, BIGNUM **v, char **salt, int *saltlen)
706
+ {
707
+     int r;
708
+     
709
+@@ -1678,7 +1739,7 @@ static int ParseUserSecret(const sasl_ut
710
+      *
711
+      *  { utf8(mda) mpi(v) os(salt) }  (base64 encoded)
712
+      */
713
+-    r = utils->decode64(secret, seclen, secret, seclen, &seclen);
714
++    r = utils->decode64(secret, seclen, secret, seclen, (unsigned *) &seclen);
715
+ 
716
+     if (!r)
717
+ 	r = UnBuffer(utils, secret, seclen, "%s%m%o", mda, v, saltlen, salt);
718
+@@ -1919,8 +1980,8 @@ static int srp_server_mech_step1(context
719
+ 	    goto cleanup;
720
+ 	}
721
+ 	
722
+-	result = CalculateV(text, &text->N, &text->g, text->authid,
723
+-			    auxprop_values[1].values[0], len,
724
++	result = CalculateV(text, text->N, text->g, text->authid,
725
++			    (unsigned char *) auxprop_values[1].values[0], len,
726
+ 			    &text->v, &text->salt, &text->saltlen);
727
+ 	if (result) {
728
+ 	    params->utils->seterror(params->utils->conn, 0, 
729
+@@ -1938,8 +1999,7 @@ static int srp_server_mech_step1(context
730
+     params->utils->prop_erase(params->propctx, password_request[1]);
731
+     
732
+     /* Calculate B */
733
+-    result = CalculateB(text, &text->v, &text->N, &text->g,
734
+-			&text->b, &text->B);
735
++    result = CalculateB(text, text->v, text->N, text->g, &text->b, &text->B);
736
+     if (result) {
737
+ 	params->utils->seterror(params->utils->conn, 0, 
738
+ 				"Error calculating B");
739
+@@ -1967,8 +2027,8 @@ static int srp_server_mech_step1(context
740
+      */
741
+     result = MakeBuffer(text->utils, &text->out_buf, &text->out_buf_len,
742
+ 			serveroutlen, "%c%m%m%o%m%s",
743
+-			0x00, &text->N, &text->g, text->saltlen, text->salt,
744
+-			&text->B, text->server_options);
745
++			0x00, text->N, text->g, text->saltlen, text->salt,
746
++			text->B, text->server_options);
747
+     if (result) {
748
+ 	params->utils->seterror(params->utils->conn, 0, 
749
+ 				"Error creating SRP buffer from data in step 1");
750
+@@ -1997,15 +2057,15 @@ static int srp_server_mech_step2(context
751
+ 			sasl_out_params_t *oparams)
752
+ {
753
+     int result;    
754
+-    char *M1 = NULL, *cIV = NULL; /* don't free */
755
+-    int M1len, cIVlen;
756
++    unsigned char *M1 = NULL, *cIV = NULL; /* don't free */
757
++    unsigned int M1len, cIVlen;
758
+     srp_options_t client_opts;
759
+-    char myM1[EVP_MAX_MD_SIZE];
760
+-    int myM1len;
761
+-    int i;
762
+-    char M2[EVP_MAX_MD_SIZE];
763
+-    int M2len;
764
+-    char sIV[SRP_MAXBLOCKSIZE];
765
++    unsigned char myM1[EVP_MAX_MD_SIZE];
766
++    unsigned int myM1len;
767
++    unsigned int i;
768
++    unsigned char M2[EVP_MAX_MD_SIZE];
769
++    unsigned int M2len;
770
++    unsigned char sIV[SRP_MAXBLOCKSIZE];
771
+     
772
+     /* Expect:
773
+      *
774
+@@ -2027,7 +2087,7 @@ static int srp_server_mech_step2(context
775
+     }
776
+     
777
+     /* Per [SRP]: reject A <= 0 */
778
+-    if (BigIntCmpWord(&text->A, 0) <= 0) {
779
++    if (BigIntCmpWord(text->A, 0) <= 0) {
780
+ 	SETERROR(params->utils, "Illegal value for 'A'\n");
781
+ 	result = SASL_BADPROT;
782
+ 	goto cleanup;
783
+@@ -2058,8 +2118,8 @@ static int srp_server_mech_step2(context
784
+     }
785
+ 
786
+     /* Calculate K */
787
+-    result = ServerCalculateK(text, &text->v, &text->N, &text->A,
788
+-			      &text->b, &text->B, text->K, &text->Klen);
789
++    result = ServerCalculateK(text, text->v, text->N, text->A,
790
++			      text->b, text->B, text->K, &text->Klen);
791
+     if (result) {
792
+ 	params->utils->seterror(params->utils->conn, 0, 
793
+ 				"Error calculating K");
794
+@@ -2067,8 +2127,8 @@ static int srp_server_mech_step2(context
795
+     }
796
+     
797
+     /* See if M1 is correct */
798
+-    result = CalculateM1(text, &text->N, &text->g, text->authid,
799
+-			 text->salt, text->saltlen, &text->A, &text->B,
800
++    result = CalculateM1(text, text->N, text->g, text->authid,
801
++			 text->salt, text->saltlen, text->A, text->B,
802
+ 			 text->K, text->Klen, text->userid,
803
+ 			 text->server_options, myM1, &myM1len);
804
+     if (result) {
805
+@@ -2095,7 +2155,7 @@ static int srp_server_mech_step2(context
806
+     }
807
+     
808
+     /* calculate M2 to send */
809
+-    result = CalculateM2(text, &text->A, M1, M1len, text->K, text->Klen,
810
++    result = CalculateM2(text, text->A, M1, M1len, text->K, text->Klen,
811
+ 			 text->userid, text->client_options, "", 0,
812
+ 			 M2, &M2len);
813
+     if (result) {
814
+@@ -2105,7 +2165,7 @@ static int srp_server_mech_step2(context
815
+     }
816
+     
817
+     /* Create sIV (server initial vector) */
818
+-    text->utils->rand(text->utils->rpool, sIV, sizeof(sIV));
819
++    text->utils->rand(text->utils->rpool, (char *) sIV, sizeof(sIV));
820
+     
821
+     /*
822
+      * Send out:
823
+@@ -2230,20 +2290,20 @@ static int srp_setpass(void *glob_contex
824
+     r = _plug_make_fulluser(sparams->utils, &user, user_only, realm);
825
+ 
826
+     if (r) {
827
+-	goto end;
828
++	goto cleanup;
829
+     }
830
+ 
831
+     if ((flags & SASL_SET_DISABLE) || pass == NULL) {
832
+ 	sec = NULL;
833
+     } else {
834
+-	context_t *text;
835
+-	BIGNUM N;
836
+-	BIGNUM g;
837
+-	BIGNUM v;
838
++	context_t *text = NULL;
839
++	BIGNUM *N = NULL;
840
++	BIGNUM *g = NULL;
841
++	BIGNUM *v = NULL;
842
+ 	char *salt;
843
+ 	int saltlen;
844
+ 	char *buffer = NULL;
845
+-	int bufferlen, alloclen, encodelen;
846
++	unsigned int bufferlen, alloclen, encodelen;
847
+ 	
848
+ 	text = sparams->utils->malloc(sizeof(context_t));
849
+ 	if (text == NULL) {
850
+@@ -2264,7 +2324,8 @@ static int srp_setpass(void *glob_contex
851
+ 	}
852
+ 
853
+ 	/* user is a full username here */
854
+-	r = CalculateV(text, &N, &g, user, pass, passlen, &v, &salt, &saltlen);
855
++	r = CalculateV(text, N, g, user,
856
++                       (unsigned char *) pass, passlen, &v, &salt, &saltlen);
857
+ 	if (r) {
858
+ 	    sparams->utils->seterror(sparams->utils->conn, 0, 
859
+ 				     "Error calculating v");
860
+@@ -2296,16 +2357,16 @@ static int srp_setpass(void *glob_contex
861
+ 	    r = SASL_NOMEM;
862
+ 	    goto end;
863
+ 	}
864
+-	sparams->utils->encode64(buffer, bufferlen, sec->data, alloclen,
865
++	sparams->utils->encode64(buffer, bufferlen, (char *) sec->data, alloclen,
866
+ 				 &encodelen);
867
+ 	sec->len = encodelen;
868
+ 	
869
+ 	/* Clean everything up */
870
+       end:
871
+ 	if (buffer) sparams->utils->free((void *) buffer);
872
+-	BN_clear_free(&N);
873
+-	BN_clear_free(&g);
874
+-	BN_clear_free(&v);
875
++	BN_clear_free(N);
876
++	BN_clear_free(g);
877
++	BN_clear_free(v);
878
+ 	sparams->utils->free(text);
879
+ 	
880
+ 	if (r) return r;
881
+@@ -2319,7 +2380,7 @@ static int srp_setpass(void *glob_contex
882
+ 	r = sparams->utils->prop_request(propctx, store_request);
883
+     if (!r)
884
+ 	r = sparams->utils->prop_set(propctx, "cmusaslsecretSRP",
885
+-				     (sec ? sec->data : NULL),
886
++				     (char *) (sec ? sec->data : NULL),
887
+ 				     (sec ? sec->len : 0));
888
+     if (!r)
889
+ 	r = sparams->utils->auxprop_store(sparams->utils->conn, propctx, user);
890
+@@ -2475,7 +2536,7 @@ static int check_N_and_g(const sasl_util
891
+ }
892
+ 
893
+ static int CalculateA(context_t *text  __attribute__((unused)),
894
+-		      BIGNUM *N, BIGNUM *g, BIGNUM *a, BIGNUM *A)
895
++		      BIGNUM *N, BIGNUM *g, BIGNUM **a, BIGNUM **A)
896
+ {
897
+     BN_CTX *ctx = BN_CTX_new();
898
+     
899
+@@ -2483,11 +2544,11 @@ static int CalculateA(context_t *text  _
900
+     GetRandBigInt(a);
901
+ 	
902
+     /* Per [SRP]: make sure a > log[g](N) -- g is always 2 */
903
+-    BN_add_word(a, BN_num_bits(N));
904
++    BN_add_word(*a, BN_num_bits(N));
905
+ 	
906
+     /* A = g^a % N */
907
+-    BN_init(A);
908
+-    BN_mod_exp(A, g, a, N, ctx);
909
++    *A = BN_new();
910
++    BN_mod_exp(*A, g, *a, N, ctx);
911
+ 
912
+     BN_CTX_free(ctx);
913
+     
914
+@@ -2495,30 +2556,30 @@ static int CalculateA(context_t *text  _
915
+ }
916
+ 	
917
+ static int ClientCalculateK(context_t *text, char *salt, int saltlen,
918
+-			    char *user, char *pass, int passlen,
919
++			    char *user, unsigned char *pass, int passlen,
920
+ 			    BIGNUM *N, BIGNUM *g, BIGNUM *a, BIGNUM *A,
921
+-			    BIGNUM *B, char *K, int *Klen)
922
++			    BIGNUM *B, unsigned char *K, unsigned int *Klen)
923
+ {
924
+     int r;
925
+     unsigned char hash[EVP_MAX_MD_SIZE];
926
+-    int hashlen;
927
+-    BIGNUM x;
928
+-    BIGNUM u;
929
+-    BIGNUM aux;
930
+-    BIGNUM gx;
931
+-    BIGNUM gx3;
932
+-    BIGNUM base;
933
+-    BIGNUM S;
934
++    unsigned int hashlen;
935
++    BIGNUM *x = NULL;
936
++    BIGNUM *u = BN_new();
937
++    BIGNUM *aux = BN_new();
938
++    BIGNUM *gx = BN_new();
939
++    BIGNUM *gx3 = BN_new();
940
++    BIGNUM *base = BN_new();
941
++    BIGNUM *S = BN_new();
942
+     BN_CTX *ctx = BN_CTX_new();
943
+     
944
+     /* u = H(A | B) */
945
+     r = MakeHash(text->md, hash, &hashlen, "%m%m", A, B);
946
+     if (r) goto err;
947
+-    BN_init(&u);
948
+-    BN_bin2bn(hash, hashlen, &u);
949
++    u = BN_new();
950
++    BN_bin2bn(hash, hashlen, u);
951
+     
952
+     /* per Tom Wu: make sure u != 0 */
953
+-    if (BN_is_zero(&u)) {
954
++    if (BN_is_zero(u)) {
955
+ 	SETERROR(text->utils, "SRP: Illegal value for 'u'\n");
956
+ 	r = SASL_BADPROT;
957
+ 	goto err;
958
+@@ -2530,48 +2591,43 @@ static int ClientCalculateK(context_t *t
959
+     if (r) return r;
960
+     
961
+     /* a + ux */
962
+-    BN_init(&aux);
963
+-    BN_mul(&aux, &u, &x, ctx);
964
+-    BN_add(&aux, &aux, a);
965
++    BN_mul(aux, u, x, ctx);
966
++    BN_add(aux, aux, a);
967
+     
968
+     /* gx3 = 3(g^x) % N */
969
+-    BN_init(&gx);
970
+-    BN_mod_exp(&gx, g, &x, N, ctx);
971
+-    BN_init(&gx3);
972
+-    BN_set_word(&gx3, 3);
973
+-    BN_mod_mul(&gx3, &gx3, &gx, N, ctx);
974
++    BN_mod_exp(gx, g, x, N, ctx);
975
++    BN_set_word(gx3, 3);
976
++    BN_mod_mul(gx3, gx3, gx, N, ctx);
977
+     
978
+     /* base = (B - 3(g^x)) % N */
979
+-    BN_init(&base);
980
+ #if OPENSSL_VERSION_NUMBER >= 0x00907000L
981
+-    BN_mod_sub(&base, B, &gx3, N, ctx);
982
++    BN_mod_sub(base, B, gx3, N, ctx);
983
+ #else
984
+-    BN_sub(&base, B, &gx3);
985
+-    BN_mod(&base, &base, N, ctx);
986
+-    if (BigIntCmpWord(&base, 0) < 0) {
987
+-	BN_add(&base, &base, N);
988
++    BN_sub(base, B, gx3);
989
++    BN_mod(base, base, N, ctx);
990
++    if (BigIntCmpWord(base, 0) < 0) {
991
++	BN_add(base, base, N);
992
+     }
993
+ #endif
994
+     
995
+     /* S = base^aux % N */
996
+-    BN_init(&S);
997
+-    BN_mod_exp(&S, &base, &aux, N, ctx);
998
++    BN_mod_exp(S, base, aux, N, ctx);
999
+     
1000
+     /* K = H(S) */
1001
+-    r = MakeHash(text->md, K, Klen, "%m", &S);
1002
++    r = MakeHash(text->md, K, Klen, "%m", S);
1003
+     if (r) goto err;
1004
+     
1005
+     r = SASL_OK;
1006
+     
1007
+   err:
1008
+     BN_CTX_free(ctx);
1009
+-    BN_clear_free(&x);
1010
+-    BN_clear_free(&u);
1011
+-    BN_clear_free(&aux);
1012
+-    BN_clear_free(&gx);
1013
+-    BN_clear_free(&gx3);
1014
+-    BN_clear_free(&base);
1015
+-    BN_clear_free(&S);
1016
++    BN_clear_free(x);
1017
++    BN_clear_free(u);
1018
++    BN_clear_free(aux);
1019
++    BN_clear_free(gx);
1020
++    BN_clear_free(gx3);
1021
++    BN_clear_free(base);
1022
++    BN_clear_free(S);
1023
+     
1024
+     return r;
1025
+ }
1026
+@@ -2709,7 +2765,7 @@ static int srp_client_mech_new(void *glo
1027
+     }
1028
+     
1029
+     memset(text, 0, sizeof(context_t));
1030
+-    
1031
++
1032
+     text->state = 1;
1033
+     text->utils = params->utils;
1034
+ 
1035
+@@ -2866,7 +2922,7 @@ srp_client_mech_step2(context_t *text,
1036
+     }
1037
+ 
1038
+     /* Check N and g to see if they are one of the recommended pairs */
1039
+-    result = check_N_and_g(params->utils, &text->N, &text->g);
1040
++    result = check_N_and_g(params->utils, text->N, text->g);
1041
+     if (result) {
1042
+ 	params->utils->log(NULL, SASL_LOG_ERR,
1043
+ 			   "Values of 'N' and 'g' are not recommended\n");
1044
+@@ -2874,7 +2930,7 @@ srp_client_mech_step2(context_t *text,
1045
+     }
1046
+     
1047
+     /* Per [SRP]: reject B <= 0, B >= N */
1048
+-    if (BigIntCmpWord(&text->B, 0) <= 0 || BN_cmp(&text->B, &text->N) >= 0) {
1049
++    if (BigIntCmpWord(text->B, 0) <= 0 || BN_cmp(text->B, text->N) >= 0) {
1050
+ 	SETERROR(params->utils, "Illegal value for 'B'\n");
1051
+ 	result = SASL_BADPROT;
1052
+ 	goto cleanup;
1053
+@@ -2913,7 +2969,7 @@ srp_client_mech_step2(context_t *text,
1054
+     }
1055
+ 
1056
+     /* Calculate A */
1057
+-    result = CalculateA(text, &text->N, &text->g, &text->a, &text->A);
1058
++    result = CalculateA(text, text->N, text->g, &text->a, &text->A);
1059
+     if (result) {
1060
+ 	params->utils->seterror(params->utils->conn, 0, 
1061
+ 				"Error calculating A");
1062
+@@ -2924,7 +2980,7 @@ srp_client_mech_step2(context_t *text,
1063
+     result = ClientCalculateK(text, text->salt, text->saltlen,
1064
+ 			      (char *) oparams->authid, 
1065
+ 			      text->password->data, text->password->len,
1066
+-			      &text->N, &text->g, &text->a, &text->A, &text->B,
1067
++			      text->N, text->g, text->a, text->A, text->B,
1068
+ 			      text->K, &text->Klen);
1069
+     if (result) {
1070
+ 	params->utils->log(NULL, SASL_LOG_ERR,
1071
+@@ -2933,8 +2989,8 @@ srp_client_mech_step2(context_t *text,
1072
+     }
1073
+     
1074
+     /* Calculate M1 (client evidence) */
1075
+-    result = CalculateM1(text, &text->N, &text->g, (char *) oparams->authid,
1076
+-			 text->salt, text->saltlen, &text->A, &text->B,
1077
++    result = CalculateM1(text, text->N, text->g, (char *) oparams->authid,
1078
++			 text->salt, text->saltlen, text->A, text->B,
1079
+ 			 text->K, text->Klen, (char *) oparams->user,
1080
+ 			 text->server_options, text->M1, &text->M1len);
1081
+     if (result) {
1082
+@@ -2944,7 +3000,7 @@ srp_client_mech_step2(context_t *text,
1083
+     }
1084
+ 
1085
+     /* Create cIV (client initial vector) */
1086
+-    text->utils->rand(text->utils->rpool, text->cIV, sizeof(text->cIV));
1087
++    text->utils->rand(text->utils->rpool, (char *) text->cIV, sizeof(text->cIV));
1088
+     
1089
+     /* Send out:
1090
+      *
1091
+@@ -2957,7 +3013,7 @@ srp_client_mech_step2(context_t *text,
1092
+      */
1093
+     result = MakeBuffer(text->utils, &text->out_buf, &text->out_buf_len,
1094
+ 			clientoutlen, "%m%o%s%o",
1095
+-			&text->A, text->M1len, text->M1, text->client_options,
1096
++			text->A, text->M1len, text->M1, text->client_options,
1097
+ 			sizeof(text->cIV), text->cIV);
1098
+     if (result) {
1099
+ 	params->utils->log(NULL, SASL_LOG_ERR, "Error making output buffer\n");
1100
+@@ -2985,13 +3041,13 @@ srp_client_mech_step3(context_t *text,
1101
+ 		      sasl_out_params_t *oparams)
1102
+ {
1103
+     int result;    
1104
+-    char *M2 = NULL, *sIV = NULL; /* don't free */
1105
++    unsigned char *M2 = NULL, *sIV = NULL; /* don't free */
1106
+     char *sid = NULL;
1107
+-    int M2len, sIVlen;
1108
++    unsigned int M2len, sIVlen;
1109
+     uint32 ttl;
1110
+-    int i;
1111
+-    char myM2[EVP_MAX_MD_SIZE];
1112
+-    int myM2len;
1113
++    unsigned int i;
1114
++    unsigned char myM2[EVP_MAX_MD_SIZE];
1115
++    unsigned int myM2len;
1116
+     
1117
+     /* Expect:
1118
+      *
1119
+@@ -3012,7 +3068,7 @@ srp_client_mech_step3(context_t *text,
1120
+     }
1121
+ 
1122
+     /* calculate our own M2 */
1123
+-    result = CalculateM2(text, &text->A, text->M1, text->M1len,
1124
++    result = CalculateM2(text, text->A, text->M1, text->M1len,
1125
+ 			 text->K, text->Klen, (char *) oparams->user,
1126
+ 			 text->client_options, "", 0,
1127
+ 			 myM2, &myM2len);
1128
+@@ -3182,3 +3238,4 @@ int srp_client_plug_init(const sasl_util
1129
+     
1130
+     return SASL_OK;
1131
+ }
1132
++
... ...
@@ -1,7 +1,7 @@
1 1
 Summary:	Cyrus Simple Authentication Service Layer (SASL) library
2 2
 Name:		cyrus-sasl
3 3
 Version:	2.1.26
4
-Release:	8%{?dist}
4
+Release:	9%{?dist}
5 5
 License:	Custom
6 6
 URL:		http://cyrusimap.web.cmu.edu/
7 7
 Group:		System Environment/Security
... ...
@@ -10,13 +10,14 @@ Distribution: 	Photon
10 10
 Source0:	ftp://ftp.cyrusimap.org/cyrus-sasl/%{name}-%{version}.tar.gz
11 11
 %define sha1 cyrus-sasl=d6669fb91434192529bd13ee95737a8a5040241c
12 12
 Patch0:		http://www.linuxfromscratch.org/patches/blfs/svn/cyrus-sasl-2.1.26-fixes-3.patch
13
+Patch1:         cyrus-sasl-mem-leak-fix.patch
13 14
 BuildRequires:  systemd
14 15
 BuildRequires:	openssl-devel
15 16
 BuildRequires:  krb5 >= 1.12
16 17
 BuildRequires:  e2fsprogs-devel
17 18
 BuildRequires:  Linux-PAM
18
-Requires:   openssl
19
-Requires:   krb5 >= 1.12
19
+Requires:       openssl
20
+Requires:       krb5 >= 1.12
20 21
 Requires:       Linux-PAM
21 22
 Requires:       systemd
22 23
 %description
... ...
@@ -30,6 +31,7 @@ protocol and the connection.
30 30
 %prep
31 31
 %setup -q
32 32
 %patch0 -p1
33
+%patch1 -p1
33 34
 %build
34 35
 autoreconf -fi
35 36
 pushd saslauthd
... ...
@@ -126,19 +128,21 @@ rm -rf %{buildroot}/*
126 126
 %{_datadir}/licenses/%{name}/LICENSE
127 127
 %{_mandir}/man8/saslauthd.8.gz
128 128
 %changelog
129
+*   Tue Oct 10 2017 Anish Swaminathan <anishs@vmware.com>  2.1.26-9
130
+-   Add patch for memory leak fix
129 131
 *   Thu May 26 2016 Divya Thaluru <dthaluru@vmware.com>  2.1.26-8
130 132
 -   Fixed logic to restart the active services after upgrade 
131
-*	Tue May 24 2016 Priyesh Padmavilasom <ppadmavilasom@vmware.com> 2.1.26-7
132
--	GA - Bump release of all rpms
133
+*   Tue May 24 2016 Priyesh Padmavilasom <ppadmavilasom@vmware.com> 2.1.26-7
134
+-   GA - Bump release of all rpms
133 135
 *   Tue May 3 2016 Divya Thaluru <dthaluru@vmware.com>  2.1.26-6
134 136
 -   Fixing spec file to handle rpm upgrade scenario correctly
135 137
 *   Thu Dec 10 2015 Xiaolin Li <xiaolinl@vmware.com>  2.1.26-5
136 138
 -   Add systemd to Requires and BuildRequires.
137 139
 *   Wed Nov 11 2015 Xiaolin Li <xiaolinl@vmware.com> 2.1.26-4
138 140
 -   Add saslauthd service to systemd.
139
-*	Tue Sep 01 2015 Vinay Kulkarni <kulkarniv@vmware.com> 2.1.26-3
140
--	Enable CRAM.
141
-*	Thu Jul 16 2015 Divya Thaluru <dthaluru@vmware.com> 2.1.26-2
142
--	Disabling parallel threads in make
143
-*	Wed Nov 5 2014 Divya Thaluru <dthaluru@vmware.com> 2.1.26-1
144
--	Initial build.	First version
141
+*   Tue Sep 01 2015 Vinay Kulkarni <kulkarniv@vmware.com> 2.1.26-3
142
+-   Enable CRAM.
143
+*   Thu Jul 16 2015 Divya Thaluru <dthaluru@vmware.com> 2.1.26-2
144
+-   Disabling parallel threads in make
145
+*   Wed Nov 5 2014 Divya Thaluru <dthaluru@vmware.com> 2.1.26-1
146
+-   Initial build.	First version