1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
|
/*
* $Id$
* 'OpenSSL for Ruby' project
* Copyright (C) 2001-2002 Michal Rokos <m.rokos@sh.cvut.cz>
* All rights reserved.
*/
/*
* This program is licenced under the same licence as Ruby.
* (See the file 'LICENCE'.)
*/
/*
* Surpress dumb warning about implicit declaration of strptime on Linux
*/
#if defined(__linux__) || defined(linux)
# define _GNU_SOURCE
#endif
#include "ossl.h"
/*
* On Windows platform there is no strptime function
* implementation in strptime.c
*/
#if !defined(HAVE_STRPTIME)
# include "./missing/strptime.c"
#endif
/*
* Check Types
*/
void
ossl_check_kind(VALUE obj, VALUE klass)
{
if (!RTEST(rb_obj_is_kind_of(obj, klass))) {
rb_raise(rb_eTypeError, "wrong argument (%s)! (Expected kind of %s)", \
rb_class2name(CLASS_OF(obj)), rb_class2name(klass));
}
}
void
ossl_check_instance(VALUE obj, VALUE klass)
{
if (!RTEST(rb_obj_is_instance_of(obj, klass))) {
rb_raise(rb_eTypeError, "wrong argument (%s)! (Expected instance of %s)",\
rb_class2name(CLASS_OF(obj)), rb_class2name(klass));
}
}
/*
* DATE conversion
*/
VALUE
asn1time_to_time(ASN1_UTCTIME *time)
{
struct tm tm;
switch(time->type) {
case V_ASN1_UTCTIME:
if (!strptime(time->data, "%y%m%d%H%M%SZ", &tm)) {
rb_raise(rb_eTypeError, "bad UTCTIME format");
}
break;
case V_ASN1_GENERALIZEDTIME:
if (!strptime(time->data, "%Y%m%d%H%M%SZ", &tm)) {
rb_raise(rb_eTypeError, "bad GENERALIZEDTIME format" );
}
break;
default:
rb_raise(rb_eTypeError, "unknown time format");
}
/*
* QUESTION:
* return rb_time_new(mktime(gmtime(mktime(&tm))), 0);
* Is this better than following?
*/
return rb_time_new(mktime(&tm), 0);
}
/*
* This function is not exported in Ruby's *.h
*/
extern struct timeval rb_time_timeval(VALUE time);
time_t
time_to_time_t(VALUE time)
{
struct timeval t = rb_time_timeval(time);
return t.tv_sec;
}
/*
* String to HEXString conversion
*/
int
string2hex(char *buf, int buf_len, char **hexbuf, int *hexbuf_len)
{
static const char hex[]="0123456789abcdef";
int i, len = 2 * buf_len;
if (buf_len < 0 || len < buf_len) { /* PARANOIA? */
return -1;
}
if (!hexbuf) {
if (hexbuf_len) {
*hexbuf_len = len;
}
return len;
}
if (!(*hexbuf = OPENSSL_malloc(len + 1))) {
return -1;
}
for (i = 0; i < buf_len; i++) {
(*hexbuf)[2 * i] = hex[((unsigned char)buf[i]) >> 4];
(*hexbuf)[2 * i + 1] = hex[buf[i] & 0x0f];
}
(*hexbuf)[2 * i] = '\0';
if (hexbuf_len) {
*hexbuf_len = len;
}
return len;
}
/*
* Modules
*/
VALUE mOSSL;
VALUE mDigest;
VALUE mNetscape;
VALUE mPKey;
VALUE mRandom;
VALUE mSSL;
/*
* OpenSSLError < StandardError
*/
VALUE eOSSLError;
/*
* OSSL library init
*/
void
Init_openssl()
{
#if defined(OSSL_DEBUG)
CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
#endif
/*
* Init all digests, ciphers
*/
OpenSSL_add_all_algorithms();
ERR_load_crypto_strings();
/*
* Universe of Modules
*/
mOSSL = rb_define_module("OpenSSL");
mPKey = rb_define_module_under(mOSSL, "PKey");
mRandom = rb_define_module_under(mOSSL, "Random");
mSSL = rb_define_module_under(mOSSL, "SSL");
/*
* Constants
*/
rb_define_const(mOSSL, "VERSION", rb_str_new2(OSSL_VERSION));
rb_define_const(mOSSL, "OPENSSL_VERSION", rb_str_new2(OPENSSL_VERSION_TEXT));
/*
* Generic error,
* common for all classes under OpenSSL module
*/
eOSSLError = rb_define_class_under(mOSSL, "OpenSSLError", rb_eStandardError);
/*
* Init components
*/
Init_ossl_bn();
Init_ossl_cipher();
Init_ossl_config();
Init_ossl_digest();
Init_ossl_hmac();
Init_ossl_ns_spki();
Init_ossl_pkcs7();
Init_ossl_pkey(mPKey);
Init_ossl_rand(mRandom);
Init_ossl_ssl(mSSL);
Init_ossl_x509();
}
#if defined(OSSL_DEBUG)
/*
* Check if all symbols are OK with 'make LDSHARED=gcc all'
*/
int
main(int argc, char *argv[], char *env[])
{
return 0;
}
#endif /* OSSL_DEBUG */
|