2014-11-04 01:57:47 +00:00
|
|
|
/* Copyright (c) 2014, Google Inc.
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
|
|
|
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2014-11-12 19:38:00 +00:00
|
|
|
#include <string.h>
|
2014-11-04 01:57:47 +00:00
|
|
|
|
2015-04-20 21:19:00 +01:00
|
|
|
#include <vector>
|
|
|
|
|
2014-11-04 01:57:47 +00:00
|
|
|
#include <openssl/crypto.h>
|
|
|
|
#include <openssl/ec_key.h>
|
|
|
|
#include <openssl/err.h>
|
2015-03-28 07:12:01 +00:00
|
|
|
#include <openssl/mem.h>
|
2014-11-04 01:57:47 +00:00
|
|
|
|
2015-04-20 21:19:00 +01:00
|
|
|
#include "../test/scoped_types.h"
|
|
|
|
#include "../test/stl_compat.h"
|
2014-11-04 01:57:47 +00:00
|
|
|
#include "internal.h"
|
|
|
|
|
|
|
|
|
|
|
|
static const uint8_t kECKeyWithoutPublic[] = {
|
|
|
|
0x30, 0x31, 0x02, 0x01, 0x01, 0x04, 0x20, 0xc6, 0xc1, 0xaa, 0xda, 0x15, 0xb0,
|
|
|
|
0x76, 0x61, 0xf8, 0x14, 0x2c, 0x6c, 0xaf, 0x0f, 0xdb, 0x24, 0x1a, 0xff, 0x2e,
|
|
|
|
0xfe, 0x46, 0xc0, 0x93, 0x8b, 0x74, 0xf2, 0xbc, 0xc5, 0x30, 0x52, 0xb0, 0x77,
|
|
|
|
0xa0, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
|
|
|
|
};
|
|
|
|
|
2015-04-20 21:19:00 +01:00
|
|
|
bool Testd2i_ECPrivateKey(void) {
|
|
|
|
const uint8_t *inp = kECKeyWithoutPublic;
|
|
|
|
ScopedEC_KEY key(d2i_ECPrivateKey(NULL, &inp, sizeof(kECKeyWithoutPublic)));
|
2014-11-04 01:57:47 +00:00
|
|
|
|
2015-04-20 21:19:00 +01:00
|
|
|
if (!key || inp != kECKeyWithoutPublic + sizeof(kECKeyWithoutPublic)) {
|
2014-11-04 01:57:47 +00:00
|
|
|
fprintf(stderr, "Failed to parse private key.\n");
|
2015-04-10 03:21:10 +01:00
|
|
|
ERR_print_errors_fp(stderr);
|
2015-04-20 21:19:00 +01:00
|
|
|
return false;
|
2014-11-04 01:57:47 +00:00
|
|
|
}
|
|
|
|
|
2015-04-20 21:19:00 +01:00
|
|
|
int len = i2d_ECPrivateKey(key.get(), NULL);
|
|
|
|
std::vector<uint8_t> out(len);
|
|
|
|
uint8_t *outp = bssl::vector_data(&out);
|
|
|
|
if (len != i2d_ECPrivateKey(key.get(), &outp)) {
|
2014-11-04 01:57:47 +00:00
|
|
|
fprintf(stderr, "Failed to serialize private key.\n");
|
2015-04-10 03:21:10 +01:00
|
|
|
ERR_print_errors_fp(stderr);
|
2015-04-20 21:19:00 +01:00
|
|
|
return false;
|
2014-11-04 01:57:47 +00:00
|
|
|
}
|
|
|
|
|
2015-04-20 21:19:00 +01:00
|
|
|
if (0 != memcmp(bssl::vector_data(&out), kECKeyWithoutPublic, len)) {
|
2014-11-04 01:57:47 +00:00
|
|
|
fprintf(stderr, "Serialisation of key doesn't match original.\n");
|
2015-04-20 21:19:00 +01:00
|
|
|
return false;
|
2014-11-04 01:57:47 +00:00
|
|
|
}
|
|
|
|
|
2015-04-20 21:19:00 +01:00
|
|
|
const EC_POINT *pub_key = EC_KEY_get0_public_key(key.get());
|
|
|
|
if (pub_key == NULL) {
|
2014-11-04 01:57:47 +00:00
|
|
|
fprintf(stderr, "Public key missing.\n");
|
2015-04-20 21:19:00 +01:00
|
|
|
return false;
|
2014-11-04 01:57:47 +00:00
|
|
|
}
|
|
|
|
|
2015-04-20 21:19:00 +01:00
|
|
|
ScopedBIGNUM x(BN_new());
|
|
|
|
ScopedBIGNUM y(BN_new());
|
|
|
|
if (!x || !y) {
|
|
|
|
return false;
|
2014-11-04 01:57:47 +00:00
|
|
|
}
|
2015-04-20 21:19:00 +01:00
|
|
|
if (!EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(key.get()),
|
|
|
|
pub_key, x.get(), y.get(), NULL)) {
|
2014-11-04 01:57:47 +00:00
|
|
|
fprintf(stderr, "Failed to get public key in affine coordinates.\n");
|
2015-04-20 21:19:00 +01:00
|
|
|
return false;
|
2014-11-04 01:57:47 +00:00
|
|
|
}
|
2015-04-20 21:19:00 +01:00
|
|
|
ScopedOpenSSLString x_hex(BN_bn2hex(x.get()));
|
|
|
|
ScopedOpenSSLString y_hex(BN_bn2hex(y.get()));
|
|
|
|
if (0 != strcmp(
|
|
|
|
x_hex.get(),
|
|
|
|
"c81561ecf2e54edefe6617db1c7a34a70744ddb261f269b83dacfcd2ade5a681") ||
|
|
|
|
0 != strcmp(
|
|
|
|
y_hex.get(),
|
|
|
|
"e0e2afa3f9b6abe4c698ef6495f1be49a3196c5056acb3763fe4507eec596e88")) {
|
|
|
|
fprintf(stderr, "Incorrect public key: %s %s\n", x_hex.get(), y_hex.get());
|
|
|
|
return false;
|
2014-11-04 01:57:47 +00:00
|
|
|
}
|
|
|
|
|
2015-04-20 21:19:00 +01:00
|
|
|
return true;
|
2014-11-04 01:57:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(void) {
|
|
|
|
CRYPTO_library_init();
|
|
|
|
ERR_load_crypto_strings();
|
|
|
|
|
2015-04-20 21:19:00 +01:00
|
|
|
if (!Testd2i_ECPrivateKey()) {
|
2014-11-04 01:57:47 +00:00
|
|
|
fprintf(stderr, "failed\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("PASS\n");
|
|
|
|
return 0;
|
|
|
|
}
|