|
Packit Service |
4684c1 |
/*
|
|
Packit Service |
4684c1 |
* Copyright (C) 2016, 2017 Red Hat, Inc.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Author: Nikos Mavrogiannopoulos
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* This file is part of GnuTLS.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* GnuTLS is free software; you can redistribute it and/or modify it
|
|
Packit Service |
4684c1 |
* under the terms of the GNU General Public License as published by
|
|
Packit Service |
4684c1 |
* the Free Software Foundation; either version 3 of the License, or
|
|
Packit Service |
4684c1 |
* (at your option) any later version.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* GnuTLS is distributed in the hope that it will be useful, but
|
|
Packit Service |
4684c1 |
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
4684c1 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit Service |
4684c1 |
* General Public License for more details.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* You should have received a copy of the GNU General Public License
|
|
Packit Service |
4684c1 |
* along with GnuTLS; if not, write to the Free Software Foundation,
|
|
Packit Service |
4684c1 |
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* Parts copied from GnuTLS example programs. */
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
#ifdef HAVE_CONFIG_H
|
|
Packit Service |
4684c1 |
#include <config.h>
|
|
Packit Service |
4684c1 |
#endif
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
#include <stdio.h>
|
|
Packit Service |
4684c1 |
#include <stdlib.h>
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
#include <string.h>
|
|
Packit Service |
4684c1 |
#include <gnutls/gnutls.h>
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
#include "utils.h"
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* This checks base64 raw encoding without PEM headers */
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
static void encode(const char *test_name, const gnutls_datum_t *raw, const char *expected)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
int ret;
|
|
Packit Service |
4684c1 |
gnutls_datum_t out;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
ret = gnutls_pem_base64_encode2(NULL, raw, &out;;
|
|
Packit Service |
4684c1 |
if (ret < 0) {
|
|
Packit Service |
4684c1 |
fail("%s: gnutls_pem_base64_encode2: %s\n", test_name, gnutls_strerror(ret));
|
|
Packit Service |
4684c1 |
exit(1);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (strlen(expected)!=out.size) {
|
|
Packit Service |
4684c1 |
fail("%s: gnutls_pem_base64_encode2: output has incorrect size (%d, expected %d)\n", test_name, (int)out.size, (int)strlen(expected));
|
|
Packit Service |
4684c1 |
exit(1);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (strncasecmp(expected, (char*)out.data, out.size) != 0) {
|
|
Packit Service |
4684c1 |
fail("%s: gnutls_pem_base64_encode2: output does not match the expected\n", test_name);
|
|
Packit Service |
4684c1 |
exit(1);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
gnutls_free(out.data);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
ret = gnutls_base64_encode2(raw, &out;;
|
|
Packit Service |
4684c1 |
if (ret < 0) {
|
|
Packit Service |
4684c1 |
fail("%s: gnutls_base64_encode2: %s\n", test_name, gnutls_strerror(ret));
|
|
Packit Service |
4684c1 |
exit(1);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (strlen(expected)!=out.size) {
|
|
Packit Service |
4684c1 |
fail("%s: gnutls_base64_encode2: output has incorrect size (%d, expected %d)\n", test_name, (int)out.size, (int)strlen(expected));
|
|
Packit Service |
4684c1 |
exit(1);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (strncasecmp(expected, (char*)out.data, out.size) != 0) {
|
|
Packit Service |
4684c1 |
fail("%s: gnutls_base64_encode2: output does not match the expected\n", test_name);
|
|
Packit Service |
4684c1 |
exit(1);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
gnutls_free(out.data);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
return;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
static void encode_new(const char *test_name, const gnutls_datum_t *raw, const char *expected)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
int ret;
|
|
Packit Service |
4684c1 |
gnutls_datum_t out, in;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
ret = gnutls_base64_encode2(raw, &out;;
|
|
Packit Service |
4684c1 |
if (ret < 0) {
|
|
Packit Service |
4684c1 |
fail("%s: gnutls_base64_encode2: %s\n", test_name, gnutls_strerror(ret));
|
|
Packit Service |
4684c1 |
exit(1);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (strlen(expected)!=out.size) {
|
|
Packit Service |
4684c1 |
fail("%s: gnutls_base64_encode2: output has incorrect size (%d, expected %d)\n", test_name, (int)out.size, (int)strlen(expected));
|
|
Packit Service |
4684c1 |
exit(1);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (strncasecmp(expected, (char*)out.data, out.size) != 0) {
|
|
Packit Service |
4684c1 |
fail("%s: gnutls_base64_encode2: output does not match the expected\n", test_name);
|
|
Packit Service |
4684c1 |
exit(1);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
gnutls_free(out.data);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
ret = gnutls_base64_encode2(raw, &out;;
|
|
Packit Service |
4684c1 |
if (ret < 0) {
|
|
Packit Service |
4684c1 |
fail("%s: gnutls_base64_encode2: %s\n", test_name, gnutls_strerror(ret));
|
|
Packit Service |
4684c1 |
exit(1);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (strlen(expected)!=out.size) {
|
|
Packit Service |
4684c1 |
fail("%s: gnutls_base64_encode2: output has incorrect size (%d, expected %d)\n", test_name, (int)out.size, (int)strlen(expected));
|
|
Packit Service |
4684c1 |
exit(1);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (strncasecmp(expected, (char*)out.data, out.size) != 0) {
|
|
Packit Service |
4684c1 |
fail("%s: gnutls_base64_encode2: output does not match the expected\n", test_name);
|
|
Packit Service |
4684c1 |
exit(1);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
gnutls_free(out.data);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
in.data = (void*)expected;
|
|
Packit Service |
4684c1 |
in.size = strlen(expected);
|
|
Packit Service |
4684c1 |
ret = gnutls_base64_decode2(&in, &out;;
|
|
Packit Service |
4684c1 |
if (ret < 0) {
|
|
Packit Service |
4684c1 |
fail("%s: gnutls_base64_decode2: %s\n", test_name, gnutls_strerror(ret));
|
|
Packit Service |
4684c1 |
exit(1);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (raw->size!=out.size) {
|
|
Packit Service |
4684c1 |
fail("%s: gnutls_base64_decode2: output has incorrect size (%d, expected %d)\n", test_name, out.size, raw->size);
|
|
Packit Service |
4684c1 |
exit(1);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (memcmp(raw->data, out.data, out.size) != 0) {
|
|
Packit Service |
4684c1 |
fail("%s: gnutls_base64_decode2: output does not match the expected\n", test_name);
|
|
Packit Service |
4684c1 |
exit(1);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
gnutls_free(out.data);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
return;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
static void decode_new(const char *test_name, const gnutls_datum_t *raw, const char *hex, int res)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
int ret;
|
|
Packit Service |
4684c1 |
gnutls_datum_t out, in;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
in.data = (void*)hex;
|
|
Packit Service |
4684c1 |
in.size = strlen(hex);
|
|
Packit Service |
4684c1 |
ret = gnutls_base64_decode2(&in, &out;;
|
|
Packit Service |
4684c1 |
if (ret < 0) {
|
|
Packit Service |
4684c1 |
if (res == ret) /* expected */
|
|
Packit Service |
4684c1 |
return;
|
|
Packit Service |
4684c1 |
fail("%s: gnutls_base64_decode2: %d/%s\n", test_name, ret, gnutls_strerror(ret));
|
|
Packit Service |
4684c1 |
exit(1);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (res != 0) {
|
|
Packit Service |
4684c1 |
fail("%s: gnutls_base64_decode2: expected failure, but succeeded!\n", test_name);
|
|
Packit Service |
4684c1 |
exit(1);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (raw->size!=out.size) {
|
|
Packit Service |
4684c1 |
fail("%s: gnutls_base64_decode2: output has incorrect size (%d, expected %d)\n", test_name, out.size, raw->size);
|
|
Packit Service |
4684c1 |
exit(1);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (memcmp(raw->data, out.data, out.size) != 0) {
|
|
Packit Service |
4684c1 |
fail("%s: gnutls_base64_decode2: output does not match the expected\n", test_name);
|
|
Packit Service |
4684c1 |
exit(1);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
gnutls_free(out.data);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
return;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
struct encode_tests_st {
|
|
Packit Service |
4684c1 |
const char *name;
|
|
Packit Service |
4684c1 |
gnutls_datum_t raw;
|
|
Packit Service |
4684c1 |
const char *pem;
|
|
Packit Service |
4684c1 |
};
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
struct encode_tests_st encode_tests[] = {
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
.name = "rnd1",
|
|
Packit Service |
4684c1 |
.pem = "9ppGioRpeiiD2lLNYC85eA==",
|
|
Packit Service |
4684c1 |
.raw = {(void*)"\xf6\x9a\x46\x8a\x84\x69\x7a\x28\x83\xda\x52\xcd\x60\x2f\x39\x78", 16}
|
|
Packit Service |
4684c1 |
},
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
.name = "rnd2",
|
|
Packit Service |
4684c1 |
.pem = "LJ/7hUZ3TtPIz2dlc5+YvELe+Q==",
|
|
Packit Service |
4684c1 |
.raw = {(void*)"\x2c\x9f\xfb\x85\x46\x77\x4e\xd3\xc8\xcf\x67\x65\x73\x9f\x98\xbc\x42\xde\xf9", 19}
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
};
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
struct decode_tests_st {
|
|
Packit Service |
4684c1 |
const char *name;
|
|
Packit Service |
4684c1 |
gnutls_datum_t raw;
|
|
Packit Service |
4684c1 |
const char *pem;
|
|
Packit Service |
4684c1 |
int res;
|
|
Packit Service |
4684c1 |
};
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
struct decode_tests_st decode_tests[] = {
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
.name = "empty",
|
|
Packit Service |
4684c1 |
.pem = "",
|
|
Packit Service |
4684c1 |
.raw = {(void*)"", 0},
|
|
Packit Service |
4684c1 |
.res = 0
|
|
Packit Service |
4684c1 |
},
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
.name = "dec-rnd1",
|
|
Packit Service |
4684c1 |
.pem = "9ppGioRpeiiD2lLNYC85eA==",
|
|
Packit Service |
4684c1 |
.raw = {(void*)"\xf6\x9a\x46\x8a\x84\x69\x7a\x28\x83\xda\x52\xcd\x60\x2f\x39\x78", 16},
|
|
Packit Service |
4684c1 |
.res = 0
|
|
Packit Service |
4684c1 |
},
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
.name = "dec-rnd2",
|
|
Packit Service |
4684c1 |
.pem = "LJ/7hUZ3TtPIz2dlc5+YvELe+Q==",
|
|
Packit Service |
4684c1 |
.raw = {(void*)"\x2c\x9f\xfb\x85\x46\x77\x4e\xd3\xc8\xcf\x67\x65\x73\x9f\x98\xbc\x42\xde\xf9", 19},
|
|
Packit Service |
4684c1 |
.res = 0
|
|
Packit Service |
4684c1 |
},
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
.name = "dec-extra-chars",
|
|
Packit Service |
4684c1 |
.pem = "\n\n LJ/7hUZ3TtPIz2dlc5+YvELe+Q== \n",
|
|
Packit Service |
4684c1 |
.raw = {(void*)"\x2c\x9f\xfb\x85\x46\x77\x4e\xd3\xc8\xcf\x67\x65\x73\x9f\x98\xbc\x42\xde\xf9", 19},
|
|
Packit Service |
4684c1 |
.res = 0
|
|
Packit Service |
4684c1 |
},
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
.name = "dec-spaces",
|
|
Packit Service |
4684c1 |
.pem = " ",
|
|
Packit Service |
4684c1 |
.raw = {(void*)"", 0},
|
|
Packit Service |
4684c1 |
.res = GNUTLS_E_BASE64_DECODING_ERROR
|
|
Packit Service |
4684c1 |
},
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
.name = "dec-invalid-data",
|
|
Packit Service |
4684c1 |
.pem = "XLJ/7hUZ3TtPIz2dlc5+YvELe+Q==",
|
|
Packit Service |
4684c1 |
.raw = {(void*)"\x2c\x9f\xfb\x85\x46\x77\x4e\xd3\xc8\xcf\x67\x65\x73\x9f\x98\xbc\x42\xde\xf9", 19},
|
|
Packit Service |
4684c1 |
.res = GNUTLS_E_BASE64_DECODING_ERROR
|
|
Packit Service |
4684c1 |
},
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
.name = "dec-invalid-suffix",
|
|
Packit Service |
4684c1 |
.pem = "LJ/7hUZ3TtPIz2dlc5+YvELe+Q==XXX",
|
|
Packit Service |
4684c1 |
.raw = {(void*)"\x2c\x9f\xfb\x85\x46\x77\x4e\xd3\xc8\xcf\x67\x65\x73\x9f\x98\xbc\x42\xde\xf9", 19},
|
|
Packit Service |
4684c1 |
.res = GNUTLS_E_BASE64_DECODING_ERROR
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
};
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
void doit(void)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
unsigned i;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
for (i=0;i
|
|
Packit Service |
4684c1 |
encode(encode_tests[i].name, &encode_tests[i].raw, encode_tests[i].pem);
|
|
Packit Service |
4684c1 |
encode_new(encode_tests[i].name, &encode_tests[i].raw, encode_tests[i].pem);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
for (i=0;i
|
|
Packit Service |
4684c1 |
decode_new(decode_tests[i].name, &decode_tests[i].raw, decode_tests[i].pem, decode_tests[i].res);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|