+#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;
+}
+
+/**
+ * html_escape - Takes a string and converts it to HTML.
+ * @src: The string to HTMLize.
+ * @src_len: The length of the source string
+ * @dst: A buffer to put the escaped string into
+ * @dst_len: Length of the destination buffer (including a trailing NULL)
+ *
+ * Takes a string and escapes any HTML entities (<, >, &, ", '). Returns
+ * dst.
+ */
+const char *html_escape(const char *src, size_t src_len,
+ char *dst, size_t dst_len)
+{
+ size_t in_pos, out_pos;
+
+ dst_len--;
+
+ for (in_pos = 0, out_pos = 0;
+ in_pos < src_len && out_pos < (dst_len - 1);
+ in_pos++, out_pos++) {
+ switch (src[in_pos]) {
+ case '<':
+ if ((out_pos + 4) >= dst_len) {
+ break;
+ }
+ dst[out_pos++] = '&';
+ dst[out_pos++] = 'l';
+ dst[out_pos++] = 't';
+ dst[out_pos] = ';';
+ break;
+ case '>':
+ if ((out_pos + 4) >= dst_len) {
+ break;
+ }
+ dst[out_pos++] = '&';
+ dst[out_pos++] = 'g';
+ dst[out_pos++] = 't';
+ dst[out_pos] = ';';
+ break;
+ case '"':
+ if ((out_pos + 6) >= dst_len) {
+ break;
+ }
+ dst[out_pos++] = '&';
+ dst[out_pos++] = 'q';
+ dst[out_pos++] = 'u';
+ dst[out_pos++] = 'o';
+ dst[out_pos++] = 't';
+ dst[out_pos] = ';';
+ break;
+ case '\'':
+ if ((out_pos + 5) >= dst_len) {
+ break;
+ }
+ dst[out_pos++] = '&';
+ dst[out_pos++] = '#';
+ dst[out_pos++] = '3';
+ dst[out_pos++] = '9';
+ dst[out_pos] = ';';
+ break;
+ case '&':
+ if ((out_pos + 5) >= dst_len) {
+ break;
+ }
+ dst[out_pos++] = '&';
+ dst[out_pos++] = 'a';
+ dst[out_pos++] = 'm';
+ dst[out_pos++] = 'p';
+ dst[out_pos] = ';';
+ break;
+ default:
+ dst[out_pos] = src[in_pos];
+ }
+ }
+ dst[out_pos] = 0;
+
+ return dst;
+}
+
+/*
+ * 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;
+ }