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