| 1 | #!perl -w | ||||||||||||
| 2 | # -*- coding: utf-8; -*- | ||||||||||||
| 3 | |||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 2 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | |||||||||||||
| 7 | package Crypt::OpenSSL::CA::Inline::C; | ||||||||||||
| 8 | |||||||||||||
| 9 | =head1 NAME | ||||||||||||
| 10 | |||||||||||||
| 11 | Crypt::OpenSSL::CA::Inline::C - A bag of XS and L<Inline::C> tricks | ||||||||||||
| 12 | |||||||||||||
| 13 | =encoding utf8 | ||||||||||||
| 14 | |||||||||||||
| 15 | =head1 SYNOPSIS | ||||||||||||
| 16 | |||||||||||||
| 17 | =for My::Tests::Below "synopsis" begin | ||||||||||||
| 18 | |||||||||||||
| 19 | package Crypt::OpenSSL::CA::Foo; | ||||||||||||
| 20 | |||||||||||||
| 21 | 1 | ▶ | use Crypt::OpenSSL::CA::Inline::C <<"C_CODE_SAMPLE"; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 22 | #include <openssl/x509.h> | ||||||||||||
| 23 | |||||||||||||
| 24 | static | ||||||||||||
| 25 | SV* mysub() { | ||||||||||||
| 26 | // Your C code here | ||||||||||||
| 27 | } | ||||||||||||
| 28 | |||||||||||||
| 29 | C_CODE_SAMPLE | ||||||||||||
| 30 | |||||||||||||
| 31 | # Then maybe some Perl... | ||||||||||||
| 32 | |||||||||||||
| 33 | use Crypt::OpenSSL::CA::Inline::C <<"MORE_C_CODE"; | ||||||||||||
| 34 | 1 | ▶ | |||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 35 | static | ||||||||||||
| 36 | void another() { | ||||||||||||
| 37 | // ... | ||||||||||||
| 38 | } | ||||||||||||
| 39 | |||||||||||||
| 40 | MORE_C_CODE | ||||||||||||
| 41 | |||||||||||||
| 42 | use Crypt::OpenSSL::CA::Inline::C "__END__"; | ||||||||||||
| 43 | 1 | ▶ | |||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 44 | =for My::Tests::Below "synopsis" end | ||||||||||||
| 45 | |||||||||||||
| 46 | =head1 DESCRIPTION | ||||||||||||
| 47 | |||||||||||||
| 48 | B<This documentation is only useful for people who want to hack | ||||||||||||
| 49 | I<Crypt::OpenSSL::CA>. It is of no interest for people who just want | ||||||||||||
| 50 | to use the module.> | ||||||||||||
| 51 | |||||||||||||
| 52 | This package provides L<Inline::C> goodness to L<Crypt::OpenSSL::CA> | ||||||||||||
| 53 | during development, plus a few tricks of our own. The idiom in | ||||||||||||
| 54 | L</SYNOPSIS>, used throughout the source code of | ||||||||||||
| 55 | L<Crypt::OpenSSL::CA>, recaps them all; noteworthy points are: | ||||||||||||
| 56 | |||||||||||||
| 57 | =over | ||||||||||||
| 58 | |||||||||||||
| 59 | =item the C<static>-newline trick | ||||||||||||
| 60 | |||||||||||||
| 61 | Because the C language doesn't have namespaces, we don't want symbols | ||||||||||||
| 62 | named e.g. C<new> appearing in the .so's symbol tables: they could | ||||||||||||
| 63 | clash with other symbols defined by Perl, or with each other. | ||||||||||||
| 64 | Therefore we have to declare them C<static>, but doing this in the | ||||||||||||
| 65 | naïve way would cause L<Inline::C> to purposefully B<not> bind them | ||||||||||||
| 66 | with Perl... The winning trick is to put the C<static> word alone on | ||||||||||||
| 67 | its line, as demonstrated. | ||||||||||||
| 68 | |||||||||||||
| 69 | =item the "__END__" pragma | ||||||||||||
| 70 | |||||||||||||
| 71 | The code in L<Crypt::OpenSSL::CA> must use the following pragma to | ||||||||||||
| 72 | signal that it won't attempt to add any L<Inline::C> code after this | ||||||||||||
| 73 | point: | ||||||||||||
| 74 | |||||||||||||
| 75 | use Crypt::OpenSSL::CA::Inline::C "__END__"; | ||||||||||||
| 76 | |||||||||||||
| 77 | =back | ||||||||||||
| 78 | |||||||||||||
| 79 | =head2 Standard Library | ||||||||||||
| 80 | |||||||||||||
| 81 | In addition to the standard library available to XS C code described | ||||||||||||
| 82 | in L<Inline::C>, L<perlxstut>, L<perlguts> and L<perlapi>, C code that | ||||||||||||
| 83 | compiles itself through I<Crypt::OpenSSL::CA::Inline::C> has access to | ||||||||||||
| 84 | the following C functions: | ||||||||||||
| 85 | |||||||||||||
| 86 | =head3 char0_value | ||||||||||||
| 87 | |||||||||||||
| 88 | static inline char* char0_value(SV* string); | ||||||||||||
| 89 | |||||||||||||
| 90 | Returns the string value of a Perl SV, making sure that it exists and is | ||||||||||||
| 91 | zero-terminated beforehand. If C<string> is undef, returns the empty string | ||||||||||||
| 92 | (B<not> NULL; see L</char0_value_or_null>). If C<string> is tainted, that makes | ||||||||||||
| 93 | no difference; return it, or the empty string, just the same. See | ||||||||||||
| 94 | L<perlguts/Working with SVs>, look for the word "Nevertheless" - I assume | ||||||||||||
| 95 | there is a macro in Perl's convenience stuff that does exactly that already, but | ||||||||||||
| 96 | I don't know it... | ||||||||||||
| 97 | |||||||||||||
| 98 | =head3 char0_value_or_null | ||||||||||||
| 99 | |||||||||||||
| 100 | static inline char* char0_value_or_null(SV* string); | ||||||||||||
| 101 | |||||||||||||
| 102 | Like L</char0_value>, except that NULL is returned if C<string> is | ||||||||||||
| 103 | undef. | ||||||||||||
| 104 | |||||||||||||
| 105 | =head3 perl_wrap | ||||||||||||
| 106 | |||||||||||||
| 107 | static inline SV* perl_wrap(class, pointer); | ||||||||||||
| 108 | |||||||||||||
| 109 | Creates read-only SV containing the integral value of C<pointer>, | ||||||||||||
| 110 | blesses it into class C<class> and returns it as a SV*. The return | ||||||||||||
| 111 | value is an adequate Perl wrapper to stand for C<pointer>, as | ||||||||||||
| 112 | demonstrated in L<Inline::C-Cookbook/Object Oriented Inline>. | ||||||||||||
| 113 | |||||||||||||
| 114 | =head3 perl_unwrap (class, typename, SV*) | ||||||||||||
| 115 | |||||||||||||
| 116 | The reverse of L</perl_wrap>. Given a L</perl_wrap>ped SV*, asserts | ||||||||||||
| 117 | that it actually contains an object blessed in class C<class> (lest it | ||||||||||||
| 118 | C<croak>s), extracts the pointer within same, casts it into | ||||||||||||
| 119 | C<typename> and returns it. This is a macro instead of a static | ||||||||||||
| 120 | inline, so as to be able to perform the polymorphic cast. | ||||||||||||
| 121 | |||||||||||||
| 122 | =head3 openssl_string_to_SV | ||||||||||||
| 123 | |||||||||||||
| 124 | static inline SV* openssl_string_to_SV(char* string); | ||||||||||||
| 125 | |||||||||||||
| 126 | Copies over C<string> to a newly-allocated C<SV*> Perl scalar, and | ||||||||||||
| 127 | then frees C<string> using C<OPENSSL_free()>. Used to transfer | ||||||||||||
| 128 | ownership of strings from OpenSSL to Perl, and thereby ensure proper | ||||||||||||
| 129 | memory management. | ||||||||||||
| 130 | |||||||||||||
| 131 | Note to I<Crypt::OpenSSL::CA> hackers: if C<string> is on an OpenSSL | ||||||||||||
| 132 | static buffer instead of having been allocated by OpenSSL, this will | ||||||||||||
| 133 | SEGV in trying to free() C<string> that was not malloc()'d; in this | ||||||||||||
| 134 | case you want to use L<perlapi/XSRETURN_PV> instead or some such. | ||||||||||||
| 135 | Check the OpenSSL documentation carefully, and make use of | ||||||||||||
| 136 | L<Crypt::OpenSSL::CA::Test/leaks_bytes_ok> to ascertain experimentally | ||||||||||||
| 137 | that your code doesn't leak memory. | ||||||||||||
| 138 | |||||||||||||
| 139 | =head3 openssl_buf_to_SV | ||||||||||||
| 140 | |||||||||||||
| 141 | static inline SV* openssl_buf_to_SV(char* string, int length); | ||||||||||||
| 142 | |||||||||||||
| 143 | Like L</openssl_string_to_SV> except that the length is specified, | ||||||||||||
| 144 | which allows for C<string> to not contain null characters or not be | ||||||||||||
| 145 | zero-terminated. Use this form e.g. for ASN.1 buffers returned by | ||||||||||||
| 146 | C<i2d_foobar> OpenSSL functions. | ||||||||||||
| 147 | |||||||||||||
| 148 | =head3 BIO_mem_to_SV | ||||||||||||
| 149 | |||||||||||||
| 150 | static inline SV* BIO_mem_to_SV(BIO *bio); | ||||||||||||
| 151 | |||||||||||||
| 152 | This inline function is intended to be used to return scalar values | ||||||||||||
| 153 | (e.g. PEM strings and RSA moduli) constructed by OpenSSL. Should be | ||||||||||||
| 154 | invoked thusly, after having freed all temporary resources except | ||||||||||||
| 155 | *bio: | ||||||||||||
| 156 | |||||||||||||
| 157 | return BIO_mem_to_SV(bio); | ||||||||||||
| 158 | |||||||||||||
| 159 | I<BIO_mem_to_SV()> turns bio into a Perl scalar and returns it, or | ||||||||||||
| 160 | C<croak()>s trying (hence the requirement not to have any outstanding | ||||||||||||
| 161 | memory resources allocated in the caller). Regardless of the outcome, | ||||||||||||
| 162 | C<bio> will be C<BIO_free>()d. | ||||||||||||
| 163 | |||||||||||||
| 164 | =head2 sslcroak | ||||||||||||
| 165 | |||||||||||||
| 166 | static void sslcroak(char *format, ...); | ||||||||||||
| 167 | |||||||||||||
| 168 | Like L<perlapi/croak>, except that a blessed exception of class | ||||||||||||
| 169 | I<Crypt::OpenSSL::CA::Error> is generated. The OpenSSL error | ||||||||||||
| 170 | stack, if any, gets recorded as an array reference inside the | ||||||||||||
| 171 | exception structure. | ||||||||||||
| 172 | |||||||||||||
| 173 | Note to I<Crypt::OpenSSL::CA> hackers: please select the appropriate | ||||||||||||
| 174 | routine between I<sslcroak> and I<croak>, depending on whether the | ||||||||||||
| 175 | current error condition is being caused by OpenSSL or not; in this way | ||||||||||||
| 176 | callers are able to discriminate errors. Also, don't be fooled into | ||||||||||||
| 177 | thinking that C<croak>-style error management acts in the same way in | ||||||||||||
| 178 | C and Perl! Because calling C<sslcroak> (or, for that matter, | ||||||||||||
| 179 | L<perlapi/croak>) will return control directly to Perl without running | ||||||||||||
| 180 | any C code, any and all temporary variables that have been allocated | ||||||||||||
| 181 | from C will fail to be de-allocated, thereby causing a memory leak. | ||||||||||||
| 182 | |||||||||||||
| 183 | Internally, I<sslcroak> works by invoking | ||||||||||||
| 184 | L<Crypt::OpenSSL::CA/_sslcroak_callback> several times, using a rough | ||||||||||||
| 185 | equivalent of the following pseudo-code: | ||||||||||||
| 186 | |||||||||||||
| 187 | _sslcroak_callback("-message", $formattedstring); | ||||||||||||
| 188 | _sslcroak_callback("-openssl", $openssl_errorstring_1); | ||||||||||||
| 189 | _sslcroak_callback("-openssl", $openssl_errorstring_2); | ||||||||||||
| 190 | ... | ||||||||||||
| 191 | _sslcroak_callback("DONE"); | ||||||||||||
| 192 | |||||||||||||
| 193 | where $formattedstring is the C<sprintf>-formatted version of the | ||||||||||||
| 194 | arguments passed to I<sslcroak>, and the OpenSSL error strings are | ||||||||||||
| 195 | retrieved using B<ERR_get_error(3)> and B<ERR_error_string(3)>. | ||||||||||||
| 196 | |||||||||||||
| 197 | =cut | ||||||||||||
| 198 | |||||||||||||
| 199 | 5 | ▶ | sub _c_boilerplate { <<'C_BOILERPLATE'; } | ||||||||||
|
Subroutine
_c_boilerplate: called CC=1 SCAR=0.0
| |||||||||||||
| 200 | #include <stdarg.h> /* For varargs stuff in sslcroak() */ | ||||||||||||
| 201 | #include <openssl/crypto.h> /* For OPENSSL_free() in openssl_buf_to_SV */ | ||||||||||||
| 202 | #include <openssl/err.h> /* For ERR_stuff in sslcroak() */ | ||||||||||||
| 203 | #include <openssl/pem.h> /* For BUF_MEM->data dereference in | ||||||||||||
| 204 | BIO_mem_to_SV(). WTF is this declaration | ||||||||||||
| 205 | doing in there?! */ | ||||||||||||
| 206 | #include <openssl/bio.h> /* Also for BIO_mem_to_SV() */ | ||||||||||||
| 207 | |||||||||||||
| 208 | #include <openssl/opensslv.h> | ||||||||||||
| 209 | #if OPENSSL_VERSION_NUMBER < 0x00907000 | ||||||||||||
| 210 | #error OpenSSL version 0.9.7 or later is required. See comments in CA.pm | ||||||||||||
| 211 | #endif | ||||||||||||
| 212 | |||||||||||||
| 213 | static inline char* char0_value_or_null(SV* perlscalar) { | ||||||||||||
| 214 | if (! SvOK(perlscalar)) { return NULL; } | ||||||||||||
| 215 | |||||||||||||
| 216 | STRLEN length; | ||||||||||||
| 217 | SvPV(perlscalar, length); | ||||||||||||
| 218 | SvGROW(perlscalar, length + 1); | ||||||||||||
| 219 | |||||||||||||
| 220 | char* retval = SvPV_nolen(perlscalar); | ||||||||||||
| 221 | retval[length] = '\0'; | ||||||||||||
| 222 | return retval; | ||||||||||||
| 223 | } | ||||||||||||
| 224 | |||||||||||||
| 225 | static inline char* char0_value(SV* perlscalar) { | ||||||||||||
| 226 | char* retval = char0_value_or_null(perlscalar); | ||||||||||||
| 227 | return ( retval ? retval : "" ); | ||||||||||||
| 228 | } | ||||||||||||
| 229 | |||||||||||||
| 230 | static inline SV* perl_wrap(const char* class, void* pointer) { | ||||||||||||
| 231 | SV* obj = sv_setref_pv(newSV(0), class, pointer); | ||||||||||||
| 232 | if (! obj) { croak("not enough memory"); } | ||||||||||||
| 233 | SvREADONLY_on(SvRV(obj)); | ||||||||||||
| 234 | return obj; | ||||||||||||
| 235 | } | ||||||||||||
| 236 | |||||||||||||
| 237 | #define perl_unwrap(class, typename, obj) \ | ||||||||||||
| 238 | ((typename) __perl_unwrap(__FILE__, __LINE__, (class), (obj))) | ||||||||||||
| 239 | |||||||||||||
| 240 | static inline void* __perl_unwrap(const char* file, int line, | ||||||||||||
| 241 | const char* class, SV* obj) { | ||||||||||||
| 242 | if (!(sv_isobject(obj) && sv_isa(obj, class))) { | ||||||||||||
| 243 | croak("%s:%d:perl_unwrap: got an invalid " | ||||||||||||
| 244 | "Perl argument (expected an object blessed " | ||||||||||||
| 245 | "in class ``%s'')", file, line, (class)); | ||||||||||||
| 246 | } | ||||||||||||
| 247 | return (void *)(intptr_t)SvIV(SvRV(obj)); | ||||||||||||
| 248 | } | ||||||||||||
| 249 | |||||||||||||
| 250 | static inline SV* openssl_buf_to_SV(char* string, int length) { | ||||||||||||
| 251 | /* Note that a newmortal is not wanted here, even though | ||||||||||||
| 252 | * caller will typically return the SV* to Perl. This is because XS | ||||||||||||
| 253 | * performs some magic of its own for functions that return an SV (as | ||||||||||||
| 254 | * documented in L<perlxs/Returning SVs, AVs and HVs through RETVAL>) | ||||||||||||
| 255 | * and Inline::C leverages that. */ | ||||||||||||
| 256 | SV* retval = newSVpv(string, length); | ||||||||||||
| 257 | OPENSSL_free(string); | ||||||||||||
| 258 | return retval; | ||||||||||||
| 259 | } | ||||||||||||
| 260 | |||||||||||||
| 261 | static inline SV* openssl_string_to_SV(char* string) { | ||||||||||||
| 262 | return openssl_buf_to_SV(string, 0); | ||||||||||||
| 263 | } | ||||||||||||
| 264 | |||||||||||||
| 265 | static inline SV* BIO_mem_to_SV(BIO *mem) { | ||||||||||||
| 266 | SV* retval; | ||||||||||||
| 267 | BUF_MEM* buf; | ||||||||||||
| 268 | |||||||||||||
| 269 | BIO_get_mem_ptr(mem, &buf); | ||||||||||||
| 270 | if (! buf) { | ||||||||||||
| 271 | BIO_free(mem); | ||||||||||||
| 272 | croak("BIO_get_mem_ptr failed"); | ||||||||||||
| 273 | } | ||||||||||||
| 274 | retval = newSVpv(buf->data, 0); | ||||||||||||
| 275 | if (! retval) { | ||||||||||||
| 276 | BIO_free(mem); | ||||||||||||
| 277 | croak("newSVpv failed"); | ||||||||||||
| 278 | } | ||||||||||||
| 279 | BIO_free(mem); | ||||||||||||
| 280 | return retval; | ||||||||||||
| 281 | } | ||||||||||||
| 282 | |||||||||||||
| 283 | #define ERRBUFSZ 512 | ||||||||||||
| 284 | #define THISPACKAGE "Crypt::OpenSSL::CA" | ||||||||||||
| 285 | static void sslcroak(char *fmt, ...) { | ||||||||||||
| 286 | va_list ap; /* The argument list hiding behind the | ||||||||||||
| 287 | hyphens in the protype above */ | ||||||||||||
| 288 | dSP; /* Required to be able to perform Perl | ||||||||||||
| 289 | callbacks */ | ||||||||||||
| 290 | char* argv[3]; /* The list of arguments to pass to the | ||||||||||||
| 291 | callback */ | ||||||||||||
| 292 | char croakbuf[ERRBUFSZ]; /* The buffer to typeset the main error | ||||||||||||
| 293 | message into */ | ||||||||||||
| 294 | char errbuf[ERRBUFSZ]; /* The buffer to typeset the auxillary error | ||||||||||||
| 295 | messages from OpenSSL into */ | ||||||||||||
| 296 | SV* dollar_at; /* Used to probe $@ to see if everything | ||||||||||||
| 297 | went well with the callback */ | ||||||||||||
| 298 | unsigned long sslerr; /* Will iterate through the OpenSSL | ||||||||||||
| 299 | error stack */ | ||||||||||||
| 300 | |||||||||||||
| 301 | va_start(ap, fmt); | ||||||||||||
| 302 | vsnprintf(croakbuf, ERRBUFSZ, fmt, ap); | ||||||||||||
| 303 | croakbuf[ERRBUFSZ - 1] = '\0'; | ||||||||||||
| 304 | va_end(ap); | ||||||||||||
| 305 | |||||||||||||
| 306 | argv[0] = "-message"; | ||||||||||||
| 307 | argv[1] = croakbuf; | ||||||||||||
| 308 | argv[2] = NULL; | ||||||||||||
| 309 | call_argv(THISPACKAGE "::_sslcroak_callback", G_DISCARD, argv); | ||||||||||||
| 310 | |||||||||||||
| 311 | argv[0] = "-openssl"; | ||||||||||||
| 312 | argv[1] = errbuf; | ||||||||||||
| 313 | while( (sslerr = ERR_get_error()) ) { | ||||||||||||
| 314 | ERR_error_string_n(sslerr, errbuf, ERRBUFSZ); | ||||||||||||
| 315 | errbuf[ERRBUFSZ - 1] = '\0'; | ||||||||||||
| 316 | call_argv(THISPACKAGE "::_sslcroak_callback", G_DISCARD, argv); | ||||||||||||
| 317 | } | ||||||||||||
| 318 | argv[0] = "DONE"; | ||||||||||||
| 319 | argv[1] = NULL; | ||||||||||||
| 320 | call_argv(THISPACKAGE "::_sslcroak_callback", G_DISCARD, argv); | ||||||||||||
| 321 | |||||||||||||
| 322 | dollar_at = get_sv("@", FALSE); | ||||||||||||
| 323 | if (dollar_at && sv_isobject(dollar_at)) { | ||||||||||||
| 324 | // Success! | ||||||||||||
| 325 | croak(Nullch); | ||||||||||||
| 326 | } else { | ||||||||||||
| 327 | // Something went bang, revert to the croakbuf. | ||||||||||||
| 328 | croak("%s", croakbuf); | ||||||||||||
| 329 | } | ||||||||||||
| 330 | } | ||||||||||||
| 331 | |||||||||||||
| 332 | C_BOILERPLATE | ||||||||||||
| 333 | |||||||||||||
| 334 | =head2 BOOT-time effect | ||||||||||||
| 335 | |||||||||||||
| 336 | Each C<.so> XS module will be fitted with a C<BOOT> section (see | ||||||||||||
| 337 | L<Inline::C/BOOT> which automatically gets executed upon loading it | ||||||||||||
| 338 | with L<DynaLoader> or L<XSLoader>. The C<BOOT> section is the same for | ||||||||||||
| 339 | all subpackages in L<Crypt::OpenSSL::CA>; it ensures that various | ||||||||||||
| 340 | stuff is loaded inside OpenSSL, such as C<ERR_load_crypto_strings()>, | ||||||||||||
| 341 | C<OpenSSL_add_all_algorithms()> and all that jazz. After the boot | ||||||||||||
| 342 | code completes, C<$Crypt::OpenSSL::CA::openssl_stuff_loaded> will be | ||||||||||||
| 343 | 1, so that the following XS modules can skip that when they in turn | ||||||||||||
| 344 | get loaded. | ||||||||||||
| 345 | |||||||||||||
| 346 | =cut | ||||||||||||
| 347 | |||||||||||||
| 348 | 5 | ▶ | sub _c_boot_section { <<"ENSURE_OPENSSL_STUFF_LOADED" } | ||||||||||
|
Subroutine
_c_boot_section: called CC=1 SCAR=0.0
| |||||||||||||
| 349 | SV* already_loaded = get_sv | ||||||||||||
| 350 | ("Crypt::OpenSSL::CA::openssl_stuff_loaded", 1); | ||||||||||||
| 351 | if (SvOK(already_loaded)) { return; } | ||||||||||||
| 352 | sv_setiv(already_loaded, 1); | ||||||||||||
| 353 | |||||||||||||
| 354 | ERR_load_crypto_strings(); | ||||||||||||
| 355 | OpenSSL_add_all_algorithms(); | ||||||||||||
| 356 | ENSURE_OPENSSL_STUFF_LOADED | ||||||||||||
| 357 | |||||||||||||
| 358 | =head1 INTERNALS | ||||||||||||
| 359 | |||||||||||||
| 360 | The C<< use Crypt::OpenSSL::CA::Inline::C >> idiom described in | ||||||||||||
| 361 | L</SYNOPSIS> is implemented in terms of L<Inline>. | ||||||||||||
| 362 | |||||||||||||
| 363 | =head3 %c_code | ||||||||||||
| 364 | |||||||||||||
| 365 | A lexical variable that L</import> uses to accumulate all the C code | ||||||||||||
| 366 | submitted by L<Crypt::OpenSSL::CA>. Keys are package names, and | ||||||||||||
| 367 | values are snippets of C. | ||||||||||||
| 368 | |||||||||||||
| 369 | =cut | ||||||||||||
| 370 | |||||||||||||
| 371 | 1 | my %c_code; | |||||||||||
| 372 | |||||||||||||
| 373 | 2 | ▶ | use Inline::C (); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 374 | |||||||||||||
| 375 | =head3 import () | ||||||||||||
| 376 | |||||||||||||
| 377 | Called whenever one of the C<< use Crypt::OpenSSL::CA::Inline::C "foo" | ||||||||||||
| 378 | >> pragmas (listed in L</SYNOPSIS>) is seen by Perl; performs the | ||||||||||||
| 379 | actual magic of the module. Stashes everything into L</%c_code>, and | ||||||||||||
| 380 | invokes L</compile_everything> at the end. | ||||||||||||
| 381 | |||||||||||||
| 382 | =cut | ||||||||||||
| 383 | |||||||||||||
| 384 | 7 | ▶ | sub import { | ||||||||||
|
Subroutine
import: called CC=3 SCAR=11.0
| |||||||||||||
| 385 | 7 | my ($class, $c_code) = @_; | |||||||||||
| 386 | |||||||||||||
| 387 | 7 | ▶ | return if ! defined $c_code; # A simple "use" | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 388 | |||||||||||||
| 389 | 7 | ▶ | return $class->compile_everything if ($c_code eq "__END__"); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 390 | |||||||||||||
| 391 | 6 | my ($package, $file, $line) = caller; | |||||||||||
| 392 | 6 | $c_code{$package} .= sprintf(qq'#line %d "%s"\n', $line + 1, $file) | |||||||||||
| 393 | . $c_code; | ||||||||||||
| 394 | 6 | return; | |||||||||||
| 395 | } | ||||||||||||
| 396 | |||||||||||||
| 397 | =head3 compile_everything () | ||||||||||||
| 398 | |||||||||||||
| 399 | Called when L</the "__END__" pragma> is seen. Invokes | ||||||||||||
| 400 | L<compile_namespace> once for every package (that is, every key in | ||||||||||||
| 401 | L</%c_code>), prepending L<_c_boilerplate> each time. | ||||||||||||
| 402 | |||||||||||||
| 403 | =cut | ||||||||||||
| 404 | |||||||||||||
| 405 | 1 | ▶ | sub compile_everything { | ||||||||||
|
Subroutine
compile_everything: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 406 | 1 | my ($class) = @_; | |||||||||||
| 407 | 1 | foreach my $package (keys %c_code) { | |||||||||||
| 408 | 5 | $class->compile_into($package, _c_boilerplate . $c_code{$package}, | |||||||||||
| 409 | -boot_section => _c_boot_section()); | ||||||||||||
| 410 | } | ||||||||||||
| 411 | } | ||||||||||||
| 412 | |||||||||||||
| 413 | =head3 compile_into ($package, $c_code, %named_options) | ||||||||||||
| 414 | |||||||||||||
| 415 | Compile $c_code and make its functions available as part of $package's | ||||||||||||
| 416 | namespace, courtesy to L<Inline::C> magic. Works by invoking | ||||||||||||
| 417 | L<Inline/import> in a tweaked fashion, so as to compile with all | ||||||||||||
| 418 | warnings turned into errors (i.e. C<-Wall -Werror>) and to link with | ||||||||||||
| 419 | the OpenSSL libraries. The environment variables are taken into | ||||||||||||
| 420 | account (see L</ENVIRONMENT VARIABLES>). | ||||||||||||
| 421 | |||||||||||||
| 422 | Available named options are: | ||||||||||||
| 423 | |||||||||||||
| 424 | =over | ||||||||||||
| 425 | |||||||||||||
| 426 | =item B<< -boot_section => $c_code >> | ||||||||||||
| 427 | |||||||||||||
| 428 | Adds $c_code to the BOOT section of the generated .so module. | ||||||||||||
| 429 | |||||||||||||
| 430 | =back | ||||||||||||
| 431 | |||||||||||||
| 432 | =cut | ||||||||||||
| 433 | |||||||||||||
| 434 | 5 | ▶ | sub compile_into { | ||||||||||
|
Subroutine
compile_into: called CC=7 SCAR=19.6
Pod
undocumented
| |||||||||||||
| 435 | 5 | my ($class, $package, $c_code, %opts) = @_; | |||||||||||
| 436 | 5 | ▶ | my $compile_params = ($class->full_debugging ? | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 437 | <<'COMPILE_PARAMS_DEBUG' : | ||||||||||||
| 438 | CCFLAGS => "-Wall -Wno-compound-token-split-by-macro -Wno-unused -Werror -save-temps", | ||||||||||||
| 439 | OPTIMIZE => "-g", | ||||||||||||
| 440 | CLEAN_AFTER_BUILD => 0, | ||||||||||||
| 441 | COMPILE_PARAMS_DEBUG | ||||||||||||
| 442 | <<'COMPILE_PARAMS_OPTIMIZED'); | ||||||||||||
| 443 | OPTIMIZE => "-g -O2 -Wno-compound-token-split-by-macro", | ||||||||||||
| 444 | COMPILE_PARAMS_OPTIMIZED | ||||||||||||
| 445 | |||||||||||||
| 446 | my $openssl_params = sprintf("LIBS => '%s -lcrypto -lssl',", | ||||||||||||
| 447 | 5 | ▶ | ($ENV{BUILD_OPENSSL_LDFLAGS} or "")); | ||||||||||
|
Condition50%1/2
$ENV{'BUILD_OPENSSL_LDFLAGS'} || ''
MC/DC0%0/1
$ENV{'BUILD_OPENSSL_LDFLAGS'} || ''
$ENV{'BUILD_OPENSSL_LDFLAGS'}
Truth table50%1/2
A
A= $ENV{'BUILD_OPENSSL_LDFLAGS'}
| |||||||||||||
| 448 | 5 | ▶ | if ($ENV{BUILD_OPENSSL_CFLAGS}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 449 | 0 | $openssl_params .= " INC => '$ENV{BUILD_OPENSSL_CFLAGS}',"; | |||||||||||
| 450 | } | ||||||||||||
| 451 | |||||||||||||
| 452 | 5 | ▶ | my $version_params = | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 453 | ( $Crypt::OpenSSL::CA::VERSION ? | ||||||||||||
| 454 | qq'VERSION => "$Crypt::OpenSSL::CA::VERSION",' : "" ); | ||||||||||||
| 455 | |||||||||||||
| 456 | 5 | ▶ | my $boot_params = ($opts{-boot_section} ? <<"BOOT_CONFIG" : ""); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 457 | BOOT => <<'BOOT_SECTION', | ||||||||||||
| 458 | $opts{-boot_section} | ||||||||||||
| 459 | BOOT_SECTION | ||||||||||||
| 460 | BOOT_CONFIG | ||||||||||||
| 461 | |||||||||||||
| 462 | 1 | ▶ | eval <<"FAKE_Inline_C_INVOCATION"; die $@ if $@; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||
| 463 | package $package; | ||||||||||||
| 464 | use Inline C => Config => | ||||||||||||
| 465 | NAME => '$package', | ||||||||||||
| 466 | $compile_params | ||||||||||||
| 467 | $version_params | ||||||||||||
| 468 | $openssl_params | ||||||||||||
| 469 | $boot_params | ||||||||||||
| 470 | ; | ||||||||||||
| 471 | use Inline C => <<'C_CODE'; | ||||||||||||
| 472 | $c_code | ||||||||||||
| 473 | C_CODE | ||||||||||||
| 474 | FAKE_Inline_C_INVOCATION | ||||||||||||
| 475 | |||||||||||||
| 476 | 5 | return 1; | |||||||||||
| 477 | } | ||||||||||||
| 478 | |||||||||||||
| 479 | =head3 full_debugging | ||||||||||||
| 480 | |||||||||||||
| 481 | Returns true iff the environment variable L</FULL_DEBUGGING> is set. | ||||||||||||
| 482 | |||||||||||||
| 483 | =cut | ||||||||||||
| 484 | |||||||||||||
| 485 | 5 | ▶ | sub full_debugging { ! ! $ENV{FULL_DEBUGGING} } | ||||||||||
|
Subroutine
full_debugging: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 486 | |||||||||||||
| 487 | =head3 installed_version | ||||||||||||
| 488 | |||||||||||||
| 489 | Returns what the source code of this module will look like (with POD | ||||||||||||
| 490 | and everything) after it is installed. The installed version is a dud | ||||||||||||
| 491 | stub; its L</import> method only loads the XS DLL, and it is no longer | ||||||||||||
| 492 | possible to alter the C code once the module has been installed. The | ||||||||||||
| 493 | upside is that in thanks to that, L<Inline> is a dependency only at | ||||||||||||
| 494 | compile time. | ||||||||||||
| 495 | |||||||||||||
| 496 | =begin this_pod_is_not_ours | ||||||||||||
| 497 | |||||||||||||
| 498 | =cut | ||||||||||||
| 499 | |||||||||||||
| 500 | 1 | ▶ | sub installed_version { <<'INSTALLED_VERSION' } | ||||||||||
|
Subroutine
installed_version: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 501 | #!perl -w | ||||||||||||
| 502 | |||||||||||||
| 503 | package Crypt::OpenSSL::CA::Inline::C; | ||||||||||||
| 504 | |||||||||||||
| 505 | use strict; | ||||||||||||
| 506 | use XSLoader; | ||||||||||||
| 507 | |||||||||||||
| 508 | sub import { | ||||||||||||
| 509 | my ($class, $stuff) = @_; | ||||||||||||
| 510 | return if ! defined $stuff; | ||||||||||||
| 511 | return if $stuff eq "__END__"; | ||||||||||||
| 512 | |||||||||||||
| 513 | my ($package) = caller; | ||||||||||||
| 514 | no strict "refs"; | ||||||||||||
| 515 | push @{$package."::ISA"}, qw(XSLoader); | ||||||||||||
| 516 | { no warnings "redefine"; XSLoader::load($package); } | ||||||||||||
| 517 | } | ||||||||||||
| 518 | |||||||||||||
| 519 | =head1 NAME | ||||||||||||
| 520 | |||||||||||||
| 521 | Crypt::OpenSSL::CA::Inline::C - The Inline magic (or lack thereof) for | ||||||||||||
| 522 | Crypt::OpenSSL::CA | ||||||||||||
| 523 | |||||||||||||
| 524 | =head1 SYNOPSIS | ||||||||||||
| 525 | |||||||||||||
| 526 | use Crypt::OpenSSL::CA::Inline::C $and_the_rest_is_ignored; | ||||||||||||
| 527 | |||||||||||||
| 528 | # ... | ||||||||||||
| 529 | |||||||||||||
| 530 | use Crypt::OpenSSL::CA::Inline::C "__END__"; | ||||||||||||
| 531 | |||||||||||||
| 532 | =head1 DESCRIPTION | ||||||||||||
| 533 | |||||||||||||
| 534 | This package simply loads the DLLs that contain the parts of | ||||||||||||
| 535 | L<Crypt::OpenSSL::CA> that are made of XS code. It is a stubbed-down | ||||||||||||
| 536 | version of the full-fledged I<Crypt::OpenSSL::CA::Inline::C> that | ||||||||||||
| 537 | replaces the real thing at module install time. | ||||||||||||
| 538 | |||||||||||||
| 539 | There is more to I<Crypt::OpenSSL::CA::Inline::C>, such as the ability | ||||||||||||
| 540 | to dynamically modify and recompile the C code snippets in | ||||||||||||
| 541 | I<Crypt::OpenSSL::CA>'s code source. But in order to grasp hold of its | ||||||||||||
| 542 | power, you have to use the full source code tarball and not just the | ||||||||||||
| 543 | installed version. | ||||||||||||
| 544 | |||||||||||||
| 545 | =cut | ||||||||||||
| 546 | |||||||||||||
| 547 | 1; | ||||||||||||
| 548 | |||||||||||||
| 549 | INSTALLED_VERSION | ||||||||||||
| 550 | |||||||||||||
| 551 | =end this_pod_is_not_ours | ||||||||||||
| 552 | |||||||||||||
| 553 | =head1 ENVIRONMENT VARIABLES | ||||||||||||
| 554 | |||||||||||||
| 555 | =head2 FULL_DEBUGGING | ||||||||||||
| 556 | |||||||||||||
| 557 | Setting this variable to 1 causes the C code to be compiled without | ||||||||||||
| 558 | optimization, allowing gdb to dump symbols of static functions with | ||||||||||||
| 559 | only one call site (which comprises most of the C code in | ||||||||||||
| 560 | L<Crypt::OpenSSL::CA>). Also, the temporary build files are left | ||||||||||||
| 561 | intact if C<FULL_DEBUGGING> is set. | ||||||||||||
| 562 | |||||||||||||
| 563 | Developpers, please note that in the absence of C<FULL_DEBUGGING>, the | ||||||||||||
| 564 | default compiler flags are C<-g -O2>, still allowing for a range of | ||||||||||||
| 565 | debugging strategies. C<FULL_DEBUGGING> should therefore only be set | ||||||||||||
| 566 | on a one-shot basis by developpers who have a specific need for it. | ||||||||||||
| 567 | |||||||||||||
| 568 | =head2 BUILD_OPENSSL_CFLAGS | ||||||||||||
| 569 | |||||||||||||
| 570 | Contains the CFLAGS to pass so as to compile C code that links against | ||||||||||||
| 571 | OpenSSL; eg C<< -I/usr/lib/openssl/include >> or something. Passed on | ||||||||||||
| 572 | to L<Inline::C/INC> by L</compile_everything>. | ||||||||||||
| 573 | |||||||||||||
| 574 | =head2 BUILD_OPENSSL_LDFLAGS | ||||||||||||
| 575 | |||||||||||||
| 576 | Contains the LDFLAGS to pass so as to link with the OpenSSL libraries; | ||||||||||||
| 577 | eg C<< -L/usr/lib/openssl/lib >> or something. Passed on to | ||||||||||||
| 578 | L<Inline::C/LIBS> by L</compile_everything>. | ||||||||||||
| 579 | |||||||||||||
| 580 | =head1 SEE ALSO | ||||||||||||
| 581 | |||||||||||||
| 582 | L<Inline::C>, L<perlxstut>, L<perlguts>, L<perlapi>. | ||||||||||||
| 583 | |||||||||||||
| 584 | =cut | ||||||||||||
| 585 | |||||||||||||
| 586 | 1 | ▶ | require My::Tests::Below unless caller(); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 587 | |||||||||||||
| 588 | 1; | ||||||||||||
| 589 | |||||||||||||
| 590 | __END__ | ||||||||||||