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