Skip to content

Instantly share code, notes, and snippets.

@zakird
Created March 9, 2013 22:39
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 zakird/5126089 to your computer and use it in GitHub Desktop.
Save zakird/5126089 to your computer and use it in GitHub Desktop.
#include "postgres.h"
#include "fmgr.h"
#include <openssl/x509.h>
#include <openssl/x509v3.h>
#include <openssl/bio.h>
#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif
PG_FUNCTION_INFO_V1(openssl_x509_check_issued);
/* postgres fn to check whether DER encoded X.509 bytea [0] signed [1] */
Datum openssl_x509_check_issued(PG_FUNCTION_ARGS)
{
uint8_t rs;
bytea *parent, *child;
uint64_t parent_len, child_len;
X509 *parent_cert, *child_cert;
if (PG_ARGISNULL(0) || PG_ARGISNULL(1)) {
PG_RETURN_NULL();
}
parent = PG_GETARG_BYTEA_P(0);
parent_len = VARSIZE(parent) - VARHDRSZ;
child = PG_GETARG_BYTEA_P(1);
child_len = VARSIZE(child) - VARHDRSZ;
parent_cert = d2i_X509(NULL, (const unsigned char **) &parent, parent_len);
if (!parent_cert) {
PG_RETURN_NULL();
}
child_cert = d2i_X509(NULL, (const unsigned char **) &child, child_len);
if (!child_cert) {
OPENSSL_free(parent_cert);
PG_RETURN_NULL();
}
if (X509_check_issued(parent_cert, child_cert) == X509_V_OK) {
rs = 1;
} else {
rs = 0;
}
OPENSSL_free(parent_cert);
OPENSSL_free(child_cert);
PG_RETURN_BOOL(rs);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment