aboutsummaryrefslogtreecommitdiffstats
path: root/ext/openssl/openssl_missing.c
blob: 2c953dd53b4c0c41c9ca851ce43aa107b881b8b9 (plain)
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
/*
 * 'OpenSSL for Ruby' project
 * Copyright (C) 2001-2002  Michal Rokos <m.rokos@sh.cvut.cz>
 * All rights reserved.
 */
/*
 * This program is licensed under the same licence as Ruby.
 * (See the file 'LICENCE'.)
 */
#include RUBY_EXTCONF_H

#if defined(HAVE_OPENSSL_ENGINE_H) && defined(HAVE_EVP_CIPHER_CTX_ENGINE)
# include <openssl/engine.h>
#endif
#include <openssl/x509_vfy.h>

#if !defined(OPENSSL_NO_HMAC)
#include <string.h> /* memcpy() */
#include <openssl/hmac.h>

#include "openssl_missing.h"

#if !defined(HAVE_HMAC_CTX_COPY)
void
HMAC_CTX_copy(HMAC_CTX *out, HMAC_CTX *in)
{
    if (!out || !in) return;
    memcpy(out, in, sizeof(HMAC_CTX));

    EVP_MD_CTX_copy(&out->md_ctx, &in->md_ctx);
    EVP_MD_CTX_copy(&out->i_ctx, &in->i_ctx);
    EVP_MD_CTX_copy(&out->o_ctx, &in->o_ctx);
}
#endif /* HAVE_HMAC_CTX_COPY */
#endif /* NO_HMAC */

#if !defined(HAVE_X509_STORE_SET_EX_DATA)
int X509_STORE_set_ex_data(X509_STORE *str, int idx, void *data)
{
    return CRYPTO_set_ex_data(&str->ex_data, idx, data);
}
#endif

#if !defined(HAVE_X509_STORE_GET_EX_DATA)
void *X509_STORE_get_ex_data(X509_STORE *str, int idx)
{
    return CRYPTO_get_ex_data(&str->ex_data, idx);
}
#endif

#if !defined(HAVE_EVP_MD_CTX_NEW)
/* new in 1.1.0 */
EVP_MD_CTX *
EVP_MD_CTX_new(void)
{
#if defined(HAVE_EVP_MD_CTX_CREATE)
    return EVP_MD_CTX_create();
#else /* 0.9.6 */
    EVP_MD_CTX *ctx = OPENSSL_malloc(sizeof(EVP_MD_CTX));
    if (!ctx)
	return NULL;
    memset(ctx, 0, sizeof(EVP_MD_CTX));
    return ctx;
#endif
}
#endif

#if !defined(HAVE_EVP_MD_CTX_FREE)
/* new in 1.1.0 */
void
EVP_MD_CTX_free(EVP_MD_CTX *ctx)
{
#if defined(HAVE_EVP_MD_CTX_DESTROY)
    EVP_MD_CTX_destroy(ctx);
#else /* 0.9.6 */
    /* EVP_MD_CTX_cleanup(ctx); */
    /* FIXME!!! */
    memset(ctx, 0, sizeof(EVP_MD_CTX));
    OPENSSL_free(ctx);
#endif
}
#endif

#if defined(HAVE_HMAC_INIT_EX)
int
HMAC_Init_ex(HMAC_CTX *ctx, const void *key, int key_len,
	     const EVP_MD *md, void *impl)
{
    if (impl)
	rb_bug("impl not supported");
    return HMAC_Init(ctx, key, key_len, md);
}
#endif

#if !defined(HAVE_HMAC_CTX_RESET)
#if !defined(HAVE_EVP_MD_CTX_INIT)
static void
EVP_MD_CTX_init(EVP_MD_CTX *ctx)
{
    memset(ctx, 0, sizeof(EVP_MD_CTX));
}
#endif

int
HMAC_CTX_reset(HMAC_CTX *ctx)
{
#if defined(HAVE_HMAC_CTX_INIT)
    HMAC_CTX_init(ctx);
#else /* 0.9.6 */
    EVP_MD_CTX_init(&ctx->i_ctx);
    EVP_MD_CTX_init(&ctx->o_ctx);
    EVP_MD_CTX_init(&ctx->md_ctx);
#endif
}
#endif

#if !defined(HAVE_HMAC_CTX_NEW)
/* new in 1.1.0 */
HMAC_CTX *
HMAC_CTX_new(void)
{
    HMAC_CTX *ctx = OPENSSL_malloc(sizeof(HMAC_CTX));
    HMAC_CTX_reset(ctx);
    if (!ctx)
	return NULL;
    return ctx;
}
#endif

#if !defined(HAVE_HMAC_CTX_FREE)
void
HMAC_CTX_free(HMAC_CTX *ctx)
{
#if defined(HAVE_HMAC_CTX_CLEANUP)
    HMAC_CTX_cleanup(ctx);
#else /* 0.9.6 */
    EVP_MD_CTX_cleanup(&ctx->i_ctx);
    EVP_MD_CTX_cleanup(&ctx->o_ctx);
    EVP_MD_CTX_cleanup(&ctx->md_ctx);
#endif
    OPENSSL_free(ctx);
}
#endif

#if !defined(HAVE_EVP_CIPHER_CTX_NEW)
/* new in 1.1.0 */
EVP_CIPHER_CTX *
EVP_CIPHER_CTX_new(void)
{
    EVP_CIPHER_CTX *ctx = OPENSSL_malloc(sizeof(EVP_CIPHER_CTX));
    if (!ctx)
	return NULL;
    EVP_CIPHER_CTX_init(ctx);
    return ctx;
}
#endif

#if !defined(HAVE_EVP_MD_CTX_FREE)
/* new in 1.1.0 */
void
EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx)
{
    EVP_CIPHER_CTX_cleanup(ctx); /* 0.9.6 also has */
    OPENSSL_free(ctx);
}
#endif

#if !defined(HAVE_EVP_CIPHER_CTX_COPY)
/*
 * this function does not exist in OpenSSL yet... or ever?.
 * a future version may break this function.
 * tested on 0.9.7d.
 */
int
EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX *out, EVP_CIPHER_CTX *in)
{
    memcpy(out, in, sizeof(EVP_CIPHER_CTX));

#if defined(HAVE_ENGINE_ADD) && defined(HAVE_EVP_CIPHER_CTX_ENGINE)
    if (in->engine) ENGINE_add(out->engine);
    if (in->cipher_data) {
	out->cipher_data = OPENSSL_malloc(in->cipher->ctx_size);
	memcpy(out->cipher_data, in->cipher_data, in->cipher->ctx_size);
    }
#endif

    return 1;
}
#endif

#if !defined(HAVE_X509_CRL_SET_VERSION)
int
X509_CRL_set_version(X509_CRL *x, long version)
{
    if (x == NULL || x->crl == NULL) return 0;
    if (x->crl->version == NULL) {
	x->crl->version = M_ASN1_INTEGER_new();
	if (x->crl->version == NULL) return 0;
    }
    return ASN1_INTEGER_set(x->crl->version, version);
}
#endif

#if !defined(HAVE_X509_CRL_SET_ISSUER_NAME)
int
X509_CRL_set_issuer_name(X509_CRL *x, X509_NAME *name)
{
    if (x == NULL || x->crl == NULL) return 0;
    return X509_NAME_set(&x->crl->issuer, name);
}
#endif

#if !defined(HAVE_X509_CRL_SORT)
int
X509_CRL_sort(X509_CRL *c)
{
    int i;
    X509_REVOKED *r;
    /* sort the data so it will be written in serial
     * number order */
    sk_X509_REVOKED_sort(c->crl->revoked);
    for (i=0; i<sk_X509_REVOKED_num(c->crl->revoked); i++) {
	r=sk_X509_REVOKED_value(c->crl->revoked, i);
	r->sequence=i;
    }
    return 1;
}
#endif

#if !defined(HAVE_X509_CRL_ADD0_REVOKED)
static int
OSSL_X509_REVOKED_cmp(const X509_REVOKED * const *a, const X509_REVOKED * const *b)
{
    return(ASN1_STRING_cmp(
		(ASN1_STRING *)(*a)->serialNumber,
		(ASN1_STRING *)(*b)->serialNumber));
}

int
X509_CRL_add0_revoked(X509_CRL *crl, X509_REVOKED *rev)
{
    X509_CRL_INFO *inf;

    inf = crl->crl;
    if (!inf->revoked)
	inf->revoked = sk_X509_REVOKED_new(OSSL_X509_REVOKED_cmp);
    if (!inf->revoked || !sk_X509_REVOKED_push(inf->revoked, rev))
	return 0;
    return 1;
}
#endif

#if !defined(HAVE_BN_MOD_SQR)
int
BN_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx)
{
    if (!BN_sqr(r, (BIGNUM*)a, ctx)) return 0;
    return BN_mod(r, r, m, ctx);
}
#endif

#if !defined(HAVE_BN_MOD_ADD) || !defined(HAVE_BN_MOD_SUB)
int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)
{
    if (!BN_mod(r,m,d,ctx)) return 0;
    if (!r->neg) return 1;
    return (d->neg ? BN_sub : BN_add)(r, r, d);
}
#endif

#if !defined(HAVE_BN_MOD_ADD)
int
BN_mod_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m, BN_CTX *ctx)
{
    if (!BN_add(r, a, b)) return 0;
    return BN_nnmod(r, r, m, ctx);
}
#endif

#if !defined(HAVE_BN_MOD_SUB)
int
BN_mod_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m, BN_CTX *ctx)
{
    if (!BN_sub(r, a, b)) return 0;
    return BN_nnmod(r, r, m, ctx);
}
#endif

#if !defined(HAVE_BN_RAND_RANGE) || !defined(HAVE_BN_PSEUDO_RAND_RANGE)
static int
bn_rand_range(int pseudo, BIGNUM *r, BIGNUM *range)
{
    int (*bn_rand)(BIGNUM *, int, int, int) = pseudo ? BN_pseudo_rand : BN_rand;
    int n;

    if (range->neg || BN_is_zero(range)) return 0;

    n = BN_num_bits(range);

    if (n == 1) {
	if (!BN_zero(r)) return 0;
    } else if (!BN_is_bit_set(range, n - 2) && !BN_is_bit_set(range, n - 3)) {
	do {
	    if (!bn_rand(r, n + 1, -1, 0)) return 0;
	    if (BN_cmp(r ,range) >= 0) {
		if (!BN_sub(r, r, range)) return 0;
		if (BN_cmp(r, range) >= 0)
		    if (!BN_sub(r, r, range)) return 0;
	    }
	} while (BN_cmp(r, range) >= 0);
    } else {
	do {
	    if (!bn_rand(r, n, -1, 0)) return 0;
	} while (BN_cmp(r, range) >= 0);
    }

    return 1;
}
#endif

#if !defined(HAVE_BN_RAND_RANGE)
int
BN_rand_range(BIGNUM *r, BIGNUM *range)
{
    return bn_rand_range(0, r, range);
}
#endif

#if !defined(HAVE_BN_PSEUDO_RAND_RANGE)
int
BN_pseudo_rand_range(BIGNUM *r, BIGNUM *range)
{
    return bn_rand_range(1, r, range);
}
#endif

#if !defined(HAVE_BN_IS_PRIME_EX) /* for 0.9.6 */
int BN_is_prime_ex(const BIGNUM *bn, int checks, BN_CTX *ctx, void *cb)
{
    if (cb)
	rb_bug("not supported");
    return BN_is_prime(bn, checks, NULL, ctx, NULL);
}
#endif

#if !defined(HAVE_BN_IS_PRIME_FASTTEST_EX) /* for 0.9.6 */
int BN_is_prime_fasttestex(const BIGNUM *bn, int checks, BN_CTX *ctx,
	int do_trial_division, void *cb)
{
    if (cb)
	rb_bug("not supported");
    return BN_is_prime_fasttest(bn, checks, NULL, ctx, NULL, do_trial_division);
}
#endif

#if !defined(HAVE_BN_GENERATE_PRIME_EX) /* for 0.9.6 */
int BN_generate_prime_ex(BIGNUM *ret, int bits, int safe,
                         const BIGNUM *add, const BIGNUM *rem, BN_GENCB *cb)
{
    if (cb)
	rb_bug("not supported");
    return BN_generate_prime(ret, bits, safe, add, rem, NULL);
}
#endif

#if !defined(HAVE_BN_GENCB_NEW)
/* BN_GENCB_{new,free,get_arg} are new in 1.1.0 */
BN_GENCB *
BN_GENCB_new(void)
{
    return (BN_GENCB *)OPENSSL_malloc(sizeof(BN_GENCB));
}

void
BN_GENCB_free(BN_GENCB *cb)
{
    OPENSSL_free(cb);
}

void *
BN_GENCB_get_arg(BN_GENCB *cb)
{
    return cb->arg;
}
#endif

#if !defined(HAVE_CONF_GET1_DEFAULT_CONFIG_FILE)
#define OPENSSL_CONF "openssl.cnf"
char *
CONF_get1_default_config_file(void)
{
    char *file;
    int len;

    file = getenv("OPENSSL_CONF");
    if (file) return BUF_strdup(file);
    len = strlen(X509_get_default_cert_area());
#ifndef OPENSSL_SYS_VMS
    len++;
#endif
    len += strlen(OPENSSL_CONF);
    file = OPENSSL_malloc(len + 1);
    if (!file) return NULL;
    strcpy(file,X509_get_default_cert_area());
#ifndef OPENSSL_SYS_VMS
    strcat(file,"/");
#endif
    strcat(file,OPENSSL_CONF);

    return file;
}
#endif

#if !defined(HAVE_PEM_DEF_CALLBACK)
#define OSSL_PASS_MIN_LENGTH 4
int
PEM_def_callback(char *buf, int num, int w, void *key)
{
    int i,j;
    const char *prompt;

    if (key) {
	i = strlen(key);
	i = (i > num) ? num : i;
	memcpy(buf, key, i);
	return i;
    }

    prompt = EVP_get_pw_prompt();
    if (prompt == NULL) prompt = "Enter PEM pass phrase:";
    for (;;) {
	i = EVP_read_pw_string(buf, num, prompt, w);
	if (i != 0) {
	    memset(buf, 0, (unsigned int)num);
	    return(-1);
	}
	j = strlen(buf);
	if (j < OSSL_PASS_MIN_LENGTH) {
	    fprintf(stderr,
		    "phrase is too short, needs to be at least %d chars\n",
		    OSSL_PASS_MIN_LENGTH);
	}
	else break;
    }
    return j;
}
#endif

#if !defined(HAVE_ASN1_PUT_EOC)
int
ASN1_put_eoc(unsigned char **pp)
{
    unsigned char *p = *pp;
    *p++ = 0;
    *p++ = 0;
    *pp = p;
    return 2;
}
#endif

#if !defined(HAVE_OCSP_ID_GET0_INFO)
int
OCSP_id_get0_info(ASN1_OCTET_STRING **piNameHash, ASN1_OBJECT **pmd,
		  ASN1_OCTET_STRING **pikeyHash,
		  ASN1_INTEGER **pserial, OCSP_CERTID *cid)
{
    if (piNameHash || pmd || pikeyHash)
	rb_bug("not supported");
    if (pserial)
	*pserial = cid->serialNumber;
    return 1;
}
#endif

#if !defined(HAVE_OCSP_SINGLERESP_DELETE_EXT)
X509_EXTENSION *
OCSP_SINGLERESP_delete_ext(OCSP_SINGLERESP *s, int loc)
{
    return sk_X509_EXTENSION_delete(s->singleExtensions, loc);
}
#endif

#if !defined(HAVE_OCSP_SINGLEREST_GET0_ID)
OCSP_CERTID *
OCSP_SINGLERESP_get0_id(OCSP_SINGLERESP *single)
{
    return single->certId;
}
#endif

#if !defined(HAVE_EVP_PKEY_id) /* 1.1.0 */
int
EVP_PKEY_id(const EVP_PKEY *pkey)
{
    return pkey->type;
}

RSA *
EVP_PKEY_get0_RSA(EVP_PKEY *pkey)
{
    if (pkey->type != EVP_PKEY_RSA)
        return NULL;
    return pkey->pkey.rsa;
}

DSA *
EVP_PKEY_get0_DSA(EVP_PKEY *pkey)
{
    if (pkey->type != EVP_PKEY_DSA)
        return NULL;
    return pkey->pkey.dsa;
}

#if !defined(OPENSSL_NO_EC)
EC_KEY *
EVP_PKEY_get0_EC_KEY(EVP_PKEY *pkey)
{
    if (pkey->type != EVP_PKEY_EC)
        return NULL;
    return pkey->pkey.ec;
}
#endif

#if !defined(OPENSSL_NO_DH)
DH *
EVP_PKEY_get0_DH(EVP_PKEY *pkey)
{
    if (pkey->type != EVP_PKEY_DH)
        return NULL;
    return pkey->pkey.dh;
}
#endif
#endif

#if !defined(HAVE_SSL_SESSION_GET_ID)
const unsigned char *
SSL_SESSION_get_id(const SSL_SESSION *s, unsigned int *len)
{
    if (len)
	*len = s->session_id_length;
    return s->session_id;
}
#endif

#if !defined(HAVE_SSL_SESSION_CMP) /* removed in 1.0.0 */
int
SSL_SESSION_cmp(const SSL_SESSION *a, const SSL_SESSION *b)
{
    unsigned int a_len;
    unsigned char *a_sid = SSL_SESSION_get_id(a, &a_len);
    unsigned int b_len;
    unsigned char *b_sid = SSL_SESSION_get_id(b, &b_len);

    if (a->ssl_version != b->ssl_version || a_len != b_len)
	return 1;
#if defined(_WIN32)
    return memcmp(a_sid, b_sid, a_len);
#else
    return CRYPTO_memcmp(a_sid, b_sid, a_len);
#endif
}
#endif

#if !defined(HAVE_X509_UP_REF)
void
X509_up_ref(X509 *x509)
{
    CRYPTO_add(&x509->references, 1, CRYPTO_LOCK_X509);
}

void
X509_CRL_up_ref(X509_CRL *crl)
{
    CRYPTO_add(&crl->references, 1, CRYPTO_LOCK_X509_CRL);
}

void
SSL_SESSION_up_ref(SSL_SESSION *sess)
{
    CRYPTO_add(&sess->references, 1, CRYPTO_LOCK_SSL_SESSION);
}

void
EVP_PKEY_up_ref(EVP_PKEY *pkey)
{
    CRYPTO_add(&pkey->references, 1, CRYPTO_LOCK_EVP_PKEY);
}
#endif

#if !defined(X509_CRL_GET0_SIGNATURE)
void
X509_CRL_get0_signature(ASN1_BIT_STRING **psig, X509_ALGOR **palg, X509_CRL *crl)
{
    if (psig != NULL)
	*psig = &crl->signature;
    if (palg != NULL)
	*palg = &crl->sig_alg;
}
#endif

#if !defined(X509_REQ_GET0_SIGNATURE)
void
X509_REQ_get0_signature(ASN1_BIT_STRING **psig, X509_ALGOR **palg, X509_REQ req)
{
    if (psig != NULL)
	*psig = &req->signature;
    if (palg != NULL)
	*palg = &ret->sig_alg;
}
#endif

#if !defined(X509_REVOKED_GET0_SERIALNUMBER)
ASN1_INTEGER *
X509_REVOKED_get0_serialNumber(X509_REVOKED *x)
{
    return &x->serialNumber;
}
#endif

#if !defined(X509_REVOKED_SET_SERIALNUMBER)
int
X509_REVOKED_set_serialNumber(X509_REVOKED *x, ASN1_INTEGER *serial)
{
    ASN1_INTEGER *in = x->serialNumber;
    if (in != serial)
        return ASN1_STRING_copy(in, serial);
    return 1;
}
#endif