Ruby  2.7.0p0(2019-12-25revision647ee6f091eafcce70ffb75ddf7e121e192ab217)
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 <ruby.h>
17 #include <errno.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/crypto.h>
31 #if !defined(OPENSSL_NO_ENGINE)
32 # include <openssl/engine.h>
33 #endif
34 #if !defined(OPENSSL_NO_OCSP)
35 # include <openssl/ocsp.h>
36 #endif
37 #include <openssl/bn.h>
38 #include <openssl/rsa.h>
39 #include <openssl/dsa.h>
40 #include <openssl/evp.h>
41 #include <openssl/dh.h>
42 
43 /*
44  * Common Module
45  */
46 extern VALUE mOSSL;
47 
48 /*
49  * Common Error Class
50  */
51 extern VALUE eOSSLError;
52 
53 /*
54  * CheckTypes
55  */
56 #define OSSL_Check_Kind(obj, klass) do {\
57  if (!rb_obj_is_kind_of((obj), (klass))) {\
58  ossl_raise(rb_eTypeError, "wrong argument (%"PRIsVALUE")! (Expected kind of %"PRIsVALUE")",\
59  rb_obj_class(obj), (klass));\
60  }\
61 } while (0)
62 
63 /*
64  * Type conversions
65  */
66 #if !defined(NUM2UINT64T) /* in case Ruby starts to provide */
67 # if SIZEOF_LONG == 8
68 # define NUM2UINT64T(x) ((uint64_t)NUM2ULONG(x))
69 # elif defined(HAVE_LONG_LONG) && SIZEOF_LONG_LONG == 8
70 # define NUM2UINT64T(x) ((uint64_t)NUM2ULL(x))
71 # else
72 # error "unknown platform; no 64-bit width integer"
73 # endif
74 #endif
75 
76 /*
77  * Data Conversion
78  */
79 STACK_OF(X509) *ossl_x509_ary2sk(VALUE);
80 STACK_OF(X509) *ossl_protect_x509_ary2sk(VALUE,int*);
81 VALUE ossl_x509_sk2ary(const STACK_OF(X509) *certs);
82 VALUE ossl_x509crl_sk2ary(const STACK_OF(X509_CRL) *crl);
83 VALUE ossl_x509name_sk2ary(const STACK_OF(X509_NAME) *names);
84 VALUE ossl_buf2str(char *buf, int len);
85 VALUE ossl_str_new(const char *, long, int *);
86 #define ossl_str_adjust(str, p) \
87 do{\
88  long len = RSTRING_LEN(str);\
89  long newlen = (long)((p) - (unsigned char*)RSTRING_PTR(str));\
90  assert(newlen <= len);\
91  rb_str_set_len((str), newlen);\
92 }while(0)
93 /*
94  * Convert binary string to hex string. The caller is responsible for
95  * ensuring out has (2 * len) bytes of capacity.
96  */
97 void ossl_bin2hex(unsigned char *in, char *out, size_t len);
98 
99 /*
100  * Our default PEM callback
101  */
102 /* Convert the argument to String and validate the length. Note this may raise. */
104 /* Can be casted to pem_password_cb. If a password (String) is passed as the
105  * "arbitrary data" (typically the last parameter of PEM_{read,write}_
106  * functions), uses the value. If not, but a block is given, yields to it.
107  * If not either, fallbacks to PEM_def_callback() which reads from stdin. */
108 int ossl_pem_passwd_cb(char *, int, int, void *);
109 
110 /*
111  * Clear BIO* with this in PEM/DER fallback scenarios to avoid decoding
112  * errors piling up in OpenSSL::Errors
113  */
114 #define OSSL_BIO_reset(bio) do { \
115  (void)BIO_reset((bio)); \
116  ossl_clear_error(); \
117 } while (0)
118 
119 /*
120  * ERRor messages
121  */
122 NORETURN(void ossl_raise(VALUE, const char *, ...));
123 /* Clear OpenSSL error queue. If dOSSL is set, rb_warn() them. */
124 void ossl_clear_error(void);
125 
126 /*
127  * String to DER String
128  */
131 
132 /*
133  * Debug
134  */
135 extern VALUE dOSSL;
136 
137 #if defined(HAVE_VA_ARGS_MACRO)
138 #define OSSL_Debug(...) do { \
139  if (dOSSL == Qtrue) { \
140  fprintf(stderr, "OSSL_DEBUG: "); \
141  fprintf(stderr, __VA_ARGS__); \
142  fprintf(stderr, " [%s:%d]\n", __FILE__, __LINE__); \
143  } \
144 } while (0)
145 
146 #else
147 void ossl_debug(const char *, ...);
148 #define OSSL_Debug ossl_debug
149 #endif
150 
151 /*
152  * Include all parts
153  */
154 #include "openssl_missing.h"
155 #include "ruby_missing.h"
156 #include "ossl_asn1.h"
157 #include "ossl_bio.h"
158 #include "ossl_bn.h"
159 #include "ossl_cipher.h"
160 #include "ossl_config.h"
161 #include "ossl_digest.h"
162 #include "ossl_hmac.h"
163 #include "ossl_ns_spki.h"
164 #include "ossl_ocsp.h"
165 #include "ossl_pkcs12.h"
166 #include "ossl_pkcs7.h"
167 #include "ossl_pkey.h"
168 #include "ossl_rand.h"
169 #include "ossl_ssl.h"
170 #include "ossl_version.h"
171 #include "ossl_x509.h"
172 #include "ossl_engine.h"
173 #include "ossl_kdf.h"
174 
175 void Init_openssl(void);
176 
177 #endif /* _OSSL_H_ */
ossl_pem_passwd_value
VALUE ossl_pem_passwd_value(VALUE)
Definition: ossl.c:151
ossl_hmac.h
ossl_engine.h
ruby_missing.h
dOSSL
VALUE dOSSL
Definition: ossl.c:357
ossl_pem_passwd_cb
int ossl_pem_passwd_cb(char *, int, int, void *)
Definition: ossl.c:177
VALUE
unsigned long VALUE
Definition: ruby.h:102
ossl_buf2str
VALUE ossl_buf2str(char *buf, int len)
Definition: ossl.c:120
ossl_x509_sk2ary
int *VALUE ossl_x509_sk2ary(const STACK_OF(X509) *certs)
mOSSL
VALUE mOSSL
Definition: ossl.c:231
ossl_debug
void ossl_debug(const char *,...)
Definition: ossl.c:361
assert.h
ossl_version.h
eOSSLError
VALUE eOSSLError
Definition: ossl.c:236
ossl_to_der_if_possible
VALUE ossl_to_der_if_possible(VALUE)
Definition: ossl.c:255
ossl_x509.h
ossl_config.h
ossl_asn1.h
ossl_x509name_sk2ary
VALUE ossl_x509name_sk2ary(const STACK_OF(X509_NAME) *names)
ossl_ns_spki.h
openssl_missing.h
ossl_to_der
VALUE ossl_to_der(VALUE)
Definition: ossl.c:244
ruby.h
ossl_raise
void ossl_raise(VALUE exc, const char *fmt,...)
Definition: ossl.c:293
NORETURN
NORETURN(void ossl_raise(VALUE, const char *,...))
ossl_str_new
VALUE ossl_str_new(const char *, long, int *)
Definition: ossl.c:101
ossl_pkcs12.h
ossl_digest.h
buf
unsigned char buf[MIME_BUF_SIZE]
Definition: nkf.c:4322
ossl_bin2hex
void ossl_bin2hex(unsigned char *in, char *out, size_t len)
Definition: ossl.c:133
STACK_OF
STACK_OF(X509) *ossl_x509_ary2sk(VALUE)
names
st_table * names
Definition: encoding.c:59
ossl_ocsp.h
ossl_cipher.h
ossl_kdf.h
ossl_rand.h
io.h
ossl_pkey.h
ossl_bn.h
ossl_bio.h
ossl_clear_error
void ossl_clear_error(void)
Definition: ossl.c:304
len
uint8_t len
Definition: escape.c:17
ossl_pkcs7.h
ossl_ssl.h
thread.h
ossl_x509crl_sk2ary
VALUE ossl_x509crl_sk2ary(const STACK_OF(X509_CRL) *crl)
Init_openssl
void Init_openssl(void)
Definition: ossl.c:1100