chiark / gitweb /
server/keyexch.c: Prefix crypto-details trace messages correctly.
[tripe] / server / servutil.c
CommitLineData
410c8acf 1/* -*-c-*-
410c8acf 2 *
3 * Various handy server-only utilities
4 *
5 * (c) 2001 Straylight/Edgeware
6 */
7
e04c2d50 8/*----- Licensing notice --------------------------------------------------*
410c8acf 9 *
10 * This file is part of Trivial IP Encryption (TrIPE).
11 *
12 * TrIPE is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
e04c2d50 16 *
410c8acf 17 * TrIPE is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
e04c2d50 21 *
410c8acf 22 * You should have received a copy of the GNU General Public License
23 * along with TrIPE; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 */
26
410c8acf 27/*----- Header files ------------------------------------------------------*/
28
29#include "tripe.h"
30
df9dfccf 31/*----- Global variables --------------------------------------------------*/
32
a4b808b0 33octet buf_i[PKBUFSZ], buf_o[PKBUFSZ], buf_t[PKBUFSZ], buf_u[PKBUFSZ];
df9dfccf 34
410c8acf 35/*----- Main code ---------------------------------------------------------*/
36
37/* --- @mpstr@ --- *
38 *
39 * Arguments: @mp *m@ = a multiprecision integer
40 *
41 * Returns: A pointer to the integer's textual representation.
42 *
43 * Use: Converts a multiprecision integer to a string. Corrupts
a4b808b0 44 * @buf_u@.
410c8acf 45 */
46
47const char *mpstr(mp *m)
48{
a4b808b0 49 if (mp_writestring(m, (char *)buf_u, sizeof(buf_u), 10))
410c8acf 50 return ("<failed>");
a4b808b0 51 return ((const char *)buf_u);
a7880467 52}
53
52c03a2a 54/* --- @gestr@ --- *
55 *
56 * Arguments: @group *g@ = a group
57 * @ge *x@ = a group element
58 *
59 * Returns: A pointer to the element's textual representation.
60 *
61 * Use: Converts a group element to a string. Corrupts
a4b808b0 62 * @buf_u@.
52c03a2a 63 */
64
65const char *gestr(group *g, ge *x)
66{
a4b808b0 67 if (group_writestring(g, x, (char *)buf_u, sizeof(buf_u)))
52c03a2a 68 return ("<failed>");
a4b808b0 69 return ((const char *)buf_u);
52c03a2a 70}
71
a7880467 72/* --- @timestr@ --- *
73 *
74 * Arguments: @time_t t@ = a time to convert
75 *
76 * Returns: A pointer to a textual representation of the time.
77 *
78 * Use: Converts a time to a textual representation. Corrupts
a4b808b0 79 * @buf_u@.
a7880467 80 */
81
82const char *timestr(time_t t)
83{
84 struct tm *tm;
85 if (!t)
86 return ("NEVER");
87 tm = localtime(&t);
a4b808b0
MW
88 strftime((char *)buf_u, sizeof(buf_u), "%Y-%m-%dT%H:%M:%S", tm);
89 return ((const char *)buf_u);
410c8acf 90}
91
52b86648
MW
92/* --- @mystrieq@ --- *
93 *
94 * Arguments: @const char *x, *y@ = two strings
95 *
96 * Returns: True if @x@ and @y are equal, up to case.
97 */
98
99int mystrieq(const char *x, const char *y)
100{
101 for (;;) {
102 if (!*x && !*y) return (1);
103 if (tolower((unsigned char)*x) != tolower((unsigned char)*y))
104 return (0);
105 x++; y++;
106 }
107}
108
37941236 109/* --- @seq_reset@ --- *
110 *
111 * Arguments: @seqwin *s@ = sequence-checking window
112 *
113 * Returns: ---
114 *
115 * Use: Resets a sequence number window.
116 */
117
118void seq_reset(seqwin *s) { s->seq = 0; s->win = 0; }
119
120/* --- @seq_check@ --- *
121 *
122 * Arguments: @seqwin *s@ = sequence-checking window
123 * @uint32 q@ = sequence number to check
f43df819 124 * @const char *service@ = service to report message from
37941236 125 *
f43df819 126 * Returns: Zero on success, nonzero if the sequence number was bad.
37941236 127 *
128 * Use: Checks a sequence number against the window, updating things
129 * as necessary.
130 */
131
f43df819 132int seq_check(seqwin *s, uint32 q, const char *service)
37941236 133{
134 uint32 qbit;
135 uint32 n;
136
f43df819
MW
137 if (q < s->seq) {
138 a_warn(service, "replay", "old-sequence", A_END);
139 return (-1);
140 }
37941236 141 if (q >= s->seq + SEQ_WINSZ) {
142 n = q - (s->seq + SEQ_WINSZ - 1);
143 if (n < SEQ_WINSZ)
144 s->win >>= n;
145 else
146 s->win = 0;
147 s->seq += n;
148 }
149 qbit = 1 << (q - s->seq);
f43df819
MW
150 if (s->win & qbit) {
151 a_warn(service, "replay", "duplicated-sequence", A_END);
152 return (-1);
153 }
37941236 154 s->win |= qbit;
155 return (0);
156}
157
410c8acf 158/*----- That's all, folks -------------------------------------------------*/