Ruby 2.0.0p247(2013-06-27revision41674)

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

ossl_pkey.h
Go to the documentation of this file.
1 /*
2 * $Id: ossl_pkey.h 33634 2011-11-04 07:19:23Z nobu $
3 * 'OpenSSL for Ruby' project
4 * Copyright (C) 2001 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 #if !defined(_OSSL_PKEY_H_)
12 #define _OSSL_PKEY_H_
13
14 extern VALUE mPKey;
15 extern VALUE cPKey;
16 extern VALUE ePKeyError;
17 extern ID id_private_q;
18
19 #define OSSL_PKEY_SET_PRIVATE(obj) rb_iv_set((obj), "private", Qtrue)
20 #define OSSL_PKEY_SET_PUBLIC(obj) rb_iv_set((obj), "private", Qfalse)
21 #define OSSL_PKEY_IS_PRIVATE(obj) (rb_iv_get((obj), "private") == Qtrue)
22
23 #define WrapPKey(klass, obj, pkey) do { \
24 if (!(pkey)) { \
25 rb_raise(rb_eRuntimeError, "PKEY wasn't initialized!"); \
26 } \
27 (obj) = Data_Wrap_Struct((klass), 0, EVP_PKEY_free, (pkey)); \
28 OSSL_PKEY_SET_PUBLIC(obj); \
29 } while (0)
30 #define GetPKey(obj, pkey) do {\
31 Data_Get_Struct((obj), EVP_PKEY, (pkey));\
32 if (!(pkey)) { \
33 rb_raise(rb_eRuntimeError, "PKEY wasn't initialized!");\
34 } \
35 } while (0)
36 #define SafeGetPKey(obj, pkey) do { \
37 OSSL_Check_Kind((obj), cPKey); \
38 GetPKey((obj), (pkey)); \
39 } while (0)
40
41 void ossl_generate_cb(int, int, void *);
42 #define HAVE_BN_GENCB defined(HAVE_RSA_GENERATE_KEY_EX) || defined(HAVE_DH_GENERATE_PARAMETERS_EX) || defined(HAVE_DSA_GENERATE_PARAMETERS_EX)
43 #if HAVE_BN_GENCB
44 struct ossl_generate_cb_arg {
45 int yield;
46 int stop;
47 int state;
48 };
49 int ossl_generate_cb_2(int p, int n, BN_GENCB *cb);
50 void ossl_generate_cb_stop(void *ptr);
51 #endif
52
53 VALUE ossl_pkey_new(EVP_PKEY *);
54 VALUE ossl_pkey_new_from_file(VALUE);
55 EVP_PKEY *GetPKeyPtr(VALUE);
56 EVP_PKEY *DupPKeyPtr(VALUE);
57 EVP_PKEY *GetPrivPKeyPtr(VALUE);
58 EVP_PKEY *DupPrivPKeyPtr(VALUE);
59 void Init_ossl_pkey(void);
60
61 /*
62 * RSA
63 */
64 extern VALUE cRSA;
65 extern VALUE eRSAError;
66
67 VALUE ossl_rsa_new(EVP_PKEY *);
68 void Init_ossl_rsa(void);
69
70 /*
71 * DSA
72 */
73 extern VALUE cDSA;
74 extern VALUE eDSAError;
75
76 VALUE ossl_dsa_new(EVP_PKEY *);
77 void Init_ossl_dsa(void);
78
79 /*
80 * DH
81 */
82 extern VALUE cDH;
83 extern VALUE eDHError;
84 extern DH *OSSL_DEFAULT_DH_512;
85 extern DH *OSSL_DEFAULT_DH_1024;
86
87 VALUE ossl_dh_new(EVP_PKEY *);
88 void Init_ossl_dh(void);
89
90 /*
91 * EC
92 */
93 extern VALUE cEC;
94 extern VALUE eECError;
95 extern VALUE cEC_GROUP;
96 extern VALUE eEC_GROUP;
97 extern VALUE cEC_POINT;
98 extern VALUE eEC_POINT;
99 VALUE ossl_ec_new(EVP_PKEY *);
100 void Init_ossl_ec(void);
101
102
103 #define OSSL_PKEY_BN(keytype, name) \
104 /* \
105 * call-seq: \
106 * key.##name -> aBN \
107 */ \
108 static VALUE ossl_##keytype##_get_##name(VALUE self) \
109 { \
110 EVP_PKEY *pkey; \
111 BIGNUM *bn; \
112 \
113 GetPKey(self, pkey); \
114 bn = pkey->pkey.keytype->name; \
115 if (bn == NULL) \
116 return Qnil; \
117 return ossl_bn_new(bn); \
118 } \
119 /* \
120 * call-seq: \
121 * key.##name = bn -> bn \
122 */ \
123 static VALUE ossl_##keytype##_set_##name(VALUE self, VALUE bignum) \
124 { \
125 EVP_PKEY *pkey; \
126 BIGNUM *bn; \
127 \
128 GetPKey(self, pkey); \
129 if (NIL_P(bignum)) { \
130 BN_clear_free(pkey->pkey.keytype->name); \
131 pkey->pkey.keytype->name = NULL; \
132 return Qnil; \
133 } \
134 \
135 bn = GetBNPtr(bignum); \
136 if (pkey->pkey.keytype->name == NULL) \
137 pkey->pkey.keytype->name = BN_new(); \
138 if (pkey->pkey.keytype->name == NULL) \
139 ossl_raise(eBNError, NULL); \
140 if (BN_copy(pkey->pkey.keytype->name, bn) == NULL) \
141 ossl_raise(eBNError, NULL); \
142 return bignum; \
143 }
144
145 #define DEF_OSSL_PKEY_BN(class, keytype, name) \
146 do { \
147 rb_define_method((class), #name, ossl_##keytype##_get_##name, 0); \
148 rb_define_method((class), #name "=", ossl_##keytype##_set_##name, 1);\
149 } while (0)
150
151 #endif /* _OSSL_PKEY_H_ */
152