Ruby  2.5.0dev(2017-10-22revision60238)
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 /*
60  * Type conversions
61  */
62 #if !defined(NUM2UINT64T) /* in case Ruby starts to provide */
63 # if SIZEOF_LONG == 8
64 # define NUM2UINT64T(x) ((uint64_t)NUM2ULONG(x))
65 # elif defined(HAVE_LONG_LONG) && SIZEOF_LONG_LONG == 8
66 # define NUM2UINT64T(x) ((uint64_t)NUM2ULL(x))
67 # else
68 # error "unknown platform; no 64-bit width integer"
69 # endif
70 #endif
71 
72 /*
73  * Data Conversion
74  */
75 STACK_OF(X509) *ossl_x509_ary2sk(VALUE);
76 STACK_OF(X509) *ossl_protect_x509_ary2sk(VALUE,int*);
77 VALUE ossl_x509_sk2ary(const STACK_OF(X509) *certs);
78 VALUE ossl_x509crl_sk2ary(const STACK_OF(X509_CRL) *crl);
79 VALUE ossl_x509name_sk2ary(const STACK_OF(X509_NAME) *names);
80 VALUE ossl_buf2str(char *buf, int len);
81 VALUE ossl_str_new(const char *, long, int *);
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 NORETURN(void ossl_raise(VALUE, const char *, ...));
119 /* Clear OpenSSL error queue. If dOSSL is set, rb_warn() them. */
120 void ossl_clear_error(void);
121 
122 /*
123  * String to DER String
124  */
127 
128 /*
129  * Debug
130  */
131 extern VALUE dOSSL;
132 
133 #if defined(HAVE_VA_ARGS_MACRO)
134 #define OSSL_Debug(...) do { \
135  if (dOSSL == Qtrue) { \
136  fprintf(stderr, "OSSL_DEBUG: "); \
137  fprintf(stderr, __VA_ARGS__); \
138  fprintf(stderr, " [%s:%d]\n", __FILE__, __LINE__); \
139  } \
140 } while (0)
141 
142 #else
143 void ossl_debug(const char *, ...);
144 #define OSSL_Debug ossl_debug
145 #endif
146 
147 /*
148  * Include all parts
149  */
150 #include "openssl_missing.h"
151 #include "ruby_missing.h"
152 #include "ossl_asn1.h"
153 #include "ossl_bio.h"
154 #include "ossl_bn.h"
155 #include "ossl_cipher.h"
156 #include "ossl_config.h"
157 #include "ossl_digest.h"
158 #include "ossl_hmac.h"
159 #include "ossl_ns_spki.h"
160 #include "ossl_ocsp.h"
161 #include "ossl_pkcs12.h"
162 #include "ossl_pkcs7.h"
163 #include "ossl_pkey.h"
164 #include "ossl_rand.h"
165 #include "ossl_ssl.h"
166 #include "ossl_version.h"
167 #include "ossl_x509.h"
168 #include "ossl_engine.h"
169 #include "ossl_kdf.h"
170 
171 void Init_openssl(void);
172 
173 #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_ary2sk(VALUE)
int ossl_pem_passwd_cb(char *, int, int, void *)
Definition: ossl.c:177
void ossl_debug(const char *,...)
Definition: ossl.c:361
VALUE ossl_x509crl_sk2ary(const STACK_OF(X509_CRL) *crl)
VALUE ossl_buf2str(char *buf, int len)
Definition: ossl.c:120
void Init_openssl(void)
Definition: ossl.c:1100
VALUE ossl_to_der_if_possible(VALUE)
Definition: ossl.c:255
unsigned char buf[MIME_BUF_SIZE]
Definition: nkf.c:4309
unsigned long VALUE
Definition: ruby.h:85
register unsigned int len
Definition: zonetab.h:51
VALUE ossl_to_der(VALUE)
Definition: ossl.c:244
VALUE mOSSL
Definition: ossl.c:231
void ossl_raise(VALUE exc, const char *fmt,...)
Definition: ossl.c:293
VALUE dOSSL
Definition: ossl.c:357
VALUE ossl_str_new(const char *, long, int *)
Definition: ossl.c:101
VALUE ossl_pem_passwd_value(VALUE)
Definition: ossl.c:151
VALUE eOSSLError
Definition: ossl.c:236
NORETURN(void ossl_raise(VALUE, const char *,...))
void ossl_bin2hex(unsigned char *in, char *out, size_t len)
Definition: ossl.c:133
void ossl_clear_error(void)
Definition: ossl.c:304