📄 crypto.c
字号:
{ return &_hx509_signature_rsa_with_md5_data; }const AlgorithmIdentifier *hx509_signature_rsa_with_md2(void){ return &_hx509_signature_rsa_with_md2_data; }const AlgorithmIdentifier *hx509_signature_rsa(void){ return &_hx509_signature_rsa_data; }const AlgorithmIdentifier *hx509_signature_rsa_pkcs1_x509(void){ return &_hx509_signature_rsa_pkcs1_x509_data; }const AlgorithmIdentifier *hx509_crypto_des_rsdi_ede3_cbc(void){ return &_hx509_des_rsdi_ede3_cbc_oid; }const AlgorithmIdentifier *hx509_crypto_aes128_cbc(void){ return &_hx509_crypto_aes128_cbc_data; }const AlgorithmIdentifier *hx509_crypto_aes256_cbc(void){ return &_hx509_crypto_aes256_cbc_data; }/* * */const AlgorithmIdentifier * _hx509_crypto_default_sig_alg = &_hx509_signature_rsa_with_sha1_data;const AlgorithmIdentifier * _hx509_crypto_default_digest_alg = &_hx509_signature_sha1_data;const AlgorithmIdentifier * _hx509_crypto_default_secret_alg = &_hx509_crypto_aes128_cbc_data;/* * */int_hx509_private_key_init(hx509_private_key *key, hx509_private_key_ops *ops, void *keydata){ *key = calloc(1, sizeof(**key)); if (*key == NULL) return ENOMEM; (*key)->ref = 1; (*key)->ops = ops; (*key)->private_key.keydata = keydata; return 0;}hx509_private_key_hx509_private_key_ref(hx509_private_key key){ if (key->ref <= 0) _hx509_abort("refcount <= 0"); key->ref++; if (key->ref == 0) _hx509_abort("refcount == 0"); return key;}const char *_hx509_private_pem_name(hx509_private_key key){ return key->ops->pemtype;}int_hx509_private_key_free(hx509_private_key *key){ if (key == NULL || *key == NULL) return 0; if ((*key)->ref <= 0) _hx509_abort("refcount <= 0"); if (--(*key)->ref > 0) return 0; if ((*key)->private_key.rsa) RSA_free((*key)->private_key.rsa); (*key)->private_key.rsa = NULL; free(*key); *key = NULL; return 0;}void_hx509_private_key_assign_rsa(hx509_private_key key, void *ptr){ if (key->private_key.rsa) RSA_free(key->private_key.rsa); key->private_key.rsa = ptr; key->signature_alg = oid_id_pkcs1_sha1WithRSAEncryption(); key->md = &pkcs1_rsa_sha1_alg;}int _hx509_private_key_oid(hx509_context context, const hx509_private_key key, heim_oid *data){ int ret; ret = der_copy_oid((*key->ops->key_oid)(), data); if (ret) hx509_set_error_string(context, 0, ret, "malloc out of memory"); return ret;}int_hx509_private_key_exportable(hx509_private_key key){ if (key->ops->export == NULL) return 0; return 1;}BIGNUM *_hx509_private_key_get_internal(hx509_context context, hx509_private_key key, const char *type){ if (key->ops->get_internal == NULL) return NULL; return (*key->ops->get_internal)(context, key, type);}int _hx509_private_key_export(hx509_context context, const hx509_private_key key, heim_octet_string *data){ if (key->ops->export == NULL) { hx509_clear_error_string(context); return HX509_UNIMPLEMENTED_OPERATION; } return (*key->ops->export)(context, key, data);}/* * */struct hx509cipher { const char *name; const heim_oid *(*oid_func)(void); const AlgorithmIdentifier *(*ai_func)(void); const EVP_CIPHER *(*evp_func)(void); int (*get_params)(hx509_context, const hx509_crypto, const heim_octet_string *, heim_octet_string *); int (*set_params)(hx509_context, const heim_octet_string *, hx509_crypto, heim_octet_string *);};struct hx509_crypto_data { char *name; const struct hx509cipher *cipher; const EVP_CIPHER *c; heim_octet_string key; heim_oid oid; void *param;};/* * */static const heim_oid *oid_private_rc2_40(void){ static unsigned oid_data[] = { 127, 1 }; static const heim_oid oid = { 2, oid_data }; return &oid;}/* * */static intCMSCBCParam_get(hx509_context context, const hx509_crypto crypto, const heim_octet_string *ivec, heim_octet_string *param){ size_t size; int ret; assert(crypto->param == NULL); if (ivec == NULL) return 0; ASN1_MALLOC_ENCODE(CMSCBCParameter, param->data, param->length, ivec, &size, ret); if (ret == 0 && size != param->length) _hx509_abort("Internal asn1 encoder failure"); if (ret) hx509_clear_error_string(context); return ret;}static intCMSCBCParam_set(hx509_context context, const heim_octet_string *param, hx509_crypto crypto, heim_octet_string *ivec){ int ret; if (ivec == NULL) return 0; ret = decode_CMSCBCParameter(param->data, param->length, ivec, NULL); if (ret) hx509_clear_error_string(context); return ret;}struct _RC2_params { int maximum_effective_key;};static intCMSRC2CBCParam_get(hx509_context context, const hx509_crypto crypto, const heim_octet_string *ivec, heim_octet_string *param){ CMSRC2CBCParameter rc2params; const struct _RC2_params *p = crypto->param; int maximum_effective_key = 128; size_t size; int ret; memset(&rc2params, 0, sizeof(rc2params)); if (p) maximum_effective_key = p->maximum_effective_key; switch(maximum_effective_key) { case 40: rc2params.rc2ParameterVersion = 160; break; case 64: rc2params.rc2ParameterVersion = 120; break; case 128: rc2params.rc2ParameterVersion = 58; break; } rc2params.iv = *ivec; ASN1_MALLOC_ENCODE(CMSRC2CBCParameter, param->data, param->length, &rc2params, &size, ret); if (ret == 0 && size != param->length) _hx509_abort("Internal asn1 encoder failure"); return ret;}static intCMSRC2CBCParam_set(hx509_context context, const heim_octet_string *param, hx509_crypto crypto, heim_octet_string *ivec){ CMSRC2CBCParameter rc2param; struct _RC2_params *p; size_t size; int ret; ret = decode_CMSRC2CBCParameter(param->data, param->length, &rc2param, &size); if (ret) { hx509_clear_error_string(context); return ret; } p = calloc(1, sizeof(*p)); if (p == NULL) { free_CMSRC2CBCParameter(&rc2param); hx509_clear_error_string(context); return ENOMEM; } switch(rc2param.rc2ParameterVersion) { case 160: crypto->c = EVP_rc2_40_cbc(); p->maximum_effective_key = 40; break; case 120: crypto->c = EVP_rc2_64_cbc(); p->maximum_effective_key = 64; break; case 58: crypto->c = EVP_rc2_cbc(); p->maximum_effective_key = 128; break; default: free(p); free_CMSRC2CBCParameter(&rc2param); return HX509_CRYPTO_SIG_INVALID_FORMAT; } if (ivec) ret = der_copy_octet_string(&rc2param.iv, ivec); free_CMSRC2CBCParameter(&rc2param); if (ret) { free(p); hx509_clear_error_string(context); } else crypto->param = p; return ret;}/* * */static const struct hx509cipher ciphers[] = { { "rc2-cbc", oid_id_pkcs3_rc2_cbc, NULL, EVP_rc2_cbc, CMSRC2CBCParam_get, CMSRC2CBCParam_set }, { "rc2-cbc", oid_id_rsadsi_rc2_cbc, NULL, EVP_rc2_cbc, CMSRC2CBCParam_get, CMSRC2CBCParam_set }, { "rc2-40-cbc", oid_private_rc2_40, NULL, EVP_rc2_40_cbc, CMSRC2CBCParam_get, CMSRC2CBCParam_set }, { "des-ede3-cbc", oid_id_pkcs3_des_ede3_cbc, NULL, EVP_des_ede3_cbc, CMSCBCParam_get, CMSCBCParam_set }, { "des-ede3-cbc", oid_id_rsadsi_des_ede3_cbc, hx509_crypto_des_rsdi_ede3_cbc, EVP_des_ede3_cbc, CMSCBCParam_get, CMSCBCParam_set }, { "aes-128-cbc", oid_id_aes_128_cbc, hx509_crypto_aes128_cbc, EVP_aes_128_cbc, CMSCBCParam_get, CMSCBCParam_set }, { "aes-192-cbc", oid_id_aes_192_cbc, NULL, EVP_aes_192_cbc, CMSCBCParam_get, CMSCBCParam_set }, { "aes-256-cbc", oid_id_aes_256_cbc, hx509_crypto_aes256_cbc, EVP_aes_256_cbc, CMSCBCParam_get, CMSCBCParam_set }};static const struct hx509cipher *find_cipher_by_oid(const heim_oid *oid){ int i; for (i = 0; i < sizeof(ciphers)/sizeof(ciphers[0]); i++) if (der_heim_oid_cmp(oid, (*ciphers[i].oid_func)()) == 0) return &ciphers[i]; return NULL;}static const struct hx509cipher *find_cipher_by_name(const char *name){ int i; for (i = 0; i < sizeof(ciphers)/sizeof(ciphers[0]); i++) if (strcasecmp(name, ciphers[i].name) == 0) return &ciphers[i]; return NULL;}const heim_oid *hx509_crypto_enctype_by_name(const char *name){ const struct hx509cipher *cipher; cipher = find_cipher_by_name(name); if (cipher == NULL) return NULL; return (*cipher->oid_func)();}inthx509_crypto_init(hx509_context context, const char *provider, const heim_oid *enctype, hx509_crypto *crypto){ const struct hx509cipher *cipher; *crypto = NULL; cipher = find_cipher_by_oid(enctype); if (cipher == NULL) { hx509_set_error_string(context, 0, HX509_ALG_NOT_SUPP, "Algorithm not supported"); return HX509_ALG_NOT_SUPP; } *crypto = calloc(1, sizeof(**crypto)); if (*crypto == NULL) { hx509_clear_error_string(context); return ENOMEM; } (*crypto)->cipher = cipher; (*crypto)->c = (*cipher->evp_func)(); if (der_copy_oid(enctype, &(*crypto)->oid)) { hx509_crypto_destroy(*crypto); *crypto = NULL; hx509_clear_error_string(context); return ENOMEM; } return 0;}const char *hx509_crypto_provider(hx509_crypto crypto){ return "unknown";}voidhx509_crypto_destroy(hx509_crypto crypto){ if (crypto->name) free(crypto->name); if (crypto->key.data) free(crypto->key.data); if (crypto->param) free(crypto->param); der_free_oid(&crypto->oid); memset(crypto, 0, sizeof(*crypto)); free(crypto);}inthx509_crypto_set_key_name(hx509_crypto crypto, const char *name){ return 0;}inthx509_crypto_set_key_data(hx509_crypto crypto, const void *data, size_t length){ if (EVP_CIPHER_key_length(crypto->c) > length) return HX509_CRYPTO_INTERNAL_ERROR; if (crypto->key.data) { free(crypto->key.data); crypto->key.data = NULL; crypto->key.length = 0; } crypto->key.data = malloc(length); if (crypto->key.data == NULL) return ENOMEM; memcpy(crypto->key.data, data, length); crypto->key.length = length; return 0;}inthx509_crypto_set_random_key(hx509_crypto crypto, heim_octet_string *key){ if (crypto->key.data) { free(crypto->key.data); crypto->key.length = 0; } crypto->key.length = EVP_CIPHER_key_length(crypto->c); crypto->key.data = malloc(crypto->key.length); if (crypto->key.data == NULL) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -