Skip to content

Commit 9eb38c6

Browse files
author
Michal Rokos
committed
HMAC and SPKI cleanups & ports
1 parent 99402ac commit 9eb38c6

File tree

11 files changed

+238
-149
lines changed

11 files changed

+238
-149
lines changed

ChangeLog

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,17 @@ ChangeLog for
33

44
### CHANGE LOG ###
55

6+
Wed, 5 Jun 2002 10:46:17 +0200 -- Michal Rokos <[email protected]>
7+
* ns_spki.h: NEW (bits from ossl.h)
8+
* ns_spki.c: cleanup & port to Ruby 1.8 interface
9+
10+
Wed, 5 Jun 2002 10:14:54 +0200 -- Michal Rokos <[email protected]>
11+
* hmac.h: NEW (bits from ossl.h)
12+
* ossl.c: helper function 'string2hex'
13+
* hmac.c: use 'string2hex'
14+
* digest.c: ditto.
15+
* hmac:c: cleanup
16+
617
Tue, 4 Jun 2002 23:26:07 +0200 -- Michal Rokos <[email protected]>
718
* bn.c: cleanup (remove oddly initialized vars)
819
* config.c: ditto.

ossl.c

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,39 @@ time_to_time_t(VALUE time)
8989
return t.tv_sec;
9090
}
9191

92+
/*
93+
* String to HEXString conversion
94+
*/
95+
int
96+
string2hex(char *buf, int buf_len, char **hexbuf, int *hexbuf_len)
97+
{
98+
static const char hex[]="0123456789abcdef";
99+
int i, len = 2 * buf_len;
100+
101+
if (buf_len < 0 || len < buf_len) { /* PARANOIA? */
102+
return -1;
103+
}
104+
if (!hexbuf) {
105+
if (hexbuf_len) {
106+
*hexbuf_len = len;
107+
}
108+
return len;
109+
}
110+
if (!(*hexbuf = OPENSSL_malloc(len + 1))) {
111+
return -1;
112+
}
113+
for (i = 0; i < buf_len; i++) {
114+
(*hexbuf)[2 * i] = hex[((unsigned char)buf[i]) >> 4];
115+
(*hexbuf)[2 * i + 1] = hex[buf[i] & 0x0f];
116+
}
117+
(*hexbuf)[2 * i] = '\0';
118+
119+
if (hexbuf_len) {
120+
*hexbuf_len = len;
121+
}
122+
return len;
123+
}
124+
92125
/*
93126
* Modules
94127
*/
@@ -125,7 +158,6 @@ Init_openssl()
125158
* Universe of Modules
126159
*/
127160
mOSSL = rb_define_module("OpenSSL");
128-
mNetscape = rb_define_module_under(mOSSL, "Netscape");
129161
mPKCS7 = rb_define_module_under(mOSSL, "PKCS7");
130162
mPKey = rb_define_module_under(mOSSL, "PKey");
131163
mRandom = rb_define_module_under(mOSSL, "Random");
@@ -150,11 +182,11 @@ Init_openssl()
150182
Init_ossl_cipher();
151183
Init_ossl_config();
152184
Init_ossl_digest();
153-
Init_ossl_hmac(mOSSL);
185+
Init_ossl_hmac();
186+
Init_ossl_ns_spki();
154187
Init_ossl_pkcs7(mPKCS7);
155188
Init_ossl_pkey(mPKey);
156189
Init_ossl_rand(mRandom);
157-
Init_ossl_spki(mNetscape);
158190
Init_ossl_ssl(mSSL);
159191
Init_ossl_x509();
160192
}

ossl.h

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ extern "C" {
5555
* Common Module
5656
*/
5757
extern VALUE mOSSL;
58-
extern VALUE mNetscape;
5958
extern VALUE mPKCS7;
6059
extern VALUE mPKey;
6160
extern VALUE mRandom;
@@ -69,8 +68,6 @@ VALUE eOSSLError;
6968
/*
7069
* Classes
7170
*/
72-
extern VALUE cSPKI;
73-
extern VALUE eSPKIError;
7471
extern VALUE eRandomError;
7572
extern VALUE cSSLSocket;
7673
extern VALUE eSSLError;
@@ -88,9 +85,6 @@ extern VALUE eDHError;
8885
extern VALUE cPKCS7;
8986
extern VALUE cPKCS7SignerInfo;
9087
extern VALUE ePKCS7Error;
91-
/* HMAC */
92-
extern VALUE cHMAC;
93-
extern VALUE eHMACError;
9488

9589
/*
9690
* CheckTypes
@@ -107,6 +101,11 @@ void ossl_check_instance(VALUE, VALUE);
107101
VALUE asn1time_to_time(ASN1_UTCTIME *);
108102
time_t time_to_time_t(VALUE);
109103

104+
/*
105+
* String to HEXString conversion
106+
*/
107+
int string2hex(char *, int, char **, int *);
108+
110109
/*
111110
* ERRor messages
112111
*/
@@ -195,20 +194,19 @@ void Init_ossl_pkcs7(VALUE);
195194
/*
196195
* HMAC
197196
*/
198-
void Init_ossl_hmac(VALUE);
199197

200198
#include "openssl_missing.h"
201199
#include "ossl_bn.h"
202200
#include "ossl_cipher.h"
203201
#include "ossl_config.h"
204202
#include "ossl_digest.h"
203+
#include "ossl_hmac.h"
204+
#include "ossl_ns_spki.h"
205205
/*
206206
* TODO
207-
#include "ossl_hmac.h"
208207
#include "ossl_pkcs7.h"
209208
#include "ossl_pkey.h"
210209
#include "ossl_rand.h"
211-
#include "ossl_spki.h"
212210
#include "ossl_ssl.h"
213211
*/
214212
#include "ossl_version.h"

ossl_bn.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
} \
2525
} while (0)
2626
#define SafeGetBN(obj, bn) do { \
27-
OSSL_Check_Instance(obj, cBN); \
27+
OSSL_Check_Kind(obj, cBN); \
2828
GetBN(obj, bn); \
2929
} while (0)
3030

ossl_cipher.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#define MakeCipher(obj, klass, ciphp) obj = Data_Make_Struct(klass, ossl_cipher, 0, ossl_cipher_free, ciphp)
1414
#define GetCipher(obj, ciphp) Data_Get_Struct(obj, ossl_cipher, ciphp)
1515
#define SafeGetCipher(obj, ciphp) do { \
16-
OSSL_Check_Instance(obj, cCipher); \
16+
OSSL_Check_Kind(obj, cCipher); \
1717
GetCipher(obj, ciphp); \
1818
} while (0)
1919

ossl_digest.c

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
} \
2424
} while (0)
2525
#define SafeGetDigest(obj, ctx) do { \
26-
OSSL_Check_Instance(obj, cDigest); \
26+
OSSL_Check_Kind(obj, cDigest); \
2727
GetDigest(obj, ctx); \
2828
} while (0)
2929

@@ -96,61 +96,57 @@ ossl_digest_update(VALUE self, VALUE data)
9696
return self;
9797
}
9898

99-
static VALUE
100-
ossl_digest_digest(VALUE self)
99+
static void
100+
digest_final(EVP_MD_CTX *ctx, char **buf, int *buf_len)
101101
{
102-
EVP_MD_CTX *ctx, final;
103-
char *digest_txt;
104-
int digest_len;
105-
VALUE digest;
106-
107-
GetDigest(self, ctx);
108-
102+
EVP_MD_CTX final;
103+
109104
if (!EVP_MD_CTX_copy(&final, ctx)) {
110105
OSSL_Raise(eDigestError, "");
111106
}
112-
if (!(digest_txt = OPENSSL_malloc(EVP_MD_CTX_size(&final)))) {
107+
if (!(*buf = OPENSSL_malloc(EVP_MD_CTX_size(&final)))) {
113108
OSSL_Raise(eDigestError, "Cannot allocate mem for digest");
114109
}
115-
EVP_DigestFinal(&final, digest_txt, &digest_len);
110+
EVP_DigestFinal(&final, *buf, buf_len);
111+
}
116112

117-
digest = rb_str_new(digest_txt, digest_len);
118-
OPENSSL_free(digest_txt);
113+
static VALUE
114+
ossl_digest_digest(VALUE self)
115+
{
116+
EVP_MD_CTX *ctx;
117+
char *buf;
118+
int buf_len;
119+
VALUE digest;
120+
121+
GetDigest(self, ctx);
122+
123+
digest_final(ctx, &buf, &buf_len);
124+
125+
digest = rb_str_new(buf, buf_len);
126+
OPENSSL_free(buf);
119127

120128
return digest;
121129
}
122130

123131
static VALUE
124132
ossl_digest_hexdigest(VALUE self)
125133
{
126-
EVP_MD_CTX *ctx, final;
127-
static const char hex[]="0123456789abcdef";
128-
char *digest_txt = NULL, *hexdigest_txt = NULL;
129-
int i, digest_len;
134+
EVP_MD_CTX *ctx;
135+
char *buf, *hexbuf;
136+
int buf_len;
130137
VALUE hexdigest;
131138

132139
GetDigest(self, ctx);
133140

134-
if (!EVP_MD_CTX_copy(&final, ctx)) {
135-
OSSL_Raise(eDigestError, "");
136-
}
137-
if (!(digest_txt = OPENSSL_malloc(EVP_MD_CTX_size(&final)))) {
138-
OSSL_Raise(eDigestError, "Cannot allocate memory for digest");
139-
}
140-
EVP_DigestFinal(&final, digest_txt, &digest_len);
141-
142-
if (!(hexdigest_txt = OPENSSL_malloc(2 * digest_len + 1))) {
143-
OPENSSL_free(digest_txt);
141+
digest_final(ctx, &buf, &buf_len);
142+
143+
if (string2hex(buf, buf_len, &hexbuf, NULL) != 2 * buf_len) {
144+
OPENSSL_free(buf);
144145
OSSL_Raise(eDigestError, "Memory alloc error");
145146
}
146-
for (i = 0; i < digest_len; i++) {
147-
hexdigest_txt[2 * i] = hex[((unsigned char)digest_txt[i]) >> 4];
148-
hexdigest_txt[2 * i + 1] = hex[digest_txt[i] & 0x0f];
149-
}
150-
hexdigest_txt[2 * i] = '\0';
151-
hexdigest = rb_str_new(hexdigest_txt, 2 * digest_len);
152-
OPENSSL_free(digest_txt);
153-
OPENSSL_free(hexdigest_txt);
147+
hexdigest = rb_str_new(hexbuf, 2 * buf_len);
148+
OPENSSL_free(buf);
149+
OPENSSL_free(hexbuf);
154150

155151
return hexdigest;
156152
}
@@ -240,7 +236,6 @@ Init_ossl_digest()
240236
rb_define_singleton_method(cDigest, "hexdigest", ossl_digest_s_hexdigest, 2);
241237

242238
rb_define_method(cDigest, "initialize", ossl_digest_initialize, 1);
243-
rb_enable_super(cDigest, "initialize");
244239

245240
rb_define_method(cDigest, "clone", ossl_digest_clone, 0);
246241

0 commit comments

Comments
 (0)