Ruby  2.0.0p247(2013-06-27revision41674)
ext/openssl/ossl_pkey.h
Go to the documentation of this file.
00001 /*
00002  * $Id: ossl_pkey.h 33634 2011-11-04 07:19:23Z nobu $
00003  * 'OpenSSL for Ruby' project
00004  * Copyright (C) 2001 Michal Rokos <m.rokos@sh.cvut.cz>
00005  * All rights reserved.
00006  */
00007 /*
00008  * This program is licenced under the same licence as Ruby.
00009  * (See the file 'LICENCE'.)
00010  */
00011 #if !defined(_OSSL_PKEY_H_)
00012 #define _OSSL_PKEY_H_
00013 
00014 extern VALUE mPKey;
00015 extern VALUE cPKey;
00016 extern VALUE ePKeyError;
00017 extern ID id_private_q;
00018 
00019 #define OSSL_PKEY_SET_PRIVATE(obj) rb_iv_set((obj), "private", Qtrue)
00020 #define OSSL_PKEY_SET_PUBLIC(obj)  rb_iv_set((obj), "private", Qfalse)
00021 #define OSSL_PKEY_IS_PRIVATE(obj)  (rb_iv_get((obj), "private") == Qtrue)
00022 
00023 #define WrapPKey(klass, obj, pkey) do { \
00024     if (!(pkey)) { \
00025         rb_raise(rb_eRuntimeError, "PKEY wasn't initialized!"); \
00026     } \
00027     (obj) = Data_Wrap_Struct((klass), 0, EVP_PKEY_free, (pkey)); \
00028     OSSL_PKEY_SET_PUBLIC(obj); \
00029 } while (0)
00030 #define GetPKey(obj, pkey) do {\
00031     Data_Get_Struct((obj), EVP_PKEY, (pkey));\
00032     if (!(pkey)) { \
00033         rb_raise(rb_eRuntimeError, "PKEY wasn't initialized!");\
00034     } \
00035 } while (0)
00036 #define SafeGetPKey(obj, pkey) do { \
00037     OSSL_Check_Kind((obj), cPKey); \
00038     GetPKey((obj), (pkey)); \
00039 } while (0)
00040 
00041 void ossl_generate_cb(int, int, void *);
00042 #define HAVE_BN_GENCB defined(HAVE_RSA_GENERATE_KEY_EX) || defined(HAVE_DH_GENERATE_PARAMETERS_EX) || defined(HAVE_DSA_GENERATE_PARAMETERS_EX)
00043 #if HAVE_BN_GENCB
00044 struct ossl_generate_cb_arg {
00045     int yield;
00046     int stop;
00047     int state;
00048 };
00049 int ossl_generate_cb_2(int p, int n, BN_GENCB *cb);
00050 void ossl_generate_cb_stop(void *ptr);
00051 #endif
00052 
00053 VALUE ossl_pkey_new(EVP_PKEY *);
00054 VALUE ossl_pkey_new_from_file(VALUE);
00055 EVP_PKEY *GetPKeyPtr(VALUE);
00056 EVP_PKEY *DupPKeyPtr(VALUE);
00057 EVP_PKEY *GetPrivPKeyPtr(VALUE);
00058 EVP_PKEY *DupPrivPKeyPtr(VALUE);
00059 void Init_ossl_pkey(void);
00060 
00061 /*
00062  * RSA
00063  */
00064 extern VALUE cRSA;
00065 extern VALUE eRSAError;
00066 
00067 VALUE ossl_rsa_new(EVP_PKEY *);
00068 void Init_ossl_rsa(void);
00069 
00070 /*
00071  * DSA
00072  */
00073 extern VALUE cDSA;
00074 extern VALUE eDSAError;
00075 
00076 VALUE ossl_dsa_new(EVP_PKEY *);
00077 void Init_ossl_dsa(void);
00078 
00079 /*
00080  * DH
00081  */
00082 extern VALUE cDH;
00083 extern VALUE eDHError;
00084 extern DH *OSSL_DEFAULT_DH_512;
00085 extern DH *OSSL_DEFAULT_DH_1024;
00086 
00087 VALUE ossl_dh_new(EVP_PKEY *);
00088 void Init_ossl_dh(void);
00089 
00090 /*
00091  * EC
00092  */
00093 extern VALUE cEC;
00094 extern VALUE eECError;
00095 extern VALUE cEC_GROUP;
00096 extern VALUE eEC_GROUP;
00097 extern VALUE cEC_POINT;
00098 extern VALUE eEC_POINT;
00099 VALUE ossl_ec_new(EVP_PKEY *);
00100 void Init_ossl_ec(void);
00101 
00102 
00103 #define OSSL_PKEY_BN(keytype, name)                                     \
00104 /*                                                                      \
00105  *  call-seq:                                                           \
00106  *     key.##name -> aBN                                                \
00107  */                                                                     \
00108 static VALUE ossl_##keytype##_get_##name(VALUE self)                    \
00109 {                                                                       \
00110         EVP_PKEY *pkey;                                                 \
00111         BIGNUM *bn;                                                     \
00112                                                                         \
00113         GetPKey(self, pkey);                                            \
00114         bn = pkey->pkey.keytype->name;                                  \
00115         if (bn == NULL)                                                 \
00116                 return Qnil;                                            \
00117         return ossl_bn_new(bn);                                         \
00118 }                                                                       \
00119 /*                                                                      \
00120  *  call-seq:                                                           \
00121  *     key.##name = bn -> bn                                            \
00122  */                                                                     \
00123 static VALUE ossl_##keytype##_set_##name(VALUE self, VALUE bignum)      \
00124 {                                                                       \
00125         EVP_PKEY *pkey;                                                 \
00126         BIGNUM *bn;                                                     \
00127                                                                         \
00128         GetPKey(self, pkey);                                            \
00129         if (NIL_P(bignum)) {                                            \
00130                 BN_clear_free(pkey->pkey.keytype->name);                \
00131                 pkey->pkey.keytype->name = NULL;                        \
00132                 return Qnil;                                            \
00133         }                                                               \
00134                                                                         \
00135         bn = GetBNPtr(bignum);                                          \
00136         if (pkey->pkey.keytype->name == NULL)                           \
00137                 pkey->pkey.keytype->name = BN_new();                    \
00138         if (pkey->pkey.keytype->name == NULL)                           \
00139                 ossl_raise(eBNError, NULL);                             \
00140         if (BN_copy(pkey->pkey.keytype->name, bn) == NULL)              \
00141                 ossl_raise(eBNError, NULL);                             \
00142         return bignum;                                                  \
00143 }
00144 
00145 #define DEF_OSSL_PKEY_BN(class, keytype, name)                          \
00146 do {                                                                    \
00147         rb_define_method((class), #name, ossl_##keytype##_get_##name, 0);       \
00148         rb_define_method((class), #name "=", ossl_##keytype##_set_##name, 1);\
00149 } while (0)
00150 
00151 #endif /* _OSSL_PKEY_H_ */
00152