📄 dname.c
字号:
break;
default:
break;
}
} /* for */
/* "+ 1" for null char at end */
delimStringLen++;
delimString = TC_Alloc(mgr, delimStringLen);
if (delimString == NULL)
return NULL;
delimStringPtr = delimString;
/*----- now create actual delimited string ----*/
/* delimit leading spaces */
j = 0;
while (buf[j] == ' ' && j < buflen) {
*delimStringPtr = '\\';
delimStringPtr++;
*delimStringPtr = ' ';
delimStringPtr++;
j++;
}
for (i = j; i < buflen - numTrailingSpaces; i++) {
switch(buf[i]) {
case '"':
case '=':
case ',':
case '+':
case '\\':
case '<':
case '>':
case ';':
case '#':
*delimStringPtr = '\\';
delimStringPtr++;
*delimStringPtr = buf[i];
delimStringPtr++;
break;
default:
*delimStringPtr = buf[i];
delimStringPtr++;
break;
}
} /* for */
/* now do the trailing spaces */
if (numTrailingSpaces != 0) {
/* we'd better be at the trailing spaces in buf */
if (buf[i] != ' ' || (buflen-i) != numTrailingSpaces) {
TC_Free(mgr, delimString);
return NULL;
}
while(buf[i] == ' ' && i < buflen) {
*delimStringPtr = '\\';
delimStringPtr++;
*delimStringPtr = ' ';
delimStringPtr++;
i++;
}
}
*delimStringPtr = '\0';
return delimString;
} /* DelimitString */
int CopyName(TC_Name **to, TC_Name *from, TC_CONTEXT *ctx)
{
int status;
int i, j;
PKIName *localTo;
PKIRDNSequence *to_rdnSeq, *from_rdnSeq;
PKIRelativeDistinguishedName *to_rdnName, *from_rdnName;
do {
if (!to || !from) {
status = TC_E_INVARGS;
break;
}
if ((status = tc_create_dname(&localTo, ctx)) != 0)
break;
to_rdnSeq = (PKIRDNSequence *)localTo->data;
from_rdnSeq = (PKIRDNSequence *)from->data;
for ( i = 0; i < from_rdnSeq->n; i++) {
to_rdnSeq->elt[i] =
TC_Alloc(ctx->memMgr,
sizeof(PKIRelativeDistinguishedName));
if (to_rdnSeq->elt[i] == NULL)
return TC_E_NOMEMORY;
to_rdnName = (PKIRelativeDistinguishedName *)to_rdnSeq->elt[i];
from_rdnName = (PKIRelativeDistinguishedName *)from_rdnSeq->elt[i];
for (j = 0; j < from_rdnName->n; j++) {
to_rdnName->elt[j] =
TC_Alloc(ctx->memMgr, sizeof(PKIAttributeTypeAndValue));
if (to_rdnName->elt[j] == NULL) {
TC_Free(ctx->memMgr, to_rdnSeq->elt[i]);
/* TODO: free rest of list... */
return TC_E_NOMEMORY;
}
memset(to_rdnName->elt[j], 0,
sizeof(PKIAttributeTypeAndValue));
(void)tc_set_avader(to_rdnName->elt[j],
from_rdnName->elt[j]->type.val,
from_rdnName->elt[j]->type.len,
from_rdnName->elt[j]->value.val,
from_rdnName->elt[j]->value.len,
ctx);
} /* for each ava in the rdn */
to_rdnName->n = from_rdnName->n;
} /* for each rdn in the sequence */
to_rdnSeq->n = from_rdnSeq->n;
} while (0);
*to = localTo;
return 0;
} /* CopyName */
/*****
*
* tc_get_dname
*
* Given a certificate or cert. request, return the pointer to the
* Name structure containing the requested Name.
*
* parameters
* input
* cert - a pointer to a CERT or a CertificateRequest
* type - the desired name, one of:
* TC_EXTRACT_ISSUER
* TC_EXTRACT_CRLISSUER
* TC_EXTRACT_SUBJECT
* TC_EXTRACT_REQ_NAME
*
* output
* dname - a pointer to the Name requested or NULL on error
*
* returns
* 0 - okay
* TC_E_???
*
*****/
int
tc_get_dname(
TC_Name **dname,
void *cert,
int type,
TC_CONTEXT *ctx)
{
int status = 0;
if (!cert || !dname)
return TC_E_INVARGS;
switch (type) {
case TC_EXTRACT_ISSUER:
status = CopyName(dname,
&((TC_CERT *) cert)->tbsCertificate->issuer,
ctx);
break;
case TC_EXTRACT_CRLISSUER:
status = CopyName(dname,
&((TC_CertificateList *) cert)->tbsCertList.issuer,
ctx);
break;
case TC_EXTRACT_SUBJECT:
status = CopyName(dname,
&((TC_CERT *) cert)->tbsCertificate->subject,
ctx);
break;
case TC_EXTRACT_REQ_NAME:
status = CopyName(dname,
&((TC_CertificationRequest *) cert)->certificationRequestInfo.subject,
ctx);
break;
default:
*dname = NULL;
return TC_E_INVARGS;
}
return status;
} /* tc_get_dname */
/*****
*
* AVAToString
*
* Create a string representatio of an AVA. This follows the syntax
* suggested in RFC 2253.
*
*****/
static char *AVAToString(
PKIAttributeTypeAndValue *ava,
TC_CONTEXT *context)
{
TC_AVA_ENTRY *avaEntry = NULL;
char *hexString;
char *valString;
char attrName[1024];
unsigned char tag;
size_t avaStringLen;
char *avaString;
unsigned char *avaData;
size_t avaDataLen;
size_t attrValLen;
size_t attrNameLen;
avaEntry = lookupAVAByOID(ava, context);
/* get the string representation of the attribute type */
if (avaEntry != NULL)
strcpy(attrName, avaEntry->attributeType);
else
OidToString(attrName, ava->type.val, ava->type.len,
context->memMgr);
attrNameLen = strlen(attrName);
/* error check here... */
(void)tc_get_ava(&tag, &avaData, &avaDataLen, &ava->value);
/* if we have a non-printable value for the AVA, then
convert the data into a hex string representation */
if (avaEntry == NULL ||
(tag != PKIID_NumericString &&
tag != PKIID_PrintableString &&
tag != PKIID_IA5String )) {
hexString = UcharToHexString(avaData, avaDataLen, context->memMgr);
if (hexString == NULL)
/* free stuff??? */
return NULL;
valString = TC_Alloc(context->memMgr,
strlen(hexString) + 2); /* 2 is for "#" and '\0' */
if (valString == NULL)
/* free stuff?? */
return NULL;
strcpy(valString, "#");
strcat(valString, hexString);
TC_Free(context->memMgr, hexString);
}
/* otherwise use the value as a string */
else {
valString = DelimitString(avaData, avaDataLen, context->memMgr);
if (valString == NULL)
/* free stuff?? */
return NULL;
}
attrValLen = strlen(valString);
/* name + "=" + value + '\0' */
avaStringLen = attrNameLen + 1 + attrValLen + 1;
avaString = TC_Alloc(context->memMgr, avaStringLen);
if (avaString == NULL)
/* free stuff?? */
return NULL;
strcpy(avaString, attrName);
strcat(avaString, "=");
strcat(avaString, valString);
TC_Free(context->memMgr, valString);
return avaString;
} /* AVAToString */
/*****
*
* RDNToString
*
* Create a string representation of a Relative Distinguished Name.
*
*****/
char *RDNToString(
PKIRelativeDistinguishedName *rdnName,
TC_CONTEXT *context)
{
int i;
int newlen;
PKIAttributeTypeAndValue *ava;
char *rdnString = NULL;
char *avaString;
if (!rdnName)
return NULL;
if ((rdnString = TC_Alloc(context->memMgr, 1)) == NULL)
return NULL;
*rdnString = '\0';
/* go through each AVA in the RDN name */
for (i = 0; i < rdnName->n; i++) {
ava = rdnName->elt[i];
avaString = AVAToString(ava, context);
if (avaString == NULL) {
TC_Free(context->memMgr, rdnString);
return NULL;
}
/* the "2" is for the '\0' and plus chars */
newlen = strlen(rdnString) + strlen(avaString) + 2;
TC_Realloc(context->memMgr, (void **)&rdnString, newlen);
if (rdnString == NULL)
return NULL;
strcat(rdnString, avaString);
if (i != rdnName->n-1)
strcat(rdnString, "+");
TC_Free(context->memMgr, avaString);
} /* for each AVA */
return rdnString;
} /* RDNToString */
/****
* tc_extractdname_fromcert
*
* Extract a subject or issuer dname from a certificate struct
* or cert. request struct and return the string representation.
*
* returns
* TC_E_INVARGS
* TC_E_NOMEMORY
* 0 - okay
*/
int tc_extractdname_fromcert(
char **dname,
void *cert,
int type,
TC_CONTEXT *context)
{
TC_Name *name;
if (!dname)
return TC_E_INVARGS;
*dname = NULL;
switch (type) {
case TC_EXTRACT_ISSUER:
name = &((TC_CERT *) cert)->tbsCertificate->issuer;
break;
case TC_EXTRACT_SUBJECT:
name = &((TC_CERT *) cert)->tbsCertificate->subject;
break;
case TC_EXTRACT_REQ_NAME:
name = &((TC_CertificationRequest *) cert)->certificationRequestInfo.subject;
break;
default:
return TC_E_INVARGS;
}
return tc_extract_dname_string(dname, name, context);
} /* tc_extractdname_fromcert */
/*****
* tc_extract_dname_string
*
* Extract string representation of a dname from a Name structure.
* Follows the string format described in RFC 2253.
*
* returns
* TC_E_INVARGS
* TC_E_NOMEMORY
* 0 - okay
*/
int tc_extract_dname_string (
char **stringname,
TC_Name *dname,
TC_CONTEXT *context)
{
int i;
int newlen;
PKIRDNSequence *rdnSeq;
PKIRelativeDistinguishedName *rdnName;
char *rdnString;
int error = 0;
char *localdname;
if (!stringname || !dname || !context)
return TC_E_INVARGS;
*stringname = NULL;
rdnSeq = (PKIRDNSequence *)dname->data;
localdname = TC_Alloc(context->memMgr, 1);
if (localdname == NULL)
return TC_E_NOMEMORY;
*localdname = '\0';
if (rdnSeq->n == 0) {
*stringname = localdname;
return 0;
}
/* go through each RDN in in rdnSeq, it is in reverse order because
we want to create the LDAP convention for a string
dname, leaf->root, left->right */
for (i = rdnSeq->n-1; i >= 0; i--) {
rdnName = rdnSeq->elt[i];
rdnString = RDNToString(rdnName, context);
if (rdnString == NULL) {
error = TC_E_DNAMEPARSE;
break;
}
/* the "2" is for '\0' and comma */
newlen = strlen(localdname) + strlen(rdnString) + 2;
TC_Realloc(context->memMgr, (void **)&localdname, newlen);
if (localdname == NULL) {
error = TC_E_NOMEMORY;
break;
}
strcat(localdname, rdnString);
if (i != 0)
strcat(localdname, ",");
TC_Free(context->memMgr, rdnString);
} /* for each rdnSeq */
if (error != 0)
TC_Free(context->memMgr, localdname);
else
*stringname = localdname;
return error;
} /* tc_extract_dname_string */
/*****
*
* Given an attribute type (ASCII representation) and a dname, return the
* value of the first AVA in the dname with that attribute type. If there is
* no attribute or the attribute is not a printable type (NumericString,
* PrintableString, or IA5String), then return NULL.
* The value returned is not delimited or changed in any way.
*
****/
int
tc_get_attributeValue(
char **value,
TC_Name *dname,
char *attributeType,
TC_CONTEXT *context)
{
TC_AVA_ENTRY *avaEntry = NULL;
PKIRDNSequence *rdnSeq;
PKIRelativeDistinguishedName *rdnName;
PKIAttributeTypeAndValue *ava;
unsigned char tag;
unsigned char *avaData;
size_t avaDataLen;
int i, j;
if (value == NULL || dname == NULL || attributeType == NULL || context == NULL)
return TC_E_INVARGS;
*value = NULL;
rdnSeq = (PKIRDNSequence *)dname->data;
for(i = 0; i < rdnSeq->n; i++ ) {
rdnName = rdnSeq->elt[i];
for (j = 0; j < rdnName->n; j++ ) {
ava = rdnName->elt[j];
avaEntry = lookupAVAByOID(ava, context);
if (avaEntry == NULL)
break;
if (strcmp(attributeType, avaEntry->attributeType) != 0)
break;
(void)tc_get_ava(&tag, &avaData, &avaDataLen, &ava->value);
/* if we have a non-printable value for the AVA, then skip it */
if (tag != PKIID_NumericString &&
tag != PKIID_PrintableString &&
tag != PKIID_IA5String)
break;
/* "+1" for teminating null char */
*value = (char *)malloc(avaDataLen + 1);
if (*value == NULL)
return TC_E_NOMEMORY;
memcpy(*value, avaData, avaDataLen);
memset(*value+avaDataLen, '\0', 1);
return 0;
} /* for each RDN */
} /* for each RDNSeq */
return 0;
} /* attributevalue */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -