Skip to content

Instantly share code, notes, and snippets.

@timh
Created December 9, 2009 03:41
Show Gist options
  • Save timh/252239 to your computer and use it in GitHub Desktop.
Save timh/252239 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <fcntl.h>
#include <openssl/pem.h>
int main(int argc, char ** argv) {
BIO * bio_pubkey = NULL;
EVP_PKEY * evp_pubkey = NULL;
RSA * rsa = NULL;
int fd = open("public.pem", O_RDONLY);
if (fd == -1) {
perror("open public.pem");
exit(-1);
}
char pubkey_pem[16 * 1024];
int totalread = 0, numread;
while ((numread = read(fd, pubkey_pem + totalread, sizeof(pubkey_pem) - totalread - 1)) > 0) {
totalread += numread;
}
close(fd);
pubkey_pem[totalread] = '\0';
printf("pubkey_pem =\n%s\n", pubkey_pem);
bio_pubkey = BIO_new_mem_buf(pubkey_pem, -1);
printf("bio_pubkey = %p\n", bio_pubkey);
rsa = PEM_read_bio_RSAPublicKey(bio_pubkey, NULL, NULL, NULL);
printf("rsa = %p\n", rsa);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment