~ubuntu-branches/ubuntu/hardy/ruby1.8/hardy-updates

« back to all changes in this revision

Viewing changes to ext/openssl/openssl_missing.c

  • Committer: Bazaar Package Importer
  • Author(s): akira yamada
  • Date: 2007-03-13 22:11:58 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20070313221158-h3oql37brlaf2go2
Tags: 1.8.6-1
* new upstream version, 1.8.6.
* libruby1.8 conflicts with libopenssl-ruby1.8 (< 1.8.6) (closes: #410018)
* changed packaging style to cdbs from dbs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * $Id: openssl_missing.c 11708 2007-02-12 23:01:19Z shyouhei $
 
3
 * 'OpenSSL for Ruby' project
 
4
 * Copyright (C) 2001-2002  Michal Rokos <m.rokos@sh.cvut.cz>
 
5
 * All rights reserved.
 
6
 */
 
7
/*
 
8
 * This program is licenced under the same licence as Ruby.
 
9
 * (See the file 'LICENCE'.)
 
10
 */
 
11
#include RUBY_EXTCONF_H
 
12
 
 
13
#if defined(HAVE_OPENSSL_ENGINE_H) && defined(HAVE_ST_ENGINE)
 
14
# include <openssl/engine.h>
 
15
#endif
 
16
#include <openssl/x509_vfy.h>
 
17
 
 
18
#if !defined(OPENSSL_NO_HMAC)
 
19
#include <string.h> /* memcpy() */
 
20
#include <openssl/hmac.h>
 
21
 
 
22
#include "openssl_missing.h"
 
23
 
 
24
#if !defined(HAVE_HMAC_CTX_COPY)
 
25
int
 
26
HMAC_CTX_copy(HMAC_CTX *out, HMAC_CTX *in)
 
27
{
 
28
    if (!out || !in) return 0;
 
29
    memcpy(out, in, sizeof(HMAC_CTX));
 
30
 
 
31
    if (!EVP_MD_CTX_copy(&out->md_ctx, &in->md_ctx)
 
32
            || !EVP_MD_CTX_copy(&out->i_ctx, &in->i_ctx)
 
33
            || !EVP_MD_CTX_copy(&out->o_ctx, &in->o_ctx))
 
34
        return 0;
 
35
    return 1;
 
36
}
 
37
#endif /* HAVE_HMAC_CTX_COPY */
 
38
#endif /* NO_HMAC */
 
39
 
 
40
#if !defined(HAVE_X509_STORE_SET_EX_DATA)
 
41
 
 
42
int X509_STORE_set_ex_data(X509_STORE *str, int idx, void *data)
 
43
{
 
44
    return CRYPTO_set_ex_data(&str->ex_data, idx, data);
 
45
}
 
46
 
 
47
void *X509_STORE_get_ex_data(X509_STORE *str, int idx)
 
48
{
 
49
    return CRYPTO_get_ex_data(&str->ex_data, idx);
 
50
}
 
51
#endif
 
52
 
 
53
#if !defined(HAVE_EVP_MD_CTX_CREATE)
 
54
EVP_MD_CTX *
 
55
EVP_MD_CTX_create(void)
 
56
{
 
57
    EVP_MD_CTX *ctx = OPENSSL_malloc(sizeof(EVP_MD_CTX));
 
58
    if (!ctx) return NULL;
 
59
 
 
60
    memset(ctx, 0, sizeof(EVP_MD_CTX));
 
61
 
 
62
    return ctx;
 
63
}
 
64
#endif
 
65
 
 
66
#if !defined(HAVE_EVP_MD_CTX_CLEANUP)
 
67
int
 
68
EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx)
 
69
{
 
70
    /* FIXME!!! */
 
71
    memset(ctx, 0, sizeof(EVP_MD_CTX));
 
72
 
 
73
    return 1;
 
74
}
 
75
#endif
 
76
 
 
77
#if !defined(HAVE_EVP_MD_CTX_DESTROY)
 
78
void
 
79
EVP_MD_CTX_destroy(EVP_MD_CTX *ctx)
 
80
{
 
81
    EVP_MD_CTX_cleanup(ctx);
 
82
    OPENSSL_free(ctx);
 
83
}
 
84
#endif
 
85
 
 
86
#if !defined(HAVE_EVP_MD_CTX_INIT)
 
87
void
 
88
EVP_MD_CTX_init(EVP_MD_CTX *ctx)
 
89
{
 
90
    memset(ctx, 0, sizeof(EVP_MD_CTX));
 
91
}
 
92
#endif
 
93
 
 
94
#if !defined(HAVE_HMAC_CTX_INIT)
 
95
void
 
96
HMAC_CTX_init(HMAC_CTX *ctx)
 
97
{
 
98
    EVP_MD_CTX_init(&ctx->i_ctx);
 
99
    EVP_MD_CTX_init(&ctx->o_ctx);
 
100
    EVP_MD_CTX_init(&ctx->md_ctx);
 
101
}
 
102
#endif
 
103
 
 
104
#if !defined(HAVE_HMAC_CTX_CLEANUP)
 
105
void
 
106
HMAC_CTX_cleanup(HMAC_CTX *ctx)
 
107
{
 
108
    EVP_MD_CTX_cleanup(&ctx->i_ctx);
 
109
    EVP_MD_CTX_cleanup(&ctx->o_ctx);
 
110
    EVP_MD_CTX_cleanup(&ctx->md_ctx);
 
111
    memset(ctx, 0, sizeof(HMAC_CTX));
 
112
}
 
113
#endif
 
114
 
 
115
#if !defined(HAVE_EVP_CIPHER_CTX_COPY)
 
116
/* 
 
117
 * this function does not exist in OpenSSL yet... or ever?.
 
118
 * a future version may break this function.
 
119
 * tested on 0.9.7d.
 
120
 */
 
121
int
 
122
EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX *out, EVP_CIPHER_CTX *in)
 
123
{
 
124
    memcpy(out, in, sizeof(EVP_CIPHER_CTX));
 
125
 
 
126
#if defined(HAVE_ENGINE_ADD) && defined(HAVE_ST_ENGINE)
 
127
    if (in->engine) ENGINE_add(out->engine);
 
128
    if (in->cipher_data) {
 
129
        out->cipher_data = OPENSSL_malloc(in->cipher->ctx_size);
 
130
        memcpy(out->cipher_data, in->cipher_data, in->cipher->ctx_size);
 
131
    }
 
132
#endif
 
133
 
 
134
    return 1;
 
135
}
 
136
#endif
 
137
 
 
138
#if !defined(HAVE_X509_CRL_SET_VERSION)
 
139
int
 
140
X509_CRL_set_version(X509_CRL *x, long version)
 
141
{
 
142
    if (x == NULL || x->crl == NULL) return 0;
 
143
    if (x->crl->version == NULL) {
 
144
        x->crl->version = M_ASN1_INTEGER_new();
 
145
        if (x->crl->version == NULL) return 0;
 
146
    }
 
147
    return ASN1_INTEGER_set(x->crl->version, version);
 
148
}
 
149
#endif
 
150
 
 
151
#if !defined(HAVE_X509_CRL_SET_ISSUER_NAME)
 
152
int
 
153
X509_CRL_set_issuer_name(X509_CRL *x, X509_NAME *name)
 
154
{
 
155
    if (x == NULL || x->crl == NULL) return 0;
 
156
    return X509_NAME_set(&x->crl->issuer, name);
 
157
}
 
158
#endif
 
159
 
 
160
#if !defined(HAVE_X509_CRL_SORT)
 
161
int
 
162
X509_CRL_sort(X509_CRL *c)
 
163
{
 
164
    int i;
 
165
    X509_REVOKED *r;
 
166
    /* sort the data so it will be written in serial
 
167
     * number order */
 
168
    sk_X509_REVOKED_sort(c->crl->revoked);
 
169
    for (i=0; i<sk_X509_REVOKED_num(c->crl->revoked); i++) {
 
170
        r=sk_X509_REVOKED_value(c->crl->revoked, i);
 
171
        r->sequence=i;
 
172
    }
 
173
    return 1;
 
174
}
 
175
#endif
 
176
 
 
177
#if !defined(HAVE_X509_CRL_ADD0_REVOKED)
 
178
static int
 
179
OSSL_X509_REVOKED_cmp(const X509_REVOKED * const *a, const X509_REVOKED * const *b)
 
180
{
 
181
    return(ASN1_STRING_cmp(
 
182
                (ASN1_STRING *)(*a)->serialNumber,
 
183
                (ASN1_STRING *)(*b)->serialNumber));
 
184
}
 
185
                    
 
186
int
 
187
X509_CRL_add0_revoked(X509_CRL *crl, X509_REVOKED *rev)
 
188
{
 
189
    X509_CRL_INFO *inf;
 
190
    
 
191
    inf = crl->crl;
 
192
    if (!inf->revoked)
 
193
        inf->revoked = sk_X509_REVOKED_new(OSSL_X509_REVOKED_cmp);
 
194
    if (!inf->revoked || !sk_X509_REVOKED_push(inf->revoked, rev))
 
195
        return 0;
 
196
    return 1;
 
197
}
 
198
#endif
 
199
 
 
200
#if !defined(HAVE_BN_MOD_SQR)
 
201
int
 
202
BN_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx)
 
203
{
 
204
    if (!BN_sqr(r, (BIGNUM*)a, ctx)) return 0;
 
205
    return BN_mod(r, r, m, ctx);
 
206
}
 
207
#endif
 
208
 
 
209
#if !defined(HAVE_BN_MOD_ADD) || !defined(HAVE_BN_MOD_SUB)
 
210
int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)
 
211
{
 
212
    if (!BN_mod(r,m,d,ctx)) return 0;
 
213
    if (!r->neg) return 1;
 
214
    return (d->neg ? BN_sub : BN_add)(r, r, d);
 
215
}
 
216
#endif
 
217
 
 
218
#if !defined(HAVE_BN_MOD_ADD)
 
219
int
 
220
BN_mod_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m, BN_CTX *ctx)
 
221
{
 
222
    if (!BN_add(r, a, b)) return 0;
 
223
    return BN_nnmod(r, r, m, ctx);
 
224
}
 
225
#endif
 
226
 
 
227
#if !defined(HAVE_BN_MOD_SUB)
 
228
int
 
229
BN_mod_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m, BN_CTX *ctx)
 
230
{
 
231
    if (!BN_sub(r, a, b)) return 0;
 
232
    return BN_nnmod(r, r, m, ctx);
 
233
}
 
234
#endif
 
235
 
 
236
#if !defined(HAVE_BN_RAND_RANGE) || !defined(HAVE_BN_PSEUDO_RAND_RANGE)
 
237
static int
 
238
bn_rand_range(int pseudo, BIGNUM *r, BIGNUM *range)
 
239
{
 
240
    int (*bn_rand)(BIGNUM *, int, int, int) = pseudo ? BN_pseudo_rand : BN_rand;
 
241
    int n;
 
242
 
 
243
    if (range->neg || BN_is_zero(range)) return 0;
 
244
 
 
245
    n = BN_num_bits(range);
 
246
 
 
247
    if (n == 1) {
 
248
        if (!BN_zero(r)) return 0;
 
249
    } else if (!BN_is_bit_set(range, n - 2) && !BN_is_bit_set(range, n - 3)) {
 
250
        do {
 
251
            if (!bn_rand(r, n + 1, -1, 0)) return 0;
 
252
            if (BN_cmp(r ,range) >= 0) {
 
253
                if (!BN_sub(r, r, range)) return 0;
 
254
                if (BN_cmp(r, range) >= 0)
 
255
                    if (!BN_sub(r, r, range)) return 0;
 
256
            }
 
257
        } while (BN_cmp(r, range) >= 0);
 
258
    } else {
 
259
        do {
 
260
            if (!bn_rand(r, n, -1, 0)) return 0;
 
261
        } while (BN_cmp(r, range) >= 0);
 
262
    }
 
263
 
 
264
    return 1;
 
265
}
 
266
#endif
 
267
 
 
268
#if !defined(HAVE_BN_RAND_RANGE)
 
269
int
 
270
BN_rand_range(BIGNUM *r, BIGNUM *range)
 
271
{
 
272
    return bn_rand_range(0, r, range);
 
273
}
 
274
#endif
 
275
 
 
276
#if !defined(HAVE_BN_PSEUDO_RAND_RANGE)
 
277
int
 
278
BN_pseudo_rand_range(BIGNUM *r, BIGNUM *range)
 
279
{
 
280
    return bn_rand_range(1, r, range);
 
281
}
 
282
#endif
 
283
 
 
284
#if !defined(HAVE_CONF_GET1_DEFAULT_CONFIG_FILE)
 
285
#define OPENSSL_CONF "openssl.cnf"
 
286
char *
 
287
CONF_get1_default_config_file(void)
 
288
{
 
289
    char *file;
 
290
    int len;
 
291
 
 
292
    file = getenv("OPENSSL_CONF");
 
293
    if (file) return BUF_strdup(file);
 
294
    len = strlen(X509_get_default_cert_area());
 
295
#ifndef OPENSSL_SYS_VMS
 
296
    len++;
 
297
#endif
 
298
    len += strlen(OPENSSL_CONF);
 
299
    file = OPENSSL_malloc(len + 1);
 
300
    if (!file) return NULL;
 
301
    strcpy(file,X509_get_default_cert_area());
 
302
#ifndef OPENSSL_SYS_VMS
 
303
    strcat(file,"/");
 
304
#endif
 
305
    strcat(file,OPENSSL_CONF);
 
306
 
 
307
    return file;
 
308
}
 
309
#endif
 
310
 
 
311
#if !defined(HAVE_PEM_DEF_CALLBACK)
 
312
#define OSSL_PASS_MIN_LENGTH 4
 
313
int
 
314
PEM_def_callback(char *buf, int num, int w, void *key)
 
315
{
 
316
    int i,j;
 
317
    const char *prompt;
 
318
    
 
319
    if (key) {
 
320
        i = strlen(key);
 
321
        i = (i > num) ? num : i;
 
322
        memcpy(buf, key, i);
 
323
        return i;
 
324
    }
 
325
 
 
326
    prompt = EVP_get_pw_prompt();
 
327
    if (prompt == NULL) prompt = "Enter PEM pass phrase:";
 
328
    for (;;) {
 
329
        i = EVP_read_pw_string(buf, num, prompt, w);
 
330
        if (i != 0) {
 
331
            memset(buf, 0, (unsigned int)num);
 
332
            return(-1);
 
333
        }
 
334
        j = strlen(buf);
 
335
        if (j < OSSL_PASS_MIN_LENGTH) {
 
336
            fprintf(stderr,
 
337
                    "phrase is too short, needs to be at least %d chars\n",
 
338
                    OSSL_PASS_MIN_LENGTH);
 
339
        }
 
340
        else break;
 
341
    }
 
342
    return j;
 
343
}
 
344
#endif
 
345