Add loaded dynamic ENGINEs to list.
[openssl.git] / apps / ecparam.c
index e0a56062d340dc7c756e06c243eb9d41e052ec2c..50eef797ccd36003e573d97433e269cd397517d4 100644 (file)
@@ -3,7 +3,7 @@
  * Written by Nils Larsch for the OpenSSL project.
  */
 /* ====================================================================
- * Copyright (c) 1998-2002 The OpenSSL Project.  All rights reserved.
+ * Copyright (c) 1998-2005 The OpenSSL Project.  All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * The Contribution is licensed pursuant to the OpenSSL open source
  * license provided above.
  *
- * In addition, Sun covenants to all licensees who provide a reciprocal
- * covenant with respect to their own patents if any, not to sue under
- * current and future patent claims necessarily infringed by the making,
- * using, practicing, selling, offering for sale and/or otherwise
- * disposing of the Contribution as delivered hereunder 
- * (or portions thereof), provided that such covenant shall not apply:
- *  1) for code that a licensee deletes from the Contribution;
- *  2) separates from the Contribution; or
- *  3) for infringements caused by:
- *       i) the modification of the Contribution or
- *      ii) the combination of the Contribution with other software or
- *          devices where such combination causes the infringement.
- *
  * The elliptic curve binary polynomial software is originally written by 
  * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories.
  *
  */
+
+#include <openssl/opensslconf.h>
 #ifndef OPENSSL_NO_EC
 #include <assert.h>
 #include <stdio.h>
@@ -140,7 +129,6 @@ int MAIN(int argc, char **argv)
        char    *infile = NULL, *outfile = NULL, *prog;
        BIO     *in = NULL, *out = NULL;
        int     informat, outformat, noout = 0, C = 0, ret = 1;
-       ENGINE  *e = NULL;
        char    *engine = NULL;
 
        BIGNUM  *ec_p = NULL, *ec_a = NULL, *ec_b = NULL,
@@ -348,40 +336,75 @@ bad:
                        }
                }
 
-       e = setup_engine(bio_err, engine, 0);
+#ifndef OPENSSL_NO_ENGINE
+       setup_engine(bio_err, engine, 0);
+#endif
 
        if (list_curves)
                {
-               int counter=0;
+               EC_builtin_curve *curves = NULL;
+               size_t crv_len = 0;
+               size_t n = 0;
+
+               crv_len = EC_get_builtin_curves(NULL, 0);
+
+               curves = OPENSSL_malloc((int)(sizeof(EC_builtin_curve) * crv_len));
+
+               if (curves == NULL)
+                       goto end;
+
+               if (!EC_get_builtin_curves(curves, crv_len))
+                       {
+                       OPENSSL_free(curves);
+                       goto end;
+                       }
 
-               for (;;)
+               
+               for (n = 0; n < crv_len; n++)
                        {
                        const char *comment;
                        const char *sname;
-                       int len, nid = ec_group_index2nid(counter++);
-                       if (!nid)
-                               break;
-                       comment = EC_GROUP_get0_comment(nid);
-                       sname   = OBJ_nid2sn(nid);
+                       comment = curves[n].comment;
+                       sname   = OBJ_nid2sn(curves[n].nid);
                        if (comment == NULL)
-                               comment = "";
+                               comment = "CURVE DESCRIPTION NOT AVAILABLE";
                        if (sname == NULL)
                                sname = "";
 
-                       len = BIO_printf(out, "  %-10s: ", sname);
-                       if (len + strlen(comment) > 80)
-                               BIO_printf(out, "\n%80s\n", comment);
-                       else
-                               BIO_printf(out, "%s\n", comment);
+                       BIO_printf(out, "  %-10s: ", sname);
+                       BIO_printf(out, "%s\n", comment);
                        } 
 
+               OPENSSL_free(curves);
                ret = 0;
                goto end;
                }
 
        if (curve_name != NULL)
                {
-               int nid = OBJ_sn2nid(curve_name);
+               int nid;
+
+               /* workaround for the SECG curve names secp192r1
+                * and secp256r1 (which are the same as the curves
+                * prime192v1 and prime256v1 defined in X9.62)
+                */
+               if (!strcmp(curve_name, "secp192r1"))
+                       {
+                       BIO_printf(bio_err, "using curve name prime192v1 "
+                               "instead of secp192r1\n");
+                       nid = NID_X9_62_prime192v1;
+                       }
+               else if (!strcmp(curve_name, "secp256r1"))
+                       {
+                       BIO_printf(bio_err, "using curve name prime256v1 "
+                               "instead of secp256r1\n");
+                       nid = NID_X9_62_prime256v1;
+                       }
+               else
+                       nid = OBJ_sn2nid(curve_name);
+
+               if (nid == 0)
+                       nid = EC_curve_nist2nid(curve_name);
        
                if (nid == 0)
                        {
@@ -390,7 +413,7 @@ bad:
                        goto end;
                        }
 
-               group = EC_GROUP_new_by_nid(nid);
+               group = EC_GROUP_new_by_curve_name(nid);
                if (group == NULL)
                        {
                        BIO_printf(bio_err, "unable to create curve (%s)\n", 
@@ -626,11 +649,11 @@ bad:
 
                assert(need_rand);
 
-               eckey->group = group;
+               if (EC_KEY_set_group(eckey, group) == 0)
+                       goto end;
                
                if (!EC_KEY_generate_key(eckey))
                        {
-                       eckey->group = NULL;
                        EC_KEY_free(eckey);
                        goto end;
                        }
@@ -643,11 +666,9 @@ bad:
                        {
                        BIO_printf(bio_err, "bad output format specified "
                                "for outfile\n");
-                       eckey->group = NULL;
                        EC_KEY_free(eckey);
                        goto end;
                        }
-               eckey->group = NULL;
                EC_KEY_free(eckey);
                }
 
@@ -677,10 +698,10 @@ end:
        if (group != NULL)
                EC_GROUP_free(group);
        apps_shutdown();
-       EXIT(ret);
+       OPENSSL_EXIT(ret);
 }
 
-int ecparam_print_var(BIO *out, BIGNUM *in, const char *var,
+static int ecparam_print_var(BIO *out, BIGNUM *in, const char *var,
        int len, unsigned char *buffer)
        {
        BIO_printf(out, "static unsigned char %s_%d[] = {", var, len);
@@ -704,4 +725,10 @@ int ecparam_print_var(BIO *out, BIGNUM *in, const char *var,
        BIO_printf(out, "\n\t};\n\n");
        return 1;
        }
+#else /* !OPENSSL_NO_EC */
+
+# if PEDANTIC
+static void *dummy=&dummy;
+# endif
+
 #endif