Skip to content

Instantly share code, notes, and snippets.

@ElvinEfendi
Last active June 22, 2019 21:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ElvinEfendi/071e99d24c2235ec892144d5991b56f6 to your computer and use it in GitHub Desktop.
Save ElvinEfendi/071e99d24c2235ec892144d5991b56f6 to your computer and use it in GitHub Desktop.
example C program to read and parse certificate
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <stdint.h>
#include <sys/mman.h>
#include <openssl/ssl.h>
#include <malloc.h>
#include <unistd.h>
#define ngx_align_ptr(p, a) \
(u_char *) (((uintptr_t) (p) + ((uintptr_t) a - 1)) & ~((uintptr_t) a - 1))
ngx_http_lua_limit_data_segment(void) {
if (sbrk(0) < (void *) 0x40000000LL) {
mmap(ngx_align_ptr(sbrk(0), getpagesize()), 1, PROT_READ,
MAP_FIXED|MAP_PRIVATE|MAP_ANON, -1, 0);
}
}
void read_cert() {
const char ca_bundlestr[] = "/etc/ssl/certs/ca-certificates.crt";
BIO *outbio = NULL;
int ret;
SSL_CTX *ctx;
outbio = BIO_new_fp(stdout, BIO_NOCLOSE);
SSL_library_init();
SSL_load_error_strings();
OpenSSL_add_all_algorithms();
ctx = SSL_CTX_new(SSLv23_method());
SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS);
SSL_CTX_set_mode(ctx, SSL_MODE_NO_AUTO_CHAIN);
SSL_CTX_set_read_ahead(ctx, 1);
ret = SSL_CTX_load_verify_locations(ctx, ca_bundlestr, NULL);
if (ret == 0)
BIO_printf(outbio, "SSL_CTX_load_verify_locations failed");
BIO_free_all(outbio);
SSL_CTX_free(ctx);
}
int main() {
ngx_http_lua_limit_data_segment();
int i = 0;
for (i = 0; i < 5000; i++) {
read_cert();
//malloc_trim(0);
}
malloc_stats();
usleep(1000 * 60);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment