Update CPK sub-lib

Use simple NID as arguments, and fix some bugs
This commit is contained in:
Zhi Guan
2018-10-21 19:43:40 +08:00
parent ce1763c9ea
commit 9eadfd4cb6
19 changed files with 5849 additions and 5740 deletions

View File

@@ -1,3 +1,2 @@
LIBS=../../libcrypto
SOURCE[../../libcrypto]=cpk_asn1.c cpk_err.c cpk_kap.c cpk_lib.c cpk_map.c \
cpk_prn.c
SOURCE[../../libcrypto]=cpk_asn1.c cpk_err.c cpk_lib.c cpk_map.c cpk_prn.c

View File

@@ -1,6 +1,6 @@
/*
* Generated by util/mkerr.pl DO NOT EDIT
* Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
* Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the OpenSSL license (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
@@ -19,26 +19,42 @@
# define ERR_REASON(reason) ERR_PACK(ERR_LIB_CPK,0,reason)
static ERR_STRING_DATA CPK_str_functs[] = {
{ERR_FUNC(CPK_F_CPK_MAP_GET_MD), "CPK_MAP_get_md"},
{ERR_FUNC(CPK_F_CPK_MAP_IS_VALID), "CPK_MAP_is_valid"},
{ERR_FUNC(CPK_F_CPK_MAP_NEW), "CPK_MAP_new"},
{ERR_FUNC(CPK_F_CPK_MAP_NEW_DEFAULT), "CPK_MAP_new_default"},
{ERR_FUNC(CPK_F_CPK_MAP_NUM_FACTORS), "CPK_MAP_num_factors"},
{ERR_FUNC(CPK_F_CPK_MAP_NUM_INDEXES), "CPK_MAP_num_indexes"},
{ERR_FUNC(CPK_F_CPK_MAP_NUM_SUBSET), "CPK_MAP_num_subset"},
{ERR_FUNC(CPK_F_CPK_MAP_STR2INDEX), "CPK_MAP_str2index"},
{ERR_FUNC(CPK_F_CPK_MASTER_SECRET_CREATE), "CPK_MASTER_SECRET_create"},
{ERR_FUNC(CPK_F_CPK_MASTER_SECRET_EXTRACT_PRIVATE_KEY),
"CPK_MASTER_SECRET_extract_private_key"},
{ERR_FUNC(CPK_F_CPK_MASTER_SECRET_EXTRACT_PUBLIC_PARAMS),
"CPK_MASTER_SECRET_extract_public_params"},
{ERR_FUNC(CPK_F_CPK_MASTER_SECRET_PRINT), "CPK_MASTER_SECRET_print"},
{ERR_FUNC(CPK_F_CPK_MASTER_SECRET_VALIDATE_PUBLIC_PARAMS),
"CPK_MASTER_SECRET_validate_public_params"},
{ERR_FUNC(CPK_F_CPK_PUBLIC_PARAMS_COMPUTE_SHARE_KEY),
"CPK_PUBLIC_PARAMS_compute_share_key"},
{ERR_FUNC(CPK_F_CPK_PUBLIC_PARAMS_EXTRACT_PUBLIC_KEY),
"CPK_PUBLIC_PARAMS_extract_public_key"},
{ERR_FUNC(CPK_F_CPK_PUBLIC_PARAMS_PRINT), "CPK_PUBLIC_PARAMS_print"},
{ERR_FUNC(CPK_F_CPK_PUBLIC_PARAMS_VALIDATE_PRIVATE_KEY),
"CPK_PUBLIC_PARAMS_validate_private_key"},
{ERR_FUNC(CPK_F_EXTRACT_EC_PARAMS), "extract_ec_params"},
{ERR_FUNC(CPK_F_EXTRACT_EC_PRIV_KEY), "extract_ec_priv_key"},
{ERR_FUNC(CPK_F_EXTRACT_EC_PUB_KEY), "extract_ec_pub_key"},
{ERR_FUNC(CPK_F_X509_ALGOR_GET1_EC_KEY), "X509_ALGOR_get1_EC_KEY"},
{0, NULL}
};
static ERR_STRING_DATA CPK_str_reasons[] = {
{ERR_REASON(CPK_R_BAD_ARGUMENT), "bad argument"},
{ERR_REASON(CPK_R_BAD_ARGUMENTS), "bad arguments"},
{ERR_REASON(CPK_R_BAD_DATA), "bad data"},
{ERR_REASON(CPK_R_INVALID_ALGORITHM), "invalid algorithm"},
{ERR_REASON(CPK_R_INVALID_ARGUMENT), "invalid argument"},
{ERR_REASON(CPK_R_INVALID_ID_LENGTH), "invalid id length"},
{ERR_REASON(CPK_R_INVALID_MAP_ALGOR), "invalid map algor"},
{ERR_REASON(CPK_R_INVALID_PKEY_TYPE), "invalid pkey type"},

View File

@@ -1,96 +0,0 @@
/* ====================================================================
* Copyright (c) 2007 - 2016 The GmSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the GmSSL Project.
* (http://gmssl.org/)"
*
* 4. The name "GmSSL Project" must not be used to endorse or promote
* products derived from this software without prior written
* permission. For written permission, please contact
* guanzhi1980@gmail.com.
*
* 5. Products derived from this software may not be called "GmSSL"
* nor may "GmSSL" appear in their names without prior written
* permission of the GmSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the GmSSL Project
* (http://gmssl.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
* ====================================================================
*/
#include <openssl/evp.h>
#include <openssl/ecdh.h>
#include <openssl/objects.h>
#include <openssl/cpk.h>
#include "cpk_lcl.h"
int CPK_PUBLIC_PARAMS_compute_share_key(CPK_PUBLIC_PARAMS *params,
void *out, size_t outlen, const char *id, EVP_PKEY *priv_key,
void *(*kdf)(const void *in, size_t inlen, void *out, size_t *outlen))
{
int ret = 0;
EVP_PKEY *pub_key = NULL;
int pkey_type = OBJ_obj2nid(params->pkey_algor->algorithm);
OPENSSL_assert(kdf != NULL);
printf("%d\n", __LINE__);
if (EVP_PKEY_id(priv_key) != pkey_type) {
CPKerr(CPK_F_CPK_PUBLIC_PARAMS_COMPUTE_SHARE_KEY,
ERR_R_MALLOC_FAILURE); //FIXME: ERR_R_XXX
goto err;
}
if (!(pub_key = CPK_PUBLIC_PARAMS_extract_public_key(params, id))) {
CPKerr(CPK_F_CPK_PUBLIC_PARAMS_COMPUTE_SHARE_KEY,
ERR_R_MALLOC_FAILURE); //FIXME: ERR_R_XXX
goto err;
}
if (pkey_type == EVP_PKEY_EC) {
if (!ECDH_compute_key(out, outlen,
EC_KEY_get0_public_key((EC_KEY *)EVP_PKEY_get0(pub_key)),
(EC_KEY *)EVP_PKEY_get0(priv_key), kdf)) {
CPKerr(CPK_F_CPK_PUBLIC_PARAMS_COMPUTE_SHARE_KEY,
ERR_R_MALLOC_FAILURE); //FIXME: ERR_R_XXX
goto err;
}
} else if (pkey_type == EVP_PKEY_DH) {
// not supported yet
goto err;
}
ret = 1;
err:
return ret;
}

View File

@@ -68,3 +68,11 @@ struct cpk_public_params_st {
X509_ALGOR *map_algor;
ASN1_OCTET_STRING *public_factors;
};
X509_ALGOR *CPK_MAP_new(int type);
X509_ALGOR * CPK_MAP_new_default(void);
int CPK_MAP_is_valid(const X509_ALGOR *algor);
int CPK_MAP_num_factors(const X509_ALGOR *algor);
int CPK_MAP_num_indexes(const X509_ALGOR *algor);
int CPK_MAP_str2index(const X509_ALGOR *algor, const char *str, int *index);

View File

@@ -62,30 +62,27 @@
#include "../x509/x509_lcl.h"
#include "cpk_lcl.h"
#define ASN1_STRING_data(a) ((a)->data)
static EC_KEY *X509_ALGOR_get1_EC_KEY(X509_ALGOR *algor);
static int extract_ec_params(CPK_MASTER_SECRET *master, CPK_PUBLIC_PARAMS *param);
static EC_KEY *extract_ec_priv_key(CPK_MASTER_SECRET *master, const char *id);
static EC_KEY *extract_ec_pub_key(CPK_PUBLIC_PARAMS *param, const char *id);
CPK_MASTER_SECRET *CPK_MASTER_SECRET_create(const char *domain_id,
EVP_PKEY *pkey, X509_ALGOR *map_algor)
CPK_MASTER_SECRET *CPK_MASTER_SECRET_create(const char *domain_id, int pkey_nid, int map_nid)
{
int e = 1;
CPK_MASTER_SECRET *master = NULL;
BIGNUM *bn = NULL;
BIGNUM *order = NULL;
X509_PUBKEY *pubkey = NULL;
const X509_ALGOR *pkey_algor;
X509_ALGOR *pkey_algor;
int pkey_type;
int i, bn_size, num_factors;
unsigned char *bn_ptr;
EVP_PKEY *pkey = NULL;
X509_ALGOR *map_algor = NULL;
if (!domain_id || !pkey || !map_algor) {
if (!domain_id) {
CPKerr(CPK_F_CPK_MASTER_SECRET_CREATE, ERR_R_PASSED_NULL_PARAMETER);
return NULL;
}
@@ -94,6 +91,21 @@ CPK_MASTER_SECRET *CPK_MASTER_SECRET_create(const char *domain_id,
return NULL;
}
/* pkey type and domain parameters is required
* EC:curve
* SM9:curve ...
*
* so we do not check pkey_nid
*/
//FIXME: merge into ec routine
EC_KEY *ec = EC_KEY_new_by_curve_name(NID_sm2p256v1);
EC_KEY_generate_key(ec);
pkey = EVP_PKEY_new();
EVP_PKEY_set1_EC_KEY(pkey, ec);
//FIXME: free ec
map_algor = CPK_MAP_new(map_nid);
pkey_type = EVP_PKEY_id(pkey);
if (pkey_type == EVP_PKEY_EC) {
const EC_GROUP *ec_group;
@@ -190,7 +202,7 @@ err:
CPK_MASTER_SECRET_free(master);
master = NULL;
}
if (pubkey) X509_PUBKEY_free(pubkey);
X509_PUBKEY_free(pubkey);
if (order && pkey_type == EVP_PKEY_EC) BN_free(order);
if (bn) BN_free(bn);
return master;
@@ -302,7 +314,6 @@ EVP_PKEY *CPK_PUBLIC_PARAMS_extract_public_key(CPK_PUBLIC_PARAMS *param,
{
EVP_PKEY *pkey = NULL;
int pkey_type;
//char domain_id[CPK_MAX_ID_LENGTH + 1];
if (!(pkey = EVP_PKEY_new())) {
CPKerr(CPK_F_CPK_PUBLIC_PARAMS_EXTRACT_PUBLIC_KEY,
@@ -340,28 +351,6 @@ err:
return NULL;
}
int CPK_MASTER_SECRET_digest(CPK_MASTER_SECRET *master, const EVP_MD *md,
unsigned char *dgst, unsigned int *dgstlen)
{
if (!EVP_Digest(ASN1_STRING_data(master->secret_factors),
ASN1_STRING_length(master->secret_factors),
dgst, dgstlen, md, NULL)) {
return 0;
}
return 1;
}
int CPK_PUBLIC_PARAMS_digest(CPK_PUBLIC_PARAMS *params, const EVP_MD *md,
unsigned char *dgst, unsigned int *dgstlen)
{
if (!EVP_Digest(ASN1_STRING_data(params->public_factors),
ASN1_STRING_length(params->public_factors),
dgst, dgstlen, md, NULL)) {
return 0;
}
return 1;
}
char *CPK_MASTER_SECRET_get_name(CPK_MASTER_SECRET *master, char *buf, int size)
{
return X509_NAME_oneline(master->id, buf, size);
@@ -379,15 +368,15 @@ int CPK_MASTER_SECRET_validate_public_params(CPK_MASTER_SECRET *master,
CPK_PUBLIC_PARAMS *tmp = NULL;
if (!(tmp = CPK_MASTER_SECRET_extract_public_params(master))) {
fprintf(stderr, "shit1\n");
CPKerr(CPK_F_CPK_MASTER_SECRET_VALIDATE_PUBLIC_PARAMS, ERR_R_CPK_LIB);
goto err;
}
if (tmp->version != params->version) {
fprintf(stderr, "shit2\n");
CPKerr(CPK_F_CPK_MASTER_SECRET_VALIDATE_PUBLIC_PARAMS, ERR_R_CPK_LIB);
goto err;
}
if (X509_NAME_cmp(tmp->id, params->id)) {
fprintf(stderr, "shit3\n");
CPKerr(CPK_F_CPK_MASTER_SECRET_VALIDATE_PUBLIC_PARAMS, ERR_R_CPK_LIB);
goto err;
}
@@ -398,23 +387,23 @@ int CPK_MASTER_SECRET_validate_public_params(CPK_MASTER_SECRET *master,
*/
if (OBJ_obj2nid(tmp->pkey_algor->algorithm) !=
OBJ_obj2nid(params->pkey_algor->algorithm)) {
fprintf(stderr, "shit4\n");
CPKerr(CPK_F_CPK_MASTER_SECRET_VALIDATE_PUBLIC_PARAMS, ERR_R_CPK_LIB);
goto err;
}
// FIXME: pkey_algor->parameters
if (OBJ_obj2nid(tmp->map_algor->algorithm) !=
OBJ_obj2nid(params->map_algor->algorithm)) {
fprintf(stderr, "shit5\n");
CPKerr(CPK_F_CPK_MASTER_SECRET_VALIDATE_PUBLIC_PARAMS, ERR_R_CPK_LIB);
goto err;
}
if (ASN1_STRING_cmp(tmp->public_factors, params->public_factors)) {
fprintf(stderr, "shit6\n");
CPKerr(CPK_F_CPK_MASTER_SECRET_VALIDATE_PUBLIC_PARAMS, ERR_R_CPK_LIB);
goto err;
}
ret = 1;
err:
if (tmp) CPK_PUBLIC_PARAMS_free(tmp);
CPK_PUBLIC_PARAMS_free(tmp);
return ret;
}
@@ -495,49 +484,60 @@ static int extract_ec_params(CPK_MASTER_SECRET *master, CPK_PUBLIC_PARAMS *param
unsigned char *pt_ptr;
if (!bn || !order || !ctx) {
CPKerr(CPK_F_EXTRACT_EC_PARAMS, ERR_R_CPK_LIB);
goto err;
}
if (!(ec_key = X509_ALGOR_get1_EC_KEY(master->pkey_algor))) {
CPKerr(CPK_F_EXTRACT_EC_PARAMS, ERR_R_CPK_LIB);
goto err;
}
ec_group = EC_KEY_get0_group(ec_key);
if (!(EC_GROUP_get_order(ec_group, order, ctx))) {
CPKerr(CPK_F_EXTRACT_EC_PARAMS, ERR_R_CPK_LIB);
goto err;
}
bn_size = BN_num_bytes(order);
pt_size = bn_size + 1;
if ((num_factors = CPK_MAP_num_factors(master->map_algor)) <= 0) {
CPKerr(CPK_F_EXTRACT_EC_PARAMS, ERR_R_CPK_LIB);
goto err;
}
if (ASN1_STRING_length(master->secret_factors) != bn_size * num_factors) {
CPKerr(CPK_F_EXTRACT_EC_PARAMS, ERR_R_CPK_LIB);
goto err;
}
if (!ASN1_STRING_set(param->public_factors, NULL, pt_size * num_factors)) {
CPKerr(CPK_F_EXTRACT_EC_PARAMS, ERR_R_CPK_LIB);
goto err;
}
bn_ptr = ASN1_STRING_data(master->secret_factors);
pt_ptr = ASN1_STRING_data(param->public_factors);
bn_ptr = ASN1_STRING_get0_data(master->secret_factors);
pt_ptr = ASN1_STRING_get0_data(param->public_factors);
memset(pt_ptr, 0, ASN1_STRING_length(param->public_factors));
if (!(pt = EC_POINT_new(ec_group))) {
CPKerr(CPK_F_EXTRACT_EC_PARAMS, ERR_R_CPK_LIB);
goto err;
}
for (i = 0; i < num_factors; i++) {
if (!BN_bin2bn(bn_ptr, bn_size, bn)) {
CPKerr(CPK_F_EXTRACT_EC_PARAMS, ERR_R_CPK_LIB);
goto err;
}
if (BN_is_zero(bn) || BN_cmp(bn, order) >= 0) {
CPKerr(CPK_F_EXTRACT_EC_PARAMS, ERR_R_CPK_LIB);
goto err;
}
if (!EC_POINT_mul(ec_group, pt, bn, NULL, NULL, ctx)) {
CPKerr(CPK_F_EXTRACT_EC_PARAMS, ERR_R_CPK_LIB);
goto err;
}
if (!EC_POINT_point2oct(ec_group, pt,
POINT_CONVERSION_COMPRESSED, pt_ptr, pt_size, ctx)) {
CPKerr(CPK_F_EXTRACT_EC_PARAMS, ERR_R_CPK_LIB);
goto err;
}
bn_ptr += bn_size;
@@ -569,56 +569,69 @@ static EC_KEY *extract_ec_priv_key(CPK_MASTER_SECRET *master, const char *id)
if (!priv_key || !bn || !order || !ctx) {
CPKerr(CPK_F_EXTRACT_EC_PRIV_KEY, ERR_R_CPK_LIB);
goto err;
}
if (!(ec_key = X509_ALGOR_get1_EC_KEY(master->pkey_algor))) {
CPKerr(CPK_F_EXTRACT_EC_PRIV_KEY, ERR_R_CPK_LIB);
goto err;
}
ec_group = EC_KEY_get0_group(ec_key);
if (!(pub_key = EC_POINT_new(ec_group))) {
CPKerr(CPK_F_EXTRACT_EC_PRIV_KEY, ERR_R_CPK_LIB);
goto err;
}
if ((num_indexes = CPK_MAP_num_indexes(master->map_algor)) <= 0) {
CPKerr(CPK_F_EXTRACT_EC_PRIV_KEY, ERR_R_CPK_LIB);
goto err;
}
if (!(index = OPENSSL_malloc(sizeof(int) * num_indexes))) {
CPKerr(CPK_F_EXTRACT_EC_PRIV_KEY, ERR_R_CPK_LIB);
goto err;
}
if (!CPK_MAP_str2index(master->map_algor, id, index)) {
CPKerr(CPK_F_EXTRACT_EC_PRIV_KEY, ERR_R_CPK_LIB);
goto err;
}
BN_zero(priv_key);
if (!(EC_GROUP_get_order(EC_KEY_get0_group(ec_key), order, ctx))) {
CPKerr(CPK_F_EXTRACT_EC_PRIV_KEY, ERR_R_CPK_LIB);
goto err;
}
bn_size = BN_num_bytes(order);
for (i = 0; i < num_indexes; i++) {
const unsigned char *p =
ASN1_STRING_data(master->secret_factors) +
const unsigned char *p =
ASN1_STRING_get0_data(master->secret_factors) +
bn_size * index[i];
if (!BN_bin2bn(p, bn_size, bn)) {
CPKerr(CPK_F_EXTRACT_EC_PRIV_KEY, ERR_R_CPK_LIB);
goto err;
}
if (BN_is_zero(bn) || BN_cmp(bn, order) >= 0) {
CPKerr(CPK_F_EXTRACT_EC_PRIV_KEY, ERR_R_CPK_LIB);
goto err;
}
}
if (!BN_mod_add(priv_key, priv_key, bn, order, ctx)) {
CPKerr(CPK_F_EXTRACT_EC_PRIV_KEY, ERR_R_CPK_LIB);
goto err;
}
}
if (!EC_KEY_set_private_key(ec_key, priv_key)) {
CPKerr(CPK_F_EXTRACT_EC_PRIV_KEY, ERR_R_CPK_LIB);
goto err;
}
if (!EC_POINT_mul(ec_group, pub_key, priv_key, NULL, NULL, ctx)) {
CPKerr(CPK_F_EXTRACT_EC_PRIV_KEY, ERR_R_CPK_LIB);
goto err;
}
if (!EC_KEY_set_public_key(ec_key, pub_key)) {
CPKerr(CPK_F_EXTRACT_EC_PRIV_KEY, ERR_R_CPK_LIB);
goto err;
}
e = 0;
@@ -651,55 +664,70 @@ static EC_KEY *extract_ec_pub_key(CPK_PUBLIC_PARAMS *param, const char *id)
int i, bn_size, pt_size, num_indexes, num_factors;
if (!(ec_key = X509_ALGOR_get1_EC_KEY(param->pkey_algor))) {
CPKerr(CPK_F_EXTRACT_EC_PUB_KEY, ERR_R_CPK_LIB);
goto err;
}
ec_group = EC_KEY_get0_group(ec_key);
if (!(pub_key = EC_POINT_new(ec_group))) {
CPKerr(CPK_F_EXTRACT_EC_PUB_KEY, ERR_R_CPK_LIB);
goto err;
}
if (!(pt = EC_POINT_new(ec_group))) {
CPKerr(CPK_F_EXTRACT_EC_PUB_KEY, ERR_R_CPK_LIB);
goto err;
}
if (!EC_GROUP_get_order(ec_group, order, ctx)) {
CPKerr(CPK_F_EXTRACT_EC_PUB_KEY, ERR_R_CPK_LIB);
goto err;
}
bn_size = BN_num_bytes(order);
pt_size = bn_size + 1;
if ((num_factors = CPK_MAP_num_factors(param->map_algor)) <= 0) {
CPKerr(CPK_F_EXTRACT_EC_PUB_KEY, ERR_R_CPK_LIB);
goto err;
}
if (ASN1_STRING_length(param->public_factors) != pt_size * num_factors) {
CPKerr(CPK_F_EXTRACT_EC_PUB_KEY, ERR_R_CPK_LIB);
goto err;
}
if ((num_indexes = CPK_MAP_num_indexes(param->map_algor)) <= 0) {
CPKerr(CPK_F_EXTRACT_EC_PUB_KEY, ERR_R_CPK_LIB);
goto err;
}
if (!(index = OPENSSL_malloc(sizeof(int) * num_indexes))) {
CPKerr(CPK_F_EXTRACT_EC_PUB_KEY, ERR_R_CPK_LIB);
goto err;
}
if (!CPK_MAP_str2index(param->map_algor, id, index)) {
CPKerr(CPK_F_EXTRACT_EC_PUB_KEY, ERR_R_CPK_LIB);
goto err;
}
if (!EC_POINT_set_to_infinity(ec_group, pub_key)) {
CPKerr(CPK_F_EXTRACT_EC_PUB_KEY, ERR_R_CPK_LIB);
goto err;
}
for (i = 0; i < num_indexes; i++) {
const unsigned char *p =
ASN1_STRING_data(param->public_factors) +
ASN1_STRING_get0_data(param->public_factors) +
pt_size * index[i];
if (!EC_POINT_oct2point(ec_group, pt, p, pt_size, ctx)) {
CPKerr(CPK_F_EXTRACT_EC_PUB_KEY, ERR_R_CPK_LIB);
goto err;
}
if (!EC_POINT_add(ec_group, pub_key, pub_key, pt, ctx)) {
CPKerr(CPK_F_EXTRACT_EC_PUB_KEY, ERR_R_CPK_LIB);
goto err;
}
}
if (!EC_KEY_set_public_key(ec_key, pub_key)) {
CPKerr(CPK_F_EXTRACT_EC_PUB_KEY, ERR_R_CPK_LIB);
goto err;
}
e = 0;

View File

@@ -45,7 +45,6 @@
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
* ====================================================================
*
*/
#include <string.h>
@@ -53,77 +52,119 @@
#include <openssl/objects.h>
#include <openssl/cpk.h>
#include "cpk_lcl.h"
#include "../../e_os.h"
X509_ALGOR *CPK_MAP_new_default()
/* Currently we only support fixed 32 indexes
* this means EC_POINT_add() called 32 times */
#define CPK_NUM_INDEXES 32
typedef struct {
int map_nid;
int md_nid;
} CPK_MAP_TABLE;
static CPK_MAP_TABLE map_table[] = {
{NID_cpk_map_sm3, NID_sm3},
{NID_cpk_map_sha1, NID_sha1},
{NID_cpk_map_sha256, NID_sha256},
{NID_cpk_map_sha384, NID_sha384},
{NID_cpk_map_sha512, NID_sha512}
};
static const EVP_MD *cpk_map2md(int type)
{
X509_ALGOR *algor = NULL;
const EVP_MD *md = EVP_sha1();
int i;
for (i = 0; i < OSSL_NELEM(map_table); i++) {
if (map_table[i].map_nid == type) {
return EVP_get_digestbynid(map_table[i].md_nid);
}
}
return NULL;
}
if (md != EVP_sha1() && md != EVP_sha384()) {
CPKerr(CPK_F_CPK_MAP_NEW_DEFAULT, CPK_R_BAD_ARGUMENT);
goto end;
static const EVP_MD *CPK_MAP_get_md(const X509_ALGOR *algor)
{
const EVP_MD *md;
if (!algor->algorithm) {
CPKerr(CPK_F_CPK_MAP_GET_MD, CPK_R_INVALID_ARGUMENT);
return NULL;
}
if (!(md = cpk_map2md(OBJ_obj2nid(algor->algorithm)))) {
CPKerr(CPK_F_CPK_MAP_GET_MD, ERR_R_CPK_LIB);
return NULL;
}
return md;
}
X509_ALGOR *CPK_MAP_new(int type)
{
X509_ALGOR *ret = NULL;
X509_ALGOR *algor = NULL;
if (!cpk_map2md(type)) {
CPKerr(CPK_F_CPK_MAP_NEW, CPK_R_INVALID_MAP_ALGOR);
return NULL;
}
if (!(algor = X509_ALGOR_new())) {
CPKerr(CPK_F_CPK_MAP_NEW_DEFAULT, ERR_R_X509_LIB);
CPKerr(CPK_F_CPK_MAP_NEW, ERR_R_X509_LIB);
goto end;
}
if (!X509_ALGOR_set0(algor, OBJ_nid2obj(EVP_MD_nid(md)),
V_ASN1_UNDEF, NULL)) {
X509_ALGOR_free(algor);
algor = NULL;
CPKerr(CPK_F_CPK_MAP_NEW_DEFAULT, ERR_R_X509_LIB);
if (!X509_ALGOR_set0(algor, OBJ_nid2obj(type), V_ASN1_UNDEF, NULL)) {
CPKerr(CPK_F_CPK_MAP_NEW, ERR_R_X509_LIB);
goto end;
}
ret = algor;
algor = NULL;
end:
return algor;
X509_ALGOR_free(algor);
return ret;
}
X509_ALGOR *CPK_MAP_new_default(void)
{
return CPK_MAP_new(NID_cpk_map_sha1);
}
int CPK_MAP_is_valid(const X509_ALGOR *algor)
{
OPENSSL_assert(algor);
OPENSSL_assert(algor->algorithm);
switch (OBJ_obj2nid(algor->algorithm)) {
case NID_sha1:
case NID_sha384:
return 1;
}
return 0;
}
int CPK_MAP_num_subset(const X509_ALGOR *algor)
{
OPENSSL_assert(algor);
OPENSSL_assert(algor->algorithm);
switch (OBJ_obj2nid(algor->algorithm)) {
case NID_sha1:
return 32;
case NID_sha384:
return 4096;
}
return -1;
}
int CPK_MAP_num_factors(const X509_ALGOR *algor)
{
return 1024;
return CPK_MAP_get_md(algor) != NULL;
}
int CPK_MAP_num_indexes(const X509_ALGOR *algor)
{
return 32;
if (!CPK_MAP_is_valid(algor)) {
CPKerr(CPK_F_CPK_MAP_NUM_INDEXES, CPK_R_INVALID_MAP_ALGOR);
return 0;
}
/* current only use fixed num_indexes */
return CPK_NUM_INDEXES;
}
int CPK_MAP_num_index(const X509_ALGOR *algor)
int CPK_MAP_num_subset(const X509_ALGOR *algor)
{
OPENSSL_assert(algor);
OPENSSL_assert(algor->algorithm);
switch (OBJ_obj2nid(algor->algorithm)) {
case NID_sha1:
return 32;
case NID_sha384:
return 32;
const EVP_MD *md;
if (!(md = CPK_MAP_get_md(algor))) {
CPKerr(CPK_F_CPK_MAP_NUM_SUBSET, ERR_R_CPK_LIB);
return 0;
}
return -1;
return 1 << ((EVP_MD_size(md) * 8) / CPK_MAP_num_indexes(algor));
}
int CPK_MAP_num_factors(const X509_ALGOR *algor)
{
int num_indexes;
int num_subset;
if (!(num_indexes = CPK_MAP_num_indexes(algor))) {
CPKerr(CPK_F_CPK_MAP_NUM_FACTORS, ERR_R_CPK_LIB);
return 0;
}
if (!(num_subset = CPK_MAP_num_subset(algor))) {
CPKerr(CPK_F_CPK_MAP_NUM_FACTORS, ERR_R_CPK_LIB);
return 0;
}
return num_indexes * num_subset;
}
int CPK_MAP_str2index(const X509_ALGOR *algor, const char *str, int *index)
@@ -133,46 +174,46 @@ int CPK_MAP_str2index(const X509_ALGOR *algor, const char *str, int *index)
unsigned char dgst[EVP_MAX_MD_SIZE];
unsigned int dgstlen;
BIGNUM *bn = NULL;
int i, num_index, num_subset;
int i;
int num_indexes, num_subset;
OPENSSL_assert(algor);
OPENSSL_assert(algor->algorithm);
OPENSSL_assert(str);
OPENSSL_assert(strlen(str) > 0);
if (!CPK_MAP_is_valid(algor)) {
if (!(md = CPK_MAP_get_md(algor))
|| !(num_indexes = CPK_MAP_num_indexes(algor))
|| !(num_subset = CPK_MAP_num_subset(algor))) {
CPKerr(CPK_F_CPK_MAP_STR2INDEX, CPK_R_INVALID_MAP_ALGOR);
goto err;
return 0;
}
if (!index) {
ret = CPK_MAP_num_index(algor);
goto err;
}
if (!(md = EVP_get_digestbyobj(algor->algorithm))) {
CPKerr(CPK_F_CPK_MAP_STR2INDEX, ERR_R_EVP_LIB);
goto err;
return CPK_MAP_num_indexes(algor);
}
if (!EVP_Digest(str, strlen(str), dgst, &dgstlen, md, NULL)) {
CPKerr(CPK_F_CPK_MAP_STR2INDEX, ERR_R_EVP_LIB);
return 0;
}
if (!(bn = BN_new())) {
CPKerr(CPK_F_CPK_MAP_STR2INDEX, ERR_R_BN_LIB);
goto err;
CPKerr(CPK_F_CPK_MAP_STR2INDEX, ERR_R_MALLOC_FAILURE);
goto end;
}
if (!BN_bin2bn(dgst, dgstlen, bn)) {
CPKerr(CPK_F_CPK_MAP_STR2INDEX, ERR_R_BN_LIB);
goto err;
goto end;
}
num_index = CPK_MAP_num_index(algor);
num_subset = CPK_MAP_num_subset(algor);
for (i = 0; i < num_index; i++) {
for (i = 0; i < num_indexes; i++) {
int r = BN_mod_word(bn, num_subset);
BN_div_word(bn, num_subset);
index[i] = num_subset * i + r;
}
ret = num_index;
err:
if (bn) BN_free(bn);
ret = num_indexes;
end:
BN_free(bn);
return ret;
}

View File

@@ -50,13 +50,50 @@
#include <string.h>
#include <openssl/bio.h>
#include <openssl/cpk.h>
#include <openssl/objects.h>
#include "cpk_lcl.h"
int CPK_MASTER_SECRET_print(BIO *out, CPK_MASTER_SECRET *master,
int indent, unsigned long flags)
{
char name[1024] = {0};
int num_factors;
const unsigned char *p;
int i, len;
BIO_printf(out, "%s() not implemented\n", __FUNCTION__);
if (!X509_NAME_oneline(master->id, name, sizeof(name))) {
CPKerr(CPK_F_CPK_MASTER_SECRET_PRINT, ERR_R_CPK_LIB);
return 0;
}
BIO_printf(out, "CPK_MASTER_SECRET\n");
BIO_printf(out, " Version : %ld\n", master->version);
BIO_printf(out, " Domain-ID : %s\n", name);
BIO_printf(out, " Public-Key-Algor : %s\n", OBJ_nid2sn(OBJ_obj2nid(master->pkey_algor->algorithm)));
BIO_printf(out, " Map-Algor : %s\n", OBJ_nid2sn(OBJ_obj2nid(master->map_algor->algorithm)));
BIO_printf(out, " Secret-Factors :\n");
if ((num_factors = CPK_MAP_num_factors(master->map_algor)) <= 0) {
fprintf(stderr, "%s %d\n", __FILE__, __LINE__);
return 0;
}
p = ASN1_STRING_get0_data(master->secret_factors);
len = ASN1_STRING_length(master->secret_factors)/num_factors;
if (ASN1_STRING_length(master->secret_factors) % num_factors) {
CPKerr(CPK_F_CPK_MASTER_SECRET_PRINT, ERR_R_CPK_LIB);
return 0;
}
for (i = 0; i < num_factors; i++) {
int j;
printf(" %-8d ", i);
for (j = 0; j < len; j++) {
BIO_printf(out, "%02X", p[j]);
}
printf("\n");
p += len;
}
return 1;
}
@@ -64,7 +101,44 @@ int CPK_MASTER_SECRET_print(BIO *out, CPK_MASTER_SECRET *master,
int CPK_PUBLIC_PARAMS_print(BIO *out, CPK_PUBLIC_PARAMS *params,
int indent, unsigned long flags)
{
BIO_printf(out, "%s() not implemented\n", __FUNCTION__);
char name[1024] = {0};
int num_factors;
const unsigned char *p;
int len, i;
if (!X509_NAME_oneline(params->id, name, sizeof(name))) {
CPKerr(CPK_F_CPK_PUBLIC_PARAMS_PRINT, ERR_R_CPK_LIB);
return 0;
}
BIO_printf(out, "CPK_PUBLIC_PARAMS\n");
BIO_printf(out, " Version : %ld\n", params->version);
BIO_printf(out, " Domain-ID : %s\n", name);
BIO_printf(out, " Public-Key-Algor : %s\n", OBJ_nid2sn(OBJ_obj2nid(params->pkey_algor->algorithm)));
BIO_printf(out, " Map-Algor : %s\n", OBJ_nid2sn(OBJ_obj2nid(params->map_algor->algorithm)));
BIO_printf(out, " Secret-Factors :\n");
if ((num_factors = CPK_MAP_num_factors(params->map_algor)) <= 0) {
fprintf(stderr, "%s %d\n", __FILE__, __LINE__);
return 0;
}
p = ASN1_STRING_get0_data(params->public_factors);
len = ASN1_STRING_length(params->public_factors)/num_factors;
if (ASN1_STRING_length(params->public_factors) % num_factors) {
CPKerr(CPK_F_CPK_PUBLIC_PARAMS_PRINT, ERR_R_CPK_LIB);
return 0;
}
for (i = 0; i < num_factors; i++) {
int j;
printf(" %-8d ", i);
for (j = 0; j < len; j++) {
BIO_printf(out, "%02X", p[j]);
}
printf("\n");
p += len;
}
return 1;
}