4 static int p5_ssl_ex_ssl_ptr=0;
5 static int p5_ssl_ex_ssl_info_callback=0;
6 static int p5_ssl_ex_ssl_ctx_ptr=0;
7 static int p5_ssl_ctx_ex_ssl_info_callback=0;
9 typedef struct ssl_ic_args_st {
14 static void p5_ssl_info_callback(ssl,mode,ret)
22 me=(SV *)SSL_get_ex_data(ssl,p5_ssl_ex_ssl_ptr);
23 cb=(SV *)SSL_get_ex_data(ssl,p5_ssl_ex_ssl_info_callback);
25 cb=(SV *)SSL_CTX_get_ex_data(
26 SSL_get_SSL_CTX(ssl),p5_ssl_ctx_ex_ssl_info_callback);
33 XPUSHs(sv_2mortal(newSViv(mode)));
34 XPUSHs(sv_2mortal(newSViv(ret)));
37 i=perl_call_sv(cb,G_DISCARD);
41 croak("Internal error in SSL p5_ssl_info_callback");
48 SSL_get_ex_new_index(0,"OpenSSL::SSL",ex_new,NULL,ex_cleanup);
49 p5_ssl_ex_ssl_info_callback=
50 SSL_get_ex_new_index(0,"ssl_info_callback",NULL,NULL,
52 p5_ssl_ex_ssl_ctx_ptr=
53 SSL_get_ex_new_index(0,"ssl_ctx_ptr",NULL,NULL,
55 p5_ssl_ctx_ex_ssl_info_callback=
56 SSL_CTX_get_ex_new_index(0,"ssl_ctx_info_callback",NULL,NULL,
61 MODULE = OpenSSL::SSL PACKAGE = OpenSSL::SSL::CTX PREFIX = p5_SSL_CTX_
72 pr_name("p5_SSL_CTX_new");
73 if ((items == 1) && SvPOK(ST(0)))
74 method=SvPV(ST(0),na);
75 else if ((items == 2) && SvPOK(ST(1)))
76 method=SvPV(ST(1),na);
78 croak("Usage: OpenSSL::SSL_CTX::new(type)");
80 if (strcmp(method,"SSLv3") == 0)
82 else if (strcmp(method,"SSLv3_client") == 0)
83 meth=SSLv3_client_method();
84 else if (strcmp(method,"SSLv3_server") == 0)
85 meth=SSLv3_server_method();
86 else if (strcmp(method,"SSLv23") == 0)
88 else if (strcmp(method,"SSLv23_client") == 0)
89 meth=SSLv23_client_method();
90 else if (strcmp(method,"SSLv23_server") == 0)
91 meth=SSLv23_server_method();
92 else if (strcmp(method,"SSLv2") == 0)
94 else if (strcmp(method,"SSLv2_client") == 0)
95 meth=SSLv2_client_method();
96 else if (strcmp(method,"SSLv2_server") == 0)
97 meth=SSLv2_server_method();
100 croak("Not passed a valid SSL method name, should be 'SSLv[23] [client|server]'");
103 PUSHs(sv_newmortal());
104 ctx=SSL_CTX_new(meth);
105 sv_setref_pv(ST(0), "OpenSSL::SSL::CTX", (void*)ctx);
108 p5_SSL_CTX_use_PrivateKey_file(ctx,file,...)
112 int i=SSL_FILETYPE_PEM;
115 pr_name("p5_SSL_CTX_use_PrivateKey_file");
117 croak("OpenSSL::SSL::CTX::use_PrivateKey_file(ssl_ctx,file[,type])");
121 if (strcmp(ptr,"der") == 0)
126 RETVAL=SSL_CTX_use_RSAPrivateKey_file(ctx,file,i);
131 p5_SSL_CTX_set_options(ctx,...)
138 pr_name("p5_SSL_CTX_set_options");
140 for (i=1; i<items; i++)
143 croak("Usage: OpenSSL::SSL_CTX::set_options(ssl_ctx[,option,value]+)");
145 if (strcmp(ptr,"-info_callback") == 0)
147 SSL_CTX_set_info_callback(ctx,
148 p5_ssl_info_callback);
149 sv=sv_mortalcopy(ST(i+1));
151 SSL_CTX_set_ex_data(ctx,
152 p5_ssl_ctx_ex_ssl_info_callback,
158 croak("OpenSSL::SSL_CTX::set_options(): unknown option");
163 p5_SSL_CTX_DESTROY(ctx)
168 pr_name_d("p5_SSL_CTX_DESTROY",ctx->references);
171 MODULE = OpenSSL::SSL PACKAGE = OpenSSL::SSL PREFIX = p5_SSL_
182 pr_name("p5_SSL_new");
183 if ((items != 1) && (items != 2))
184 croak("Usage: OpenSSL::SSL::new(ssl_ctx)");
185 if (sv_derived_from(ST(items-1),"OpenSSL::SSL::CTX"))
187 IV tmp = SvIV((SV*)SvRV(ST(items-1)));
192 croak("ssl_ctx is not of type OpenSSL::SSL::CTX");
195 PUSHs(sv_newmortal());
197 sv_setref_pv(ST(0), "OpenSSL::SSL", (void*)ssl);
199 /* Now this is being a little hairy, we keep a pointer to
200 * our perl reference. We need to do a different one
201 * to the one we return because it will have it's reference
202 * count droped to 0 apon return and if we up its reference
203 * count, it will never be DESTROYED */
205 SSL_set_ex_data(ssl,p5_ssl_ex_ssl_ptr,(char *)arg);
206 SvREFCNT_inc(sv_ctx);
207 SSL_set_ex_data(ssl,p5_ssl_ex_ssl_ctx_ptr,(char *)sv_ctx);
213 RETVAL=SSL_connect(ssl);
221 RETVAL=SSL_connect(ssl);
226 p5_SSL_sysread(ssl,in,num, ...)
245 croak("Offset outside string");
249 if ((num+offset) > olen)
251 SvGROW(in,num+offset+1);
253 memset(&(p[olen]),0,(num+offset)-olen+1);
257 i=SSL_read(ssl,p+offset,num);
260 SvCUR_set(in,offset+i);
265 p5_SSL_syswrite(ssl,in, ...)
277 len=SvOK(ST(2))?SvIV(ST(2)):in_len;
283 if (-offset > in_len)
284 croak("Offset outside string");
287 else if ((offset >= in_len) && (in_len > 0))
288 croak("Offset outside string");
290 if (len >= (in_len-offset))
296 RETVAL=SSL_write(ssl,ptr+offset,len);
301 p5_SSL_set_bio(ssl,bio)
306 SSL_set_bio(ssl,bio,bio);
309 p5_SSL_set_options(ssl,...)
316 pr_name("p5_SSL_set_options");
318 for (i=1; i<items; i++)
321 croak("Usage: OpenSSL::SSL::set_options(ssl[,option,value]+)");
323 if (strcmp(ptr,"-info_callback") == 0)
325 SSL_set_info_callback(ssl,
326 p5_ssl_info_callback);
327 sv=sv_mortalcopy(ST(i+1));
330 p5_ssl_ex_ssl_info_callback,(char *)sv);
333 else if (strcmp(ptr,"-connect_state") == 0)
335 SSL_set_connect_state(ssl);
337 else if (strcmp(ptr,"-accept_state") == 0)
339 SSL_set_accept_state(ssl);
343 croak("OpenSSL::SSL::set_options(): unknown option");
353 pr_name("p5_SSL_state");
355 PUSHs(sv_newmortal());
356 state=SSL_state(ssl);
357 sv_setpv(ST(0),SSL_state_string_long(ssl));
358 sv_setiv(ST(0),state);
365 pr_name_dd("p5_SSL_DESTROY",ssl->references,ssl->ctx->references);
366 fprintf(stderr,"SSL_DESTROY %d\n",ssl->references);
370 p5_SSL_references(ssl)
373 RETVAL=ssl->references;
378 p5_SSL_do_handshake(ssl)
381 RETVAL=SSL_do_handshake(ssl);
386 p5_SSL_renegotiate(ssl)
389 RETVAL=SSL_renegotiate(ssl);
397 RETVAL=SSL_shutdown(ssl);
402 p5_SSL_get_version(ssl)
405 RETVAL=SSL_get_version(ssl);
410 p5_SSL_get_current_cipher(ssl)
413 RETVAL=SSL_get_current_cipher(ssl);
418 p5_SSL_get_peer_certificate(ssl)
421 RETVAL=SSL_get_peer_certificate(ssl);
425 MODULE = OpenSSL::SSL PACKAGE = OpenSSL::SSL::CIPHER PREFIX = p5_SSL_CIPHER_
428 p5_SSL_CIPHER_get_bits(sc)
434 PUSHs(sv_newmortal());
435 PUSHs(sv_newmortal());
436 ret=SSL_CIPHER_get_bits(sc,&i);
437 sv_setiv(ST(0),(IV)ret);
438 sv_setiv(ST(1),(IV)i);
441 p5_SSL_CIPHER_get_version(sc)
444 RETVAL=SSL_CIPHER_get_version(sc);
449 p5_SSL_CIPHER_get_name(sc)
452 RETVAL=SSL_CIPHER_get_name(sc);
456 MODULE = OpenSSL::SSL PACKAGE = OpenSSL::BIO PREFIX = p5_BIO_
466 if ((i=BIO_get_ssl(bio,&ssl)) > 0)
468 ret=(SV *)SSL_get_ex_data(ssl,p5_ssl_ex_ssl_ptr);
469 ret=sv_mortalcopy(ret);