📄 emailtype.c
字号:
/* Copyright 2003-2005, Voltage Security, all rights reserved.
*/
#include "vibe.h"
#include "environment.h"
#include "base.h"
#include "libctx.h"
#include "idobj.h"
#include "oidlist.h"
#include "emailschema.h"
#include "vtime.h"
/* Gets the email address out of an identity object.
*
* @param idObj The object from which the email address is to be
* extracted.
* @param getInfo The address where the function will deposit the
* pointer to the info.
* @return an int, 0 if the function completed successfully or a
* non-zero error code.
*/
int VOLT_CALLING_CONV VoltIdentityGet822Email VOLT_PROTO_LIST ((
VtIdentityObject idObj,
Pointer *getInfo
));
int VtIdentityParam822Email (
VtIdentityObject idObj,
Pointer info,
unsigned int flag
)
{
int status;
VtEmailInfo *inputInfo;
VoltTime baseTimeSeconds;
VtEmailValidityInfo emailInfo;
if (flag == VOLT_ID_GET_TYPE_FLAG)
return (VoltIdentityGet822Email (idObj, (Pointer *)info));
do
{
status = VT_ERROR_INVALID_ASSOCIATED_INFO;
if (info == (Pointer)0)
break;
/* Build up the emailInfo for VtIdentityParam822EmailValidity and
* call that IdnetityParam. That Param needs a base time and
* validity period, we'll use the default values.
*/
inputInfo = (VtEmailInfo *)info;
emailInfo.emailAddress = inputInfo->emailAddress;
emailInfo.emailTime = inputInfo->emailTime;
VoltGetDefaultEmailBaseTime (
&(emailInfo.baseTime), &baseTimeSeconds, &(emailInfo.validityPeriod),
&(emailInfo.segmentCount));
status = VtIdentityParam822EmailValidity
(idObj, (Pointer)&emailInfo, flag);
} while (0);
return (status);
}
int VtIdentityParam822EmailValidity (
VtIdentityObject idObj,
Pointer info,
unsigned int flag
)
{
int status;
#if VOLT_ALIGNMENT != 1
unsigned int pad;
#endif
unsigned int bufferSize, offset, index, emailLen;
VoltEmailSchema *localEmailSchema;
VoltIdentityObject *obj = (VoltIdentityObject *)idObj;
VoltLibCtx *libCtx = (VoltLibCtx *)(obj->voltObject.libraryCtx);
VtEmailValidityInfo *emailInfo = (VtEmailValidityInfo *)info;
unsigned char *buffer = (unsigned char *)0;
VoltIdentitySchema *schema;
VoltTime baseTimeSeconds;
unsigned char oid[VoltIdSchemaOid822EmailLen] = { VoltIdSchemaOid822Email };
if (flag == VOLT_ID_GET_TYPE_FLAG)
return (VoltIdentityGet822Email (idObj, (Pointer *)info));
do
{
/* Check the flag, it should be VOLT_ID_SET_TYPE_FLAG.
*/
status = VT_ERROR_INVALID_TYPE;
if (flag != VOLT_ID_SET_TYPE_FLAG)
break;
/* The associated info should be a pointer to a VtEmailValidityInfo
* struct.
*/
status = VT_ERROR_INVALID_ASSOCIATED_INFO;
if (info == (Pointer)0)
break;
if (VoltIsValidTime (&(emailInfo->emailTime)) != 0)
break;
if (VoltIsValidTime (&(emailInfo->baseTime)) != 0)
break;
/* The base time cannot be after the email time.
*/
if (VoltCompareTime (&(emailInfo->baseTime), &(emailInfo->emailTime)) > 0)
break;
if (VoltConvertTimeToSeconds (
&(emailInfo->baseTime), &baseTimeSeconds) != 0)
break;
if (emailInfo->validityPeriod == 0)
break;
if ( (emailInfo->segmentCount == 0) || (emailInfo->segmentCount > 52) ||
(emailInfo->segmentCount > emailInfo->validityPeriod) )
break;
emailLen = Z2Strlen (emailInfo->emailAddress);
/* Allocate space for the VoltIdentitySchema struct, the email
* schema struct, the email address, a copy of the email address
* with all lower case letters (add 2 for a pair of NULL
* terminating characters), and the OID.
*/
bufferSize =
sizeof (VoltIdentitySchema) + sizeof (VoltEmailSchema) +
(2 * emailLen) + 2 + VoltIdSchemaOid822EmailLen;
#if VOLT_ALIGNMENT != 1
/* If the alignment is 1, there's no need to pad. If not, compute
* the pad length.
*/
VOLT_COMPUTE_ALIGN_PAD (
VOLT_ALIGNMENT, sizeof (VoltIdentitySchema), pad)
bufferSize += pad;
#endif
status = VT_ERROR_MEMORY;
buffer = (unsigned char *)Z2Malloc (bufferSize, 0);
if (buffer == (unsigned char *)0)
break;
Z2Memset (buffer, 0, bufferSize);
/* Locate the struct and pointers.
*/
schema = (VoltIdentitySchema *)buffer;
offset = sizeof (VoltIdentitySchema);
#if VOLT_ALIGNMENT != 1
offset += pad;
#endif
schema->value = (Pointer)(buffer + offset);
localEmailSchema = (VoltEmailSchema *)(schema->value);
offset += sizeof (VoltEmailSchema);
localEmailSchema->emailInfo.emailAddress = buffer + offset;
offset += emailLen + 1;
localEmailSchema->lowerCaseEmail = buffer + offset;
offset += emailLen + 1;
schema->oid.data = buffer + offset;
/* Fill in the fields. Include NULL-terminating characters where
* necessary.
*/
localEmailSchema->baseTimeSeconds = baseTimeSeconds;
localEmailSchema->emailInfo.baseTime = emailInfo->baseTime;
localEmailSchema->emailInfo.validityPeriod = emailInfo->validityPeriod;
localEmailSchema->emailInfo.segmentCount = emailInfo->segmentCount;
Z2Memcpy (
localEmailSchema->emailInfo.emailAddress, emailInfo->emailAddress,
emailLen + 1);
Z2Memcpy (
localEmailSchema->lowerCaseEmail, emailInfo->emailAddress, emailLen + 1);
localEmailSchema->lowerCaseLen = emailLen;
/* Go through the email address and convert all the letters to
* lower case.
*/
index = 0;
while (localEmailSchema->lowerCaseEmail[index] != 0)
{
localEmailSchema->lowerCaseEmail[index] = (unsigned char)Z2Tolower (
localEmailSchema->lowerCaseEmail[index]);
index++;
}
if (emailInfo->emailTime.month != 0)
{
localEmailSchema->emailInfo.emailTime = emailInfo->emailTime;
}
else
{
status = VtGetTime (
(VtLibCtx)libCtx, &(localEmailSchema->emailInfo.emailTime));
if (status != 0)
break;
}
/* Store the time internally as number of seconds
*/
VoltConvertTimeToSeconds (
&(localEmailSchema->emailInfo.emailTime),
&(localEmailSchema->internalTime));
/* Find the @ sign in the lower case version.
* If we run through the entire address without finding it, error.
* If we find it, change status back to 0.
*/
index = 0;
status = VT_ERROR_INVALID_ASSOCIATED_INFO;
while (index < localEmailSchema->lowerCaseLen)
{
if (localEmailSchema->lowerCaseEmail[index] != '@')
{
index++;
continue;
}
/* We found it, set status to 0 and exit this while loop.
*/
status = 0;
break;
}
if (status != 0)
break;
localEmailSchema->atSignOffset = index;
Z2Memcpy (schema->oid.data, oid, VoltIdSchemaOid822EmailLen);
schema->oid.len = VoltIdSchemaOid822EmailLen;
schema->commonName.data = localEmailSchema->lowerCaseEmail;
schema->commonName.len = localEmailSchema->lowerCaseLen;
schema->IdentityParam = VtIdentityParam822Email;
schema->EncodeSchemaAlloc = EncodeSchemaEmailAlloc;
schema->DestroySchema = VoltDefaultCtxDestroy;
obj->schema = schema;
} while (0);
/* If everything worked, return 0.
*/
if (status == 0)
return (0);
/* If something went wrong, destroy anything we created.
*/
if (buffer != (unsigned char *)0)
Z2Free (buffer);
return (status);
}
int VoltIdentityGet822Email (
VtIdentityObject idObj,
Pointer *getInfo
)
{
int status;
VoltIdentityObject *obj = (VoltIdentityObject *)idObj;
VoltLibCtx *libCtx = (VoltLibCtx *)(obj->voltObject.libraryCtx);
VoltIdentitySchema *schema;
VoltEmailSchema *emailSchema;
unsigned char oid[VoltIdSchemaOid822EmailLen] = { VoltIdSchemaOid822Email };
schema = obj->schema;
do
{
/* Is this the email oid?
*/
status = VT_ERROR_GET_INFO_UNAVAILABLE;
if (schema == (VoltIdentitySchema *)0)
break;
if (schema->oid.len != VoltIdSchemaOid822EmailLen)
break;
if (Z2Memcmp (oid, schema->oid.data, VoltIdSchemaOid822EmailLen) != 0)
break;
/* The current schema is email. If the identity is not encoded, the
* info in the schema struct is the info to return.
* If the identity is encoded, get the time out of the schema's
* idTime (the actual time used).
*/
status = 0;
emailSchema = (VoltEmailSchema *)(schema->value);
if (obj->encoding.data != (unsigned char *)0)
emailSchema->emailInfo.emailTime = schema->idTime;
*getInfo = (Pointer)&(emailSchema->emailInfo);
} while (0);
return (status);
}
int VoltGetEmailAndDomain (
VoltIdentityObject *idObj,
unsigned char **emailAddress,
unsigned int *emailAddressLen,
unsigned char **domain,
unsigned int *domainLen
)
{
unsigned int index;
VoltIdentitySchema *schema;
VoltLibCtx *libCtx = (VoltLibCtx *)(idObj->voltObject.libraryCtx);
unsigned char oid[VoltIdSchemaOid822EmailLen] = { VoltIdSchemaOid822Email };
VoltEmailSchema *localEmailSchema;
*emailAddress = (unsigned char *)0;
*emailAddressLen = 0;
*domain = (unsigned char *)0;
*domainLen = 0;
schema = idObj->schema;
/* If the schema is not email, return 0 with the domain and address
* set to NULL.
*/
/* Is this the email oid?
*/
if (schema->oid.len != VoltIdSchemaOid822EmailLen)
return (0);
if (Z2Memcmp (oid, schema->oid.data, VoltIdSchemaOid822EmailLen) != 0)
return (0);
/* This is the email schema, open the VoltEmailSchema.
*/
localEmailSchema = (VoltEmailSchema *)(schema->value);
/* The lowerCaseEmail field points to the version of the email with
* all the characters lower case. The atSignOffset indicates where, in
* that buffer, the @ sign is. For the domain, we want to return
* everything after the @ sign.
*/
*emailAddress = localEmailSchema->lowerCaseEmail;
*emailAddressLen = localEmailSchema->lowerCaseLen;
index = localEmailSchema->atSignOffset + 1;
*domain = (localEmailSchema->lowerCaseEmail + index);
*domainLen = localEmailSchema->lowerCaseLen - index;
return (0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -