2003-07-23 12:12:24 -04:00
|
|
|
/*
|
|
|
|
* 'OpenSSL for Ruby' project
|
|
|
|
* Copyright (C) 2001-2002 Michal Rokos <m.rokos@sh.cvut.cz>
|
|
|
|
* All rights reserved.
|
|
|
|
*/
|
|
|
|
/*
|
2015-04-19 23:55:09 -04:00
|
|
|
* This program is licensed under the same licence as Ruby.
|
2003-07-23 12:12:24 -04:00
|
|
|
* (See the file 'LICENCE'.)
|
|
|
|
*/
|
2006-06-02 06:03:16 -04:00
|
|
|
#include RUBY_EXTCONF_H
|
|
|
|
|
2016-05-25 04:50:03 -04:00
|
|
|
#include <string.h> /* memcpy() */
|
|
|
|
#if !defined(OPENSSL_NO_ENGINE)
|
2005-07-25 23:59:39 -04:00
|
|
|
# include <openssl/engine.h>
|
|
|
|
#endif
|
2003-07-23 12:12:24 -04:00
|
|
|
#if !defined(OPENSSL_NO_HMAC)
|
2016-05-25 04:50:03 -04:00
|
|
|
# include <openssl/hmac.h>
|
2003-07-23 12:12:24 -04:00
|
|
|
#endif
|
2016-05-25 04:50:03 -04:00
|
|
|
#include <openssl/x509_vfy.h>
|
2003-07-23 12:12:24 -04:00
|
|
|
|
2016-05-25 04:50:03 -04:00
|
|
|
#include "openssl_missing.h"
|
2003-07-23 12:12:24 -04:00
|
|
|
|
2016-06-05 11:35:12 -04:00
|
|
|
/* added in 0.9.8X */
|
|
|
|
#if !defined(HAVE_EVP_CIPHER_CTX_NEW)
|
|
|
|
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_CIPHER_CTX_FREE)
|
|
|
|
void
|
|
|
|
EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx)
|
|
|
|
{
|
|
|
|
if (ctx) {
|
|
|
|
EVP_CIPHER_CTX_cleanup(ctx);
|
|
|
|
OPENSSL_free(ctx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-05-25 04:50:03 -04:00
|
|
|
/* added in 1.0.0 */
|
* ext/openssl/extconf.rb: check for EVP_CIPHER_CTX_copy, ENGINE_add,
EVP_CIPHER_CTX_set_padding, EVP_CipherFinal_ex, EVP_CipherInit_ex,
EVP_DigestFinal_ex and EVP_DigestInit_ex.
* ext/openssl/openssl_missing.c (EVP_CIPHER_CTX_copy): new function.
* ext/openssl/openssl_missing.h (EVP_DigestInit_ex, EVP_DigestFinal_ex,
EVP_CipherInit_ex, EVP_CipherFinal_ex, HMAC_Init_ex): new macro for
OpenSSL 0.9.6.
* ext/openssl/ossl_cipher.c (ossl_cipher_alloc, ossl_cipher_initialize,
ossl_cipher_copy, ossl_cipher_reset, ossl_cipher_encrypt,
ossl_cipher_decrypt, ossl_cipher_final, ossl_cipher_set_key,
ossl_cipher_set_iv): replace all EVP_CipherInit and
EVP_CipherFinal into EVP_CipherInit_ex and EVP_CipherFinal_ex.
and EVP_CIPHER_CTX_init should only be called once.
* ext/openssl/ossl_cipher.c (ossl_cipher_set_padding): check for
EVP_CIPHER_CTX_set_padding.
* ext/openssl/ossl_cipher.c (Init_ossl_cipher): Cipher#<< is deprecated.
* ext/openssl/ossl_digest.c: replace all EVP_DigestInit and
EVP_DigestFinal into EVP_DigestInit_ex and EVP_DigestFinal_ex.
and EVP_MD_CTX_init should only be called once.
* ext/openssl/ossl_digest.c (digest_final): should call
EVP_MD_CTX_cleanup to avoid memory leak.
* ext/openssl/ossl_hmac.c (ossl_hmac_initialize): repalce HMAC_init
into HMAC_init_ex. and HMAC_CTX_init is moved to ossl_hmac_alloc.
* ext/openssl/ossl_hmac.c (hmac_final): should call
HMAC_CTX_cleanup to avoid memory leak.
* test/openssl/test_cipher.rb, test/openssl/test_digest.rb,
test/openssl/test_hmac.rb: new file.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6548 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-06-30 06:48:43 -04:00
|
|
|
#if !defined(HAVE_EVP_CIPHER_CTX_COPY)
|
2010-04-22 04:04:13 -04:00
|
|
|
/*
|
* ext/openssl/extconf.rb: check for EVP_CIPHER_CTX_copy, ENGINE_add,
EVP_CIPHER_CTX_set_padding, EVP_CipherFinal_ex, EVP_CipherInit_ex,
EVP_DigestFinal_ex and EVP_DigestInit_ex.
* ext/openssl/openssl_missing.c (EVP_CIPHER_CTX_copy): new function.
* ext/openssl/openssl_missing.h (EVP_DigestInit_ex, EVP_DigestFinal_ex,
EVP_CipherInit_ex, EVP_CipherFinal_ex, HMAC_Init_ex): new macro for
OpenSSL 0.9.6.
* ext/openssl/ossl_cipher.c (ossl_cipher_alloc, ossl_cipher_initialize,
ossl_cipher_copy, ossl_cipher_reset, ossl_cipher_encrypt,
ossl_cipher_decrypt, ossl_cipher_final, ossl_cipher_set_key,
ossl_cipher_set_iv): replace all EVP_CipherInit and
EVP_CipherFinal into EVP_CipherInit_ex and EVP_CipherFinal_ex.
and EVP_CIPHER_CTX_init should only be called once.
* ext/openssl/ossl_cipher.c (ossl_cipher_set_padding): check for
EVP_CIPHER_CTX_set_padding.
* ext/openssl/ossl_cipher.c (Init_ossl_cipher): Cipher#<< is deprecated.
* ext/openssl/ossl_digest.c: replace all EVP_DigestInit and
EVP_DigestFinal into EVP_DigestInit_ex and EVP_DigestFinal_ex.
and EVP_MD_CTX_init should only be called once.
* ext/openssl/ossl_digest.c (digest_final): should call
EVP_MD_CTX_cleanup to avoid memory leak.
* ext/openssl/ossl_hmac.c (ossl_hmac_initialize): repalce HMAC_init
into HMAC_init_ex. and HMAC_CTX_init is moved to ossl_hmac_alloc.
* ext/openssl/ossl_hmac.c (hmac_final): should call
HMAC_CTX_cleanup to avoid memory leak.
* test/openssl/test_cipher.rb, test/openssl/test_digest.rb,
test/openssl/test_hmac.rb: new file.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6548 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-06-30 06:48:43 -04:00
|
|
|
* this function does not exist in OpenSSL yet... or ever?.
|
|
|
|
* a future version may break this function.
|
|
|
|
* tested on 0.9.7d.
|
|
|
|
*/
|
|
|
|
int
|
2016-05-25 04:50:03 -04:00
|
|
|
EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX *out, const EVP_CIPHER_CTX *in)
|
* ext/openssl/extconf.rb: check for EVP_CIPHER_CTX_copy, ENGINE_add,
EVP_CIPHER_CTX_set_padding, EVP_CipherFinal_ex, EVP_CipherInit_ex,
EVP_DigestFinal_ex and EVP_DigestInit_ex.
* ext/openssl/openssl_missing.c (EVP_CIPHER_CTX_copy): new function.
* ext/openssl/openssl_missing.h (EVP_DigestInit_ex, EVP_DigestFinal_ex,
EVP_CipherInit_ex, EVP_CipherFinal_ex, HMAC_Init_ex): new macro for
OpenSSL 0.9.6.
* ext/openssl/ossl_cipher.c (ossl_cipher_alloc, ossl_cipher_initialize,
ossl_cipher_copy, ossl_cipher_reset, ossl_cipher_encrypt,
ossl_cipher_decrypt, ossl_cipher_final, ossl_cipher_set_key,
ossl_cipher_set_iv): replace all EVP_CipherInit and
EVP_CipherFinal into EVP_CipherInit_ex and EVP_CipherFinal_ex.
and EVP_CIPHER_CTX_init should only be called once.
* ext/openssl/ossl_cipher.c (ossl_cipher_set_padding): check for
EVP_CIPHER_CTX_set_padding.
* ext/openssl/ossl_cipher.c (Init_ossl_cipher): Cipher#<< is deprecated.
* ext/openssl/ossl_digest.c: replace all EVP_DigestInit and
EVP_DigestFinal into EVP_DigestInit_ex and EVP_DigestFinal_ex.
and EVP_MD_CTX_init should only be called once.
* ext/openssl/ossl_digest.c (digest_final): should call
EVP_MD_CTX_cleanup to avoid memory leak.
* ext/openssl/ossl_hmac.c (ossl_hmac_initialize): repalce HMAC_init
into HMAC_init_ex. and HMAC_CTX_init is moved to ossl_hmac_alloc.
* ext/openssl/ossl_hmac.c (hmac_final): should call
HMAC_CTX_cleanup to avoid memory leak.
* test/openssl/test_cipher.rb, test/openssl/test_digest.rb,
test/openssl/test_hmac.rb: new file.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6548 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-06-30 06:48:43 -04:00
|
|
|
{
|
|
|
|
memcpy(out, in, sizeof(EVP_CIPHER_CTX));
|
|
|
|
|
2016-05-25 04:50:03 -04:00
|
|
|
#if !defined(OPENSSL_NO_ENGINE)
|
* ext/openssl/extconf.rb: check for EVP_CIPHER_CTX_copy, ENGINE_add,
EVP_CIPHER_CTX_set_padding, EVP_CipherFinal_ex, EVP_CipherInit_ex,
EVP_DigestFinal_ex and EVP_DigestInit_ex.
* ext/openssl/openssl_missing.c (EVP_CIPHER_CTX_copy): new function.
* ext/openssl/openssl_missing.h (EVP_DigestInit_ex, EVP_DigestFinal_ex,
EVP_CipherInit_ex, EVP_CipherFinal_ex, HMAC_Init_ex): new macro for
OpenSSL 0.9.6.
* ext/openssl/ossl_cipher.c (ossl_cipher_alloc, ossl_cipher_initialize,
ossl_cipher_copy, ossl_cipher_reset, ossl_cipher_encrypt,
ossl_cipher_decrypt, ossl_cipher_final, ossl_cipher_set_key,
ossl_cipher_set_iv): replace all EVP_CipherInit and
EVP_CipherFinal into EVP_CipherInit_ex and EVP_CipherFinal_ex.
and EVP_CIPHER_CTX_init should only be called once.
* ext/openssl/ossl_cipher.c (ossl_cipher_set_padding): check for
EVP_CIPHER_CTX_set_padding.
* ext/openssl/ossl_cipher.c (Init_ossl_cipher): Cipher#<< is deprecated.
* ext/openssl/ossl_digest.c: replace all EVP_DigestInit and
EVP_DigestFinal into EVP_DigestInit_ex and EVP_DigestFinal_ex.
and EVP_MD_CTX_init should only be called once.
* ext/openssl/ossl_digest.c (digest_final): should call
EVP_MD_CTX_cleanup to avoid memory leak.
* ext/openssl/ossl_hmac.c (ossl_hmac_initialize): repalce HMAC_init
into HMAC_init_ex. and HMAC_CTX_init is moved to ossl_hmac_alloc.
* ext/openssl/ossl_hmac.c (hmac_final): should call
HMAC_CTX_cleanup to avoid memory leak.
* test/openssl/test_cipher.rb, test/openssl/test_digest.rb,
test/openssl/test_hmac.rb: new file.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6548 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-06-30 06:48:43 -04:00
|
|
|
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
|
|
|
|
|
2016-05-25 04:50:03 -04:00
|
|
|
#if !defined(OPENSSL_NO_HMAC)
|
|
|
|
#if !defined(HAVE_HMAC_CTX_COPY)
|
2016-06-05 11:35:12 -04:00
|
|
|
int
|
2016-05-25 04:50:03 -04:00
|
|
|
HMAC_CTX_copy(HMAC_CTX *out, HMAC_CTX *in)
|
2003-07-23 12:12:24 -04:00
|
|
|
{
|
2016-06-05 11:35:12 -04:00
|
|
|
if (!out || !in)
|
|
|
|
return 0;
|
|
|
|
|
2016-05-25 04:50:03 -04:00
|
|
|
memcpy(out, in, sizeof(HMAC_CTX));
|
2003-07-23 12:12:24 -04:00
|
|
|
|
2016-05-25 04:50:03 -04:00
|
|
|
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);
|
2016-06-05 11:35:12 -04:00
|
|
|
|
|
|
|
return 1;
|
2011-06-25 21:32:03 -04:00
|
|
|
}
|
2016-05-25 04:50:03 -04:00
|
|
|
#endif /* HAVE_HMAC_CTX_COPY */
|
|
|
|
#endif /* NO_HMAC */
|
2016-05-30 05:30:38 -04:00
|
|
|
|
|
|
|
/* added in 1.0.2 */
|
|
|
|
#if !defined(OPENSSL_NO_EC)
|
|
|
|
#if !defined(HAVE_EC_CURVE_NIST2NID)
|
|
|
|
static struct {
|
|
|
|
const char *name;
|
|
|
|
int nid;
|
|
|
|
} nist_curves[] = {
|
|
|
|
{"B-163", NID_sect163r2},
|
|
|
|
{"B-233", NID_sect233r1},
|
|
|
|
{"B-283", NID_sect283r1},
|
|
|
|
{"B-409", NID_sect409r1},
|
|
|
|
{"B-571", NID_sect571r1},
|
|
|
|
{"K-163", NID_sect163k1},
|
|
|
|
{"K-233", NID_sect233k1},
|
|
|
|
{"K-283", NID_sect283k1},
|
|
|
|
{"K-409", NID_sect409k1},
|
|
|
|
{"K-571", NID_sect571k1},
|
|
|
|
{"P-192", NID_X9_62_prime192v1},
|
|
|
|
{"P-224", NID_secp224r1},
|
|
|
|
{"P-256", NID_X9_62_prime256v1},
|
|
|
|
{"P-384", NID_secp384r1},
|
|
|
|
{"P-521", NID_secp521r1}
|
|
|
|
};
|
|
|
|
|
|
|
|
int
|
|
|
|
EC_curve_nist2nid(const char *name)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
for (i = 0; i < (sizeof(nist_curves) / sizeof(nist_curves[0])); i++) {
|
|
|
|
if (!strcmp(nist_curves[i].name, name))
|
|
|
|
return nist_curves[i].nid;
|
|
|
|
}
|
|
|
|
return NID_undef;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
2016-06-05 11:35:12 -04:00
|
|
|
|
|
|
|
/*** added in 1.1.0 ***/
|
|
|
|
#if !defined(HAVE_HMAC_CTX_NEW)
|
|
|
|
HMAC_CTX *
|
|
|
|
HMAC_CTX_new(void)
|
|
|
|
{
|
|
|
|
HMAC_CTX *ctx = OPENSSL_malloc(sizeof(HMAC_CTX));
|
|
|
|
if (!ctx)
|
|
|
|
return NULL;
|
|
|
|
HMAC_CTX_init(ctx);
|
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !defined(HAVE_HMAC_CTX_FREE)
|
|
|
|
void
|
|
|
|
HMAC_CTX_free(HMAC_CTX *ctx)
|
|
|
|
{
|
|
|
|
if (ctx) {
|
|
|
|
HMAC_CTX_cleanup(ctx);
|
|
|
|
OPENSSL_free(ctx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !defined(HAVE_X509_CRL_GET0_SIGNATURE)
|
|
|
|
void
|
2016-08-29 01:47:09 -04:00
|
|
|
X509_CRL_get0_signature(const X509_CRL *crl, const ASN1_BIT_STRING **psig,
|
|
|
|
const X509_ALGOR **palg)
|
2016-06-05 11:35:12 -04:00
|
|
|
{
|
|
|
|
if (psig != NULL)
|
|
|
|
*psig = crl->signature;
|
|
|
|
if (palg != NULL)
|
|
|
|
*palg = crl->sig_alg;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !defined(HAVE_X509_REQ_GET0_SIGNATURE)
|
|
|
|
void
|
2016-08-29 01:47:09 -04:00
|
|
|
X509_REQ_get0_signature(const X509_REQ *req, const ASN1_BIT_STRING **psig,
|
|
|
|
const X509_ALGOR **palg)
|
2016-06-05 11:35:12 -04:00
|
|
|
{
|
|
|
|
if (psig != NULL)
|
|
|
|
*psig = req->signature;
|
|
|
|
if (palg != NULL)
|
|
|
|
*palg = req->sig_alg;
|
|
|
|
}
|
|
|
|
#endif
|