+#include "onak.h"
+#include "openpgp.h"
+
+/*
+ * Convert a Public Key algorithm to its single character representation.
+ */
+char pkalgo2char(uint8_t algo)
+{
+ char typech;
+
+ switch (algo) {
+ case OPENPGP_PKALGO_DSA:
+ typech = 'D';
+ break;
+ case OPENPGP_PKALGO_ECDSA:
+ case OPENPGP_PKALGO_EDDSA:
+ typech = 'E';
+ break;
+ case OPENPGP_PKALGO_EC:
+ typech = 'e';
+ break;
+ case OPENPGP_PKALGO_ELGAMAL_SIGN:
+ typech = 'G';
+ break;
+ case OPENPGP_PKALGO_ELGAMAL_ENC:
+ typech = 'g';
+ break;
+ case OPENPGP_PKALGO_RSA:
+ typech = 'R';
+ break;
+ case OPENPGP_PKALGO_RSA_ENC:
+ typech = 'r';
+ break;
+ case OPENPGP_PKALGO_RSA_SIGN:
+ typech = 's';
+ break;
+ default:
+ typech = '?';
+ break;
+ }
+
+ return typech;
+}
+
+/**
+ * txt2html - Takes a string and converts it to HTML.
+ * @string: The string to HTMLize.
+ *
+ * Takes a string and escapes any HTML entities.
+ */
+const char *txt2html(const char *string)
+{
+ static char buf[1024];
+ char *ptr = NULL;
+ char *nextptr = NULL;
+
+ if (strlen(string) > 1000) {
+ return string;
+ }
+
+ memset(buf, 0, 1024);
+
+ ptr = strchr(string, '<');
+ if (ptr != NULL) {
+ nextptr = ptr + 1;
+ *ptr = 0;
+ strncpy(buf, string, 1023);
+ strncat(buf, "<", 1023 - strlen(buf));
+ string = nextptr;
+ }
+
+ ptr = strchr(string, '>');
+ if (ptr != NULL) {
+ nextptr = ptr + 1;
+ *ptr = 0;
+ strncat(buf, string, 1023 - strlen(buf));
+ strncat(buf, ">", 1023 - strlen(buf));
+ string = nextptr;
+ }
+
+ /*
+ * TODO: We need to while() this really as each entity may appear more
+ * than once. We need to start with & and ; as we replace with those
+ * throughout. Fuck it for the moment though; it's Easter and < & > are
+ * the most common and tend to only appear once.
+ */
+
+ strncat(buf, string, 1023 - strlen(buf));
+
+ return buf;
+}
+
+/*
+ * Given a public key/subkey packet return the key length.
+ */
+unsigned int keylength(struct openpgp_packet *keydata)
+{
+ unsigned int length;
+ uint8_t keyofs;
+ enum onak_oid oid;
+
+ switch (keydata->data[0]) {
+ case 2:
+ case 3:
+ length = (keydata->data[8] << 8) +
+ keydata->data[9];
+ break;
+ case 4:
+ case 5:
+ /* v5 has an additional 4 bytes of key length data */
+ keyofs = (keydata->data[0] == 4) ? 6 : 10;
+ switch (keydata->data[5]) {
+ case OPENPGP_PKALGO_EC:
+ case OPENPGP_PKALGO_ECDSA:
+ case OPENPGP_PKALGO_EDDSA:
+ /* Elliptic curve key size is based on OID */
+ oid = onak_parse_oid(&keydata->data[keyofs],
+ keydata->length - keyofs);
+ if (oid == ONAK_OID_CURVE25519) {
+ length = 255;
+ } else if (oid == ONAK_OID_ED25519) {
+ length = 255;
+ } else if (oid == ONAK_OID_NISTP256) {
+ length = 256;
+ } else if (oid == ONAK_OID_NISTP384) {
+ length = 384;
+ } else if (oid == ONAK_OID_NISTP521) {
+ length = 521;
+ } else if (oid == ONAK_OID_BRAINPOOLP256R1) {
+ length = 256;
+ } else if (oid == ONAK_OID_BRAINPOOLP384R1) {
+ length = 384;
+ } else if (oid == ONAK_OID_BRAINPOOLP512R1) {
+ length = 512;
+ } else if (oid == ONAK_OID_SECP256K1) {
+ length = 256;
+ } else {
+ logthing(LOGTHING_ERROR,
+ "Unknown elliptic curve size");
+ length = 0;
+ }
+ break;
+ default:
+ length = (keydata->data[keyofs] << 8) +
+ keydata->data[keyofs + 1];
+ }
+ break;
+ default:
+ logthing(LOGTHING_ERROR, "Unknown key version: %d",
+ keydata->data[0]);
+ length = 0;
+ }