+#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;
+}
+
+/*
+ * 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;
+ }