mirror of
https://github.com/guanzhi/GmSSL.git
synced 2026-08-11 00:13:55 +08:00
Add sm9 and paillier pem support
This commit is contained in:
@@ -48,11 +48,16 @@
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <openssl/err.h>
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/x509.h>
|
||||
#include <openssl/asn1t.h>
|
||||
#include <openssl/objects.h>
|
||||
#include <openssl/paillier.h>
|
||||
#include "internal/cryptlib.h"
|
||||
#include "internal/asn1_int.h"
|
||||
#include "internal/evp_int.h"
|
||||
#include "pai_lcl.h"
|
||||
|
||||
|
||||
static int paillier_pub_encode(X509_PUBKEY *pubkey, const EVP_PKEY *pkey)
|
||||
@@ -60,7 +65,7 @@ static int paillier_pub_encode(X509_PUBKEY *pubkey, const EVP_PKEY *pkey)
|
||||
unsigned char *penc = NULL;
|
||||
int penclen;
|
||||
|
||||
if ((penclen = i2d_PAILLIER_PUBLIC_KEY(pkey->pkey.paillier, &penc)) < = 0) {
|
||||
if ((penclen = i2d_PaillierPublicKey(pkey->pkey.paillier, &penc)) <= 0) {
|
||||
return 0;
|
||||
}
|
||||
if (X509_PUBKEY_set0_param(pubkey, OBJ_nid2obj(EVP_PKEY_PAILLIER),
|
||||
@@ -81,8 +86,7 @@ static int paillier_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey)
|
||||
if (!X509_PUBKEY_get0_param(NULL, &cp, &len, NULL, pubkey)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!(paillier = d2i_PAILLIER_PUBLIC_KEY(NULL, &cp, len))) {
|
||||
if (!(paillier = d2i_PaillierPublicKey(NULL, &cp, len))) {
|
||||
PAILLIERerr(PAILLIER_F_PAILLIER_PUB_DECODE, ERR_R_PAILLIER_LIB);
|
||||
return 0;
|
||||
}
|
||||
@@ -93,7 +97,131 @@ static int paillier_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey)
|
||||
|
||||
static int paillier_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
|
||||
{
|
||||
return -1;
|
||||
if (BN_cmp(a->pkey.paillier->n, b->pkey.paillier->n) != 0) {
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int do_paillier_print(BIO *bp, const PAILLIER *x, int off, int priv)
|
||||
{
|
||||
char *str;
|
||||
int ret = 0;
|
||||
if (!BIO_indent(bp, off, 128))
|
||||
goto end;
|
||||
|
||||
if (priv && x->lambda) {
|
||||
if (BIO_printf(bp, "Private-Key: (%d bit)\n", x->bits) <= 0)
|
||||
goto end;
|
||||
str = "modulus";
|
||||
} else {
|
||||
if (BIO_printf(bp, "Public-Key: (%d bit)\n", x->bits) <= 0)
|
||||
goto end;
|
||||
str = "Modulus";
|
||||
}
|
||||
|
||||
if (!ASN1_bn_print(bp, str, x->n, NULL, off))
|
||||
goto end;
|
||||
if (priv) {
|
||||
if (!ASN1_bn_print(bp, "lambda:", x->lambda, NULL, off))
|
||||
goto end;
|
||||
if (!ASN1_bn_print(bp, "x:", x->x, NULL, off))
|
||||
goto end;
|
||||
}
|
||||
ret = 1;
|
||||
|
||||
end:
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int paillier_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
|
||||
ASN1_PCTX *ctx)
|
||||
{
|
||||
return do_paillier_print(bp, pkey->pkey.paillier, indent, 0);
|
||||
}
|
||||
|
||||
static int paillier_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
|
||||
ASN1_PCTX *ctx)
|
||||
{
|
||||
return do_paillier_print(bp, pkey->pkey.paillier, indent, 1);
|
||||
}
|
||||
|
||||
static int paillier_priv_decode(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8)
|
||||
{
|
||||
const unsigned char *p;
|
||||
int pklen;
|
||||
PAILLIER *paillier;
|
||||
|
||||
if (!PKCS8_pkey_get0(NULL, &p, &pklen, NULL, p8))
|
||||
return 0;
|
||||
if (!(paillier = d2i_PaillierPrivateKey(NULL, &p, pklen))) {
|
||||
PAILLIERerr(PAILLIER_F_PAILLIER_PRIV_DECODE, ERR_R_PAILLIER_LIB);
|
||||
return 0;
|
||||
}
|
||||
EVP_PKEY_assign_PAILLIER(pkey, paillier);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int paillier_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
|
||||
{
|
||||
unsigned char *rk = NULL;
|
||||
int rklen;
|
||||
|
||||
if ((rklen = i2d_PaillierPrivateKey(pkey->pkey.paillier, &rk)) <= 0) {
|
||||
PAILLIERerr(PAILLIER_F_PAILLIER_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(NID_paillier), 0, V_ASN1_NULL, NULL,
|
||||
rk, rklen)) {
|
||||
PAILLIERerr(PAILLIER_F_PAILLIER_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int int_paillier_size(const EVP_PKEY *pkey)
|
||||
{
|
||||
return PAILLIER_size(pkey->pkey.paillier);
|
||||
}
|
||||
|
||||
static int paillier_bits(const EVP_PKEY *pkey)
|
||||
{
|
||||
return pkey->pkey.paillier->bits;
|
||||
}
|
||||
|
||||
static int paillier_security_bits(const EVP_PKEY *pkey)
|
||||
{
|
||||
return PAILLIER_security_bits(pkey->pkey.paillier);
|
||||
}
|
||||
|
||||
static void int_paillier_free(EVP_PKEY *pkey)
|
||||
{
|
||||
PAILLIER_free(pkey->pkey.paillier);
|
||||
}
|
||||
|
||||
static int paillier_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
|
||||
{
|
||||
return -2;
|
||||
}
|
||||
|
||||
static int old_paillier_priv_decode(EVP_PKEY *pkey,
|
||||
const unsigned char **pder, int derlen)
|
||||
{
|
||||
PAILLIER *pai;
|
||||
if ((pai = d2i_PaillierPrivateKey(NULL, pder, derlen)) == NULL) {
|
||||
PAILLIERerr(PAILLIER_F_OLD_PAILLIER_PRIV_DECODE,
|
||||
PAILLIER_R_DECODE_ERROR);
|
||||
return 0;
|
||||
}
|
||||
EVP_PKEY_assign_PAILLIER(pkey, pai);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int old_paillier_priv_encode(const EVP_PKEY *pkey, unsigned char **pder)
|
||||
{
|
||||
return i2d_PaillierPrivateKey(pkey->pkey.paillier, pder);
|
||||
}
|
||||
|
||||
const EVP_PKEY_ASN1_METHOD paillier_asn1_meth = {
|
||||
|
||||
Reference in New Issue
Block a user