34e4f738 |
1 | /* -*-c-*- |
2 | * |
b817bfc6 |
3 | * $Id: group-parse.c,v 1.3 2004/04/08 01:36:15 mdw Exp $ |
34e4f738 |
4 | * |
5 | * Parse group description strings |
6 | * |
7 | * (c) 2004 Straylight/Edgeware |
8 | */ |
9 | |
45c0fd36 |
10 | /*----- Licensing notice --------------------------------------------------* |
34e4f738 |
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. |
45c0fd36 |
18 | * |
34e4f738 |
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. |
45c0fd36 |
23 | * |
34e4f738 |
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 | |
34e4f738 |
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, '{'); |
f923fb18 |
56 | if (dh_parse(qd, &dp)) goto ouch; |
34e4f738 |
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, '{'); |
f923fb18 |
64 | if (ec_infoparse(qd, &ei)) goto ouch; |
34e4f738 |
65 | qd_delim(qd, '}'); |
66 | g = group_ec(&ei); |
67 | } break; |
68 | } |
02d7884d |
69 | if (!g) qd->e = "bad group parameters"; |
f923fb18 |
70 | ouch: |
34e4f738 |
71 | return (g); |
72 | } |
73 | |
74 | /* --- @group_fromstring@ --- * |
75 | * |
76 | * Arguments: @const char *p@ = pointer to string to read |
77 | * @group **gg@ = where to put the group pointer |
78 | * |
79 | * Returns: Null if OK, or an error string. |
80 | * |
81 | * Use: Parses a group spec from a string, and returns the group. |
82 | */ |
83 | |
84 | const char *group_fromstring(const char *p, group **gg) |
85 | { |
86 | group *g; |
87 | qd_parse qd; |
88 | |
89 | qd.p = p; |
90 | qd.e = 0; |
91 | if ((g = group_parse(&qd)) == 0) return (qd.e); |
92 | if (!qd_eofp(&qd)) { G_DESTROYGROUP(g); return ("junk at end of string"); } |
93 | *gg = g; |
94 | return (0); |
95 | } |
96 | |
97 | /*----- That's all, folks -------------------------------------------------*/ |