-
Notifications
You must be signed in to change notification settings - Fork 2
/
rpmpgp_internal_armor.c
223 lines (199 loc) · 5.06 KB
/
rpmpgp_internal_armor.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/** \ingroup rpmio signature
* \file rpmio/rpmpgp_internal_armor.c
* Routines to handle RFC-2440 ascii armor.
*/
#include "system.h"
#include <rpm/rpmstring.h>
#include <rpm/rpmlog.h>
#include <rpm/rpmbase64.h>
#include "rpmpgpval.h"
#include "rpmpgp_internal.h"
/** \ingroup rpmpgp
* Return value of an OpenPGP string.
* @param vs table of (string,value) pairs
* @param s string token to lookup
* @param se end-of-string address
* @return byte value
*/
static inline
int pgpValTok(pgpValTbl vs, const char * s, const char * se)
{
do {
size_t vlen = strlen(vs->str);
if (vlen <= (se-s) && rstreqn(s, vs->str, vlen))
break;
} while ((++vs)->val != -1);
return vs->val;
}
#define CRC24_INIT 0xb704ce
#define CRC24_POLY 0x1864cfb
/** \ingroup rpmpgp
* Return CRC of a buffer.
* @param octets bytes
* @param len no. of bytes
* @return crc of buffer
*/
static inline
unsigned int pgpCRC(const uint8_t *octets, size_t len)
{
unsigned int crc = CRC24_INIT;
size_t i;
while (len--) {
crc ^= (*octets++) << 16;
for (i = 0; i < 8; i++) {
crc <<= 1;
if (crc & 0x1000000)
crc ^= CRC24_POLY;
}
}
return crc & 0xffffff;
}
static pgpArmor decodePkts(uint8_t *b, uint8_t **pkt, size_t *pktlen)
{
const char * enc = NULL;
const char * crcenc = NULL;
uint8_t * dec;
uint8_t * crcdec;
size_t declen;
size_t crclen;
uint32_t crcpkt, crc;
const char * armortype = NULL;
char * t, * te;
int pstate = 0;
pgpArmor ec = PGPARMOR_ERR_NO_BEGIN_PGP; /* XXX assume failure */
#define TOKEQ(_s, _tok) (rstreqn((_s), (_tok), sizeof(_tok)-1))
for (t = (char *)b; t && *t; t = te) {
int rc;
if ((te = strchr(t, '\n')) == NULL)
te = t + strlen(t);
else
te++;
switch (pstate) {
case 0:
armortype = NULL;
if (!TOKEQ(t, "-----BEGIN PGP "))
continue;
t += sizeof("-----BEGIN PGP ")-1;
rc = pgpValTok(pgpArmorTbl, t, te);
if (rc < 0) {
ec = PGPARMOR_ERR_UNKNOWN_ARMOR_TYPE;
goto exit;
}
if (rc != PGPARMOR_PUBKEY) /* XXX ASCII Pubkeys only, please. */
continue;
armortype = pgpValString(PGPVAL_ARMORBLOCK, rc);
t += strlen(armortype);
if (!TOKEQ(t, "-----"))
continue;
t += sizeof("-----")-1;
if (*t != '\n' && *t != '\r')
continue;
*t = '\0';
pstate++;
break;
case 1:
enc = NULL;
rc = pgpValTok(pgpArmorKeyTbl, t, te);
if (rc >= 0)
continue;
if (*t != '\n' && *t != '\r') {
pstate = 0;
continue;
}
enc = te; /* Start of encoded packets */
pstate++;
break;
case 2:
crcenc = NULL;
if (*t != '=')
continue;
*t++ = '\0'; /* Terminate encoded packets */
crcenc = t; /* Start of encoded crc */
pstate++;
break;
case 3:
pstate = 0;
if (!TOKEQ(t, "-----END PGP ")) {
ec = PGPARMOR_ERR_NO_END_PGP;
goto exit;
}
*t = '\0'; /* Terminate encoded crc */
t += sizeof("-----END PGP ")-1;
if (t >= te) continue;
if (armortype == NULL) /* XXX can't happen */
continue;
if (!rstreqn(t, armortype, strlen(armortype)))
continue;
t += strlen(armortype);
if (t >= te) continue;
if (!TOKEQ(t, "-----")) {
ec = PGPARMOR_ERR_NO_END_PGP;
goto exit;
}
t += (sizeof("-----")-1);
/* Handle EOF without EOL here, *t == '\0' at EOF */
if (*t && (t >= te)) continue;
/* XXX permitting \r here is not RFC-2440 compliant <shrug> */
if (!(*t == '\n' || *t == '\r' || *t == '\0')) continue;
crcdec = NULL;
crclen = 0;
if (rpmBase64Decode(crcenc, (void **)&crcdec, &crclen) != 0 || crclen != 3) {
crcdec = _free(crcdec);
ec = PGPARMOR_ERR_CRC_DECODE;
goto exit;
}
crcpkt = crcdec[0] << 16 | crcdec[1] << 8 | crcdec[2];
crcdec = _free(crcdec);
dec = NULL;
declen = 0;
if (rpmBase64Decode(enc, (void **)&dec, &declen) != 0) {
ec = PGPARMOR_ERR_BODY_DECODE;
goto exit;
}
crc = pgpCRC(dec, declen);
if (crcpkt != crc) {
ec = PGPARMOR_ERR_CRC_CHECK;
_free(dec);
goto exit;
}
if (pkt)
*pkt = dec;
else
_free(dec);
if (pktlen) *pktlen = declen;
ec = PGPARMOR_PUBKEY; /* XXX ASCII Pubkeys only, please. */
goto exit;
break;
}
}
ec = PGPARMOR_NONE;
exit:
return ec;
}
pgpArmor pgpParsePkts(const char *armor, uint8_t ** pkt, size_t * pktlen)
{
pgpArmor ec = PGPARMOR_ERR_NO_BEGIN_PGP; /* XXX assume failure */
if (armor && strlen(armor) > 0) {
uint8_t *b = (uint8_t*) xstrdup(armor);
ec = decodePkts(b, pkt, pktlen);
free(b);
}
return ec;
}
char * pgpArmorWrap(int atype, const unsigned char * s, size_t ns)
{
char *buf = NULL, *val = NULL;
char *enc = rpmBase64Encode(s, ns, -1);
char *crc = rpmBase64CRC(s, ns);
const char *valstr = pgpValString(PGPVAL_ARMORBLOCK, atype);
if (crc != NULL && enc != NULL) {
rasprintf(&buf, "%s=%s", enc, crc);
}
free(crc);
free(enc);
rasprintf(&val, "-----BEGIN PGP %s-----\nVersion: rpm-" VERSION"\n\n"
"%s\n-----END PGP %s-----\n",
valstr, buf != NULL ? buf : "", valstr);
free(buf);
return val;
}