[BACK]Return to ecparam.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / openssl

Diff for /src/usr.bin/openssl/ecparam.c between version 1.22 and 1.23

version 1.22, 2022/11/11 17:07:38 version 1.23, 2023/03/06 14:32:06
Line 107 
Line 107 
         char *outfile;          char *outfile;
         int outformat;          int outformat;
         int text;          int text;
 } ecparam_config;  } cfg;
   
 static int  static int
 ecparam_opt_form(char *arg)  ecparam_opt_form(char *arg)
 {  {
         if (strcmp(arg, "compressed") == 0)          if (strcmp(arg, "compressed") == 0)
                 ecparam_config.form = POINT_CONVERSION_COMPRESSED;                  cfg.form = POINT_CONVERSION_COMPRESSED;
         else if (strcmp(arg, "uncompressed") == 0)          else if (strcmp(arg, "uncompressed") == 0)
                 ecparam_config.form = POINT_CONVERSION_UNCOMPRESSED;                  cfg.form = POINT_CONVERSION_UNCOMPRESSED;
         else if (strcmp(arg, "hybrid") == 0)          else if (strcmp(arg, "hybrid") == 0)
                 ecparam_config.form = POINT_CONVERSION_HYBRID;                  cfg.form = POINT_CONVERSION_HYBRID;
         else          else
                 return (1);                  return (1);
   
         ecparam_config.new_form = 1;          cfg.new_form = 1;
         return (0);          return (0);
 }  }
   
Line 129 
Line 129 
 ecparam_opt_enctype(char *arg)  ecparam_opt_enctype(char *arg)
 {  {
         if (strcmp(arg, "explicit") == 0)          if (strcmp(arg, "explicit") == 0)
                 ecparam_config.asn1_flag = 0;                  cfg.asn1_flag = 0;
         else if (strcmp(arg, "named_curve") == 0)          else if (strcmp(arg, "named_curve") == 0)
                 ecparam_config.asn1_flag = OPENSSL_EC_NAMED_CURVE;                  cfg.asn1_flag = OPENSSL_EC_NAMED_CURVE;
         else          else
                 return (1);                  return (1);
   
         ecparam_config.new_asn1_flag = 1;          cfg.new_asn1_flag = 1;
         return (0);          return (0);
 }  }
   
Line 144 
Line 144 
                 .name = "C",                  .name = "C",
                 .desc = "Convert the EC parameters into C code",                  .desc = "Convert the EC parameters into C code",
                 .type = OPTION_FLAG,                  .type = OPTION_FLAG,
                 .opt.flag = &ecparam_config.C,                  .opt.flag = &cfg.C,
         },          },
         {          {
                 .name = "check",                  .name = "check",
                 .desc = "Validate the elliptic curve parameters",                  .desc = "Validate the elliptic curve parameters",
                 .type = OPTION_FLAG,                  .type = OPTION_FLAG,
                 .opt.flag = &ecparam_config.check,                  .opt.flag = &cfg.check,
         },          },
         {          {
                 .name = "conv_form",                  .name = "conv_form",
Line 165 
Line 165 
                 .desc = "Generate an EC private key using the specified "                  .desc = "Generate an EC private key using the specified "
                     "parameters",                      "parameters",
                 .type = OPTION_FLAG,                  .type = OPTION_FLAG,
                 .opt.flag = &ecparam_config.genkey,                  .opt.flag = &cfg.genkey,
         },          },
         {          {
                 .name = "in",                  .name = "in",
                 .argname = "file",                  .argname = "file",
                 .desc = "Input file to read parameters from (default stdin)",                  .desc = "Input file to read parameters from (default stdin)",
                 .type = OPTION_ARG,                  .type = OPTION_ARG,
                 .opt.arg = &ecparam_config.infile,                  .opt.arg = &cfg.infile,
         },          },
         {          {
                 .name = "inform",                  .name = "inform",
                 .argname = "format",                  .argname = "format",
                 .desc = "Input format (DER or PEM)",                  .desc = "Input format (DER or PEM)",
                 .type = OPTION_ARG_FORMAT,                  .type = OPTION_ARG_FORMAT,
                 .opt.value = &ecparam_config.informat,                  .opt.value = &cfg.informat,
         },          },
         {          {
                 .name = "list_curves",                  .name = "list_curves",
                 .desc = "Print list of all currently implemented EC "                  .desc = "Print list of all currently implemented EC "
                     "parameter names",                      "parameter names",
                 .type = OPTION_FLAG,                  .type = OPTION_FLAG,
                 .opt.flag = &ecparam_config.list_curves,                  .opt.flag = &cfg.list_curves,
         },          },
         {          {
                 .name = "name",                  .name = "name",
                 .argname = "curve",                  .argname = "curve",
                 .desc = "Use the EC parameters with the specified name",                  .desc = "Use the EC parameters with the specified name",
                 .type = OPTION_ARG,                  .type = OPTION_ARG,
                 .opt.arg = &ecparam_config.curve_name,                  .opt.arg = &cfg.curve_name,
         },          },
         {          {
                 .name = "no_seed",                  .name = "no_seed",
                 .desc = "Do not output seed with explicit parameter encoding",                  .desc = "Do not output seed with explicit parameter encoding",
                 .type = OPTION_FLAG,                  .type = OPTION_FLAG,
                 .opt.flag = &ecparam_config.no_seed,                  .opt.flag = &cfg.no_seed,
         },          },
         {          {
                 .name = "noout",                  .name = "noout",
                 .desc = "Do not output encoded version of EC parameters",                  .desc = "Do not output encoded version of EC parameters",
                 .type = OPTION_FLAG,                  .type = OPTION_FLAG,
                 .opt.flag = &ecparam_config.noout,                  .opt.flag = &cfg.noout,
         },          },
         {          {
                 .name = "out",                  .name = "out",
                 .argname = "file",                  .argname = "file",
                 .desc = "Output file to write parameters to (default stdout)",                  .desc = "Output file to write parameters to (default stdout)",
                 .type = OPTION_ARG,                  .type = OPTION_ARG,
                 .opt.arg = &ecparam_config.outfile,                  .opt.arg = &cfg.outfile,
         },          },
         {          {
                 .name = "outform",                  .name = "outform",
                 .argname = "format",                  .argname = "format",
                 .desc = "Output format (DER or PEM)",                  .desc = "Output format (DER or PEM)",
                 .type = OPTION_ARG_FORMAT,                  .type = OPTION_ARG_FORMAT,
                 .opt.value = &ecparam_config.outformat,                  .opt.value = &cfg.outformat,
         },          },
         {          {
                 .name = "param_enc",                  .name = "param_enc",
Line 233 
Line 233 
                 .name = "text",                  .name = "text",
                 .desc = "Print out the EC parameters in human readable form",                  .desc = "Print out the EC parameters in human readable form",
                 .type = OPTION_FLAG,                  .type = OPTION_FLAG,
                 .opt.flag = &ecparam_config.text,                  .opt.flag = &cfg.text,
         },          },
         {NULL},          {NULL},
 };  };
Line 264 
Line 264 
                 exit(1);                  exit(1);
         }          }
   
         memset(&ecparam_config, 0, sizeof(ecparam_config));          memset(&cfg, 0, sizeof(cfg));
         ecparam_config.asn1_flag = OPENSSL_EC_NAMED_CURVE;          cfg.asn1_flag = OPENSSL_EC_NAMED_CURVE;
         ecparam_config.form = POINT_CONVERSION_UNCOMPRESSED;          cfg.form = POINT_CONVERSION_UNCOMPRESSED;
         ecparam_config.informat = FORMAT_PEM;          cfg.informat = FORMAT_PEM;
         ecparam_config.outformat = FORMAT_PEM;          cfg.outformat = FORMAT_PEM;
   
         if (options_parse(argc, argv, ecparam_options, NULL, NULL) != 0) {          if (options_parse(argc, argv, ecparam_options, NULL, NULL) != 0) {
                 ecparam_usage();                  ecparam_usage();
Line 281 
Line 281 
                 ERR_print_errors(bio_err);                  ERR_print_errors(bio_err);
                 goto end;                  goto end;
         }          }
         if (ecparam_config.infile == NULL)          if (cfg.infile == NULL)
                 BIO_set_fp(in, stdin, BIO_NOCLOSE);                  BIO_set_fp(in, stdin, BIO_NOCLOSE);
         else {          else {
                 if (BIO_read_filename(in, ecparam_config.infile) <= 0) {                  if (BIO_read_filename(in, cfg.infile) <= 0) {
                         perror(ecparam_config.infile);                          perror(cfg.infile);
                         goto end;                          goto end;
                 }                  }
         }          }
         if (ecparam_config.outfile == NULL) {          if (cfg.outfile == NULL) {
                 BIO_set_fp(out, stdout, BIO_NOCLOSE);                  BIO_set_fp(out, stdout, BIO_NOCLOSE);
         } else {          } else {
                 if (BIO_write_filename(out, ecparam_config.outfile) <= 0) {                  if (BIO_write_filename(out, cfg.outfile) <= 0) {
                         perror(ecparam_config.outfile);                          perror(cfg.outfile);
                         goto end;                          goto end;
                 }                  }
         }          }
   
         if (ecparam_config.list_curves) {          if (cfg.list_curves) {
                 EC_builtin_curve *curves = NULL;                  EC_builtin_curve *curves = NULL;
                 size_t crv_len = 0;                  size_t crv_len = 0;
                 size_t n = 0;                  size_t n = 0;
Line 331 
Line 331 
                 ret = 0;                  ret = 0;
                 goto end;                  goto end;
         }          }
         if (ecparam_config.curve_name != NULL) {          if (cfg.curve_name != NULL) {
                 int nid;                  int nid;
   
                 /*                  /*
Line 339 
Line 339 
                  * secp256r1 (which are the same as the curves prime192v1 and                   * secp256r1 (which are the same as the curves prime192v1 and
                  * prime256v1 defined in X9.62)                   * prime256v1 defined in X9.62)
                  */                   */
                 if (!strcmp(ecparam_config.curve_name, "secp192r1")) {                  if (!strcmp(cfg.curve_name, "secp192r1")) {
                         BIO_printf(bio_err, "using curve name prime192v1 "                          BIO_printf(bio_err, "using curve name prime192v1 "
                             "instead of secp192r1\n");                              "instead of secp192r1\n");
                         nid = NID_X9_62_prime192v1;                          nid = NID_X9_62_prime192v1;
                 } else if (!strcmp(ecparam_config.curve_name, "secp256r1")) {                  } else if (!strcmp(cfg.curve_name, "secp256r1")) {
                         BIO_printf(bio_err, "using curve name prime256v1 "                          BIO_printf(bio_err, "using curve name prime256v1 "
                             "instead of secp256r1\n");                              "instead of secp256r1\n");
                         nid = NID_X9_62_prime256v1;                          nid = NID_X9_62_prime256v1;
                 } else                  } else
                         nid = OBJ_sn2nid(ecparam_config.curve_name);                          nid = OBJ_sn2nid(cfg.curve_name);
   
                 if (nid == 0)                  if (nid == 0)
                         nid = EC_curve_nist2nid(ecparam_config.curve_name);                          nid = EC_curve_nist2nid(cfg.curve_name);
   
                 if (nid == 0) {                  if (nid == 0) {
                         BIO_printf(bio_err, "unknown curve name (%s)\n",                          BIO_printf(bio_err, "unknown curve name (%s)\n",
                             ecparam_config.curve_name);                              cfg.curve_name);
                         goto end;                          goto end;
                 }                  }
                 group = EC_GROUP_new_by_curve_name(nid);                  group = EC_GROUP_new_by_curve_name(nid);
                 if (group == NULL) {                  if (group == NULL) {
                         BIO_printf(bio_err, "unable to create curve (%s)\n",                          BIO_printf(bio_err, "unable to create curve (%s)\n",
                             ecparam_config.curve_name);                              cfg.curve_name);
                         goto end;                          goto end;
                 }                  }
                 EC_GROUP_set_asn1_flag(group, ecparam_config.asn1_flag);                  EC_GROUP_set_asn1_flag(group, cfg.asn1_flag);
                 EC_GROUP_set_point_conversion_form(group, ecparam_config.form);                  EC_GROUP_set_point_conversion_form(group, cfg.form);
         } else if (ecparam_config.informat == FORMAT_ASN1) {          } else if (cfg.informat == FORMAT_ASN1) {
                 group = d2i_ECPKParameters_bio(in, NULL);                  group = d2i_ECPKParameters_bio(in, NULL);
         } else if (ecparam_config.informat == FORMAT_PEM) {          } else if (cfg.informat == FORMAT_PEM) {
                 group = PEM_read_bio_ECPKParameters(in, NULL, NULL, NULL);                  group = PEM_read_bio_ECPKParameters(in, NULL, NULL, NULL);
         } else {          } else {
                 BIO_printf(bio_err, "bad input format specified\n");                  BIO_printf(bio_err, "bad input format specified\n");
Line 381 
Line 381 
                 ERR_print_errors(bio_err);                  ERR_print_errors(bio_err);
                 goto end;                  goto end;
         }          }
         if (ecparam_config.new_form)          if (cfg.new_form)
                 EC_GROUP_set_point_conversion_form(group, ecparam_config.form);                  EC_GROUP_set_point_conversion_form(group, cfg.form);
   
         if (ecparam_config.new_asn1_flag)          if (cfg.new_asn1_flag)
                 EC_GROUP_set_asn1_flag(group, ecparam_config.asn1_flag);                  EC_GROUP_set_asn1_flag(group, cfg.asn1_flag);
   
         if (ecparam_config.no_seed)          if (cfg.no_seed)
                 EC_GROUP_set_seed(group, NULL, 0);                  EC_GROUP_set_seed(group, NULL, 0);
   
         if (ecparam_config.text) {          if (cfg.text) {
                 if (!ECPKParameters_print(out, group, 0))                  if (!ECPKParameters_print(out, group, 0))
                         goto end;                          goto end;
         }          }
         if (ecparam_config.check) {          if (cfg.check) {
                 BIO_printf(bio_err, "checking elliptic curve parameters: ");                  BIO_printf(bio_err, "checking elliptic curve parameters: ");
                 if (!EC_GROUP_check(group, NULL)) {                  if (!EC_GROUP_check(group, NULL)) {
                         BIO_printf(bio_err, "failed\n");                          BIO_printf(bio_err, "failed\n");
Line 403 
Line 403 
                         BIO_printf(bio_err, "ok\n");                          BIO_printf(bio_err, "ok\n");
   
         }          }
         if (ecparam_config.C) {          if (cfg.C) {
                 size_t buf_len = 0, tmp_len = 0;                  size_t buf_len = 0, tmp_len = 0;
                 const EC_POINT *point;                  const EC_POINT *point;
                 int is_prime, len = 0;                  int is_prime, len = 0;
Line 516 
Line 516 
                 BIO_printf(out, "\t\t}\n");                  BIO_printf(out, "\t\t}\n");
                 BIO_printf(out, "\treturn(group);\n\t}\n");                  BIO_printf(out, "\treturn(group);\n\t}\n");
         }          }
         if (!ecparam_config.noout) {          if (!cfg.noout) {
                 if (ecparam_config.outformat == FORMAT_ASN1)                  if (cfg.outformat == FORMAT_ASN1)
                         i = i2d_ECPKParameters_bio(out, group);                          i = i2d_ECPKParameters_bio(out, group);
                 else if (ecparam_config.outformat == FORMAT_PEM)                  else if (cfg.outformat == FORMAT_PEM)
                         i = PEM_write_bio_ECPKParameters(out, group);                          i = PEM_write_bio_ECPKParameters(out, group);
                 else {                  else {
                         BIO_printf(bio_err, "bad output format specified for"                          BIO_printf(bio_err, "bad output format specified for"
Line 533 
Line 533 
                         goto end;                          goto end;
                 }                  }
         }          }
         if (ecparam_config.genkey) {          if (cfg.genkey) {
                 EC_KEY *eckey = EC_KEY_new();                  EC_KEY *eckey = EC_KEY_new();
   
                 if (eckey == NULL)                  if (eckey == NULL)
Line 548 
Line 548 
                         EC_KEY_free(eckey);                          EC_KEY_free(eckey);
                         goto end;                          goto end;
                 }                  }
                 if (ecparam_config.outformat == FORMAT_ASN1)                  if (cfg.outformat == FORMAT_ASN1)
                         i = i2d_ECPrivateKey_bio(out, eckey);                          i = i2d_ECPrivateKey_bio(out, eckey);
                 else if (ecparam_config.outformat == FORMAT_PEM)                  else if (cfg.outformat == FORMAT_PEM)
                         i = PEM_write_bio_ECPrivateKey(out, eckey, NULL,                          i = PEM_write_bio_ECPrivateKey(out, eckey, NULL,
                             NULL, 0, NULL, NULL);                              NULL, 0, NULL, NULL);
                 else {                  else {

Legend:
Removed from v.1.22  
changed lines
  Added in v.1.23