[openssl-users] get type of PEM data
Michael Wojcik
Michael.Wojcik at microfocus.com
Wed Mar 28 16:26:06 UTC 2018
enum pem_type {
PEM_TYPE_NONE = 0,
PEM_TYPE_CERTIFICATE,
PEM_TYPE_RSA_PRIVATE
};
struct pem_map {
enum pem_type type;
const char *pem_string;
};
#include <openssl/pem.h>
enum pem_type identify_pem(const char *pem) {
static const struct pem_map map[] = {
{ PEM_TYPE_CERTIFICATE, PEM_STRING_X509 "-----" },
{ PEM_TYPE_RSA_PRIVATE, PEM_STRING_RSA "-----" },
};
const char *pem_begin;
int idx;
if (!pem) return PEM_TYPE_NONE;
if (! (pem_begin = strstr(pem, "-----BEGIN "))) return PEM_TYPE_NONE;
pem_begin += 11;
for (idx = 0; idx < sizeof map / sizeof *map; idx++) {
if (strncmp(pem_begin, map[idx].pem_string, strlen(map[idx].pem_string)) == 0) {
return map[idx].type;
}
}
return PEM_TYPE_NONE;
}
Untested. Extending to the remainder of the PEM types (see pem.h) is left as an exercise for the reader.
--
Michael Wojcik
Distinguished Engineer, Micro Focus
More information about the openssl-users
mailing list