1+ #include <ruby.h>
2+ // #include <openssl/ossl.h>
3+ // #include <openssl/evp.h>
4+
5+ #define USM_LENGTH_EXPANDED_PASSPHRASE (1024 * 1024) /* 1Meg. */
6+ #define USM_LENGTH_KU_HASHBLOCK 64
7+
8+ // // from https://github.com/ruby/openssl/blob/master/ext/openssl/ossl_digest.c
9+ // static void
10+ // ossl_digest_free(void *ctx)
11+ // {
12+ // EVP_MD_CTX_destroy(ctx);
13+ // }
14+
15+ // static const rb_data_type_t ossl_digest_type = {
16+ // "OpenSSL/Digest",
17+ // {
18+ // 0, ossl_digest_free,
19+ // },
20+ // 0, 0, RUBY_TYPED_FREE_IMMEDIATELY,
21+ // };
22+
23+ // #define GetDigest(obj, ctx) do { \
24+ // TypedData_Get_Struct((obj), EVP_MD_CTX, &ossl_digest_type, (ctx)); \
25+ // if (!(ctx)) { \
26+ // ossl_raise(rb_eRuntimeError, "Digest CTX wasn't initialized!"); \
27+ // } \
28+ // } while (0)
29+
30+ static VALUE mNETSNMP ;
31+ static VALUE cNETSNMP_Sec_Params ;
32+
33+ // static VALUE NETSNMP_passkey(VALUE self, VALUE password)
34+ // {
35+ // char *P;
36+ // size_t P_len;
37+ // int nbytes = USM_LENGTH_EXPANDED_PASSPHRASE,
38+ // auth_type;
39+ // EVP_MD_CTX *ctx = NULL;
40+ // u_int i, pindex = 0;
41+ // u_char buf[USM_LENGTH_KU_HASHBLOCK], *bufp;
42+ // unsigned char *Ku;
43+ // size_t kulen;
44+ // VALUE digest;
45+
46+ // StringValue(password);
47+ // P = RSTRING_PTR(password);
48+ // P_len = RSTRING_LEN(password);
49+
50+ // digest = rb_funcall(self, rb_intern("digest"), 0);
51+ // if (NIL_P(digest)) {
52+ // // raise exception
53+ // }
54+ // rb_funcall(digest, rb_intern("reset"), 0);
55+
56+ // GetDigest(digest, ctx);
57+
58+ // while (nbytes > 0) {
59+ // bufp = buf;
60+ // for (i = 0; i < USM_LENGTH_KU_HASHBLOCK; i++) {
61+ // *bufp++ = P[pindex++ % P_len];
62+ // }
63+ // if (!EVP_DigestUpdate(ctx, buf, USM_LENGTH_KU_HASHBLOCK))
64+ // ossl_raise(eDigestError, "EVP_DigestUpdate");
65+
66+ // nbytes -= USM_LENGTH_KU_HASHBLOCK;
67+ // }
68+
69+ // if (!EVP_DigestFinal_ex(ctx, &Ku, &kulen))
70+ // ossl_raise(eDigestError, "EVP_DigestFinal_ex");
71+
72+ // memset(buf, 0, sizeof(buf));
73+
74+ // // TODO: trim to 16 bytes if auth protocol is md5
75+
76+ // return rb_usascii_str_new(Ku, kulen);
77+ // }
78+
79+
80+ static VALUE NETSNMP_expand_passphrase (VALUE self , VALUE password )
81+ {
82+ char * P ;
83+ size_t P_len ;
84+ int nbytes = USM_LENGTH_EXPANDED_PASSPHRASE ;
85+ u_int pindex = 0 ;
86+ u_char buf [USM_LENGTH_EXPANDED_PASSPHRASE ], * bufp ;
87+
88+ StringValue (password );
89+ P = RSTRING_PTR (password );
90+ P_len = RSTRING_LEN (password );
91+
92+ bufp = buf ;
93+ while (nbytes > 0 ) {
94+ * bufp ++ = P [pindex ++ % P_len ];
95+ // if (!EVP_DigestUpdate(ctx, buf, USM_LENGTH_KU_HASHBLOCK))
96+ // ossl_raise(eDigestError, "EVP_DigestUpdate");
97+
98+ nbytes -- ;
99+ }
100+
101+ // if (!EVP_DigestFinal_ex(ctx, &Ku, &kulen))
102+ // ossl_raise(eDigestError, "EVP_DigestFinal_ex");
103+
104+ // memset(buf, 0, sizeof(buf));
105+
106+ // TODO: trim to 16 bytes if auth protocol is md5
107+
108+ return rb_usascii_str_new ((unsigned char * ) buf , USM_LENGTH_EXPANDED_PASSPHRASE );
109+ }
110+
111+ void Init_netsnmp_ext ()
112+ {
113+ mNETSNMP = rb_define_module ("NETSNMP" );
114+ cNETSNMP_Sec_Params = rb_define_class_under (mNETSNMP , "SecurityParameters" , rb_cObject );
115+ // rb_define_method(cNETSNMP_Sec_Params, "passkey", NETSNMP_passkey, 1);
116+ rb_define_method (cNETSNMP_Sec_Params , "expand_passphrase" , NETSNMP_expand_passphrase , 1 );
117+ }
0 commit comments