Ruby  2.4.2p198(2017-09-14revision59899)
openssl_missing.c
Go to the documentation of this file.
1 /*
2  * 'OpenSSL for Ruby' project
3  * Copyright (C) 2001-2002 Michal Rokos <m.rokos@sh.cvut.cz>
4  * All rights reserved.
5  */
6 /*
7  * This program is licensed under the same licence as Ruby.
8  * (See the file 'LICENCE'.)
9  */
10 #include RUBY_EXTCONF_H
11 
12 #include <string.h> /* memcpy() */
13 #if !defined(OPENSSL_NO_ENGINE)
14 # include <openssl/engine.h>
15 #endif
16 #if !defined(OPENSSL_NO_HMAC)
17 # include <openssl/hmac.h>
18 #endif
19 #include <openssl/x509_vfy.h>
20 
21 #include "openssl_missing.h"
22 
23 /* added in 0.9.8X */
24 #if !defined(HAVE_EVP_CIPHER_CTX_NEW)
25 EVP_CIPHER_CTX *
27 {
28  EVP_CIPHER_CTX *ctx = OPENSSL_malloc(sizeof(EVP_CIPHER_CTX));
29  if (!ctx)
30  return NULL;
31  EVP_CIPHER_CTX_init(ctx);
32  return ctx;
33 }
34 #endif
35 
36 #if !defined(HAVE_EVP_CIPHER_CTX_FREE)
37 void
38 ossl_EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx)
39 {
40  if (ctx) {
41  EVP_CIPHER_CTX_cleanup(ctx);
42  OPENSSL_free(ctx);
43  }
44 }
45 #endif
46 
47 /* added in 1.0.0 */
48 #if !defined(HAVE_EVP_CIPHER_CTX_COPY)
49 /*
50  * this function does not exist in OpenSSL yet... or ever?.
51  * a future version may break this function.
52  * tested on 0.9.7d.
53  */
54 int
55 ossl_EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX *out, const EVP_CIPHER_CTX *in)
56 {
57  memcpy(out, in, sizeof(EVP_CIPHER_CTX));
58 
59 #if !defined(OPENSSL_NO_ENGINE)
60  if (in->engine) ENGINE_add(out->engine);
61  if (in->cipher_data) {
62  out->cipher_data = OPENSSL_malloc(in->cipher->ctx_size);
63  memcpy(out->cipher_data, in->cipher_data, in->cipher->ctx_size);
64  }
65 #endif
66 
67  return 1;
68 }
69 #endif
70 
71 #if !defined(OPENSSL_NO_HMAC)
72 #if !defined(HAVE_HMAC_CTX_COPY)
73 int
74 ossl_HMAC_CTX_copy(HMAC_CTX *out, HMAC_CTX *in)
75 {
76  if (!out || !in)
77  return 0;
78 
79  memcpy(out, in, sizeof(HMAC_CTX));
80 
81  EVP_MD_CTX_copy(&out->md_ctx, &in->md_ctx);
82  EVP_MD_CTX_copy(&out->i_ctx, &in->i_ctx);
83  EVP_MD_CTX_copy(&out->o_ctx, &in->o_ctx);
84 
85  return 1;
86 }
87 #endif /* HAVE_HMAC_CTX_COPY */
88 #endif /* NO_HMAC */
89 
90 /* added in 1.0.2 */
91 #if !defined(OPENSSL_NO_EC)
92 #if !defined(HAVE_EC_CURVE_NIST2NID)
93 static struct {
94  const char *name;
95  int nid;
96 } nist_curves[] = {
97  {"B-163", NID_sect163r2},
98  {"B-233", NID_sect233r1},
99  {"B-283", NID_sect283r1},
100  {"B-409", NID_sect409r1},
101  {"B-571", NID_sect571r1},
102  {"K-163", NID_sect163k1},
103  {"K-233", NID_sect233k1},
104  {"K-283", NID_sect283k1},
105  {"K-409", NID_sect409k1},
106  {"K-571", NID_sect571k1},
107  {"P-192", NID_X9_62_prime192v1},
108  {"P-224", NID_secp224r1},
109  {"P-256", NID_X9_62_prime256v1},
110  {"P-384", NID_secp384r1},
111  {"P-521", NID_secp521r1}
112 };
113 
114 int
116 {
117  size_t i;
118  for (i = 0; i < (sizeof(nist_curves) / sizeof(nist_curves[0])); i++) {
119  if (!strcmp(nist_curves[i].name, name))
120  return nist_curves[i].nid;
121  }
122  return NID_undef;
123 }
124 #endif
125 #endif
126 
127 /*** added in 1.1.0 ***/
128 #if !defined(HAVE_HMAC_CTX_NEW)
129 HMAC_CTX *
131 {
132  HMAC_CTX *ctx = OPENSSL_malloc(sizeof(HMAC_CTX));
133  if (!ctx)
134  return NULL;
135  HMAC_CTX_init(ctx);
136  return ctx;
137 }
138 #endif
139 
140 #if !defined(HAVE_HMAC_CTX_FREE)
141 void
142 ossl_HMAC_CTX_free(HMAC_CTX *ctx)
143 {
144  if (ctx) {
145  HMAC_CTX_cleanup(ctx);
146  OPENSSL_free(ctx);
147  }
148 }
149 #endif
150 
151 #if !defined(HAVE_X509_CRL_GET0_SIGNATURE)
152 void
153 ossl_X509_CRL_get0_signature(const X509_CRL *crl, const ASN1_BIT_STRING **psig,
154  const X509_ALGOR **palg)
155 {
156  if (psig != NULL)
157  *psig = crl->signature;
158  if (palg != NULL)
159  *palg = crl->sig_alg;
160 }
161 #endif
162 
163 #if !defined(HAVE_X509_REQ_GET0_SIGNATURE)
164 void
165 ossl_X509_REQ_get0_signature(const X509_REQ *req, const ASN1_BIT_STRING **psig,
166  const X509_ALGOR **palg)
167 {
168  if (psig != NULL)
169  *psig = req->signature;
170  if (palg != NULL)
171  *palg = req->sig_alg;
172 }
173 #endif
int ossl_HMAC_CTX_copy(HMAC_CTX *out, HMAC_CTX *in)
const char * name
int ossl_EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX *out, const EVP_CIPHER_CTX *in)
void ossl_HMAC_CTX_free(HMAC_CTX *ctx)
void ossl_X509_CRL_get0_signature(const X509_CRL *crl, const ASN1_BIT_STRING **psig, const X509_ALGOR **palg)
void ossl_EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx)
static struct @47 nist_curves[]
HMAC_CTX * ossl_HMAC_CTX_new(void)
void ossl_X509_REQ_get0_signature(const X509_REQ *req, const ASN1_BIT_STRING **psig, const X509_ALGOR **palg)
EVP_CIPHER_CTX * ossl_EVP_CIPHER_CTX_new(void)
int ossl_EC_curve_nist2nid(const char *name)
int nid
#define memcpy(d, s, n)
Definition: ffi_common.h:55
#define NULL
Definition: _sdbm.c:102