Ruby  3.1.0dev(2021-09-10revisionb76ad15ed0da636161de0243c547ee1e6fc95681)
ossl_pkey.h
Go to the documentation of this file.
1 /*
2  * 'OpenSSL for Ruby' project
3  * Copyright (C) 2001 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 #if !defined(OSSL_PKEY_H)
11 #define OSSL_PKEY_H
12 
13 extern VALUE mPKey;
14 extern VALUE cPKey;
15 extern VALUE ePKeyError;
17 
18 #define OSSL_PKEY_SET_PRIVATE(obj) rb_iv_set((obj), "private", Qtrue)
19 #define OSSL_PKEY_SET_PUBLIC(obj) rb_iv_set((obj), "private", Qfalse)
20 #define OSSL_PKEY_IS_PRIVATE(obj) (rb_iv_get((obj), "private") == Qtrue)
21 
22 #define NewPKey(klass) \
23  TypedData_Wrap_Struct((klass), &ossl_evp_pkey_type, 0)
24 #define SetPKey(obj, pkey) do { \
25  if (!(pkey)) { \
26  rb_raise(rb_eRuntimeError, "PKEY wasn't initialized!"); \
27  } \
28  RTYPEDDATA_DATA(obj) = (pkey); \
29  OSSL_PKEY_SET_PUBLIC(obj); \
30 } while (0)
31 #define GetPKey(obj, pkey) do {\
32  TypedData_Get_Struct((obj), EVP_PKEY, &ossl_evp_pkey_type, (pkey)); \
33  if (!(pkey)) { \
34  rb_raise(rb_eRuntimeError, "PKEY wasn't initialized!");\
35  } \
36 } while (0)
37 
38 VALUE ossl_pkey_new(EVP_PKEY *);
39 void ossl_pkey_check_public_key(const EVP_PKEY *);
40 EVP_PKEY *ossl_pkey_read_generic(BIO *, VALUE);
41 EVP_PKEY *GetPKeyPtr(VALUE);
42 EVP_PKEY *DupPKeyPtr(VALUE);
43 EVP_PKEY *GetPrivPKeyPtr(VALUE);
44 
45 /*
46  * Serializes _self_ in X.509 SubjectPublicKeyInfo format and returns the
47  * resulting String. Sub-classes use this when overriding #to_der.
48  */
49 VALUE ossl_pkey_export_spki(VALUE self, int to_der);
50 /*
51  * Serializes the private key _self_ in the traditional private key format
52  * and returns the resulting String. Sub-classes use this when overriding
53  * #to_der.
54  */
56  int to_der);
57 
58 void Init_ossl_pkey(void);
59 
60 /*
61  * RSA
62  */
63 extern VALUE cRSA;
64 extern VALUE eRSAError;
65 
66 void Init_ossl_rsa(void);
67 
68 /*
69  * DSA
70  */
71 extern VALUE cDSA;
72 extern VALUE eDSAError;
73 
74 void Init_ossl_dsa(void);
75 
76 /*
77  * DH
78  */
79 extern VALUE cDH;
80 extern VALUE eDHError;
81 
82 void Init_ossl_dh(void);
83 
84 /*
85  * EC
86  */
87 extern VALUE cEC;
88 extern VALUE eECError;
89 extern VALUE cEC_GROUP;
90 extern VALUE eEC_GROUP;
91 extern VALUE cEC_POINT;
92 extern VALUE eEC_POINT;
93 VALUE ossl_ec_new(EVP_PKEY *);
94 void Init_ossl_ec(void);
95 
96 #define OSSL_PKEY_BN_DEF_GETTER0(_keytype, _type, _name, _get) \
97 /* \
98  * call-seq: \
99  * _keytype##.##_name -> aBN \
100  */ \
101 static VALUE ossl_##_keytype##_get_##_name(VALUE self) \
102 { \
103  _type *obj; \
104  const BIGNUM *bn; \
105  \
106  Get##_type(self, obj); \
107  _get; \
108  if (bn == NULL) \
109  return Qnil; \
110  return ossl_bn_new(bn); \
111 }
112 
113 #define OSSL_PKEY_BN_DEF_GETTER3(_keytype, _type, _group, a1, a2, a3) \
114  OSSL_PKEY_BN_DEF_GETTER0(_keytype, _type, a1, \
115  _type##_get0_##_group(obj, &bn, NULL, NULL)) \
116  OSSL_PKEY_BN_DEF_GETTER0(_keytype, _type, a2, \
117  _type##_get0_##_group(obj, NULL, &bn, NULL)) \
118  OSSL_PKEY_BN_DEF_GETTER0(_keytype, _type, a3, \
119  _type##_get0_##_group(obj, NULL, NULL, &bn))
120 
121 #define OSSL_PKEY_BN_DEF_GETTER2(_keytype, _type, _group, a1, a2) \
122  OSSL_PKEY_BN_DEF_GETTER0(_keytype, _type, a1, \
123  _type##_get0_##_group(obj, &bn, NULL)) \
124  OSSL_PKEY_BN_DEF_GETTER0(_keytype, _type, a2, \
125  _type##_get0_##_group(obj, NULL, &bn))
126 
127 #define OSSL_PKEY_BN_DEF_SETTER3(_keytype, _type, _group, a1, a2, a3) \
128 /* \
129  * call-seq: \
130  * _keytype##.set_##_group(a1, a2, a3) -> self \
131  */ \
132 static VALUE ossl_##_keytype##_set_##_group(VALUE self, VALUE v1, VALUE v2, VALUE v3) \
133 { \
134  _type *obj; \
135  BIGNUM *bn1 = NULL, *orig_bn1 = NIL_P(v1) ? NULL : GetBNPtr(v1);\
136  BIGNUM *bn2 = NULL, *orig_bn2 = NIL_P(v2) ? NULL : GetBNPtr(v2);\
137  BIGNUM *bn3 = NULL, *orig_bn3 = NIL_P(v3) ? NULL : GetBNPtr(v3);\
138  \
139  Get##_type(self, obj); \
140  if ((orig_bn1 && !(bn1 = BN_dup(orig_bn1))) || \
141  (orig_bn2 && !(bn2 = BN_dup(orig_bn2))) || \
142  (orig_bn3 && !(bn3 = BN_dup(orig_bn3)))) { \
143  BN_clear_free(bn1); \
144  BN_clear_free(bn2); \
145  BN_clear_free(bn3); \
146  ossl_raise(eBNError, NULL); \
147  } \
148  \
149  if (!_type##_set0_##_group(obj, bn1, bn2, bn3)) { \
150  BN_clear_free(bn1); \
151  BN_clear_free(bn2); \
152  BN_clear_free(bn3); \
153  ossl_raise(ePKeyError, #_type"_set0_"#_group); \
154  } \
155  return self; \
156 }
157 
158 #define OSSL_PKEY_BN_DEF_SETTER2(_keytype, _type, _group, a1, a2) \
159 /* \
160  * call-seq: \
161  * _keytype##.set_##_group(a1, a2) -> self \
162  */ \
163 static VALUE ossl_##_keytype##_set_##_group(VALUE self, VALUE v1, VALUE v2) \
164 { \
165  _type *obj; \
166  BIGNUM *bn1 = NULL, *orig_bn1 = NIL_P(v1) ? NULL : GetBNPtr(v1);\
167  BIGNUM *bn2 = NULL, *orig_bn2 = NIL_P(v2) ? NULL : GetBNPtr(v2);\
168  \
169  Get##_type(self, obj); \
170  if ((orig_bn1 && !(bn1 = BN_dup(orig_bn1))) || \
171  (orig_bn2 && !(bn2 = BN_dup(orig_bn2)))) { \
172  BN_clear_free(bn1); \
173  BN_clear_free(bn2); \
174  ossl_raise(eBNError, NULL); \
175  } \
176  \
177  if (!_type##_set0_##_group(obj, bn1, bn2)) { \
178  BN_clear_free(bn1); \
179  BN_clear_free(bn2); \
180  ossl_raise(ePKeyError, #_type"_set0_"#_group); \
181  } \
182  return self; \
183 }
184 
185 #define OSSL_PKEY_BN_DEF3(_keytype, _type, _group, a1, a2, a3) \
186  OSSL_PKEY_BN_DEF_GETTER3(_keytype, _type, _group, a1, a2, a3) \
187  OSSL_PKEY_BN_DEF_SETTER3(_keytype, _type, _group, a1, a2, a3)
188 
189 #define OSSL_PKEY_BN_DEF2(_keytype, _type, _group, a1, a2) \
190  OSSL_PKEY_BN_DEF_GETTER2(_keytype, _type, _group, a1, a2) \
191  OSSL_PKEY_BN_DEF_SETTER2(_keytype, _type, _group, a1, a2)
192 
193 #define DEF_OSSL_PKEY_BN(class, keytype, name) \
194  rb_define_method((class), #name, ossl_##keytype##_get_##name, 0)
195 
196 #endif /* OSSL_PKEY_H */
mPKey
VALUE mPKey
Definition: ossl_pkey.c:15
Init_ossl_rsa
void Init_ossl_rsa(void)
Definition: ossl_pkey_rsa.c:491
ossl_pkey_export_traditional
VALUE ossl_pkey_export_traditional(int argc, VALUE *argv, VALUE self, int to_der)
Definition: ossl_pkey.c:580
cDSA
VALUE cDSA
Definition: ossl_pkey_dsa.c:43
ePKeyError
VALUE ePKeyError
Definition: ossl_pkey.c:17
argv
char ** argv
Definition: ruby.c:243
GetPKeyPtr
EVP_PKEY * GetPKeyPtr(VALUE)
Definition: ossl_pkey.c:432
Init_ossl_dh
void Init_ossl_dh(void)
Definition: ossl_pkey_dh.c:331
ossl_pkey_new
VALUE ossl_pkey_new(EVP_PKEY *)
Definition: ossl_pkey.c:67
ossl_pkey_read_generic
EVP_PKEY * ossl_pkey_read_generic(BIO *, VALUE)
Definition: ossl_pkey.c:82
cEC_POINT
VALUE cEC_POINT
Definition: ossl_pkey_ec.c:47
cDH
VALUE cDH
Definition: ossl_pkey_dh.c:29
ossl_ec_new
VALUE ossl_ec_new(EVP_PKEY *)
cEC_GROUP
VALUE cEC_GROUP
Definition: ossl_pkey_ec.c:45
GetPrivPKeyPtr
EVP_PKEY * GetPrivPKeyPtr(VALUE)
Definition: ossl_pkey.c:442
eRSAError
VALUE eRSAError
Definition: ossl_pkey_rsa.c:45
Init_ossl_dsa
void Init_ossl_dsa(void)
Definition: ossl_pkey_dsa.c:288
ossl_evp_pkey_type
const rb_data_type_t ossl_evp_pkey_type
Definition: ossl_pkey.c:29
ossl_pkey_check_public_key
void ossl_pkey_check_public_key(const EVP_PKEY *)
Definition: ossl_pkey.c:392
Init_ossl_pkey
void Init_ossl_pkey(void)
Definition: ossl_pkey.c:1399
cPKey
VALUE cPKey
Definition: ossl_pkey.c:16
VALUE
unsigned long VALUE
Definition: value.h:38
Init_ossl_ec
void Init_ossl_ec(void)
Definition: ossl_pkey_ec.c:1462
eDHError
VALUE eDHError
Definition: ossl_pkey_dh.c:30
eEC_GROUP
VALUE eEC_GROUP
Definition: ossl_pkey_ec.c:46
eECError
VALUE eECError
Definition: ossl_pkey_ec.c:44
cRSA
VALUE cRSA
Definition: ossl_pkey_rsa.c:44
ossl_pkey_export_spki
VALUE ossl_pkey_export_spki(VALUE self, int to_der)
Definition: ossl_pkey.c:695
cEC
VALUE cEC
Definition: ossl_pkey_ec.c:43
argc
int argc
Definition: ruby.c:242
DupPKeyPtr
EVP_PKEY * DupPKeyPtr(VALUE)
Definition: ossl_pkey.c:462
rb_data_type_struct
Definition: rtypeddata.h:70
eEC_POINT
VALUE eEC_POINT
Definition: ossl_pkey_ec.c:48
eDSAError
VALUE eDSAError
Definition: ossl_pkey_dsa.c:44