|
Packit Service |
4684c1 |
/*
|
|
Packit Service |
4684c1 |
* Copyright (C) 2017-2019 Red Hat, Inc.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Author: Nikos Mavrogiannopoulos, Daiki Ueno
|
|
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 |
#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 |
#include <string.h>
|
|
Packit Service |
4684c1 |
#include <sys/types.h>
|
|
Packit Service |
4684c1 |
#ifndef _WIN32
|
|
Packit Service |
4684c1 |
# include <netinet/in.h>
|
|
Packit Service |
4684c1 |
# include <sys/socket.h>
|
|
Packit Service |
4684c1 |
# include <arpa/inet.h>
|
|
Packit Service |
4684c1 |
#endif
|
|
Packit Service |
4684c1 |
#include <unistd.h>
|
|
Packit Service |
4684c1 |
#include <gnutls/gnutls.h>
|
|
Packit Service |
4684c1 |
#include <gnutls/x509.h>
|
|
Packit Service |
4684c1 |
#include <gnutls/abstract.h>
|
|
Packit Service |
4684c1 |
#include "utils.h"
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* verifies whether the sign-data and verify-data APIs
|
|
Packit Service |
4684c1 |
* operate as expected with deterministic ECDSA/DSA (RFC 6979) */
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
static void tls_log_func(int level, const char *str)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
fprintf(stderr, "<%d> %s", level, str);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
struct _key_tests_st {
|
|
Packit Service |
4684c1 |
const char *name;
|
|
Packit Service |
4684c1 |
gnutls_datum_t key;
|
|
Packit Service |
4684c1 |
gnutls_datum_t msg;
|
|
Packit Service |
4684c1 |
gnutls_datum_t sig;
|
|
Packit Service |
4684c1 |
gnutls_pk_algorithm_t pk;
|
|
Packit Service |
4684c1 |
gnutls_digest_algorithm_t digest;
|
|
Packit Service |
4684c1 |
gnutls_sign_algorithm_t sigalgo;
|
|
Packit Service |
4684c1 |
unsigned int sign_flags;
|
|
Packit Service |
4684c1 |
};
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* Test vectors from RFC 6979 */
|
|
Packit Service |
4684c1 |
static const char dsa_privkey_rfc6979[] =
|
|
Packit Service |
4684c1 |
"-----BEGIN DSA PRIVATE KEY-----\n"
|
|
Packit Service |
4684c1 |
"MIIBugIBAAKBgQCG9coD3P6yJQY/+DCgx2m53Z1hU62R184n94fEMni0R+ZTO4ax\n"
|
|
Packit Service |
4684c1 |
"i+1uiki3hKFMJSxb4Nv2C4bWOFvS8S+3Y+2Ic6v9P1ui4KjApZCC6sBWk15Sna98\n"
|
|
Packit Service |
4684c1 |
"YQRniZx3re38hGyIGHC3sZsrWPm+BSGhcALjvda4ZoXukLPZobAreCsXeQIVAJlv\n"
|
|
Packit Service |
4684c1 |
"ln9sjjiNnijQHiBfupV6VpixAoGAB7D5JUYVC2JRS7dx4qDAzjh/A72mxWtQUgn/\n"
|
|
Packit Service |
4684c1 |
"Jf08Ez2Ju82X6QTgkRTZp9796t/JB46lRNLkAa7sxAu5+794/YeZWhChwny3eJtZ\n"
|
|
Packit Service |
4684c1 |
"S6fvtcQyap/lmgcOE223cXVGStykF75dzi9A0QpGo6OUPyarf9nAOY/4x27gpWgm\n"
|
|
Packit Service |
4684c1 |
"qKiPHb0CgYBd9eAd7THQKX4nThaRwZL+WGj++eGahHdkVLEAzxb2U5IZWji5BSPi\n"
|
|
Packit Service |
4684c1 |
"VC7mGHHARAy4fDIvxLTS7F4efsdm4b6NTOk1Q33BHDyP1CYziTPr/nOcs0ZfTTZo\n"
|
|
Packit Service |
4684c1 |
"xeRzUIJTseaC9ly9xPrpPC6iEjkOVJBahuIiMXC0Tqp9pd2f/Pt/OwIUQRYCyxmm\n"
|
|
Packit Service |
4684c1 |
"zMNElNedmO8eftWvJfc=\n"
|
|
Packit Service |
4684c1 |
"-----END DSA PRIVATE KEY-----\n";
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
static const char ecdsa_secp256r1_privkey_rfc6979[] =
|
|
Packit Service |
4684c1 |
"-----BEGIN EC PRIVATE KEY-----\n"
|
|
Packit Service |
4684c1 |
"MHgCAQEEIQDJr6nYRbp1FmtcIVdnsdaTTlDD2zbomxJ7imIrEg9nIaAKBggqhkjO\n"
|
|
Packit Service |
4684c1 |
"PQMBB6FEA0IABGD+1LolWp0xyWHrdMY1bWjASbiSO2H6bOZpYi5g8p+2eQP+EAi4\n"
|
|
Packit Service |
4684c1 |
"vJmkGunpVii8ZPLxsgwtfp9Rd6PClNRGIpk=\n"
|
|
Packit Service |
4684c1 |
"-----END EC PRIVATE KEY-----\n";
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
static const char sample[] = "sample";
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
static const
|
|
Packit Service |
4684c1 |
struct _key_tests_st tests[] = {
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
.name = "dsa key",
|
|
Packit Service |
4684c1 |
.key = {(void *) dsa_privkey_rfc6979, sizeof(dsa_privkey_rfc6979)-1},
|
|
Packit Service |
4684c1 |
.msg = {(void *) sample, sizeof(sample)-1},
|
|
Packit Service |
4684c1 |
.sig = {(void *) "\x30\x2d\x02\x15\x00\x81\xf2\xf5\x85\x0b\xe5\xbc\x12\x3c\x43\xf7\x1a\x30\x33\xe9\x38\x46\x11\xc5\x45\x02\x14\x4c\xdd\x91\x4b\x65\xeb\x6c\x66\xa8\xaa\xad\x27\x29\x9b\xee\x6b\x03\x5f\x5e\x89", 47},
|
|
Packit Service |
4684c1 |
.pk = GNUTLS_PK_DSA,
|
|
Packit Service |
4684c1 |
.digest = GNUTLS_DIG_SHA256,
|
|
Packit Service |
4684c1 |
.sigalgo = GNUTLS_SIGN_DSA_SHA256,
|
|
Packit Service |
4684c1 |
.sign_flags = GNUTLS_PRIVKEY_FLAG_REPRODUCIBLE
|
|
Packit Service |
4684c1 |
},
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
.name = "ecdsa key",
|
|
Packit Service |
4684c1 |
.key = {(void *) ecdsa_secp256r1_privkey_rfc6979, sizeof(ecdsa_secp256r1_privkey_rfc6979)-1},
|
|
Packit Service |
4684c1 |
.msg = {(void *) sample, sizeof(sample)-1},
|
|
Packit Service |
4684c1 |
.sig = {(void *) "\x30\x46\x02\x21\x00\xef\xd4\x8b\x2a\xac\xb6\xa8\xfd\x11\x40\xdd\x9c\xd4\x5e\x81\xd6\x9d\x2c\x87\x7b\x56\xaa\xf9\x91\xc3\x4d\x0e\xa8\x4e\xaf\x37\x16\x02\x21\x00\xf7\xcb\x1c\x94\x2d\x65\x7c\x41\xd4\x36\xc7\xa1\xb6\xe2\x9f\x65\xf3\xe9\x00\xdb\xb9\xaf\xf4\x06\x4d\xc4\xab\x2f\x84\x3a\xcd\xa8", 72},
|
|
Packit Service |
4684c1 |
.pk = GNUTLS_PK_ECDSA,
|
|
Packit Service |
4684c1 |
.digest = GNUTLS_DIG_SHA256,
|
|
Packit Service |
4684c1 |
.sigalgo = GNUTLS_SIGN_ECDSA_SECP256R1_SHA256,
|
|
Packit Service |
4684c1 |
.sign_flags = GNUTLS_PRIVKEY_FLAG_REPRODUCIBLE
|
|
Packit Service |
4684c1 |
},
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
.name = "ecdsa key",
|
|
Packit Service |
4684c1 |
.key = {(void *) ecdsa_secp256r1_privkey_rfc6979, sizeof(ecdsa_secp256r1_privkey_rfc6979)-1},
|
|
Packit Service |
4684c1 |
.msg = {(void *) sample, sizeof(sample)-1},
|
|
Packit Service |
4684c1 |
.sig = {(void *) "\x30\x46\x02\x21\x00\xef\xd4\x8b\x2a\xac\xb6\xa8\xfd\x11\x40\xdd\x9c\xd4\x5e\x81\xd6\x9d\x2c\x87\x7b\x56\xaa\xf9\x91\xc3\x4d\x0e\xa8\x4e\xaf\x37\x16\x02\x21\x00\xf7\xcb\x1c\x94\x2d\x65\x7c\x41\xd4\x36\xc7\xa1\xb6\xe2\x9f\x65\xf3\xe9\x00\xdb\xb9\xaf\xf4\x06\x4d\xc4\xab\x2f\x84\x3a\xcd\xa8", 72},
|
|
Packit Service |
4684c1 |
.pk = GNUTLS_PK_ECDSA,
|
|
Packit Service |
4684c1 |
.digest = GNUTLS_DIG_SHA256,
|
|
Packit Service |
4684c1 |
.sigalgo = GNUTLS_SIGN_ECDSA_SHA256,
|
|
Packit Service |
4684c1 |
.sign_flags = GNUTLS_PRIVKEY_FLAG_REPRODUCIBLE
|
|
Packit Service |
4684c1 |
},
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
.name = "ecdsa key (q bits < h bits)",
|
|
Packit Service |
4684c1 |
.key = {(void *) ecdsa_secp256r1_privkey_rfc6979, sizeof(ecdsa_secp256r1_privkey_rfc6979)-1},
|
|
Packit Service |
4684c1 |
.msg = {(void *) sample, sizeof(sample)-1},
|
|
Packit Service |
4684c1 |
.sig = {(void *) "\x30\x44\x02\x20\x0e\xaf\xea\x03\x9b\x20\xe9\xb4\x23\x09\xfb\x1d\x89\xe2\x13\x05\x7c\xbf\x97\x3d\xc0\xcf\xc8\xf1\x29\xed\xdd\xc8\x00\xef\x77\x19\x02\x20\x48\x61\xf0\x49\x1e\x69\x98\xb9\x45\x51\x93\xe3\x4e\x7b\x0d\x28\x4d\xdd\x71\x49\xa7\x4b\x95\xb9\x26\x1f\x13\xab\xde\x94\x09\x54", 70},
|
|
Packit Service |
4684c1 |
.pk = GNUTLS_PK_ECDSA,
|
|
Packit Service |
4684c1 |
.digest = GNUTLS_DIG_SHA384,
|
|
Packit Service |
4684c1 |
.sigalgo = GNUTLS_SIGN_ECDSA_SHA384,
|
|
Packit Service |
4684c1 |
.sign_flags = GNUTLS_PRIVKEY_FLAG_REPRODUCIBLE
|
|
Packit Service |
4684c1 |
},
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
.name = "ecdsa key (q bits > h bits)",
|
|
Packit Service |
4684c1 |
.key = {(void *) ecdsa_secp256r1_privkey_rfc6979, sizeof(ecdsa_secp256r1_privkey_rfc6979)-1},
|
|
Packit Service |
4684c1 |
.msg = {(void *) sample, sizeof(sample)-1},
|
|
Packit Service |
4684c1 |
.sig = {(void *) "\x30\x45\x02\x20\x53\xb2\xff\xf5\xd1\x75\x2b\x2c\x68\x9d\xf2\x57\xc0\x4c\x40\xa5\x87\xfa\xba\xbb\x3f\x6f\xc2\x70\x2f\x13\x43\xaf\x7c\xa9\xaa\x3f\x02\x21\x00\xb9\xaf\xb6\x4f\xdc\x03\xdc\x1a\x13\x1c\x7d\x23\x86\xd1\x1e\x34\x9f\x07\x0a\xa4\x32\xa4\xac\xc9\x18\xbe\xa9\x88\xbf\x75\xc7\x4c", 71},
|
|
Packit Service |
4684c1 |
.pk = GNUTLS_PK_ECDSA,
|
|
Packit Service |
4684c1 |
.digest = GNUTLS_DIG_SHA224,
|
|
Packit Service |
4684c1 |
.sigalgo = GNUTLS_SIGN_ECDSA_SHA224,
|
|
Packit Service |
4684c1 |
.sign_flags = GNUTLS_PRIVKEY_FLAG_REPRODUCIBLE
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
};
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
#define testfail(fmt, ...) \
|
|
Packit Service |
4684c1 |
fail("%s: "fmt, tests[i].name, ##__VA_ARGS__)
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
void doit(void)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
gnutls_pubkey_t pubkey;
|
|
Packit Service |
4684c1 |
gnutls_privkey_t privkey;
|
|
Packit Service |
4684c1 |
gnutls_datum_t signature;
|
|
Packit Service |
4684c1 |
int ret;
|
|
Packit Service |
4684c1 |
size_t i;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
global_init();
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
gnutls_global_set_log_function(tls_log_func);
|
|
Packit Service |
4684c1 |
if (debug)
|
|
Packit Service |
4684c1 |
gnutls_global_set_log_level(6);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
|
|
Packit Service |
4684c1 |
success("testing: %s - %s", tests[i].name, gnutls_sign_algorithm_get_name(tests[i].sigalgo));
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
ret = gnutls_pubkey_init(&pubkey);
|
|
Packit Service |
4684c1 |
if (ret < 0)
|
|
Packit Service |
4684c1 |
testfail("gnutls_pubkey_init\n");
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
ret = gnutls_privkey_init(&privkey);
|
|
Packit Service |
4684c1 |
if (ret < 0)
|
|
Packit Service |
4684c1 |
testfail("gnutls_privkey_init\n");
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
signature.data = NULL;
|
|
Packit Service |
4684c1 |
signature.size = 0;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
ret = gnutls_privkey_import_x509_raw(privkey, &tests[i].key, GNUTLS_X509_FMT_PEM, NULL, 0);
|
|
Packit Service |
4684c1 |
if (ret < 0)
|
|
Packit Service |
4684c1 |
testfail("gnutls_privkey_import_x509_raw\n");
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
ret = gnutls_privkey_sign_data(privkey, tests[i].digest, tests[i].sign_flags,
|
|
Packit Service |
4684c1 |
&tests[i].msg, &signature);
|
|
Packit Service |
4684c1 |
if (gnutls_fips140_mode_enabled()) {
|
|
Packit Service |
4684c1 |
/* deterministic ECDSA/DSA is prohibited under FIPS */
|
|
Packit Service |
4684c1 |
if (ret != GNUTLS_E_INVALID_REQUEST)
|
|
Packit Service |
4684c1 |
testfail("gnutls_privkey_sign_data unexpectedly succeeds\n");
|
|
Packit Service |
4684c1 |
success(" - skipping\n");
|
|
Packit Service |
4684c1 |
goto next;
|
|
Packit Service |
4684c1 |
} else {
|
|
Packit Service |
4684c1 |
if (ret < 0)
|
|
Packit Service |
4684c1 |
testfail("gnutls_privkey_sign_data\n");
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (signature.size != tests[i].sig.size ||
|
|
Packit Service |
4684c1 |
memcmp(signature.data, tests[i].sig.data, signature.size) != 0)
|
|
Packit Service |
4684c1 |
testfail("signature does not match");
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
ret = gnutls_pubkey_import_privkey(pubkey, privkey, 0, 0);
|
|
Packit Service |
4684c1 |
if (ret < 0)
|
|
Packit Service |
4684c1 |
testfail("gnutls_pubkey_import_privkey\n");
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
ret =
|
|
Packit Service |
4684c1 |
gnutls_pubkey_verify_data2(pubkey, tests[i].sigalgo, 0, &tests[i].msg,
|
|
Packit Service |
4684c1 |
&signature);
|
|
Packit Service |
4684c1 |
if (ret < 0)
|
|
Packit Service |
4684c1 |
testfail("gnutls_pubkey_verify_data2\n");
|
|
Packit Service |
4684c1 |
success(" - pass");
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
next:
|
|
Packit Service |
4684c1 |
gnutls_free(signature.data);
|
|
Packit Service |
4684c1 |
gnutls_privkey_deinit(privkey);
|
|
Packit Service |
4684c1 |
gnutls_pubkey_deinit(pubkey);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
gnutls_global_deinit();
|
|
Packit Service |
4684c1 |
}
|