Re: get type of PEM data

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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 



			
-- 
openssl-users mailing list
To unsubscribe: https://mta.openssl.org/mailman/listinfo/openssl-users



[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Index of Archives]     [Linux ARM Kernel]     [Linux ARM]     [Linux Omap]     [Fedora ARM]     [IETF Annouce]     [Security]     [Bugtraq]     [Linux]     [Linux OMAP]     [Linux MIPS]     [ECOS]     [Asterisk Internet PBX]     [Linux API]

  Powered by Linux