/source/ruby-1.9.2-p180/ext/openssl/ossl_pkey_dh.c
C | 532 lines | 344 code | 65 blank | 123 comment | 47 complexity | 6ef02b33af215af0914ccdde3f2714b7 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause
- /*
- * $Id: ossl_pkey_dh.c 27440 2010-04-22 08:21:01Z nobu $
- * 'OpenSSL for Ruby' project
- * Copyright (C) 2001-2002 Michal Rokos <m.rokos@sh.cvut.cz>
- * All rights reserved.
- */
- /*
- * This program is licenced under the same licence as Ruby.
- * (See the file 'LICENCE'.)
- */
- #if !defined(OPENSSL_NO_DH)
- #include "ossl.h"
- #define GetPKeyDH(obj, pkey) do { \
- GetPKey(obj, pkey); \
- if (EVP_PKEY_type(pkey->type) != EVP_PKEY_DH) { /* PARANOIA? */ \
- ossl_raise(rb_eRuntimeError, "THIS IS NOT A DH!") ; \
- } \
- } while (0)
- #define DH_HAS_PRIVATE(dh) ((dh)->priv_key)
- #ifdef OSSL_ENGINE_ENABLED
- # define DH_PRIVATE(dh) (DH_HAS_PRIVATE(dh) || (dh)->engine)
- #else
- # define DH_PRIVATE(dh) DH_HAS_PRIVATE(dh)
- #endif
- /*
- * Classes
- */
- VALUE cDH;
- VALUE eDHError;
- /*
- * Public
- */
- static VALUE
- dh_instance(VALUE klass, DH *dh)
- {
- EVP_PKEY *pkey;
- VALUE obj;
- if (!dh) {
- return Qfalse;
- }
- if (!(pkey = EVP_PKEY_new())) {
- return Qfalse;
- }
- if (!EVP_PKEY_assign_DH(pkey, dh)) {
- EVP_PKEY_free(pkey);
- return Qfalse;
- }
- WrapPKey(klass, obj, pkey);
- return obj;
- }
- VALUE
- ossl_dh_new(EVP_PKEY *pkey)
- {
- VALUE obj;
- if (!pkey) {
- obj = dh_instance(cDH, DH_new());
- } else {
- if (EVP_PKEY_type(pkey->type) != EVP_PKEY_DH) {
- ossl_raise(rb_eTypeError, "Not a DH key!");
- }
- WrapPKey(cDH, obj, pkey);
- }
- if (obj == Qfalse) {
- ossl_raise(eDHError, NULL);
- }
- return obj;
- }
- /*
- * Private
- */
- static DH *
- dh_generate(int size, int gen)
- {
- DH *dh;
- dh = DH_generate_parameters(size, gen,
- rb_block_given_p() ? ossl_generate_cb : NULL,
- NULL);
- if (!dh) return 0;
- if (!DH_generate_key(dh)) {
- DH_free(dh);
- return 0;
- }
- return dh;
- }
- /*
- * call-seq:
- * DH.generate(size [, generator]) -> dh
- *
- * === Parameters
- * * +size+ is an integer representing the desired key size. Keys smaller than 1024 should be considered insecure.
- * * +generator+ is a small number > 1, typically 2 or 5.
- *
- */
- static VALUE
- ossl_dh_s_generate(int argc, VALUE *argv, VALUE klass)
- {
- DH *dh ;
- int g = 2;
- VALUE size, gen, obj;
- if (rb_scan_args(argc, argv, "11", &size, &gen) == 2) {
- g = NUM2INT(gen);
- }
- dh = dh_generate(NUM2INT(size), g);
- obj = dh_instance(klass, dh);
- if (obj == Qfalse) {
- DH_free(dh);
- ossl_raise(eDHError, NULL);
- }
- return obj;
- }
- /*
- * call-seq:
- * DH.new([size [, generator] | string]) -> dh
- *
- * === Parameters
- * * +size+ is an integer representing the desired key size. Keys smaller than 1024 should be considered insecure.
- * * +generator+ is a small number > 1, typically 2 or 5.
- * * +string+ contains the DER or PEM encoded key.
- *
- * === Examples
- * * DH.new -> dh
- * * DH.new(1024) -> dh
- * * DH.new(1024, 5) -> dh
- * * DH.new(File.read('key.pem')) -> dh
- */
- static VALUE
- ossl_dh_initialize(int argc, VALUE *argv, VALUE self)
- {
- EVP_PKEY *pkey;
- DH *dh;
- int g = 2;
- BIO *in;
- VALUE arg, gen;
- GetPKey(self, pkey);
- if(rb_scan_args(argc, argv, "02", &arg, &gen) == 0) {
- dh = DH_new();
- }
- else if (FIXNUM_P(arg)) {
- if (!NIL_P(gen)) {
- g = NUM2INT(gen);
- }
- if (!(dh = dh_generate(FIX2INT(arg), g))) {
- ossl_raise(eDHError, NULL);
- }
- }
- else {
- arg = ossl_to_der_if_possible(arg);
- in = ossl_obj2bio(arg);
- dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
- if (!dh){
- (void)BIO_reset(in);
- dh = d2i_DHparams_bio(in, NULL);
- }
- BIO_free(in);
- if (!dh) ossl_raise(eDHError, NULL);
- }
- if (!EVP_PKEY_assign_DH(pkey, dh)) {
- DH_free(dh);
- ossl_raise(eDHError, NULL);
- }
- return self;
- }
- /*
- * call-seq:
- * dh.public? -> true | false
- *
- */
- static VALUE
- ossl_dh_is_public(VALUE self)
- {
- EVP_PKEY *pkey;
- GetPKeyDH(self, pkey);
- return (pkey->pkey.dh->pub_key) ? Qtrue : Qfalse;
- }
- /*
- * call-seq:
- * dh.private? -> true | false
- *
- */
- static VALUE
- ossl_dh_is_private(VALUE self)
- {
- EVP_PKEY *pkey;
- GetPKeyDH(self, pkey);
- return (DH_PRIVATE(pkey->pkey.dh)) ? Qtrue : Qfalse;
- }
- /*
- * call-seq:
- * dh.to_pem -> aString
- *
- */
- static VALUE
- ossl_dh_export(VALUE self)
- {
- EVP_PKEY *pkey;
- BIO *out;
- VALUE str;
- GetPKeyDH(self, pkey);
- if (!(out = BIO_new(BIO_s_mem()))) {
- ossl_raise(eDHError, NULL);
- }
- if (!PEM_write_bio_DHparams(out, pkey->pkey.dh)) {
- BIO_free(out);
- ossl_raise(eDHError, NULL);
- }
- str = ossl_membio2str(out);
- return str;
- }
- /*
- * call-seq:
- * dh.to_der -> aString
- *
- */
- static VALUE
- ossl_dh_to_der(VALUE self)
- {
-