2008-06-09 16:49:28 +02:00
|
|
|
/* b64dec.c - Simple Base64 decoder.
|
2011-01-20 14:12:53 +01:00
|
|
|
* Copyright (C) 2008, 2011 Free Software Foundation, Inc.
|
2016-06-23 09:21:45 +02:00
|
|
|
* Copyright (C) 2008, 2011, 2016 g10 Code GmbH
|
2008-06-09 16:49:28 +02:00
|
|
|
*
|
|
|
|
* This file is part of GnuPG.
|
|
|
|
*
|
Change license for some files in common to LGPLv3+/GPLv2+.
Having the LGPL on the common GnuPG code helps to share code
between GnuPG and related projects (like GPGME and Libassuan). This
is good for interoperability and to reduces bugs.
* common/asshelp.c, common/asshelp.h, common/asshelp2.c, common/b64dec.c
* common/b64enc.c, common/convert.c, common/dns-cert.c
* common/dns-cert.h common/exechelp-posix.c, common/exechelp-w32.c
* common/exechelp-w32ce.c, common/exechelp.h, common/get-passphrase.c
* common/get-passphrase.h, common/gettime.c, common/gpgrlhelp.c
* common/helpfile.c, common/homedir.c, common/http.c, common/http.h
* common/i18n.c, common/init.c, common/init.h, common/iobuf.c
* common/iobuf.h, common/localename.c, common/membuf.c, common/membuf.h
* common/miscellaneous.c, common/openpgp-oid.c, common/openpgpdefs.h
* common/percent.c, common/pka.c, common/pka.h, common/session-env.c
* common/session-env.h, common/sexp-parse.h, common/sexputil.c
* common/signal.c, common/srv.c, common/srv.h, common/ssh-utils.c
* common/ssh-utils.h, common/sysutils.c, common/sysutils.h
* common/tlv.c, common/tlv.h, common/ttyio.c, common/ttyio.h
* common/userids.c, common/userids.h, common/xasprintf.c: Change
license to LGPLv3+/GPLv2+/
2012-04-20 15:43:06 +02:00
|
|
|
* This file is free software; you can redistribute it and/or modify
|
2016-06-23 09:21:45 +02:00
|
|
|
* it under the terms of the GNU Lesser General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2.1 of
|
|
|
|
* the License, or (at your option) any later version.
|
Change license for some files in common to LGPLv3+/GPLv2+.
Having the LGPL on the common GnuPG code helps to share code
between GnuPG and related projects (like GPGME and Libassuan). This
is good for interoperability and to reduces bugs.
* common/asshelp.c, common/asshelp.h, common/asshelp2.c, common/b64dec.c
* common/b64enc.c, common/convert.c, common/dns-cert.c
* common/dns-cert.h common/exechelp-posix.c, common/exechelp-w32.c
* common/exechelp-w32ce.c, common/exechelp.h, common/get-passphrase.c
* common/get-passphrase.h, common/gettime.c, common/gpgrlhelp.c
* common/helpfile.c, common/homedir.c, common/http.c, common/http.h
* common/i18n.c, common/init.c, common/init.h, common/iobuf.c
* common/iobuf.h, common/localename.c, common/membuf.c, common/membuf.h
* common/miscellaneous.c, common/openpgp-oid.c, common/openpgpdefs.h
* common/percent.c, common/pka.c, common/pka.h, common/session-env.c
* common/session-env.h, common/sexp-parse.h, common/sexputil.c
* common/signal.c, common/srv.c, common/srv.h, common/ssh-utils.c
* common/ssh-utils.h, common/sysutils.c, common/sysutils.h
* common/tlv.c, common/tlv.h, common/ttyio.c, common/ttyio.h
* common/userids.c, common/userids.h, common/xasprintf.c: Change
license to LGPLv3+/GPLv2+/
2012-04-20 15:43:06 +02:00
|
|
|
*
|
|
|
|
* This file is distributed in the hope that it will be useful,
|
2008-06-09 16:49:28 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
2016-06-23 09:21:45 +02:00
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
2016-11-05 12:02:19 +01:00
|
|
|
* along with this program; if not, see <https://www.gnu.org/licenses/>.
|
2008-06-09 16:49:28 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include "i18n.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
|
|
|
|
/* The reverse base-64 list used for base-64 decoding. */
|
2011-02-04 12:57:53 +01:00
|
|
|
static unsigned char const asctobin[128] =
|
2008-06-09 16:49:28 +02:00
|
|
|
{
|
|
|
|
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
|
|
|
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
|
|
|
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
|
|
|
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
|
|
|
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
|
|
|
0xff, 0xff, 0xff, 0x3e, 0xff, 0xff, 0xff, 0x3f,
|
|
|
|
0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
|
|
|
|
0x3c, 0x3d, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
|
|
|
0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
|
|
|
|
0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
|
|
|
|
0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
|
|
|
|
0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff,
|
|
|
|
0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
|
|
|
|
0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
|
|
|
|
0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
|
|
|
|
0x31, 0x32, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff
|
|
|
|
};
|
|
|
|
|
2011-02-04 12:57:53 +01:00
|
|
|
enum decoder_states
|
2008-06-09 16:49:28 +02:00
|
|
|
{
|
2016-06-23 09:20:24 +02:00
|
|
|
s_init, s_idle, s_lfseen, s_beginseen, s_waitheader, s_waitblank, s_begin,
|
2008-06-09 16:49:28 +02:00
|
|
|
s_b64_0, s_b64_1, s_b64_2, s_b64_3,
|
|
|
|
s_waitendtitle, s_waitend
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Initialize the context for the base64 decoder. If TITLE is NULL a
|
|
|
|
plain base64 decoding is done. If it is the empty string the
|
|
|
|
decoder will skip everything until a "-----BEGIN " line has been
|
2016-06-23 09:20:24 +02:00
|
|
|
seen, decoding ends at a "----END " line. */
|
2008-06-09 16:49:28 +02:00
|
|
|
gpg_error_t
|
|
|
|
b64dec_start (struct b64state *state, const char *title)
|
|
|
|
{
|
|
|
|
memset (state, 0, sizeof *state);
|
|
|
|
if (title)
|
|
|
|
{
|
2016-06-23 09:20:24 +02:00
|
|
|
state->title = xtrystrdup (title);
|
|
|
|
if (!state->title)
|
|
|
|
state->lasterr = gpg_error_from_syserror ();
|
2011-01-20 14:12:53 +01:00
|
|
|
else
|
2016-06-23 09:20:24 +02:00
|
|
|
state->idx = s_init;
|
2008-06-09 16:49:28 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
state->idx = s_b64_0;
|
2011-01-20 14:12:53 +01:00
|
|
|
return state->lasterr;
|
2008-06-09 16:49:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Do in-place decoding of base-64 data of LENGTH in BUFFER. Stores the
|
|
|
|
new length of the buffer at R_NBYTES. */
|
|
|
|
gpg_error_t
|
|
|
|
b64dec_proc (struct b64state *state, void *buffer, size_t length,
|
|
|
|
size_t *r_nbytes)
|
|
|
|
{
|
|
|
|
enum decoder_states ds = state->idx;
|
|
|
|
unsigned char val = state->radbuf[0];
|
2011-02-04 12:57:53 +01:00
|
|
|
int pos = state->quad_count;
|
2008-06-09 16:49:28 +02:00
|
|
|
char *d, *s;
|
|
|
|
|
2011-01-20 14:12:53 +01:00
|
|
|
if (state->lasterr)
|
|
|
|
return state->lasterr;
|
|
|
|
|
2008-06-09 16:49:28 +02:00
|
|
|
if (state->stop_seen)
|
|
|
|
{
|
|
|
|
*r_nbytes = 0;
|
2011-01-20 14:12:53 +01:00
|
|
|
state->lasterr = gpg_error (GPG_ERR_EOF);
|
|
|
|
xfree (state->title);
|
|
|
|
state->title = NULL;
|
|
|
|
return state->lasterr;
|
2008-06-09 16:49:28 +02:00
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2008-06-09 16:49:28 +02:00
|
|
|
for (s=d=buffer; length && !state->stop_seen; length--, s++)
|
|
|
|
{
|
2016-06-23 09:20:24 +02:00
|
|
|
again:
|
2008-06-09 16:49:28 +02:00
|
|
|
switch (ds)
|
|
|
|
{
|
|
|
|
case s_idle:
|
|
|
|
if (*s == '\n')
|
|
|
|
{
|
|
|
|
ds = s_lfseen;
|
|
|
|
pos = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case s_init:
|
|
|
|
ds = s_lfseen;
|
2017-05-10 04:01:15 +02:00
|
|
|
/* fall through */
|
2008-06-09 16:49:28 +02:00
|
|
|
case s_lfseen:
|
|
|
|
if (*s != "-----BEGIN "[pos])
|
2016-06-23 09:20:24 +02:00
|
|
|
{
|
|
|
|
ds = s_idle;
|
|
|
|
goto again;
|
|
|
|
}
|
2008-06-09 16:49:28 +02:00
|
|
|
else if (pos == 10)
|
2016-06-23 09:20:24 +02:00
|
|
|
{
|
|
|
|
pos = 0;
|
|
|
|
ds = s_beginseen;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
pos++;
|
|
|
|
break;
|
|
|
|
case s_beginseen:
|
|
|
|
if (*s != "PGP "[pos])
|
|
|
|
ds = s_begin; /* Not a PGP armor. */
|
|
|
|
else if (pos == 3)
|
|
|
|
ds = s_waitheader;
|
2008-06-09 16:49:28 +02:00
|
|
|
else
|
|
|
|
pos++;
|
|
|
|
break;
|
2016-06-23 09:20:24 +02:00
|
|
|
case s_waitheader:
|
|
|
|
if (*s == '\n')
|
|
|
|
ds = s_waitblank;
|
|
|
|
break;
|
|
|
|
case s_waitblank:
|
|
|
|
if (*s == '\n')
|
|
|
|
ds = s_b64_0; /* blank line found. */
|
|
|
|
else if (*s == ' ' || *s == '\r' || *s == '\t')
|
|
|
|
; /* Ignore spaces. */
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Armor header line. Note that we don't care that our
|
|
|
|
* FSM accepts a header prefixed with spaces. */
|
|
|
|
ds = s_waitheader; /* Wait for next header. */
|
|
|
|
}
|
|
|
|
break;
|
2008-06-09 16:49:28 +02:00
|
|
|
case s_begin:
|
|
|
|
if (*s == '\n')
|
|
|
|
ds = s_b64_0;
|
|
|
|
break;
|
|
|
|
case s_b64_0:
|
|
|
|
case s_b64_1:
|
|
|
|
case s_b64_2:
|
|
|
|
case s_b64_3:
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
|
2011-02-04 12:57:53 +01:00
|
|
|
if (*s == '-' && state->title)
|
2008-06-09 16:49:28 +02:00
|
|
|
{
|
|
|
|
/* Not a valid Base64 character: assume end
|
|
|
|
header. */
|
|
|
|
ds = s_waitend;
|
|
|
|
}
|
|
|
|
else if (*s == '=')
|
|
|
|
{
|
|
|
|
/* Pad character: stop */
|
|
|
|
if (ds == s_b64_1)
|
|
|
|
*d++ = val;
|
|
|
|
ds = state->title? s_waitendtitle : s_waitend;
|
|
|
|
}
|
|
|
|
else if (*s == '\n' || *s == ' ' || *s == '\r' || *s == '\t')
|
|
|
|
; /* Skip white spaces. */
|
2011-02-04 12:57:53 +01:00
|
|
|
else if ( (*s & 0x80)
|
2008-06-09 16:49:28 +02:00
|
|
|
|| (c = asctobin[*(unsigned char *)s]) == 255)
|
|
|
|
{
|
|
|
|
/* Skip invalid encodings. */
|
|
|
|
state->invalid_encoding = 1;
|
|
|
|
}
|
|
|
|
else if (ds == s_b64_0)
|
|
|
|
{
|
|
|
|
val = c << 2;
|
|
|
|
ds = s_b64_1;
|
|
|
|
}
|
|
|
|
else if (ds == s_b64_1)
|
|
|
|
{
|
|
|
|
val |= (c>>4)&3;
|
|
|
|
*d++ = val;
|
|
|
|
val = (c<<4)&0xf0;
|
|
|
|
ds = s_b64_2;
|
|
|
|
}
|
|
|
|
else if (ds == s_b64_2)
|
|
|
|
{
|
|
|
|
val |= (c>>2)&15;
|
|
|
|
*d++ = val;
|
|
|
|
val = (c<<6)&0xc0;
|
|
|
|
ds = s_b64_3;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
val |= c&0x3f;
|
|
|
|
*d++ = val;
|
|
|
|
ds = s_b64_0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case s_waitendtitle:
|
|
|
|
if (*s == '-')
|
|
|
|
ds = s_waitend;
|
|
|
|
break;
|
|
|
|
case s_waitend:
|
|
|
|
if ( *s == '\n')
|
|
|
|
state->stop_seen = 1;
|
2011-02-04 12:57:53 +01:00
|
|
|
break;
|
|
|
|
default:
|
2008-06-09 16:49:28 +02:00
|
|
|
BUG();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
state->idx = ds;
|
|
|
|
state->radbuf[0] = val;
|
2011-02-04 12:57:53 +01:00
|
|
|
state->quad_count = pos;
|
2008-06-09 16:49:28 +02:00
|
|
|
*r_nbytes = (d -(char*) buffer);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* This function needs to be called before releasing the decoder
|
|
|
|
state. It may return an error code in case an encoding error has
|
|
|
|
been found during decoding. */
|
|
|
|
gpg_error_t
|
|
|
|
b64dec_finish (struct b64state *state)
|
|
|
|
{
|
2016-06-23 17:44:55 +02:00
|
|
|
xfree (state->title);
|
|
|
|
state->title = NULL;
|
|
|
|
|
2011-01-20 14:12:53 +01:00
|
|
|
if (state->lasterr)
|
|
|
|
return state->lasterr;
|
|
|
|
|
2008-06-09 16:49:28 +02:00
|
|
|
return state->invalid_encoding? gpg_error(GPG_ERR_BAD_DATA): 0;
|
|
|
|
}
|