Browse Source

update ecc_import_x509()

Steffen Jaeckel 6 years ago
parent
commit
f6299995f8

+ 12 - 8
src/pk/asn1/x509/x509_decode_public_key_from_certificate.c

@@ -33,7 +33,7 @@
    @param parameters_len   [in/out] The number of parameters to include
    @param callback         The callback
    @param ctx              The context passed to the callback
-   @return CRYPT_OK on success
+   @return CRYPT_OK on success, CRYPT_NOP if no SubjectPublicKeyInfo was found
 */
 int x509_decode_public_key_from_certificate(const unsigned char *in, unsigned long inlen,
                                             enum ltc_oid_id algorithm, ltc_asn1_type param_type,
@@ -59,7 +59,7 @@ int x509_decode_public_key_from_certificate(const unsigned char *in, unsigned lo
    if ((err = der_decode_sequence_flexi(in, &tmp_inlen, &decoded_list)) == CRYPT_OK) {
       l = decoded_list;
 
-      err = CRYPT_INVALID_ARG;
+      err = CRYPT_NOP;
 
       /* Move 2 levels up in the tree
          SEQUENCE
@@ -86,12 +86,16 @@ int x509_decode_public_key_from_certificate(const unsigned char *in, unsigned lo
                if ((l->type == LTC_ASN1_SEQUENCE)
                      && (l->data != NULL)
                      && LOOKS_LIKE_SPKI(l->child)) {
-                  err = x509_decode_subject_public_key_info(l->data, l->size,
-                                                            algorithm, tmpbuf, &tmpbuf_len,
-                                                            param_type, parameters, parameters_len);
-                  if (err == CRYPT_OK) {
-                     err = callback(tmpbuf, tmpbuf_len, ctx);
-                     goto LBL_OUT;
+                  if (algorithm == PKA_EC) {
+                     err = ecc_import_subject_public_key_info(l->data, l->size, ctx);
+                  } else {
+                     err = x509_decode_subject_public_key_info(l->data, l->size,
+                                                               algorithm, tmpbuf, &tmpbuf_len,
+                                                               param_type, parameters, parameters_len);
+                     if (err == CRYPT_OK) {
+                        err = callback(tmpbuf, tmpbuf_len, ctx);
+                        goto LBL_OUT;
+                  }
                   }
                }
                l = l->next;

+ 1 - 30
src/pk/ecc/ecc_import_x509.c

@@ -112,36 +112,7 @@ success:
 */
 int ecc_import_x509(const unsigned char *in, unsigned long inlen, ecc_key *key)
 {
-   int           err;
-   unsigned long len;
-   ltc_asn1_list *decoded_list = NULL, *l;
-
-   LTC_ARGCHK(in  != NULL);
-   LTC_ARGCHK(key != NULL);
-
-   len = inlen;
-   if ((err = der_decode_sequence_flexi(in, &len, &decoded_list)) == CRYPT_OK) {
-      err = CRYPT_ERROR;
-      l = decoded_list;
-      if (l->type == LTC_ASN1_SEQUENCE &&
-          l->child && l->child->type == LTC_ASN1_SEQUENCE) {
-         l = l->child->child;
-         while (l) {
-            if (l->type == LTC_ASN1_SEQUENCE && l->data &&
-                l->child && l->child->type == LTC_ASN1_SEQUENCE &&
-                l->child->child && l->child->child->type == LTC_ASN1_OBJECT_IDENTIFIER &&
-                l->child->next && l->child->next->type == LTC_ASN1_BIT_STRING) {
-               err = ecc_import_subject_public_key_info(l->data, l->size, key);
-               goto LBL_DONE;
-            }
-            l = l->next;
-         }
-      }
-   }
-
-LBL_DONE:
-   if (decoded_list) der_free_sequence_flexi(decoded_list);
-   return err;
+   return x509_decode_public_key_from_certificate(in, inlen, PKA_EC, LTC_ASN1_EOL, NULL, NULL, NULL, key);
 }
 
 #endif /* LTC_MECC */