Ruby  2.4.2p198(2017-09-14revision59899)
ossl.h
Go to the documentation of this file.
1 /*
2  * 'OpenSSL for Ruby' project
3  * Copyright (C) 2001-2002 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_H_)
11 #define _OSSL_H_
12 
13 #include RUBY_EXTCONF_H
14 
15 #include <assert.h>
16 #include <errno.h>
17 #include <ruby.h>
18 #include <ruby/io.h>
19 #include <ruby/thread.h>
20 #include <openssl/opensslv.h>
21 #include <openssl/err.h>
22 #include <openssl/asn1.h>
23 #include <openssl/x509v3.h>
24 #include <openssl/ssl.h>
25 #include <openssl/pkcs12.h>
26 #include <openssl/pkcs7.h>
27 #include <openssl/hmac.h>
28 #include <openssl/rand.h>
29 #include <openssl/conf.h>
30 #include <openssl/conf_api.h>
31 #include <openssl/crypto.h>
32 #if !defined(OPENSSL_NO_ENGINE)
33 # include <openssl/engine.h>
34 #endif
35 #if !defined(OPENSSL_NO_OCSP)
36 # include <openssl/ocsp.h>
37 #endif
38 
39 /*
40  * Common Module
41  */
42 extern VALUE mOSSL;
43 
44 /*
45  * Common Error Class
46  */
47 extern VALUE eOSSLError;
48 
49 /*
50  * CheckTypes
51  */
52 #define OSSL_Check_Kind(obj, klass) do {\
53  if (!rb_obj_is_kind_of((obj), (klass))) {\
54  ossl_raise(rb_eTypeError, "wrong argument (%"PRIsVALUE")! (Expected kind of %"PRIsVALUE")",\
55  rb_obj_class(obj), (klass));\
56  }\
57 } while (0)
58 
59 #define OSSL_Check_Instance(obj, klass) do {\
60  if (!rb_obj_is_instance_of((obj), (klass))) {\
61  ossl_raise(rb_eTypeError, "wrong argument (%"PRIsVALUE")! (Expected instance of %"PRIsVALUE")",\
62  rb_obj_class(obj), (klass));\
63  }\
64 } while (0)
65 
66 #define OSSL_Check_Same_Class(obj1, obj2) do {\
67  if (!rb_obj_is_instance_of((obj1), rb_obj_class(obj2))) {\
68  ossl_raise(rb_eTypeError, "wrong argument type");\
69  }\
70 } while (0)
71 
72 /*
73  * Data Conversion
74  */
75 STACK_OF(X509) *ossl_x509_ary2sk0(VALUE);
76 STACK_OF(X509) *ossl_x509_ary2sk(VALUE);
77 STACK_OF(X509) *ossl_protect_x509_ary2sk(VALUE,int*);
78 VALUE ossl_x509_sk2ary(const STACK_OF(X509) *certs);
79 VALUE ossl_x509crl_sk2ary(const STACK_OF(X509_CRL) *crl);
80 VALUE ossl_x509name_sk2ary(const STACK_OF(X509_NAME) *names);
81 VALUE ossl_buf2str(char *buf, int len);
82 #define ossl_str_adjust(str, p) \
83 do{\
84  long len = RSTRING_LEN(str);\
85  long newlen = (long)((p) - (unsigned char*)RSTRING_PTR(str));\
86  assert(newlen <= len);\
87  rb_str_set_len((str), newlen);\
88 }while(0)
89 /*
90  * Convert binary string to hex string. The caller is responsible for
91  * ensuring out has (2 * len) bytes of capacity.
92  */
93 void ossl_bin2hex(unsigned char *in, char *out, size_t len);
94 
95 /*
96  * Our default PEM callback
97  */
98 /* Convert the argument to String and validate the length. Note this may raise. */
100 /* Can be casted to pem_password_cb. If a password (String) is passed as the
101  * "arbitrary data" (typically the last parameter of PEM_{read,write}_
102  * functions), uses the value. If not, but a block is given, yields to it.
103  * If not either, fallbacks to PEM_def_callback() which reads from stdin. */
104 int ossl_pem_passwd_cb(char *, int, int, void *);
105 
106 /*
107  * Clear BIO* with this in PEM/DER fallback scenarios to avoid decoding
108  * errors piling up in OpenSSL::Errors
109  */
110 #define OSSL_BIO_reset(bio) do { \
111  (void)BIO_reset((bio)); \
112  ossl_clear_error(); \
113 } while (0)
114 
115 /*
116  * ERRor messages
117  */
118 #define OSSL_ErrMsg() ERR_reason_error_string(ERR_get_error())
119 NORETURN(void ossl_raise(VALUE, const char *, ...));
120 /* Clear OpenSSL error queue. If dOSSL is set, rb_warn() them. */
121 void ossl_clear_error(void);
122 
123 /*
124  * String to DER String
125  */
126 extern ID ossl_s_to_der;
129 
130 /*
131  * Debug
132  */
133 extern VALUE dOSSL;
134 
135 #if defined(HAVE_VA_ARGS_MACRO)
136 #define OSSL_Debug(...) do { \
137  if (dOSSL == Qtrue) { \
138  fprintf(stderr, "OSSL_DEBUG: "); \
139  fprintf(stderr, __VA_ARGS__); \
140  fprintf(stderr, " [%s:%d]\n", __FILE__, __LINE__); \
141  } \
142 } while (0)
143 
144 #define OSSL_Warning(fmt, ...) do { \
145  OSSL_Debug((fmt), ##__VA_ARGS__); \
146  rb_warning((fmt), ##__VA_ARGS__); \
147 } while (0)
148 
149 #define OSSL_Warn(fmt, ...) do { \
150  OSSL_Debug((fmt), ##__VA_ARGS__); \
151  rb_warn((fmt), ##__VA_ARGS__); \
152 } while (0)
153 #else
154 void ossl_debug(const char *, ...);
155 #define OSSL_Debug ossl_debug
156 #define OSSL_Warning rb_warning
157 #define OSSL_Warn rb_warn
158 #endif
159 
160 /*
161  * Include all parts
162  */
163 #include "openssl_missing.h"
164 #include "ruby_missing.h"
165 #include "ossl_asn1.h"
166 #include "ossl_bio.h"
167 #include "ossl_bn.h"
168 #include "ossl_cipher.h"
169 #include "ossl_config.h"
170 #include "ossl_digest.h"
171 #include "ossl_hmac.h"
172 #include "ossl_ns_spki.h"
173 #include "ossl_ocsp.h"
174 #include "ossl_pkcs12.h"
175 #include "ossl_pkcs7.h"
176 #include "ossl_pkcs5.h"
177 #include "ossl_pkey.h"
178 #include "ossl_rand.h"
179 #include "ossl_ssl.h"
180 #include "ossl_version.h"
181 #include "ossl_x509.h"
182 #include "ossl_engine.h"
183 
184 void Init_openssl(void);
185 
186 #endif /* _OSSL_H_ */
int *VALUE ossl_x509_sk2ary(const STACK_OF(X509) *certs)
st_table * names
Definition: encoding.c:58
VALUE ossl_x509name_sk2ary(const STACK_OF(X509_NAME) *names)
STACK_OF(X509) *ossl_x509_ary2sk0(VALUE)
int ossl_pem_passwd_cb(char *, int, int, void *)
Definition: ossl.c:159
void ossl_debug(const char *,...)
Definition: ossl.c:345
VALUE ossl_x509crl_sk2ary(const STACK_OF(X509_CRL) *crl)
VALUE ossl_buf2str(char *buf, int len)
Definition: ossl.c:101
void Init_openssl(void)
Definition: ossl.c:999
VALUE ossl_to_der_if_possible(VALUE)
Definition: ossl.c:237
unsigned char buf[MIME_BUF_SIZE]
Definition: nkf.c:4309
unsigned long ID
Definition: ruby.h:86
unsigned long VALUE
Definition: ruby.h:85
ID ossl_s_to_der
Definition: ossl.c:223
register unsigned int len
Definition: zonetab.h:51
VALUE ossl_to_der(VALUE)
Definition: ossl.c:226
VALUE mOSSL
Definition: ossl.c:213
void ossl_raise(VALUE exc, const char *fmt,...)
Definition: ossl.c:278
VALUE dOSSL
Definition: ossl.c:341
VALUE ossl_pem_passwd_value(VALUE)
Definition: ossl.c:133
VALUE eOSSLError
Definition: ossl.c:218
NORETURN(void ossl_raise(VALUE, const char *,...))
void ossl_bin2hex(unsigned char *in, char *out, size_t len)
Definition: ossl.c:115
void ossl_clear_error(void)
Definition: ossl.c:289