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/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 #include <openssl/bn.h> 39 #include <openssl/rsa.h> 40 #include <openssl/dsa.h> 41 #include <openssl/evp.h> 42 #include <openssl/dh.h> 43 44 /* 45 * Common Module 46 */ 47 extern VALUE mOSSL; 48 49 /* 50 * Common Error Class 51 */ 52 extern VALUE eOSSLError; 53 54 /* 55 * CheckTypes 56 */ 57 #define OSSL_Check_Kind(obj, klass) do {\ 58 if (!rb_obj_is_kind_of((obj), (klass))) {\ 59 ossl_raise(rb_eTypeError, "wrong argument (%"PRIsVALUE")! (Expected kind of %"PRIsVALUE")",\ 60 rb_obj_class(obj), (klass));\ 61 }\ 62 } while (0) 63 64 /* 65 * Type conversions 66 */ 67 #if !defined(NUM2UINT64T) /* in case Ruby starts to provide */ 68 # if SIZEOF_LONG == 8 69 # define NUM2UINT64T(x) ((uint64_t)NUM2ULONG(x)) 70 # elif defined(HAVE_LONG_LONG) && SIZEOF_LONG_LONG == 8 71 # define NUM2UINT64T(x) ((uint64_t)NUM2ULL(x)) 72 # else 73 # error "unknown platform; no 64-bit width integer" 74 # endif 75 #endif 76 77 /* 78 * Data Conversion 79 */ 80 STACK_OF(X509) *ossl_x509_ary2sk(VALUE); 81 STACK_OF(X509) *ossl_protect_x509_ary2sk(VALUE,int*); 82 VALUE ossl_x509_sk2ary(const STACK_OF(X509) *certs); 83 VALUE ossl_x509crl_sk2ary(const STACK_OF(X509_CRL) *crl); 84 VALUE ossl_x509name_sk2ary(const STACK_OF(X509_NAME) *names); 85 VALUE ossl_buf2str(char *buf, int len); 86 VALUE ossl_str_new(const char *, long, int *); 87 #define ossl_str_adjust(str, p) \ 88 do{\ 89 long len = RSTRING_LEN(str);\ 90 long newlen = (long)((p) - (unsigned char*)RSTRING_PTR(str));\ 91 assert(newlen <= len);\ 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. */ 104 VALUE ossl_pem_passwd_value(VALUE); 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 */ 130 VALUE ossl_to_der(VALUE); 131 VALUE ossl_to_der_if_possible(VALUE); 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 #include "ossl_version.h" 172 #include "ossl_x509.h" 173 #include "ossl_engine.h" 174 #include "ossl_kdf.h" 175 176 void Init_openssl(void); 177 178 #endif /* _OSSL_H_ */ 179