mirror of
https://github.com/guanzhi/GmSSL.git
synced 2026-09-23 13:54:07 +08:00
new modules
This commit is contained in:
539
crypto/cpk/cpk.h
Normal file
539
crypto/cpk/cpk.h
Normal file
@@ -0,0 +1,539 @@
|
||||
#ifndef HEADER_CPK_H
|
||||
#define HEADER_CPK_H
|
||||
|
||||
#include <openssl/err.h>
|
||||
#include <openssl/x509.h>
|
||||
#include <openssl/ossl_typ.h>
|
||||
#include "ecies.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define CPK_LIB_VERSION "0.9"
|
||||
#define CPK_MAX_ID_LENGTH 64
|
||||
|
||||
|
||||
/**
|
||||
* @struct CPK_MASTER_SECRET
|
||||
* @brief The in-memory structure to represent a cpk master secret.
|
||||
*/
|
||||
typedef struct cpk_master_secret_st {
|
||||
long version; /**< The version of the master secret.*/
|
||||
X509_NAME *id; /**< The id of the master secret.*/
|
||||
X509_ALGOR *pkey_algor; /**< The public key algorithm used in the master secret.*/
|
||||
X509_ALGOR *map_algor; /**< The map algorithm used in the master secret.*/
|
||||
ASN1_OCTET_STRING *secret_factors; /**< The secret factors of the master secret.*/
|
||||
} CPK_MASTER_SECRET;
|
||||
/**
|
||||
* @def
|
||||
* @brief Declare 4 basic ASN1 functions of CPK_MASTER_SECRET and a pointer
|
||||
* to an ASN1_ITEM with detail information of the fields of
|
||||
* CPK_MASTER_SECRET in it.
|
||||
*
|
||||
* The macro would generate following 4 function declarations and 1 pointer:\n
|
||||
* CPK_MASTER_SECRET *CPK_MASTER_SECRET_new(void): alloc a new instance of CPK_MASTER_SECRET.\n
|
||||
* void CPK_MASTER_SECRET_free(CPK_MASTER_SECRET* a): free the instance a.\n
|
||||
* CPK_MASTER_SECRET *d2i_CPK_MASTER_SECRET(CPK_MASTER_SECRET **a, const unsigned char **in, long len):
|
||||
* convert the CPK_MASTER_SECRET instance from the DER format to the internal format.\n
|
||||
* int i2d_CPK_MASTER_SECRET(CPK_MASTER_SECRET *a, unsigned char* out): convert an CPK_MASTER_SECRET
|
||||
* to the DER format.\n
|
||||
* ASN1_ITEM* CPK_MASTER_SECRET_it: a pointer to a instance of ASN1_ITEM struct which contains
|
||||
* information on the conversion between DER and internal.\n
|
||||
*/
|
||||
DECLARE_ASN1_FUNCTIONS(CPK_MASTER_SECRET)
|
||||
|
||||
|
||||
/**
|
||||
* @struct CPK_PUBLIC_PARAMS
|
||||
* @brief The in-memory structure to represent a set of cpk public parameters.
|
||||
*/
|
||||
typedef struct cpk_public_params_st {
|
||||
long version; /**< The version of the public parameters.*/
|
||||
X509_NAME *id; /**< The id of the public parameters.*/
|
||||
X509_ALGOR *pkey_algor; /**< The public key algorithm used in the public parameters.*/
|
||||
X509_ALGOR *map_algor; /**< The map algorithm used in the public parameters.*/
|
||||
ASN1_OCTET_STRING *public_factors;/**< The public factors of the public parameters.*/
|
||||
} CPK_PUBLIC_PARAMS;
|
||||
/**
|
||||
* @brief Declare 4 basic ASN1 functions of CPK_PUBLIC_PARAMS and a pointer to an ASN1_ITEM
|
||||
* with detail information of the fields of CPK_PUBLIC_PARAMS in it.
|
||||
*
|
||||
* The macro would generate following 4 function declarations and 1 pointer:\n
|
||||
* CPK_PUBLIC_PARAMS *CPK_PUBLIC_PARAMS_new(void): alloc a new instance of CPK_PUBLIC_PARAMS.\n
|
||||
* void CPK_PUBLIC_PARAMS_free(CPK_PUBLIC_PARAMS* a): free the instance a.\n
|
||||
* CPK_PUBLIC_PARAMS *d2i_CPK_PUBLIC_PARAMS(CPK_PUBLIC_PARAMS **a, const unsigned char **in, long len):
|
||||
* convert the CPK_PUBLIC_PARAMS instance from the DER format to the internal format.\n
|
||||
* int i2d_CPK_PUBLIC_PARAMS(CPK_PUBLIC_PARAMS *a, unsigned char* out): convert a CPK_PUBLIC_PARAMS
|
||||
* to the DER format.\n
|
||||
* ASN1_ITEM* CPK_PUBLIC_PARAMS_it: a pointer to a instance of ASN1_ITEM struct which contains
|
||||
* information on the conversion between DER and internal.\n
|
||||
*/
|
||||
DECLARE_ASN1_FUNCTIONS(CPK_PUBLIC_PARAMS)
|
||||
|
||||
/**
|
||||
* @brief Get a new default map algorithm.
|
||||
*
|
||||
* @return Returns a pointer to a new instance of the default map algorithm of the type X509_ALGOR.
|
||||
*/
|
||||
X509_ALGOR *CPK_MAP_new_default();
|
||||
|
||||
/**
|
||||
* @brief Check if the given map algorithm is valid.
|
||||
*
|
||||
* @param[in] algor The pointer to the algorithm to check.
|
||||
* @return Returns 1 if the given algorithm is valid.
|
||||
*/
|
||||
int CPK_MAP_is_valid(const X509_ALGOR *algor);
|
||||
|
||||
/**
|
||||
* @brief Get the number of factors of the given algorithm.
|
||||
*
|
||||
* @param[in] algor The pointer to the algorithm.
|
||||
* @return Returns the number of factors of the algorithm.
|
||||
*/
|
||||
int CPK_MAP_num_factors(const X509_ALGOR *algor);
|
||||
|
||||
/**
|
||||
* @brief Get the number of indexes of the given algorithm.
|
||||
*
|
||||
* @param[in] algor The pointer to the algorithm.
|
||||
* @return Returns the number of indexes of the algorithm.
|
||||
*/
|
||||
int CPK_MAP_num_indexes(const X509_ALGOR *algor);
|
||||
|
||||
/**
|
||||
* @brief Convert the string to the index vector.
|
||||
*
|
||||
* @param[in] algor The pointer to the algorithm to do the map function.
|
||||
* @param[in] str The pointer to a string in the memory, ended by '\0'.
|
||||
* @param[out] index The pointer to a array which will receive the index.
|
||||
* @return Returns 1 on success.
|
||||
*/
|
||||
int CPK_MAP_str2index(const X509_ALGOR *algor, const char *str, int *index);
|
||||
|
||||
/**
|
||||
* @brief Print the parameters of the map algortihm.
|
||||
*
|
||||
* @param[out] out A IO abstraction to receive the output stream.
|
||||
* @param[in] indent The amount of the indentation in the output stream.
|
||||
* @param[in] flags The flag set to control the ouput.
|
||||
* @return Returns 1 on success.
|
||||
*/
|
||||
int CPK_MAP_print(BIO *out, X509_ALGOR *map, int indent, unsigned long flags);
|
||||
|
||||
/**
|
||||
* @brief Create a master secret with the given domain id, public key algorithm and map algorithm.
|
||||
*
|
||||
* @param[in] domain_id The domain identifier of the master secret.
|
||||
* @param[in] pkey The pointer to the public key algorithm of the master secret.
|
||||
* @param[in] map_algor The pointer to the map algorithm of the master secret.
|
||||
* @return Returns a poniter to the created master secret on success, or NULL on failure.
|
||||
*/
|
||||
CPK_MASTER_SECRET *CPK_MASTER_SECRET_create(const char *domain_id, EVP_PKEY *pkey, X509_ALGOR *map_algor);
|
||||
|
||||
/**
|
||||
* @brief Extract the public parameters from the master secret.
|
||||
*
|
||||
* @param[in] master The master secret to extract from.
|
||||
* @return Returns the pointer to the extracted public parameters on success, or NULL on failure.
|
||||
*/
|
||||
CPK_PUBLIC_PARAMS *CPK_MASTER_SECRET_extract_public_params(CPK_MASTER_SECRET *master);
|
||||
|
||||
/**
|
||||
* @brief Extract the private key of a given identifier from the master secret.
|
||||
*
|
||||
* @param[in] master The master secret to extract from.
|
||||
* @param[in] id The identifier which is used to maps to the private key.
|
||||
* @return Returns the pointer to the extracted private key on success, or NULL on failure.
|
||||
*/
|
||||
EVP_PKEY *CPK_MASTER_SECRET_extract_private_key(CPK_MASTER_SECRET *master, const char *id);
|
||||
|
||||
/**
|
||||
* @brief Extract the public key of a given identifier from the public parameters.
|
||||
*
|
||||
* @param[in] params The public parameters to extract from.
|
||||
* @param[in] id The identifier which is used to maps to the public key.
|
||||
* @return Returns the pointer to the extracted public key EVP_PKEY on success, or NULL on failure.
|
||||
*/
|
||||
EVP_PKEY *CPK_PUBLIC_PARAMS_extract_public_key(CPK_PUBLIC_PARAMS *params, const char *id);
|
||||
|
||||
|
||||
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));
|
||||
|
||||
char *CPK_MASTER_SECRET_get_name(CPK_MASTER_SECRET *master, char *buf, int size);
|
||||
char *CPK_PUBLIC_PARAMS_get_name(CPK_PUBLIC_PARAMS *params);
|
||||
|
||||
/**
|
||||
* @brief Generate the message digest of the given master secret with the given parameters.
|
||||
*
|
||||
* This function takes the secret_factors field of the given parameter of CPK_MASTER_SECRET as
|
||||
* the input and the parameter type of EVP_MD as the message digest
|
||||
* algorithm to compute the message digest, and put the result in the parameter md, the length
|
||||
* of the result in the paramter len.
|
||||
* @param[in] master The master secret to compute the digest.
|
||||
* @param[in] type The message digest algorithm to use to comput the digest.
|
||||
* @param[out] md The buffer to receive the result of the computation of message digest.
|
||||
* @param[out] len If len is not null, the variable it point to will be assigned the length of
|
||||
* the message digest.
|
||||
* @return Returns 1 on success, 0 on failure.
|
||||
*/
|
||||
int CPK_MASTER_SECRET_digest(CPK_MASTER_SECRET *master, const EVP_MD *type, unsigned char *md, unsigned int *len);
|
||||
|
||||
/**
|
||||
* @brief Generate the message digest of the given public parameters with the given parameters.
|
||||
*
|
||||
* This function takes the secret_factors field of the given parameter of CPK_PUBLIC_PARAMS as
|
||||
* the input and the parameter type of EVP_MD as the message digest
|
||||
* algorithm to compute the message digest, and put the result in the parameter md, the length
|
||||
* of the result in the paramter len.
|
||||
* @param[in] params The public parameters to compute the digest.
|
||||
* @param[in] type The message digest algorithm to use to comput the digest.
|
||||
* @param[out] md The buffer to receive the result of the computation of message digest.
|
||||
* @param[out] len If len is not null, the variable it point to will be assigned the length of
|
||||
* the message digest.
|
||||
* @return Returns 1 on success, 0 on failure.
|
||||
*/
|
||||
int CPK_PUBLIC_PARAMS_digest(CPK_PUBLIC_PARAMS *params, const EVP_MD *type, unsigned char *md, unsigned int *len);
|
||||
|
||||
/**
|
||||
* @brief Print the master secret to a BIO, including the version, the domain uri, the public
|
||||
* algorithm and the map algorithm.
|
||||
*
|
||||
* @param[out] out A IO abstraction to receive the output stream.
|
||||
* @param[in] master The CPK_MASTER_SECRET instance to print.
|
||||
* @param[in] indent The amount of the indentation.
|
||||
* @param[in] flags The flag set to control the ouput.
|
||||
* @return Returns 1 on success, 0 on failure.
|
||||
*/
|
||||
int CPK_MASTER_SECRET_print(BIO *out, CPK_MASTER_SECRET *master, int indent, unsigned long flags);
|
||||
|
||||
/**
|
||||
* @brief Print the public parameters to a BIO.
|
||||
*
|
||||
* @param[out] out A IO abstraction to receive the output stream.
|
||||
* @param[in] params The CPK_PUBLIC_PARAMS instance to print.
|
||||
* @param[in] indent The amount of the indentation.
|
||||
* @param[in] flags The flag set to control the ouput.
|
||||
* @return Returns 1 on success, 0 on failure.
|
||||
*/
|
||||
int CPK_PUBLIC_PARAMS_print(BIO *out, CPK_PUBLIC_PARAMS *params, int indent, unsigned long flags);
|
||||
|
||||
/**
|
||||
* @brief Validate the public parameters with the given master secret.
|
||||
*
|
||||
* @param[in] master The master secret used for the validation.
|
||||
* @param[in] params The public parameters to validate.
|
||||
* @return Returns 1 if the public parameter is valid, returns 0 otherwise.
|
||||
*/
|
||||
int CPK_MASTER_SECRET_validate_public_params(CPK_MASTER_SECRET *master, CPK_PUBLIC_PARAMS *params);
|
||||
|
||||
/**
|
||||
* @brief Validate the private key with the given public parameters.
|
||||
*
|
||||
* @param[in] params The public parameter used for the validation.
|
||||
* @param[in] id the identifier of the private key owner.
|
||||
* @param[in] pkey pkey The private key to validate.
|
||||
* @return Returns 1 if the private key is valid, returns a integer less or equal than 0 otherwise.
|
||||
*/
|
||||
int CPK_PUBLIC_PARAMS_validate_private_key(CPK_PUBLIC_PARAMS *params, const char *id, const EVP_PKEY *pkey);
|
||||
|
||||
/**
|
||||
* @brief Convert the master secret in DER format in the IO abstraction to an instance of CPK_MASTER_SECRET.
|
||||
*
|
||||
* @param[in] bp A pointer to the IO abstraction which ocntaints the master secret in DER format.
|
||||
* @param[out] master A pointer to receive the pointer to the converted master secret of the type CPK_MASTER_SECRET.
|
||||
* @return Returns the pointer to the converted master secret of the type CPK_MASTER_SECRET on success,
|
||||
* or null on failure.
|
||||
*/
|
||||
CPK_MASTER_SECRET *d2i_CPK_MASTER_SECRET_bio(BIO *bp, CPK_MASTER_SECRET **master);
|
||||
|
||||
/**
|
||||
* @brief Convert the master key from CPK_MASTER_SECRET to a byte stream in DER format, and write
|
||||
* the stream to an IO abstraction.
|
||||
*
|
||||
* @param[out] bp A pointer to the IO abstraction which receives the stream.
|
||||
* @param[in] master A pointer to the master key of the type CPK_MASTER_SECRET.
|
||||
* @return Returns the size of the output stream on success, of an integer less or equal than 0
|
||||
* indicating an error.
|
||||
*/
|
||||
int i2d_CPK_MASTER_SECRET_bio(BIO *bp, CPK_MASTER_SECRET *master);
|
||||
|
||||
/**
|
||||
* @brief Convert the public parameters in DER format in the IO abstraction to an instance of CPK_PUBLIC_PARAMS.
|
||||
*
|
||||
* @param[in] bp A pointer to the IO abstraction which ocntaints the public parameters in DER format.
|
||||
* @param[out] params A pointer to receive the pointer to the converted public parameters of the type CPK_PUBLIC_PARAMS.
|
||||
* @return Returns the pointer to the converted public parameters of the type CPK_PUBLIC_PARAMS on success,
|
||||
* or null on failure.
|
||||
*/
|
||||
CPK_PUBLIC_PARAMS *d2i_CPK_PUBLIC_PARAMS_bio(BIO *bp, CPK_PUBLIC_PARAMS **params);
|
||||
|
||||
/**
|
||||
* @brief Convert the public parameters from CPK_PUBLIC_PARAMS to a byte stream in DER format,
|
||||
* and write the stream to an IO abstraction.
|
||||
*
|
||||
* @param[out] bp A pointer to the IO abstraction which receives the stream.
|
||||
* @param[in] master A pointer to the public parameters of the type CPK_PUBLIC_PARAMS.
|
||||
* @return Returns the size of the output stream on success, of an integer less or equal than 0
|
||||
* indicating an error.
|
||||
*/
|
||||
int i2d_CPK_PUBLIC_PARAMS_bio(BIO *bp, CPK_PUBLIC_PARAMS *params);
|
||||
|
||||
|
||||
/*
|
||||
* SignerInfo ::= SEQUENCE {
|
||||
* version INTEGER {1},
|
||||
* signer IssuerAndSerialNumber,
|
||||
* digestAlgor DigestAlgorithmIdentifier,
|
||||
* signedAttrs [0] IMPLICIT Attributes OPTIONAL,
|
||||
* signingAlgor SigningAlgorithmIdentifier,
|
||||
* signature OCTET STRING {{ECDSASigValue}},
|
||||
* unsignedAttrs [1] IMPLICIT Attributes OPTIONAL
|
||||
* }
|
||||
*/
|
||||
typedef struct cpk_signer_info_st {
|
||||
long version;
|
||||
X509_NAME *signer;
|
||||
X509_ALGOR *digest_algor;
|
||||
STACK_OF(X509_ATTRIBUTE) *signed_attr;
|
||||
X509_ALGOR *sign_algor;
|
||||
ASN1_OCTET_STRING *signature;
|
||||
STACK_OF(X509_ATTRIBTE) *unsigned_attr;
|
||||
EVP_PKEY *_privkey; /* private member */
|
||||
} CPK_SIGNER_INFO;
|
||||
DECLARE_STACK_OF(CPK_SIGNER_INFO)
|
||||
DECLARE_ASN1_SET_OF(CPK_SIGNER_INFO)
|
||||
DECLARE_ASN1_FUNCTIONS(CPK_SIGNER_INFO)
|
||||
|
||||
int CPK_SIGNER_INFO_set(CPK_SIGNER_INFO *si, const EVP_MD *sign_alg, const EVP_PKEY *sign_key);
|
||||
int CPK_SIGNER_INFO_add_attr(CPK_SIGNER_INFO *si, int nid, int atrtype, void *value);
|
||||
int CPK_SIGNER_INFO_add_signed_attr(CPK_SIGNER_INFO *si, int nid, int atrtype, void *value);
|
||||
int CPK_SIGNER_INFO_add_signed_time(CPK_SIGNER_INFO *si);
|
||||
int CPK_SIGNER_INFO_add_signed_digest(CPK_SIGNER_INFO *si, const EVP_MD_CTX *ctx);
|
||||
ASN1_TYPE *CPK_SIGNER_INFO_get_attr(CPK_SIGNER_INFO *si, int nid);
|
||||
ASN1_TYPE *CPK_SIGNER_INFO_get_signed_attr(CPK_SIGNER_INFO *si, int nid);
|
||||
ASN1_UTCTIME *CPK_SIGNER_INFO_get_signed_time(CPK_SIGNER_INFO *si);
|
||||
|
||||
int CPK_SIGNER_INFO_do_sign(CPK_SIGNER_INFO *si, EVP_MD_CTX *md_ctx);
|
||||
int CPK_SIGNER_INFO_do_verify(const CPK_SIGNER_INFO *si, EVP_MD_CTX *ctx, const CPK_PUBLIC_PARAMS *params);
|
||||
|
||||
/*
|
||||
* RecipientInfo ::= SEQUENCE {
|
||||
* version INTEGER {0},
|
||||
* recipient IssuerAndSerialNumber,
|
||||
* keyEncryptionAlgor EncryptionAlgorithmIdentifier,
|
||||
* encryptedKey OCTET STRING
|
||||
* }
|
||||
* RecipientInfos ::= SET OF RecipientInfo
|
||||
*/
|
||||
typedef struct cpk_recip_info_st {
|
||||
long version;
|
||||
X509_NAME *recipient;
|
||||
X509_ALGOR *enc_algor;
|
||||
ASN1_OCTET_STRING *enc_data;
|
||||
/* private */
|
||||
EVP_PKEY *_pubkey;
|
||||
} CPK_RECIP_INFO;
|
||||
DECLARE_STACK_OF(CPK_RECIP_INFO)
|
||||
DECLARE_ASN1_SET_OF(CPK_RECIP_INFO)
|
||||
DECLARE_ASN1_FUNCTIONS(CPK_RECIP_INFO)
|
||||
|
||||
int CPK_RECIP_INFO_set(CPK_RECIP_INFO *ri, const X509_NAME *recipient, const ECIES_PARAMS *ecies);
|
||||
int CPK_RECIP_INFO_do_encrypt(CPK_RECIP_INFO *ri, const unsigned char *in, size_t inlen);
|
||||
int CPK_RECIP_INFO_do_decrypt(CPK_RECIP_INFO *ri, const EVP_PKEY *pkey, unsigned char *out, size_t *outlen);
|
||||
|
||||
|
||||
/*
|
||||
* SingerInfos ::= SET OF SignerInfo
|
||||
*
|
||||
* SignedData ::= SEQUENCE {
|
||||
* version INTEGER,
|
||||
* digestAlgors DigestAlgorithmIdentifiers,
|
||||
* contentInfo ContentInfo,
|
||||
* signerInfos SignerInfos,
|
||||
* }
|
||||
*/
|
||||
typedef struct cpk_signed_st {
|
||||
long version;
|
||||
STACK_OF(X509_ALGOR) *digest_algors;
|
||||
STACK_OF(X509) *cert; /* [ 0 ] */
|
||||
STACK_OF(X509_CRL) *crl; /* [ 1 ] */
|
||||
STACK_OF(CPK_SIGINFO) *signer_infos;
|
||||
struct CPK_CMS_st *contents;
|
||||
} CPK_SIGNED;
|
||||
DECLARE_ASN1_FUNCTIONS(CPK_SIGNED)
|
||||
|
||||
typedef struct cpk_enc_content_st {
|
||||
ASN1_OBJECT *content_type;
|
||||
X509_ALGOR *enc_algor;
|
||||
ASN1_OCTET_STRING *enc_data; /* [ 0 ] */
|
||||
/* private */
|
||||
const EVP_CIPHER *cipher;
|
||||
} CPK_ENC_CONTENT;
|
||||
DECLARE_ASN1_FUNCTIONS(CPK_ENC_CONTENT)
|
||||
|
||||
typedef struct cpk_envelope_st {
|
||||
long version;
|
||||
STACK_OF(CPK_RECIP_INFO) *recip_infos;
|
||||
CPK_ENC_CONTENT *enc_data;
|
||||
} CPK_ENVELOPE;
|
||||
DECLARE_ASN1_FUNCTIONS(CPK_ENVELOPE)
|
||||
|
||||
typedef struct cpk_sign_envelope_st {
|
||||
long version;
|
||||
STACK_OF(X509_ALGOR) *digest_algors;
|
||||
STACK_OF(X509) *cert; /* [ 0 ] */
|
||||
STACK_OF(X509_CRL) *crl; /* [ 1 ] */
|
||||
STACK_OF(CPK_SIGNER_INFO) *signer_infos;
|
||||
CPK_ENC_CONTENT *enc_data;
|
||||
STACK_OF(CPK_RECIP_INFO) *recip_infos;
|
||||
} CPK_SIGN_ENVELOPE;
|
||||
DECLARE_ASN1_FUNCTIONS(CPK_SIGN_ENVELOPE)
|
||||
|
||||
typedef struct cpk_cms_st {
|
||||
int state; /* used during processing */
|
||||
int detached;
|
||||
|
||||
ASN1_OBJECT *type;
|
||||
union {
|
||||
char *ptr;
|
||||
|
||||
/* NID_pkcs7_data */
|
||||
ASN1_OCTET_STRING *data;
|
||||
|
||||
/* NID_pkcs7_signed */
|
||||
CPK_SIGNED *sign;
|
||||
|
||||
/* NID_pkcs7_enveloped */
|
||||
CPK_ENVELOPE *enveloped;
|
||||
|
||||
/* NID_pkcs7_signedAndEnveloped */
|
||||
CPK_SIGN_ENVELOPE *signed_and_enveloped;
|
||||
|
||||
/* Anything else */
|
||||
ASN1_TYPE *other;
|
||||
} d;
|
||||
} CPK_CMS;
|
||||
DECLARE_STACK_OF(CPK_CMS)
|
||||
DECLARE_ASN1_SET_OF(CPK_CMS)
|
||||
DECLARE_PKCS12_STACK_OF(CPK_CMS)
|
||||
DECLARE_ASN1_FUNCTIONS(CPK_CMS)
|
||||
|
||||
DECLARE_ASN1_ITEM(CPK_CMS_ATTR_SIGN)
|
||||
DECLARE_ASN1_ITEM(CPK_CMS_ATTR_VERIFY)
|
||||
DECLARE_ASN1_NDEF_FUNCTION(CPK_CMS)
|
||||
|
||||
|
||||
|
||||
#define CPK_CMS_OP_SET_DETACHED_SIGNATURE 1
|
||||
#define CPK_CMS_OP_GET_DETACHED_SIGNATURE 2
|
||||
|
||||
#define CPK_CMS_get_signed_attributes(si) ((si)->auth_attr)
|
||||
#define CPK_CMS_get_attributes(si) ((si)->unauth_attr)
|
||||
|
||||
#define CPK_CMS_type_is_signed(a) \
|
||||
(OBJ_obj2nid((a)->type) == NID_pkcs7_signed)
|
||||
#define CPK_CMS_type_is_enveloped(a) \
|
||||
(OBJ_obj2nid((a)->type) == NID_pkcs7_enveloped)
|
||||
#define CPK_CMS_type_is_signedAndEnveloped(a) \
|
||||
(OBJ_obj2nid((a)->type) == NID_pkcs7_signedAndEnveloped)
|
||||
#define CPK_CMS_type_is_data(a) \
|
||||
(OBJ_obj2nid((a)->type) == NID_pkcs7_data)
|
||||
#define CPK_CMS_set_detached(p,v) \
|
||||
CPK_CMS_ctrl(p,CPK_CMS_OP_SET_DETACHED_SIGNATURE,v,NULL)
|
||||
#define CPK_CMS_get_detached(p) \
|
||||
CPK_CMS_ctrl(p,CPK_CMS_OP_GET_DETACHED_SIGNATURE,0,NULL)
|
||||
#define CPK_CMS_is_detached(p7) \
|
||||
(CPK_CMS_type_is_signed(p7) && CPK_CMS_get_detached(p7))
|
||||
|
||||
long CPK_CMS_ctrl(CPK_CMS *p7, int cmd, long larg, char *parg);
|
||||
int CPK_CMS_set_type(CPK_CMS *p7, int type);
|
||||
int CPK_CMS_set_cipher(CPK_CMS *p7, const EVP_CIPHER *cipher);
|
||||
int CPK_CMS_set_content(CPK_CMS *p7, CPK_CMS *p7_data);
|
||||
int CPK_CMS_content_new(CPK_CMS *p7, int type);
|
||||
int CPK_CMS_add_signer(CPK_CMS *p7, const EVP_MD *sign_alg, const EVP_PKEY *sign_key);
|
||||
int CPK_CMS_add_recipient(CPK_CMS *p7, const X509_NAME *id, const ECIES_PARAMS *params);
|
||||
|
||||
|
||||
BIO *CPK_CMS_dataInit(CPK_CMS *p7, BIO *bio);
|
||||
BIO *CPK_CMS_dataDecode(CPK_CMS *p7, BIO *in_bio, const EVP_PKEY *keyinfo);
|
||||
int CPK_CMS_dataUpdate(CPK_CMS *p7, BIO *bio, const unsigned char *data, int len);
|
||||
int CPK_CMS_dataFinal(CPK_CMS *p7, BIO *bio);
|
||||
STACK_OF(CPK_SIGNER_INFO) *CPK_CMS_get_signer_infos(CPK_CMS *p7);
|
||||
int CPK_CMS_dataVerify(CPK_PUBLIC_PARAMS *params, BIO *bio, CPK_CMS *p7, CPK_SIGNER_INFO *si);
|
||||
|
||||
|
||||
|
||||
/* ERR function (should in openssl/err.h) begin */
|
||||
#define ERR_LIB_CPK 130
|
||||
#define ERR_R_CPK_LIB ERR_LIB_CPK
|
||||
#define CPKerr(f,r) ERR_PUT_error(ERR_LIB_CPK,(f),(r),__FILE__,__LINE__)
|
||||
/* end */
|
||||
|
||||
|
||||
void ERR_load_CPK_strings(void);
|
||||
|
||||
/**
|
||||
* @defgroup error_cpk Definations to handle errors of cpk runtime.
|
||||
* @{
|
||||
*/
|
||||
/* Error codes for the ECIES functions. */
|
||||
|
||||
/* Function codes. */
|
||||
#define CPK_F_CPK_MASTER_SECRET_CREATE 100
|
||||
#define CPK_F_CPK_MASTER_SECRET_EXTRACT_PUBLIC_PARAMS 101
|
||||
#define CPK_F_CPK_MASTER_SECRET_EXTRACT_PRIVATE_KEY 102
|
||||
#define CPK_F_CPK_PUBLIC_PARAMS_EXTRACT_PUBLIC_KEY 103
|
||||
#define CPK_F_CPK_PUBLIC_PARAMS_COMPUTE_SHARE_KEY 116
|
||||
#define CPK_F_CPK_MASTER_SECRET_DIGEST 104
|
||||
#define CPK_F_CPK_PUBLIC_PARAMS_DIGEST 105
|
||||
#define CPK_F_CPK_MASTER_SECRET_PRINT 106
|
||||
#define CPK_F_CPK_PUBLIC_PARAMS_PRINT 107
|
||||
#define CPK_F_CPK_MASTER_SECRET_VALIDATE_PUBLIC_PARAMS 108
|
||||
#define CPK_F_CPK_PUBLIC_PARAMS_VALIDATE_PRIVATE_KEY 109
|
||||
#define CPK_F_CPK_MAP_NEW_DEFAULT 110
|
||||
#define CPK_F_CPK_MAP_NUM_FACTORS 111
|
||||
#define CPK_F_CPK_MAP_NUM_INDEXES 112
|
||||
#define CPK_F_CPK_MAP_STR2INDEX 113
|
||||
#define CPK_F_X509_ALGOR_GET1_EC_KEY 114
|
||||
#define CPK_F_X509_ALGOR_GET1_DSA 115
|
||||
|
||||
/* Reason codes. */
|
||||
#define CPK_R_BAD_ARGUMENT 100
|
||||
#define CPK_R_UNKNOWN_DIGEST_TYPE 101
|
||||
#define CPK_R_UNKNOWN_CIPHER_TYPE 102
|
||||
#define CPK_R_UNKNOWN_MAP_TYPE 103
|
||||
#define CPK_R_UNKNOWN_CURVE 104
|
||||
#define CPK_R_STACK_ERROR 105
|
||||
#define CPK_R_DERIVE_KEY_FAILED 106
|
||||
#define CPK_R_ECIES_ENCRYPT_FAILED 107
|
||||
#define CPK_R_ECIES_DECRYPT_FAILED 108
|
||||
#define CPK_R_DER_DECODE_FAILED 109
|
||||
#define CPK_R_UNSUPPORTED_PKCS7_CONTENT_TYPE 110
|
||||
#define CPK_R_SET_SIGNER 111
|
||||
#define CPK_R_SET_RECIP_INFO 112
|
||||
#define CPK_R_UNABLE_TO_FIND_MESSAGE_DIGEST 113
|
||||
#define CPK_R_BAD_DATA 114
|
||||
#define CPK_R_MAP_FAILED 115
|
||||
#define CPK_R_ADD_SIGNING_TIME 116
|
||||
#define CPK_R_VERIFY_FAILED 117
|
||||
#define CPK_R_UNKNOWN_ECDH_TYPE 118
|
||||
#define CPK_R_DIGEST_FAILED 119
|
||||
#define CPK_R_WITHOUT_DECRYPT_KEY 120
|
||||
#define CPK_R_UNKNOWN_PKCS7_TYPE 121
|
||||
#define CPK_R_INVALID_ID_LENGTH 122
|
||||
#define CPK_R_INVALID_PKEY_TYPE 123
|
||||
#define CPK_R_INVALID_MAP_ALGOR 124
|
||||
#define CPK_R_PKEY_TYPE_NOT_MATCH 125
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
124
crypto/cpk/cpk_asn1.c
Normal file
124
crypto/cpk/cpk_asn1.c
Normal file
@@ -0,0 +1,124 @@
|
||||
#include <openssl/asn1t.h>
|
||||
#include "cpk.h"
|
||||
|
||||
ASN1_SEQUENCE(CPK_MASTER_SECRET) = {
|
||||
ASN1_SIMPLE(CPK_MASTER_SECRET, version, LONG),
|
||||
ASN1_SIMPLE(CPK_MASTER_SECRET, id, X509_NAME),
|
||||
ASN1_SIMPLE(CPK_MASTER_SECRET, pkey_algor, X509_ALGOR),
|
||||
ASN1_SIMPLE(CPK_MASTER_SECRET, map_algor, X509_ALGOR),
|
||||
ASN1_SIMPLE(CPK_MASTER_SECRET, secret_factors, ASN1_OCTET_STRING)
|
||||
} ASN1_SEQUENCE_END(CPK_MASTER_SECRET)
|
||||
IMPLEMENT_ASN1_FUNCTIONS(CPK_MASTER_SECRET)
|
||||
IMPLEMENT_ASN1_DUP_FUNCTION(CPK_MASTER_SECRET)
|
||||
|
||||
ASN1_SEQUENCE(CPK_PUBLIC_PARAMS) = {
|
||||
ASN1_SIMPLE(CPK_PUBLIC_PARAMS, version, LONG),
|
||||
ASN1_SIMPLE(CPK_PUBLIC_PARAMS, id, X509_NAME),
|
||||
ASN1_SIMPLE(CPK_PUBLIC_PARAMS, pkey_algor, X509_ALGOR),
|
||||
ASN1_SIMPLE(CPK_PUBLIC_PARAMS, map_algor, X509_ALGOR),
|
||||
ASN1_SIMPLE(CPK_PUBLIC_PARAMS, public_factors, ASN1_OCTET_STRING)
|
||||
} ASN1_SEQUENCE_END(CPK_PUBLIC_PARAMS)
|
||||
IMPLEMENT_ASN1_FUNCTIONS(CPK_PUBLIC_PARAMS)
|
||||
IMPLEMENT_ASN1_DUP_FUNCTION(CPK_PUBLIC_PARAMS)
|
||||
|
||||
|
||||
CPK_MASTER_SECRET *d2i_CPK_MASTER_SECRET_bio(BIO *bp, CPK_MASTER_SECRET **master) {
|
||||
return ASN1_item_d2i_bio(ASN1_ITEM_rptr(CPK_MASTER_SECRET), bp, master);
|
||||
}
|
||||
|
||||
int i2d_CPK_MASTER_SECRET_bio(BIO *bp, CPK_MASTER_SECRET *master) {
|
||||
return ASN1_item_i2d_bio(ASN1_ITEM_rptr(CPK_MASTER_SECRET), bp, master);
|
||||
}
|
||||
|
||||
CPK_PUBLIC_PARAMS *d2i_CPK_PUBLIC_PARAMS_bio(BIO *bp, CPK_PUBLIC_PARAMS **params) {
|
||||
return ASN1_item_d2i_bio(ASN1_ITEM_rptr(CPK_PUBLIC_PARAMS), bp, params);
|
||||
}
|
||||
|
||||
int i2d_CPK_PUBLIC_PARAMS_bio(BIO *bp, CPK_PUBLIC_PARAMS *params) {
|
||||
return ASN1_item_i2d_bio(ASN1_ITEM_rptr(CPK_PUBLIC_PARAMS), bp, params);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* This is the ANY DEFINED BY table for the top level PKCS#7 structure */
|
||||
ASN1_ADB_TEMPLATE(cpkcmsdefault) = ASN1_EXP_OPT(CPK_CMS, d.other, ASN1_ANY, 0);
|
||||
|
||||
ASN1_ADB(CPK_CMS) = {
|
||||
ADB_ENTRY(NID_pkcs7_data, ASN1_NDEF_EXP_OPT(CPK_CMS, d.data, ASN1_OCTET_STRING_NDEF, 0)),
|
||||
ADB_ENTRY(NID_pkcs7_signed, ASN1_NDEF_EXP_OPT(CPK_CMS, d.sign, CPK_SIGNED, 0)),
|
||||
ADB_ENTRY(NID_pkcs7_enveloped, ASN1_NDEF_EXP_OPT(CPK_CMS, d.enveloped, CPK_ENVELOPE, 0)),
|
||||
ADB_ENTRY(NID_pkcs7_signedAndEnveloped,
|
||||
ASN1_NDEF_EXP_OPT(CPK_CMS, d.signed_and_enveloped, CPK_SIGN_ENVELOPE, 0)),
|
||||
} ASN1_ADB_END(CPK_CMS, 0, type, 0, &cpkcmsdefault_tt, NULL);
|
||||
|
||||
|
||||
ASN1_NDEF_SEQUENCE(CPK_CMS) = {
|
||||
ASN1_SIMPLE(CPK_CMS, type, ASN1_OBJECT),
|
||||
ASN1_ADB_OBJECT(CPK_CMS)
|
||||
}ASN1_NDEF_SEQUENCE_END(CPK_CMS)
|
||||
IMPLEMENT_ASN1_FUNCTIONS(CPK_CMS)
|
||||
IMPLEMENT_ASN1_NDEF_FUNCTION(CPK_CMS)
|
||||
IMPLEMENT_ASN1_DUP_FUNCTION(CPK_CMS)
|
||||
|
||||
|
||||
ASN1_NDEF_SEQUENCE(CPK_SIGNED) = {
|
||||
ASN1_SIMPLE(CPK_SIGNED, version, LONG),
|
||||
ASN1_SET_OF(CPK_SIGNED, digest_algors, X509_ALGOR),
|
||||
ASN1_SIMPLE(CPK_SIGNED, contents, CPK_CMS),
|
||||
ASN1_IMP_SEQUENCE_OF_OPT(CPK_SIGNED, cert, X509, 0),
|
||||
ASN1_IMP_SET_OF_OPT(CPK_SIGNED, crl, X509_CRL, 1),
|
||||
ASN1_SET_OF(CPK_SIGNED, signer_infos, CPK_SIGNER_INFO)
|
||||
} ASN1_NDEF_SEQUENCE_END(CPK_SIGNED)
|
||||
IMPLEMENT_ASN1_FUNCTIONS(CPK_SIGNED)
|
||||
|
||||
|
||||
ASN1_SEQUENCE(CPK_SIGNER_INFO) = {
|
||||
ASN1_SIMPLE(CPK_SIGNER_INFO, version, LONG),
|
||||
ASN1_SIMPLE(CPK_SIGNER_INFO, signer, X509_NAME),
|
||||
ASN1_SIMPLE(CPK_SIGNER_INFO, digest_algor, X509_ALGOR),
|
||||
ASN1_IMP_SEQUENCE_OF_OPT(CPK_SIGNER_INFO, signed_attr, X509_ATTRIBUTE, 0),
|
||||
ASN1_SIMPLE(CPK_SIGNER_INFO, sign_algor, X509_ALGOR),
|
||||
ASN1_SIMPLE(CPK_SIGNER_INFO, signature, ASN1_OCTET_STRING),
|
||||
ASN1_IMP_SET_OF_OPT(CPK_SIGNER_INFO, unsigned_attr, X509_ATTRIBUTE, 1)
|
||||
} ASN1_SEQUENCE_END(CPK_SIGNER_INFO)
|
||||
IMPLEMENT_ASN1_FUNCTIONS(CPK_SIGNER_INFO)
|
||||
IMPLEMENT_ASN1_DUP_FUNCTION(CPK_SIGNER_INFO)
|
||||
|
||||
|
||||
ASN1_NDEF_SEQUENCE(CPK_ENVELOPE) = {
|
||||
ASN1_SIMPLE(CPK_ENVELOPE, version, LONG),
|
||||
ASN1_SET_OF(CPK_ENVELOPE, recip_infos, CPK_RECIP_INFO),
|
||||
ASN1_SIMPLE(CPK_ENVELOPE, enc_data, CPK_ENC_CONTENT)
|
||||
} ASN1_NDEF_SEQUENCE_END(CPK_ENVELOPE)
|
||||
IMPLEMENT_ASN1_FUNCTIONS(CPK_ENVELOPE)
|
||||
|
||||
|
||||
ASN1_SEQUENCE(CPK_RECIP_INFO) = {
|
||||
ASN1_SIMPLE(CPK_RECIP_INFO, version, LONG),
|
||||
ASN1_SIMPLE(CPK_RECIP_INFO, recipient, X509_NAME),
|
||||
ASN1_SIMPLE(CPK_RECIP_INFO, enc_algor, X509_ALGOR),
|
||||
ASN1_SIMPLE(CPK_RECIP_INFO, enc_data, ASN1_OCTET_STRING)
|
||||
} ASN1_SEQUENCE_END(CPK_RECIP_INFO)
|
||||
IMPLEMENT_ASN1_FUNCTIONS(CPK_RECIP_INFO)
|
||||
IMPLEMENT_ASN1_DUP_FUNCTION(CPK_RECIP_INFO)
|
||||
|
||||
|
||||
ASN1_NDEF_SEQUENCE(CPK_ENC_CONTENT) = {
|
||||
ASN1_SIMPLE(CPK_ENC_CONTENT, content_type, ASN1_OBJECT),
|
||||
ASN1_SIMPLE(CPK_ENC_CONTENT, enc_algor, X509_ALGOR),
|
||||
ASN1_IMP_OPT(CPK_ENC_CONTENT, enc_data, ASN1_OCTET_STRING, 0)
|
||||
} ASN1_NDEF_SEQUENCE_END(CPK_ENC_CONTENT)
|
||||
IMPLEMENT_ASN1_FUNCTIONS(CPK_ENC_CONTENT)
|
||||
|
||||
|
||||
ASN1_NDEF_SEQUENCE(CPK_SIGN_ENVELOPE) = {
|
||||
ASN1_SIMPLE(CPK_SIGN_ENVELOPE, version, LONG),
|
||||
ASN1_SET_OF(CPK_SIGN_ENVELOPE, recip_infos, CPK_RECIP_INFO),
|
||||
ASN1_SET_OF(CPK_SIGN_ENVELOPE, digest_algors, X509_ALGOR),
|
||||
ASN1_SIMPLE(CPK_SIGN_ENVELOPE, enc_data, CPK_ENC_CONTENT),
|
||||
ASN1_IMP_SET_OF_OPT(CPK_SIGN_ENVELOPE, cert, X509, 0),
|
||||
ASN1_IMP_SET_OF_OPT(CPK_SIGN_ENVELOPE, crl, X509_CRL, 1),
|
||||
ASN1_SET_OF(CPK_SIGN_ENVELOPE, signer_infos, CPK_SIGNER_INFO)
|
||||
} ASN1_NDEF_SEQUENCE_END(CPK_SIGN_ENVELOPE)
|
||||
IMPLEMENT_ASN1_FUNCTIONS(CPK_SIGN_ENVELOPE)
|
||||
|
||||
75
crypto/cpk/cpk_err.c
Normal file
75
crypto/cpk/cpk_err.c
Normal file
@@ -0,0 +1,75 @@
|
||||
#include <openssl/err.h>
|
||||
#include "cpk.h"
|
||||
|
||||
/* BEGIN ERROR CODES */
|
||||
#ifndef OPENSSL_NO_ERR
|
||||
|
||||
#define ERR_FUNC(func) ERR_PACK(ERR_LIB_CPK,func,0)
|
||||
#define ERR_REASON(reason) ERR_PACK(ERR_LIB_CPK,0,reason)
|
||||
|
||||
static ERR_STRING_DATA CPK_str_functs[] =
|
||||
{
|
||||
{ERR_FUNC(CPK_F_CPK_MASTER_SECRET_CREATE), "CPK_MASTER_SECRET_create"},
|
||||
{ERR_FUNC(CPK_F_CPK_MASTER_SECRET_EXTRACT_PUBLIC_PARAMS), "CPK_MASTER_SECRET_extract_public_params"},
|
||||
{ERR_FUNC(CPK_F_CPK_MASTER_SECRET_EXTRACT_PRIVATE_KEY), "CPK_MASTER_SECRET_extract_private_key"},
|
||||
{ERR_FUNC(CPK_F_CPK_PUBLIC_PARAMS_EXTRACT_PUBLIC_KEY), "CPK_PUBLIC_PARAMS_extract_public_key"},
|
||||
{ERR_FUNC(CPK_F_CPK_PUBLIC_PARAMS_COMPUTE_SHARE_KEY), "CPK_PUBLIC_PARAMS_compute_share_key"},
|
||||
{ERR_FUNC(CPK_F_CPK_MASTER_SECRET_DIGEST), "CPK_F_CPK_MASTER_SECRET_digest"},
|
||||
{ERR_FUNC(CPK_F_CPK_PUBLIC_PARAMS_DIGEST), "CPK_F_CPK_PUBLIC_PARAMS_digest"},
|
||||
{ERR_FUNC(CPK_F_CPK_MASTER_SECRET_PRINT), "CPK_F_CPK_MASTER_SECRET_print"},
|
||||
{ERR_FUNC(CPK_F_CPK_PUBLIC_PARAMS_PRINT), "CPK_F_CPK_PUBLIC_PARAMS_print"},
|
||||
{ERR_FUNC(CPK_F_CPK_MASTER_SECRET_VALIDATE_PUBLIC_PARAMS), "CPK_F_CPK_MASTER_SECRET_validate_public_params"},
|
||||
{ERR_FUNC(CPK_F_CPK_PUBLIC_PARAMS_VALIDATE_PRIVATE_KEY), "CPK_F_CPK_PUBLIC_PARAMS_validate_private_key"},
|
||||
{ERR_FUNC(CPK_F_CPK_MAP_NEW_DEFAULT), "CPK_F_CPK_MAP_new_default"},
|
||||
{ERR_FUNC(CPK_F_CPK_MAP_NUM_FACTORS), "CPK_F_CPK_MAP_num_factors"},
|
||||
{ERR_FUNC(CPK_F_CPK_MAP_NUM_INDEXES), "CPK_F_CPK_MAP_num_indexes"},
|
||||
{ERR_FUNC(CPK_F_CPK_MAP_STR2INDEX), "CPK_F_CPK_MAP_STR2INDEX"},
|
||||
{ERR_FUNC(CPK_F_X509_ALGOR_GET1_EC_KEY), "X509_ALGOR_get1_ec_key"},
|
||||
{ERR_FUNC(CPK_F_X509_ALGOR_GET1_DSA), "X509_ALGOR_get1_dsa"},
|
||||
{0, NULL}
|
||||
};
|
||||
|
||||
static ERR_STRING_DATA CPK_str_reasons[] =
|
||||
{
|
||||
{ERR_REASON(CPK_R_BAD_ARGUMENT), "bad argument"},
|
||||
{ERR_REASON(CPK_R_UNKNOWN_DIGEST_TYPE), "unknown digest algorithm"},
|
||||
{ERR_REASON(CPK_R_UNKNOWN_CIPHER_TYPE), "unknown cipher algorithm"},
|
||||
{ERR_REASON(CPK_R_UNKNOWN_MAP_TYPE), "unknown cpk map algorithm"},
|
||||
{ERR_REASON(CPK_R_UNKNOWN_CURVE), "unknown elliptic curve"},
|
||||
{ERR_REASON(CPK_R_STACK_ERROR), "stack error"},
|
||||
{ERR_REASON(CPK_R_DERIVE_KEY_FAILED), "derive key failed"},
|
||||
{ERR_REASON(CPK_R_ECIES_ENCRYPT_FAILED), "ecies encryption failed"},
|
||||
{ERR_REASON(CPK_R_ECIES_DECRYPT_FAILED), "ecies decryption failed"},
|
||||
{ERR_REASON(CPK_R_DER_DECODE_FAILED), "DER decode failed"},
|
||||
{ERR_REASON(CPK_R_UNSUPPORTED_PKCS7_CONTENT_TYPE),"CPK_R_UNSUPPORTED_PKCS7_CONTENT_TYPE"},
|
||||
{ERR_REASON(CPK_R_SET_SIGNER), "CPK_R_SET_SIGNER"},
|
||||
{ERR_REASON(CPK_R_SET_RECIP_INFO), "CPK_R_SET_RECIP_INFO"},
|
||||
{ERR_REASON(CPK_R_UNABLE_TO_FIND_MESSAGE_DIGEST),"CPK_R_UNABLE_TO_FIND_MESSAGE_DIGEST"},
|
||||
{ERR_REASON(CPK_R_BAD_DATA), "bad data"},
|
||||
{ERR_REASON(CPK_R_MAP_FAILED), "CPK_R_MAP_FAILED"},
|
||||
{ERR_REASON(CPK_R_ADD_SIGNING_TIME), "CPK_R_ADD_SIGNING_TIME"},
|
||||
{ERR_REASON(CPK_R_VERIFY_FAILED), "CPK_R_VERIFY_FAILED"},
|
||||
{ERR_REASON(CPK_R_UNKNOWN_ECDH_TYPE), "CPK_R_UNKNOWN_ECDH_TYPE"},
|
||||
{ERR_REASON(CPK_R_DIGEST_FAILED), "CPK_R_DIGEST_FAILED"},
|
||||
{ERR_REASON(CPK_R_WITHOUT_DECRYPT_KEY), "CPK_R_WITHOUT_DECRYPT_KEY"},
|
||||
{ERR_REASON(CPK_R_UNKNOWN_PKCS7_TYPE), "CPK_R_UNKNOWN_PKCS7_TYPE"},
|
||||
{ERR_REASON(CPK_R_INVALID_ID_LENGTH), "invalid identity length"},
|
||||
{ERR_REASON(CPK_R_INVALID_PKEY_TYPE), "invalid public key type"},
|
||||
{ERR_REASON(CPK_R_INVALID_MAP_ALGOR), "invalid map algorithm"},
|
||||
{ERR_REASON(CPK_R_PKEY_TYPE_NOT_MATCH), "public key type not match"},
|
||||
{0, NULL}
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
void ERR_load_CPK_strings(void)
|
||||
{
|
||||
#ifndef OPENSSL_NO_ERR
|
||||
|
||||
if (ERR_func_error_string(CPK_str_functs[0].error) == NULL) {
|
||||
ERR_load_strings(0, CPK_str_functs);
|
||||
ERR_load_strings(0, CPK_str_reasons);
|
||||
}
|
||||
|
||||
#endif
|
||||
}
|
||||
46
crypto/cpk/cpk_kap.c
Normal file
46
crypto/cpk/cpk_kap.c
Normal file
@@ -0,0 +1,46 @@
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/ecdh.h>
|
||||
#include <openssl/objects.h>
|
||||
#include "cpk.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;
|
||||
}
|
||||
|
||||
1097
crypto/cpk/cpk_lib.c
Normal file
1097
crypto/cpk/cpk_lib.c
Normal file
File diff suppressed because it is too large
Load Diff
128
crypto/cpk/cpk_map.c
Normal file
128
crypto/cpk/cpk_map.c
Normal file
@@ -0,0 +1,128 @@
|
||||
#include <string.h>
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/objects.h>
|
||||
#include "cpk.h"
|
||||
|
||||
X509_ALGOR *CPK_MAP_new_default()
|
||||
{
|
||||
X509_ALGOR *algor = NULL;
|
||||
const EVP_MD *md = EVP_sha1();
|
||||
|
||||
if (md != EVP_sha1() && md != EVP_sha384()) {
|
||||
CPKerr(CPK_F_CPK_MAP_NEW_DEFAULT, CPK_R_BAD_ARGUMENT);
|
||||
goto end;
|
||||
}
|
||||
if (!(algor = X509_ALGOR_new())) {
|
||||
CPKerr(CPK_F_CPK_MAP_NEW_DEFAULT, 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);
|
||||
goto end;
|
||||
}
|
||||
end:
|
||||
return algor;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
int CPK_MAP_num_indexes(const X509_ALGOR *algor)
|
||||
{
|
||||
return 32;
|
||||
}
|
||||
|
||||
int CPK_MAP_num_index(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;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int CPK_MAP_str2index(const X509_ALGOR *algor, const char *str, int *index)
|
||||
{
|
||||
int ret = 0;
|
||||
const EVP_MD *md;
|
||||
unsigned char dgst[EVP_MAX_MD_SIZE];
|
||||
unsigned int dgstlen;
|
||||
BIGNUM *bn = NULL;
|
||||
int i, num_index, num_subset;
|
||||
|
||||
OPENSSL_assert(algor);
|
||||
OPENSSL_assert(algor->algorithm);
|
||||
OPENSSL_assert(str);
|
||||
OPENSSL_assert(strlen(str) > 0);
|
||||
|
||||
if (!CPK_MAP_is_valid(algor)) {
|
||||
CPKerr(CPK_F_CPK_MAP_STR2INDEX, CPK_R_INVALID_MAP_ALGOR);
|
||||
goto err;
|
||||
}
|
||||
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;
|
||||
}
|
||||
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;
|
||||
}
|
||||
if (!BN_bin2bn(dgst, dgstlen, bn)) {
|
||||
CPKerr(CPK_F_CPK_MAP_STR2INDEX, ERR_R_BN_LIB);
|
||||
goto err;
|
||||
}
|
||||
num_index = CPK_MAP_num_index(algor);
|
||||
num_subset = CPK_MAP_num_subset(algor);
|
||||
|
||||
for (i = 0; i < num_index; i++) {
|
||||
int r = BN_mod_word(bn, num_subset);
|
||||
index[i] = num_subset * i + r;
|
||||
}
|
||||
ret = num_index;
|
||||
err:
|
||||
if (bn) BN_free(bn);
|
||||
return ret;
|
||||
}
|
||||
|
||||
20
crypto/cpk/cpk_prn.c
Normal file
20
crypto/cpk/cpk_prn.c
Normal file
@@ -0,0 +1,20 @@
|
||||
#include <string.h>
|
||||
#include <openssl/bio.h>
|
||||
#include "cpk.h"
|
||||
|
||||
int CPK_MASTER_SECRET_print(BIO *out, CPK_MASTER_SECRET *master,
|
||||
int indent, unsigned long flags)
|
||||
{
|
||||
|
||||
BIO_printf(out, "%s() not implemented\n", __FUNCTION__);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int CPK_PUBLIC_PARAMS_print(BIO *out, CPK_PUBLIC_PARAMS *params,
|
||||
int indent, unsigned long flags)
|
||||
{
|
||||
BIO_printf(out, "%s() not implemented\n", __FUNCTION__);
|
||||
return 1;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user