📄 str_lib.c
字号:
parameters)) { STOREerr(STORE_F_STORE_DELETE_ARBITRARY, STORE_R_FAILED_DELETING_ARBITRARY); return 0; } return 1; }STORE_OBJECT *STORE_OBJECT_new(void) { STORE_OBJECT *object = OPENSSL_malloc(sizeof(STORE_OBJECT)); if (object) memset(object, 0, sizeof(STORE_OBJECT)); return object; }void STORE_OBJECT_free(STORE_OBJECT *data) { if (!data) return; switch (data->type) { case STORE_OBJECT_TYPE_X509_CERTIFICATE: X509_free(data->data.x509.certificate); break; case STORE_OBJECT_TYPE_X509_CRL: X509_CRL_free(data->data.crl); break; case STORE_OBJECT_TYPE_PRIVATE_KEY: case STORE_OBJECT_TYPE_PUBLIC_KEY: EVP_PKEY_free(data->data.key); break; case STORE_OBJECT_TYPE_NUMBER: BN_free(data->data.number); break; case STORE_OBJECT_TYPE_ARBITRARY: BUF_MEM_free(data->data.arbitrary); break; } OPENSSL_free(data); }IMPLEMENT_STACK_OF(STORE_OBJECT*)struct STORE_attr_info_st { unsigned char set[(STORE_ATTR_TYPE_NUM + 8) / 8]; union { char *cstring; unsigned char *sha1string; X509_NAME *dn; BIGNUM *number; void *any; } values[STORE_ATTR_TYPE_NUM+1]; size_t value_sizes[STORE_ATTR_TYPE_NUM+1]; };#define ATTR_IS_SET(a,i) ((i) > 0 && (i) < STORE_ATTR_TYPE_NUM \ && ((a)->set[(i) / 8] & (1 << ((i) % 8))))#define SET_ATTRBIT(a,i) ((a)->set[(i) / 8] |= (1 << ((i) % 8)))#define CLEAR_ATTRBIT(a,i) ((a)->set[(i) / 8] &= ~(1 << ((i) % 8)))STORE_ATTR_INFO *STORE_ATTR_INFO_new(void) { return (STORE_ATTR_INFO *)OPENSSL_malloc(sizeof(STORE_ATTR_INFO)); }static void STORE_ATTR_INFO_attr_free(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code) { if (ATTR_IS_SET(attrs,code)) { switch(code) { case STORE_ATTR_FRIENDLYNAME: case STORE_ATTR_EMAIL: case STORE_ATTR_FILENAME: STORE_ATTR_INFO_modify_cstr(attrs, code, NULL, 0); break; case STORE_ATTR_KEYID: case STORE_ATTR_ISSUERKEYID: case STORE_ATTR_SUBJECTKEYID: case STORE_ATTR_ISSUERSERIALHASH: case STORE_ATTR_CERTHASH: STORE_ATTR_INFO_modify_sha1str(attrs, code, NULL, 0); break; case STORE_ATTR_ISSUER: case STORE_ATTR_SUBJECT: STORE_ATTR_INFO_modify_dn(attrs, code, NULL); break; case STORE_ATTR_SERIAL: STORE_ATTR_INFO_modify_number(attrs, code, NULL); break; default: break; } } }int STORE_ATTR_INFO_free(STORE_ATTR_INFO *attrs) { if (attrs) { STORE_ATTR_TYPES i; for(i = 0; i++ < STORE_ATTR_TYPE_NUM;) STORE_ATTR_INFO_attr_free(attrs, i); OPENSSL_free(attrs); } return 1; }char *STORE_ATTR_INFO_get0_cstr(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code) { if (!attrs) { STOREerr(STORE_F_STORE_ATTR_INFO_GET0_CSTR, ERR_R_PASSED_NULL_PARAMETER); return NULL; } if (ATTR_IS_SET(attrs,code)) return attrs->values[code].cstring; STOREerr(STORE_F_STORE_ATTR_INFO_GET0_CSTR, STORE_R_NO_VALUE); return NULL; }unsigned char *STORE_ATTR_INFO_get0_sha1str(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code) { if (!attrs) { STOREerr(STORE_F_STORE_ATTR_INFO_GET0_SHA1STR, ERR_R_PASSED_NULL_PARAMETER); return NULL; } if (ATTR_IS_SET(attrs,code)) return attrs->values[code].sha1string; STOREerr(STORE_F_STORE_ATTR_INFO_GET0_SHA1STR, STORE_R_NO_VALUE); return NULL; }X509_NAME *STORE_ATTR_INFO_get0_dn(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code) { if (!attrs) { STOREerr(STORE_F_STORE_ATTR_INFO_GET0_DN, ERR_R_PASSED_NULL_PARAMETER); return NULL; } if (ATTR_IS_SET(attrs,code)) return attrs->values[code].dn; STOREerr(STORE_F_STORE_ATTR_INFO_GET0_DN, STORE_R_NO_VALUE); return NULL; }BIGNUM *STORE_ATTR_INFO_get0_number(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code) { if (!attrs) { STOREerr(STORE_F_STORE_ATTR_INFO_GET0_NUMBER, ERR_R_PASSED_NULL_PARAMETER); return NULL; } if (ATTR_IS_SET(attrs,code)) return attrs->values[code].number; STOREerr(STORE_F_STORE_ATTR_INFO_GET0_NUMBER, STORE_R_NO_VALUE); return NULL; }int STORE_ATTR_INFO_set_cstr(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code, char *cstr, size_t cstr_size) { if (!attrs) { STOREerr(STORE_F_STORE_ATTR_INFO_SET_CSTR, ERR_R_PASSED_NULL_PARAMETER); return 0; } if (!ATTR_IS_SET(attrs,code)) { if ((attrs->values[code].cstring = BUF_strndup(cstr, cstr_size))) return 1; STOREerr(STORE_F_STORE_ATTR_INFO_SET_CSTR, ERR_R_MALLOC_FAILURE); return 0; } STOREerr(STORE_F_STORE_ATTR_INFO_SET_CSTR, STORE_R_ALREADY_HAS_A_VALUE); return 0; }int STORE_ATTR_INFO_set_sha1str(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code, unsigned char *sha1str, size_t sha1str_size) { if (!attrs) { STOREerr(STORE_F_STORE_ATTR_INFO_SET_SHA1STR, ERR_R_PASSED_NULL_PARAMETER); return 0; } if (!ATTR_IS_SET(attrs,code)) { if ((attrs->values[code].sha1string = (unsigned char *)BUF_memdup(sha1str, sha1str_size))) return 1; STOREerr(STORE_F_STORE_ATTR_INFO_SET_SHA1STR, ERR_R_MALLOC_FAILURE); return 0; } STOREerr(STORE_F_STORE_ATTR_INFO_SET_SHA1STR, STORE_R_ALREADY_HAS_A_VALUE); return 0; }int STORE_ATTR_INFO_set_dn(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code, X509_NAME *dn) { if (!attrs) { STOREerr(STORE_F_STORE_ATTR_INFO_SET_DN, ERR_R_PASSED_NULL_PARAMETER); return 0; } if (!ATTR_IS_SET(attrs,code)) { if ((attrs->values[code].dn = X509_NAME_dup(dn))) return 1; STOREerr(STORE_F_STORE_ATTR_INFO_SET_DN, ERR_R_MALLOC_FAILURE); return 0; } STOREerr(STORE_F_STORE_ATTR_INFO_SET_DN, STORE_R_ALREADY_HAS_A_VALUE); return 0; }int STORE_ATTR_INFO_set_number(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code, BIGNUM *number) { if (!attrs) { STOREerr(STORE_F_STORE_ATTR_INFO_SET_NUMBER, ERR_R_PASSED_NULL_PARAMETER); return 0; } if (!ATTR_IS_SET(attrs,code)) { if ((attrs->values[code].number = BN_dup(number))) return 1; STOREerr(STORE_F_STORE_ATTR_INFO_SET_NUMBER, ERR_R_MALLOC_FAILURE); return 0; } STOREerr(STORE_F_STORE_ATTR_INFO_SET_NUMBER, STORE_R_ALREADY_HAS_A_VALUE); return 0; }int STORE_ATTR_INFO_modify_cstr(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code, char *cstr, size_t cstr_size) { if (!attrs) { STOREerr(STORE_F_STORE_ATTR_INFO_MODIFY_CSTR, ERR_R_PASSED_NULL_PARAMETER); return 0; } if (ATTR_IS_SET(attrs,code)) { OPENSSL_free(attrs->values[code].cstring); attrs->values[code].cstring = NULL; CLEAR_ATTRBIT(attrs, code); } return STORE_ATTR_INFO_set_cstr(attrs, code, cstr, cstr_size); }int STORE_ATTR_INFO_modify_sha1str(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code, unsigned char *sha1str, size_t sha1str_size) { if (!attrs) { STOREerr(STORE_F_STORE_ATTR_INFO_MODIFY_SHA1STR, ERR_R_PASSED_NULL_PARAMETER); return 0; } if (ATTR_IS_SET(attrs,code)) { OPENSSL_free(attrs->values[code].sha1string); attrs->values[code].sha1string = NULL; CLEAR_ATTRBIT(attrs, code); } return STORE_ATTR_INFO_set_sha1str(attrs, code, sha1str, sha1str_size); }int STORE_ATTR_INFO_modify_dn(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code, X509_NAME *dn) { if (!attrs) { STOREerr(STORE_F_STORE_ATTR_INFO_MODIFY_DN, ERR_R_PASSED_NULL_PARAMETER); return 0; } if (ATTR_IS_SET(attrs,code)) { OPENSSL_free(attrs->values[code].dn); attrs->values[code].dn = NULL; CLEAR_ATTRBIT(attrs, code); } return STORE_ATTR_INFO_set_dn(attrs, code, dn); }int STORE_ATTR_INFO_modify_number(STORE_ATTR_INFO *attrs, STORE_ATTR_TYPES code, BIGNUM *number) { if (!attrs) { STOREerr(STORE_F_STORE_ATTR_INFO_MODIFY_NUMBER, ERR_R_PASSED_NULL_PARAMETER); return 0; } if (ATTR_IS_SET(attrs,code)) { OPENSSL_free(attrs->values[code].number); attrs->values[code].number = NULL; CLEAR_ATTRBIT(attrs, code); } return STORE_ATTR_INFO_set_number(attrs, code, number); }struct attr_list_ctx_st { OPENSSL_ITEM *attributes; };void *STORE_parse_attrs_start(OPENSSL_ITEM *attributes) { if (attributes) { struct attr_list_ctx_st *context = (struct attr_list_ctx_st *)OPENSSL_malloc(sizeof(struct attr_list_ctx_st)); if (context) context->attributes = attributes; else STOREerr(STORE_F_STORE_PARSE_ATTRS_START, ERR_R_MALLOC_FAILURE); return context; } STOREerr(STORE_F_STORE_PARSE_ATTRS_START, ERR_R_PASSED_NULL_PARAMETER); return 0; }STORE_ATTR_INFO *STORE_parse_attrs_next(void *handle) { struct attr_list_ctx_st *context = (struct attr_list_ctx_st *)handle; if (context && context->attributes) { STORE_ATTR_INFO *attrs = NULL; while(context->attributes && context->attributes->code != STORE_ATTR_OR && context->attributes->code != STORE_ATTR_END) { switch(context->attributes->code) { case STORE_ATTR_FRIENDLYNAME: case STORE_ATTR_EMAIL: case STORE_ATTR_FILENAME: if (!attrs) attrs = STORE_ATTR_INFO_new(); if (attrs == NULL) { STOREerr(STORE_F_STORE_PARSE_ATTRS_NEXT, ERR_R_MALLOC_FAILURE); goto err; } STORE_ATTR_INFO_set_cstr(attrs, context->attributes->code, context->attributes->value, context->attributes->value_size); break; case STORE_ATTR_KEYID: case STORE_ATTR_ISSUERKEYID: case STORE_ATTR_SUBJECTKEYID: case STORE_ATTR_ISSUERSERIALHASH: case STORE_ATTR_CERTHASH: if (!attrs) attrs = STORE_ATTR_INFO_new(); if (attrs == NULL) { STOREerr(STORE_F_STORE_PARSE_ATTRS_NEXT, ERR_R_MALLOC_FAILURE); goto err; } STORE_ATTR_INFO_set_sha1str(attrs, context->attributes->code, context->attributes->value, context->attributes->value_size); break; case STORE_ATTR_ISSUER: case STORE_ATTR_SUBJECT: if (!attrs) attrs = STORE_ATTR_INFO_new(); if (attrs == NULL) { STOREerr(STORE_F_STORE_PARSE_ATTRS_NEXT, ERR_R_MALLOC_FAILURE); goto err; } STORE_ATTR_INFO_modify_dn(attrs, context->attributes->code, context->attributes->value); break; case STORE_ATTR_SERIAL: if (!attrs) attrs = STORE_ATTR_INFO_new(); if (attrs == NULL) { STOREerr(STORE_F_STORE_PARSE_ATTRS_NEXT, ERR_R_MALLOC_FAILURE); goto err; } STORE_ATTR_INFO_modify_number(attrs, context->attributes->code, context->attributes->value); break; } context->attributes++; } if (context->attributes->code == STORE_ATTR_OR) context->attributes++; return attrs; err: while(context->attributes && context->attributes->code != STORE_ATTR_OR && context->attributes->code != STORE_ATTR_END) context->attributes++; if (context->attributes->code == STORE_ATTR_OR) context->attributes++; return NULL; } STOREerr(STORE_F_STORE_PARSE_ATTRS_NEXT, ERR_R_PASSED_NULL_PARAMETER); return NULL; }int STORE_parse_attrs_end(void *handle) { struct attr_list_ctx_st *context = (struct attr_list_ctx_st *)handle; if (context && context->attributes) {#if 0 OPENSSL_ITEM *attributes = context->attributes;#endif OPENSSL_free(context); return 1; } STOREerr(STORE_F_STORE_PARSE_ATTRS_END, ERR_R_PASSED_NULL_PARAMETER); return 0; }int STORE_parse_attrs_endp(void *handle) { struct attr_list_ctx_st *context = (struct attr_list_ctx_st *)handle; if (context && context->attributes) { return context->attributes->code == STORE_ATTR_END; } STOREerr(STORE_F_STORE_PARSE_ATTRS_ENDP, ERR_R_PASSED_NULL_PARAMETER); return 0; }static int attr_info_compare_compute_range( unsigned char *abits, unsigned char *bbits, unsigned int *alowp, unsigned int *ahighp, unsigned int *blowp, unsigned int *bhighp) { unsigned int alow = (unsigned int)-1, ahigh = 0; unsigned int blow = (unsigned int)-1, bhigh = 0; int i, res = 0; for (i = 0; i < (STORE_ATTR_TYPE_NUM + 8) / 8; i++, abits++, bbits++) { if (res == 0) { if (*abits < *bbits) res = -1; if (*abits > *bbits) res = 1; } if (*abits) { if (alow == (unsigned int)-1) { alow = i * 8; if (!(*abits & 0x01)) alow++; if (!(*abits & 0x02)) alow++; if (!(*abits & 0x04)) alow++; if (!(*abits & 0x08)) alow++; if (!(*abits & 0x10)) alow++; if (!(*abits & 0x20)) alow++; if (!(*abits & 0x40)) alow++; } ahigh = i * 8 + 7; if (!(*abits & 0x80)) ahigh++; if (!(*abits & 0x40)) ahigh++; if (!(*abits & 0x20)) ahigh++; if (!(*abits & 0x10)) ahigh++; if (!(*abits & 0x08)) ahigh++; if (!(*abits & 0x04)) ahigh++; if (!(*abits & 0x02)) ahigh++; } if (*bbits) { if (blow == (unsigned int)-1) { blow = i * 8; if (!(*bbits & 0x01)) blow++; if (!(*bbits & 0x02)) blow++; if (!(*bbits & 0x04)) blow++; if (!(*bbits & 0x08)) blow++; if (!(*bbits & 0x10)) blow++; if (!(*bbits & 0x20)) blow++; if (!(*bbits & 0x40)) blow++; } bhigh = i * 8 + 7; if (!(*bbits & 0x80)) bhigh++; if (!(*bbits & 0x40)) bhigh++; if (!(*bbits & 0x20)) bhigh++; if (!(*bbits & 0x10)) bhigh++; if (!(*bbits & 0x08)) bhigh++; if (!(*bbits & 0x04)) bhigh++; if (!(*bbits & 0x02)) bhigh++; } } if (ahigh + alow < bhigh + blow) res = -1; if (ahigh + alow > bhigh + blow) res = 1; if (alowp) *alowp = alow; if (ahighp) *ahighp = ahigh; if (blowp) *blowp = blow; if (bhighp) *bhighp = bhigh; return res; }int STORE_ATTR_INFO_compare(STORE_ATTR_INFO *a, STORE_ATTR_INFO *b) { if (a == b) return 0; if (!a) return -1; if (!b) return 1; return attr_info_compare_compute_range(a->set, b->set, 0, 0, 0, 0); }int STORE_ATTR_INFO_in_range(STORE_ATTR_INFO *a, STORE_ATTR_INFO *b) { unsigned int alow, ahigh, blow, bhigh; if (a == b) return 1; if (!a) return 0; if (!b) return 0; attr_info_compare_compute_range(a->set, b->set, &alow, &ahigh, &blow, &bhigh); if (alow >= blow && ahigh <= bhigh) return 1; return 0; }int STORE_ATTR_INFO_in(STORE_ATTR_INFO *a, STORE_ATTR_INFO *b) { unsigned char *abits, *bbits; int i; if (a == b) return 1; if (!a) return 0; if (!b) return 0; abits = a->set; bbits = b->set; for (i = 0; i < (STORE_ATTR_TYPE_NUM + 8) / 8; i++, abits++, bbits++) { if (*abits && (*bbits & *abits) != *abits) return 0; } return 1; }int STORE_ATTR_INFO_in_ex(STORE_ATTR_INFO *a, STORE_ATTR_INFO *b) { STORE_ATTR_TYPES i; if (a == b) return 1; if (!STORE_ATTR_INFO_in(a, b)) return 0; for (i = 1; i < STORE_ATTR_TYPE_NUM; i++) if (ATTR_IS_SET(a, i)) { switch(i) { case STORE_ATTR_FRIENDLYNAME: case STORE_ATTR_EMAIL: case STORE_ATTR_FILENAME: if (strcmp(a->values[i].cstring, b->values[i].cstring)) return 0; break; case STORE_ATTR_KEYID: case STORE_ATTR_ISSUERKEYID: case STORE_ATTR_SUBJECTKEYID: case STORE_ATTR_ISSUERSERIALHASH: case STORE_ATTR_CERTHASH: if (memcmp(a->values[i].sha1string, b->values[i].sha1string, a->value_sizes[i])) return 0; break; case STORE_ATTR_ISSUER: case STORE_ATTR_SUBJECT: if (X509_NAME_cmp(a->values[i].dn, b->values[i].dn)) return 0; break; case STORE_ATTR_SERIAL: if (BN_cmp(a->values[i].number, b->values[i].number)) return 0; break; default: break; } } return 1; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -