Browse Source

add `pk_get_oid_from_asn1()`

Signed-off-by: Steffen Jaeckel <[email protected]>
Steffen Jaeckel 3 years ago
parent
commit
35c306feae
2 changed files with 37 additions and 6 deletions
  1. 1 0
      src/headers/tomcrypt_private.h
  2. 36 6
      src/pk/asn1/oid/pk_get.c

+ 1 - 0
src/headers/tomcrypt_private.h

@@ -314,6 +314,7 @@ int rand_bn_bits(void *N, int bits, prng_state *prng, int wprng);
 int rand_bn_upto(void *N, void *limit, prng_state *prng, int wprng);
 
 int pk_get_oid(enum ltc_oid_id id, const char **st);
+int pk_get_oid_from_asn1(const ltc_asn1_list *oid, enum ltc_oid_id *id);
 int pk_oid_str_to_num(const char *OID, unsigned long *oid, unsigned long *oidlen);
 int pk_oid_num_to_str(const unsigned long *oid, unsigned long oidlen, char *OID, unsigned long *outlen);
 

+ 36 - 6
src/pk/asn1/oid/pk_get.c

@@ -6,16 +6,17 @@
 
 typedef struct {
    enum ltc_oid_id id;
+   enum ltc_pka_id pka;
    const char* oid;
 } oid_table_entry;
 
 static const oid_table_entry pka_oids[] = {
-                                              { LTC_OID_RSA,       "1.2.840.113549.1.1.1" },
-                                              { LTC_OID_DSA,       "1.2.840.10040.4.1" },
-                                              { LTC_OID_EC,        "1.2.840.10045.2.1" },
-                                              { LTC_OID_EC_PRIMEF, "1.2.840.10045.1.1" },
-                                              { LTC_OID_X25519,    "1.3.101.110" },
-                                              { LTC_OID_ED25519,   "1.3.101.112" },
+                                              { LTC_OID_RSA,       LTC_PKA_RSA,        "1.2.840.113549.1.1.1" },
+                                              { LTC_OID_DSA,       LTC_PKA_DSA,        "1.2.840.10040.4.1" },
+                                              { LTC_OID_EC,        LTC_PKA_EC,         "1.2.840.10045.2.1" },
+                                              { LTC_OID_EC_PRIMEF, LTC_PKA_EC,         "1.2.840.10045.1.1" },
+                                              { LTC_OID_X25519,    LTC_PKA_CURVE25519, "1.3.101.110" },
+                                              { LTC_OID_ED25519,   LTC_PKA_CURVE25519, "1.3.101.112" },
 };
 
 /*
@@ -34,4 +35,33 @@ int pk_get_oid(enum ltc_oid_id id, const char **st)
    }
    return CRYPT_INVALID_ARG;
 }
+
+/*
+   Returns the PKA ID of an OID.
+   @return CRYPT_OK if valid
+*/
+int pk_get_oid_from_asn1(const ltc_asn1_list *oid, enum ltc_oid_id *id)
+{
+   unsigned long i;
+   char tmp[LTC_OID_MAX_STRLEN] = { 0 };
+   int err;
+
+   LTC_ARGCHK(oid != NULL);
+   LTC_ARGCHK(id != NULL);
+
+   if (oid->type != LTC_ASN1_OBJECT_IDENTIFIER) return CRYPT_INVALID_ARG;
+
+   i = sizeof(tmp);
+   if ((err = pk_oid_num_to_str(oid->data, oid->size, tmp, &i)) != CRYPT_OK) {
+      return err;
+   }
+
+   for (i = 0; i < sizeof(pka_oids)/sizeof(pka_oids[0]); ++i) {
+      if (XSTRCMP(pka_oids[i].oid, tmp) == 0) {
+         *id = pka_oids[i].id;
+         return CRYPT_OK;
+      }
+   }
+   return CRYPT_INVALID_ARG;
+}
 #endif