openssl_ssl.xs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. #include "openssl.h"
  2. static int p5_ssl_ex_ssl_ptr=0;
  3. static int p5_ssl_ex_ssl_info_callback=0;
  4. static int p5_ssl_ex_ssl_ctx_ptr=0;
  5. static int p5_ssl_ctx_ex_ssl_info_callback=0;
  6. typedef struct ssl_ic_args_st {
  7. SV *cb;
  8. SV *arg;
  9. } SSL_IC_ARGS;
  10. static void p5_ssl_info_callback(ssl,mode,ret)
  11. SSL *ssl;
  12. int mode;
  13. int ret;
  14. {
  15. int i;
  16. SV *me,*cb;
  17. me=(SV *)SSL_get_ex_data(ssl,p5_ssl_ex_ssl_ptr);
  18. cb=(SV *)SSL_get_ex_data(ssl,p5_ssl_ex_ssl_info_callback);
  19. if (cb == NULL)
  20. cb=(SV *)SSL_CTX_get_ex_data(
  21. SSL_get_SSL_CTX(ssl),p5_ssl_ctx_ex_ssl_info_callback);
  22. if (cb != NULL)
  23. {
  24. dSP;
  25. PUSHMARK(sp);
  26. XPUSHs(me);
  27. XPUSHs(sv_2mortal(newSViv(mode)));
  28. XPUSHs(sv_2mortal(newSViv(ret)));
  29. PUTBACK;
  30. i=perl_call_sv(cb,G_DISCARD);
  31. }
  32. else
  33. {
  34. croak("Internal error in SSL p5_ssl_info_callback");
  35. }
  36. }
  37. int boot_ssl()
  38. {
  39. p5_ssl_ex_ssl_ptr=
  40. SSL_get_ex_new_index(0,"OpenSSL::SSL",ex_new,NULL,ex_cleanup);
  41. p5_ssl_ex_ssl_info_callback=
  42. SSL_get_ex_new_index(0,"ssl_info_callback",NULL,NULL,
  43. ex_cleanup);
  44. p5_ssl_ex_ssl_ctx_ptr=
  45. SSL_get_ex_new_index(0,"ssl_ctx_ptr",NULL,NULL,
  46. ex_cleanup);
  47. p5_ssl_ctx_ex_ssl_info_callback=
  48. SSL_CTX_get_ex_new_index(0,"ssl_ctx_info_callback",NULL,NULL,
  49. ex_cleanup);
  50. return(1);
  51. }
  52. MODULE = OpenSSL::SSL PACKAGE = OpenSSL::SSL::CTX PREFIX = p5_SSL_CTX_
  53. PROTOTYPES: ENABLE
  54. VERSIONCHECK: DISABLE
  55. void
  56. p5_SSL_CTX_new(...)
  57. PREINIT:
  58. SSL_METHOD *meth;
  59. SSL_CTX *ctx;
  60. char *method;
  61. PPCODE:
  62. pr_name("p5_SSL_CTX_new");
  63. if ((items == 1) && SvPOK(ST(0)))
  64. method=SvPV_nolen(ST(0));
  65. else if ((items == 2) && SvPOK(ST(1)))
  66. method=SvPV_nolen(ST(1));
  67. else
  68. croak("Usage: OpenSSL::SSL::CTX::new(type)");
  69. if (strcmp(method,"SSLv3") == 0)
  70. meth=SSLv3_method();
  71. else if (strcmp(method,"SSLv3_client") == 0)
  72. meth=SSLv3_client_method();
  73. else if (strcmp(method,"SSLv3_server") == 0)
  74. meth=SSLv3_server_method();
  75. else if (strcmp(method,"SSLv23") == 0)
  76. meth=SSLv23_method();
  77. else if (strcmp(method,"SSLv23_client") == 0)
  78. meth=SSLv23_client_method();
  79. else if (strcmp(method,"SSLv23_server") == 0)
  80. meth=SSLv23_server_method();
  81. else if (strcmp(method,"SSLv2") == 0)
  82. meth=SSLv2_method();
  83. else if (strcmp(method,"SSLv2_client") == 0)
  84. meth=SSLv2_client_method();
  85. else if (strcmp(method,"SSLv2_server") == 0)
  86. meth=SSLv2_server_method();
  87. else if (strcmp(method,"TLSv1") == 0)
  88. meth=TLSv1_method();
  89. else if (strcmp(method,"TLSv1_client") == 0)
  90. meth=TLSv1_client_method();
  91. else if (strcmp(method,"TLSv1_server") == 0)
  92. meth=TLSv1_server_method();
  93. else
  94. {
  95. croak("Not a valid SSL method name, should be 'SSLv[23] [client|server]'");
  96. }
  97. EXTEND(sp,1);
  98. PUSHs(sv_newmortal());
  99. ctx=SSL_CTX_new(meth);
  100. sv_setref_pv(ST(0), "OpenSSL::SSL::CTX", (void*)ctx);
  101. int
  102. p5_SSL_CTX_use_PrivateKey_file(ctx,file,...)
  103. SSL_CTX *ctx;
  104. char *file;
  105. PREINIT:
  106. int i=SSL_FILETYPE_PEM;
  107. char *ptr;
  108. CODE:
  109. pr_name("p5_SSL_CTX_use_PrivateKey_file");
  110. if (items > 3)
  111. croak("OpenSSL::SSL::CTX::use_PrivateKey_file(ssl_ctx,file[,type])");
  112. if (items == 3)
  113. {
  114. ptr=SvPV_nolen(ST(2));
  115. if (strcmp(ptr,"der") == 0)
  116. i=SSL_FILETYPE_ASN1;
  117. else
  118. i=SSL_FILETYPE_PEM;
  119. }
  120. RETVAL=SSL_CTX_use_RSAPrivateKey_file(ctx,file,i);
  121. OUTPUT:
  122. RETVAL
  123. int
  124. p5_SSL_CTX_set_options(ctx,...)
  125. SSL_CTX *ctx;
  126. PREINIT:
  127. int i;
  128. char *ptr;
  129. SV *sv;
  130. CODE:
  131. pr_name("p5_SSL_CTX_set_options");
  132. for (i=1; i<items; i++)
  133. {
  134. if (!SvPOK(ST(i)))
  135. croak("Usage: OpenSSL::SSL_CTX::set_options(ssl_ctx[,option,value]+)");
  136. ptr=SvPV_nolen(ST(i));
  137. if (strcmp(ptr,"-info_callback") == 0)
  138. {
  139. SSL_CTX_set_info_callback(ctx,
  140. p5_ssl_info_callback);
  141. sv=sv_mortalcopy(ST(i+1));
  142. SvREFCNT_inc(sv);
  143. SSL_CTX_set_ex_data(ctx,
  144. p5_ssl_ctx_ex_ssl_info_callback,
  145. (char *)sv);
  146. i++;
  147. }
  148. else
  149. {
  150. croak("OpenSSL::SSL_CTX::set_options(): unknown option");
  151. }
  152. }
  153. void
  154. p5_SSL_CTX_DESTROY(ctx)
  155. SSL_CTX *ctx
  156. PREINIT:
  157. SV *sv;
  158. PPCODE:
  159. pr_name_d("p5_SSL_CTX_DESTROY",ctx->references);
  160. SSL_CTX_free(ctx);
  161. MODULE = OpenSSL::SSL PACKAGE = OpenSSL::SSL PREFIX = p5_SSL_
  162. void
  163. p5_SSL_new(...)
  164. PREINIT:
  165. SV *sv_ctx;
  166. SSL_CTX *ctx;
  167. SSL *ssl;
  168. SV *arg;
  169. PPCODE:
  170. pr_name("p5_SSL_new");
  171. if ((items != 1) && (items != 2))
  172. croak("Usage: OpenSSL::SSL::new(ssl_ctx)");
  173. if (sv_derived_from(ST(items-1),"OpenSSL::SSL::CTX"))
  174. {
  175. IV tmp = SvIV((SV*)SvRV(ST(items-1)));
  176. ctx=(SSL_CTX *)tmp;
  177. sv_ctx=ST(items-1);
  178. }
  179. else
  180. croak("ssl_ctx is not of type OpenSSL::SSL::CTX");
  181. EXTEND(sp,1);
  182. PUSHs(sv_newmortal());
  183. ssl=SSL_new(ctx);
  184. sv_setref_pv(ST(0), "OpenSSL::SSL", (void*)ssl);
  185. /* Now this is being a little hairy, we keep a pointer to
  186. * our perl reference. We need to do a different one
  187. * to the one we return because it will have its reference
  188. * count dropped to 0 upon return and if we up its reference
  189. * count, it will never be DESTROYED */
  190. arg=newSVsv(ST(0));
  191. SSL_set_ex_data(ssl,p5_ssl_ex_ssl_ptr,(char *)arg);
  192. SvREFCNT_inc(sv_ctx);
  193. SSL_set_ex_data(ssl,p5_ssl_ex_ssl_ctx_ptr,(char *)sv_ctx);
  194. int
  195. p5_SSL_connect(ssl)
  196. SSL *ssl;
  197. CODE:
  198. RETVAL=SSL_connect(ssl);
  199. OUTPUT:
  200. RETVAL
  201. int
  202. p5_SSL_accept(ssl)
  203. SSL *ssl;
  204. CODE:
  205. RETVAL=SSL_connect(ssl);
  206. OUTPUT:
  207. RETVAL
  208. int
  209. p5_SSL_sysread(ssl,in,num, ...)
  210. SSL *ssl;
  211. SV *in;
  212. int num;
  213. PREINIT:
  214. int i,n,olen;
  215. int offset;
  216. char *p;
  217. CODE:
  218. offset=0;
  219. if (!SvPOK(in))
  220. sv_setpvn(in,"",0);
  221. SvPV(in,olen);
  222. if (items > 3)
  223. {
  224. offset=SvIV(ST(3));
  225. if (offset < 0)
  226. {
  227. if (-offset > olen)
  228. croak("Offset outside string");
  229. offset+=olen;
  230. }
  231. }
  232. if ((num+offset) > olen)
  233. {
  234. SvGROW(in,num+offset+1);
  235. p=SvPV(in,i);
  236. memset(&(p[olen]),0,(num+offset)-olen+1);
  237. }
  238. p=SvPV(in,n);
  239. i=SSL_read(ssl,p+offset,num);
  240. RETVAL=i;
  241. if (i <= 0) i=0;
  242. SvCUR_set(in,offset+i);
  243. OUTPUT:
  244. RETVAL
  245. int
  246. p5_SSL_syswrite(ssl,in, ...)
  247. SSL *ssl;
  248. SV *in;
  249. PREINIT:
  250. char *ptr;
  251. int len,in_len;
  252. int offset=0;
  253. int n;
  254. CODE:
  255. ptr=SvPV(in,in_len);
  256. if (items > 2)
  257. {
  258. len=SvOK(ST(2))?SvIV(ST(2)):in_len;
  259. if (items > 3)
  260. {
  261. offset=SvIV(ST(3));
  262. if (offset < 0)
  263. {
  264. if (-offset > in_len)
  265. croak("Offset outside string");
  266. offset+=in_len;
  267. }
  268. else if ((offset >= in_len) && (in_len > 0))
  269. croak("Offset outside string");
  270. }
  271. if (len >= (in_len-offset))
  272. len=in_len-offset;
  273. }
  274. else
  275. len=in_len;
  276. RETVAL=SSL_write(ssl,ptr+offset,len);
  277. OUTPUT:
  278. RETVAL
  279. void
  280. p5_SSL_set_bio(ssl,bio)
  281. SSL *ssl;
  282. BIO *bio;
  283. CODE:
  284. bio->references++;
  285. SSL_set_bio(ssl,bio,bio);
  286. int
  287. p5_SSL_set_options(ssl,...)
  288. SSL *ssl;
  289. PREINIT:
  290. int i;
  291. char *ptr;
  292. SV *sv;
  293. CODE:
  294. pr_name("p5_SSL_set_options");
  295. for (i=1; i<items; i++)
  296. {
  297. if (!SvPOK(ST(i)))
  298. croak("Usage: OpenSSL::SSL::set_options(ssl[,option,value]+)");
  299. ptr=SvPV_nolen(ST(i));
  300. if (strcmp(ptr,"-info_callback") == 0)
  301. {
  302. SSL_set_info_callback(ssl,
  303. p5_ssl_info_callback);
  304. sv=sv_mortalcopy(ST(i+1));
  305. SvREFCNT_inc(sv);
  306. SSL_set_ex_data(ssl,
  307. p5_ssl_ex_ssl_info_callback,(char *)sv);
  308. i++;
  309. }
  310. else if (strcmp(ptr,"-connect_state") == 0)
  311. {
  312. SSL_set_connect_state(ssl);
  313. }
  314. else if (strcmp(ptr,"-accept_state") == 0)
  315. {
  316. SSL_set_accept_state(ssl);
  317. }
  318. else
  319. {
  320. croak("OpenSSL::SSL::set_options(): unknown option");
  321. }
  322. }
  323. void
  324. p5_SSL_state(ssl)
  325. SSL *ssl;
  326. PREINIT:
  327. int state;
  328. PPCODE:
  329. pr_name("p5_SSL_state");
  330. EXTEND(sp,1);
  331. PUSHs(sv_newmortal());
  332. state=SSL_state(ssl);
  333. sv_setpv(ST(0),SSL_state_string_long(ssl));
  334. sv_setiv(ST(0),state);
  335. SvPOK_on(ST(0));
  336. void
  337. p5_SSL_DESTROY(ssl)
  338. SSL *ssl;
  339. CODE:
  340. pr_name_dd("p5_SSL_DESTROY",ssl->references,ssl->ctx->references);
  341. #ifdef DEBUG
  342. fprintf(stderr,"SSL_DESTROY %d\n",ssl->references);
  343. #endif
  344. SSL_free(ssl);
  345. int
  346. p5_SSL_references(ssl)
  347. SSL *ssl;
  348. CODE:
  349. RETVAL=ssl->references;
  350. OUTPUT:
  351. RETVAL
  352. int
  353. p5_SSL_do_handshake(ssl)
  354. SSL *ssl;
  355. CODE:
  356. RETVAL=SSL_do_handshake(ssl);
  357. OUTPUT:
  358. RETVAL
  359. int
  360. p5_SSL_renegotiate(ssl)
  361. SSL *ssl;
  362. CODE:
  363. RETVAL=SSL_renegotiate(ssl);
  364. OUTPUT:
  365. RETVAL
  366. int
  367. p5_SSL_shutdown(ssl)
  368. SSL *ssl;
  369. CODE:
  370. RETVAL=SSL_shutdown(ssl);
  371. OUTPUT:
  372. RETVAL
  373. char *
  374. p5_SSL_get_version(ssl)
  375. SSL *ssl;
  376. CODE:
  377. RETVAL=SSL_get_version(ssl);
  378. OUTPUT:
  379. RETVAL
  380. SSL_CIPHER *
  381. p5_SSL_get_current_cipher(ssl)
  382. SSL *ssl;
  383. CODE:
  384. RETVAL=SSL_get_current_cipher(ssl);
  385. OUTPUT:
  386. RETVAL
  387. X509 *
  388. p5_SSL_get_peer_certificate(ssl)
  389. SSL *ssl
  390. CODE:
  391. RETVAL=SSL_get_peer_certificate(ssl);
  392. OUTPUT:
  393. RETVAL
  394. MODULE = OpenSSL::SSL PACKAGE = OpenSSL::SSL::CIPHER PREFIX = p5_SSL_CIPHER_
  395. int
  396. p5_SSL_CIPHER_get_bits(sc)
  397. SSL_CIPHER *sc
  398. PREINIT:
  399. int i,ret;
  400. PPCODE:
  401. EXTEND(sp,2);
  402. PUSHs(sv_newmortal());
  403. PUSHs(sv_newmortal());
  404. ret=SSL_CIPHER_get_bits(sc,&i);
  405. sv_setiv(ST(0),(IV)ret);
  406. sv_setiv(ST(1),(IV)i);
  407. char *
  408. p5_SSL_CIPHER_get_version(sc)
  409. SSL_CIPHER *sc
  410. CODE:
  411. RETVAL=SSL_CIPHER_get_version(sc);
  412. OUTPUT:
  413. RETVAL
  414. char *
  415. p5_SSL_CIPHER_get_name(sc)
  416. SSL_CIPHER *sc
  417. CODE:
  418. RETVAL=SSL_CIPHER_get_name(sc);
  419. OUTPUT:
  420. RETVAL
  421. MODULE = OpenSSL::SSL PACKAGE = OpenSSL::BIO PREFIX = p5_BIO_
  422. void
  423. p5_BIO_get_ssl(bio)
  424. BIO *bio;
  425. PREINIT:
  426. SSL *ssl;
  427. SV *ret;
  428. int i;
  429. PPCODE:
  430. if ((i=BIO_get_ssl(bio,&ssl)) > 0)
  431. {
  432. ret=(SV *)SSL_get_ex_data(ssl,p5_ssl_ex_ssl_ptr);
  433. ret=sv_mortalcopy(ret);
  434. }
  435. else
  436. ret= &PL_sv_undef;
  437. EXTEND(sp,1);
  438. PUSHs(ret);