зеркало из https://github.com/mozilla/pjs.git
Add decryption functions for SDR. Add SDR to UNIX builds.
This commit is contained in:
Родитель
215b1e19c7
Коммит
84ea5e1f61
|
@ -61,6 +61,7 @@ CSRCS = cmtinit.c \
|
|||
cmtpasswd.c \
|
||||
cmtadvisor.c \
|
||||
cmtrng.c \
|
||||
cmtsdr.c \
|
||||
$(NULL)
|
||||
|
||||
EXTRA_DSO_LDOPTS += -L$(DIST)/bin -lprotocol
|
||||
|
|
|
@ -2208,6 +2208,31 @@ CMTStatus CMT_SDREncrypt(PCMT_CONTROL control,
|
|||
const unsigned char *data, CMUint32 dataLen,
|
||||
unsigned char **result, CMUint32 *resultLen);
|
||||
|
||||
/*
|
||||
* FUNCTION: CMT_SDRDecrypt
|
||||
* ----------------------------------
|
||||
* INPUTS
|
||||
* control
|
||||
* A control connection that has been established with the psm server.
|
||||
* data
|
||||
* A buffer containing the the results of a call to SDREncrypt
|
||||
* dataLen
|
||||
* The length of the data buffer
|
||||
* result
|
||||
* Recieves a pointer to a buffer containing the result of the
|
||||
* decryption
|
||||
* resultLen
|
||||
* Receives the length of the result buffer
|
||||
* NOTES
|
||||
*
|
||||
* RETURN
|
||||
* CMTSuccess - the encryption worked.
|
||||
* CMTFailure - some (unspecified) error occurred (needs work)
|
||||
*/
|
||||
CMTStatus CMT_SDRDecrypt(PCMT_CONTROL control,
|
||||
const unsigned char *data, CMUint32 dataLen,
|
||||
unsigned char **result, CMUint32 *resultLen);
|
||||
|
||||
/* Lock operations */
|
||||
void CMT_LockConnection(PCMT_CONTROL control);
|
||||
void CMT_UnlockConnection(PCMT_CONTROL control);
|
||||
|
|
|
@ -62,7 +62,7 @@ static CMTMessageTemplate EncryptRequestTemplate[] =
|
|||
/* Decrypt reply message - SingleItemMessage */
|
||||
|
||||
/* Constants for testing */
|
||||
static const char *kSuccess = "Success:";
|
||||
static const char *kPrefix = "Encrypted:";
|
||||
static const char *kFailure = "Failure:";
|
||||
|
||||
static CMTItem
|
||||
|
@ -77,40 +77,42 @@ CMT_CopyDataToItem(const unsigned char *data, CMUint32 len)
|
|||
return item;
|
||||
}
|
||||
|
||||
/* encryption request */
|
||||
static CMTStatus
|
||||
tmp_SendMessage(PCMT_CONTROL control, CMTItem *message)
|
||||
tmp_DoEncryptionRequest(CMTItem *message)
|
||||
{
|
||||
CMTStatus rv = CMTSuccess;
|
||||
EncryptRequestMessage request;
|
||||
SingleItemMessage reply;
|
||||
int prefixLen = strlen(kSuccess);
|
||||
|
||||
if (message->type != SSM_SDR_ENCRYPT_REQUEST) {
|
||||
rv = CMTFailure;
|
||||
goto loser;
|
||||
}
|
||||
CMUint32 pLen = strlen(kPrefix);
|
||||
|
||||
/* Initialize */
|
||||
request.keyid.data = 0;
|
||||
request.data.data = 0;
|
||||
|
||||
/* Decode incoming message */
|
||||
rv = CMT_DecodeMessage(EncryptRequestTemplate, &request, message);
|
||||
if (rv != CMTSuccess) goto loser;
|
||||
if (rv != CMTSuccess) goto loser; /* Protocol error */
|
||||
|
||||
/* Concatinate the prefix with the data */
|
||||
reply.item.len = request.data.len + request.keyid.len;
|
||||
/* Free incoming message */
|
||||
free(message->data);
|
||||
message->data = NULL;
|
||||
|
||||
/* "Encrypt" by prefixing the data */
|
||||
reply.item.len = request.data.len + pLen;
|
||||
reply.item.data = calloc(reply.item.len, 1);
|
||||
if (!reply.item.data) {
|
||||
rv = CMTFailure;
|
||||
goto loser;
|
||||
}
|
||||
|
||||
message->type = SSM_SDR_ENCRYPT_REPLY;
|
||||
if (request.keyid.len) memcpy(reply.item.data, request.keyid.data, request.keyid.len);
|
||||
memcpy(&reply.item.data[request.keyid.len], request.data.data, request.data.len);
|
||||
if (pLen) memcpy(reply.item.data, kPrefix, pLen);
|
||||
memcpy(&reply.item.data[pLen], request.data.data, request.data.len);
|
||||
|
||||
/* Free old message ?? */
|
||||
/* Generate response */
|
||||
message->type = SSM_SDR_ENCRYPT_REPLY;
|
||||
rv = CMT_EncodeMessage(SingleItemMessageTemplate, message, &reply);
|
||||
if (rv != CMTSuccess) goto loser;
|
||||
if (rv != CMTSuccess) goto loser; /* Unknown error */
|
||||
|
||||
loser:
|
||||
if (request.keyid.data) free(request.keyid.data);
|
||||
|
@ -118,6 +120,62 @@ loser:
|
|||
|
||||
return rv;
|
||||
}
|
||||
|
||||
/* decryption request */
|
||||
static CMTStatus
|
||||
tmp_DoDecryptionRequest(CMTItem *message)
|
||||
{
|
||||
CMTStatus rv = CMTSuccess;
|
||||
SingleItemMessage request;
|
||||
SingleItemMessage reply;
|
||||
CMUint32 pLen = strlen(kPrefix);
|
||||
|
||||
/* Initialize */
|
||||
request.item.data = 0;
|
||||
reply.item.data = 0;
|
||||
|
||||
/* Decode the message */
|
||||
rv = CMT_DecodeMessage(SingleItemMessageTemplate, &request, message);
|
||||
if (rv != CMTSuccess) goto loser;
|
||||
|
||||
/* Free incoming message */
|
||||
free(message->data);
|
||||
message->data = NULL;
|
||||
|
||||
/* "Decrypt" the message by removing the key */
|
||||
if (pLen && memcmp(request.item.data, kPrefix, pLen) != 0) {
|
||||
rv = CMTFailure; /* Invalid format */
|
||||
goto loser;
|
||||
}
|
||||
|
||||
reply.item.len = request.item.len - pLen;
|
||||
reply.item.data = calloc(reply.item.len, 1);
|
||||
if (!reply.item.data) { rv = CMTFailure; goto loser; }
|
||||
|
||||
memcpy(reply.item.data, &request.item.data[pLen], reply.item.len);
|
||||
|
||||
/* Create reply message */
|
||||
message->type = SSM_SDR_DECRYPT_REPLY;
|
||||
rv = CMT_EncodeMessage(SingleItemMessageTemplate, message, &reply);
|
||||
if (rv != CMTSuccess) goto loser;
|
||||
|
||||
loser:
|
||||
if (request.item.data) free(request.item.data);
|
||||
if (reply.item.data) free(reply.item.data);
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
static CMTStatus
|
||||
tmp_SendMessage(PCMT_CONTROL control, CMTItem *message)
|
||||
{
|
||||
if (message->type == SSM_SDR_ENCRYPT_REQUEST)
|
||||
return tmp_DoEncryptionRequest(message);
|
||||
else if (message->type == SSM_SDR_DECRYPT_REQUEST)
|
||||
return tmp_DoDecryptionRequest(message);
|
||||
|
||||
return CMTFailure;
|
||||
}
|
||||
/* End test code */
|
||||
|
||||
CMTStatus
|
||||
|
@ -160,5 +218,45 @@ loser:
|
|||
if (request.data.data) free(request.data.data);
|
||||
if (reply.item.data) free(reply.item.data);
|
||||
|
||||
return CMTSuccess; /* need return value */
|
||||
}
|
||||
|
||||
CMTStatus
|
||||
CMT_SDRDecrypt(PCMT_CONTROL control, const unsigned char *data, CMUint32 dataLen,
|
||||
unsigned char **result, CMUint32 *resultLen)
|
||||
{
|
||||
CMTItem message;
|
||||
SingleItemMessage request;
|
||||
SingleItemMessage reply;
|
||||
|
||||
/* Fill in the request */
|
||||
request.item = CMT_CopyDataToItem(data, dataLen);
|
||||
|
||||
/* Encode */
|
||||
if (CMT_EncodeMessage(SingleItemMessageTemplate, &message, &request) != CMTSuccess) {
|
||||
goto loser;
|
||||
}
|
||||
|
||||
message.type = SSM_SDR_DECRYPT_REQUEST;
|
||||
|
||||
/* Send */
|
||||
/* if (CMT_SendMessage(control, &message) != CMTSuccess) goto loser; */
|
||||
if (tmp_SendMessage(control, &message) != CMTSuccess) goto loser;
|
||||
|
||||
if (message.type != SSM_SDR_DECRYPT_REPLY) goto loser;
|
||||
|
||||
if (CMT_DecodeMessage(SingleItemMessageTemplate, &reply, &message) != CMTSuccess)
|
||||
goto loser;
|
||||
|
||||
*result = reply.item.data;
|
||||
*resultLen = reply.item.len;
|
||||
|
||||
reply.item.data = 0;
|
||||
|
||||
loser:
|
||||
if (message.data) free(message.data);
|
||||
if (request.item.data) free(request.item.data);
|
||||
if (reply.item.data) free(reply.item.data);
|
||||
|
||||
return CMTSuccess; /* need return value */
|
||||
}
|
Загрузка…
Ссылка в новой задаче