The code to generate these messages is deep inside the OpenSSL library and you have no direct access from Perl to this and I doubt even there is a public C interface. But you can get the generated messages with a memory BIO and use of BIO_read, BIO_write etc. And again, on the Perl side AnyEvent will probably the best source to look into. You might have a look at code from other languages but I doubt that you will find much more because the use case you have is really special and weird.