| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: group-parse.c,v 1.3 2004/04/08 01:36:15 mdw Exp $ |
| 4 | * |
| 5 | * Parse group description strings |
| 6 | * |
| 7 | * (c) 2004 Straylight/Edgeware |
| 8 | */ |
| 9 | |
| 10 | /*----- Licensing notice --------------------------------------------------* |
| 11 | * |
| 12 | * This file is part of Trivial IP Encryption (TrIPE). |
| 13 | * |
| 14 | * TrIPE is free software; you can redistribute it and/or modify |
| 15 | * it under the terms of the GNU General Public License as published by |
| 16 | * the Free Software Foundation; either version 2 of the License, or |
| 17 | * (at your option) any later version. |
| 18 | * |
| 19 | * TrIPE is distributed in the hope that it will be useful, |
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | * GNU General Public License for more details. |
| 23 | * |
| 24 | * You should have received a copy of the GNU General Public License |
| 25 | * along with TrIPE; if not, write to the Free Software Foundation, |
| 26 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 27 | */ |
| 28 | |
| 29 | /*----- Header files ------------------------------------------------------*/ |
| 30 | |
| 31 | #include "group.h" |
| 32 | #include "dh.h" |
| 33 | |
| 34 | /*----- Main code ---------------------------------------------------------*/ |
| 35 | |
| 36 | /* --- @group_parse@ --- * |
| 37 | * |
| 38 | * Arguments: @qd_parse *qd@ = quick-and-dirty parser |
| 39 | * |
| 40 | * Returns: Group pointer, or null for failure. |
| 41 | * |
| 42 | * Use: Parses a group description and returns the group. This has |
| 43 | * the form `TYPE { SPEC }' where TYPE is `prime' or `ec', and |
| 44 | * SPEC is the appropriate kind of group specification of the |
| 45 | * given type. |
| 46 | */ |
| 47 | |
| 48 | group *group_parse(qd_parse *qd) |
| 49 | { |
| 50 | group *g = 0; |
| 51 | |
| 52 | switch (qd_enum(qd, "prime,ec")) { |
| 53 | case 0: { |
| 54 | dh_param dp; |
| 55 | qd_delim(qd, '{'); |
| 56 | if (dh_parse(qd, &dp)) break; |
| 57 | qd_delim(qd, '}'); |
| 58 | g = group_prime(&dp); |
| 59 | dh_paramfree(&dp); |
| 60 | } break; |
| 61 | case 1: { |
| 62 | ec_info ei; |
| 63 | qd_delim(qd, '{'); |
| 64 | if (ec_infoparse(qd, &ei)) break; |
| 65 | qd_delim(qd, '}'); |
| 66 | g = group_ec(&ei); |
| 67 | } break; |
| 68 | } |
| 69 | if (!g) qd->e = "bad group parameters"; |
| 70 | return (g); |
| 71 | } |
| 72 | |
| 73 | /* --- @group_fromstring@ --- * |
| 74 | * |
| 75 | * Arguments: @const char *p@ = pointer to string to read |
| 76 | * @group **gg@ = where to put the group pointer |
| 77 | * |
| 78 | * Returns: Null if OK, or an error string. |
| 79 | * |
| 80 | * Use: Parses a group spec from a string, and returns the group. |
| 81 | */ |
| 82 | |
| 83 | const char *group_fromstring(const char *p, group **gg) |
| 84 | { |
| 85 | group *g; |
| 86 | qd_parse qd; |
| 87 | |
| 88 | qd.p = p; |
| 89 | qd.e = 0; |
| 90 | if ((g = group_parse(&qd)) == 0) return (qd.e); |
| 91 | if (!qd_eofp(&qd)) { G_DESTROYGROUP(g); return ("junk at end of string"); } |
| 92 | *gg = g; |
| 93 | return (0); |
| 94 | } |
| 95 | |
| 96 | /*----- That's all, folks -------------------------------------------------*/ |