mirror of
https://repo.dactyloidae.xyz/Dactyloidae/UXP.git
synced 2026-10-31 19:47:32 +09:00
Bug 1416940; r=snorp
This commit is contained in:
parent
c86a78aa94
commit
03078aa806
2 changed files with 52 additions and 66 deletions
|
|
@ -37,6 +37,10 @@ NSS_WRAPPER_INT(PL_Base64Encode)
|
||||||
NSS_WRAPPER_INT(PL_Base64Decode)
|
NSS_WRAPPER_INT(PL_Base64Decode)
|
||||||
NSS_WRAPPER_INT(PL_strfree)
|
NSS_WRAPPER_INT(PL_strfree)
|
||||||
|
|
||||||
|
SECStatus doCrypto(JNIEnv* jenv, const char *path, const char *value, char** result, bool doEncrypt);
|
||||||
|
SECStatus encode(const uint8_t* data, uint32_t srclen, char** result);
|
||||||
|
SECStatus decode(const char* data, uint8_t** result, uint32_t* length);
|
||||||
|
|
||||||
int
|
int
|
||||||
setup_nss_functions(void *nss_handle,
|
setup_nss_functions(void *nss_handle,
|
||||||
void *nspr_handle,
|
void *nspr_handle,
|
||||||
|
|
@ -189,41 +193,40 @@ doCrypto(JNIEnv* jenv, const char *path, const char *value, char** result, bool
|
||||||
keyid.len = 0;
|
keyid.len = 0;
|
||||||
rv = f_PK11SDR_Encrypt(&keyid, &request, &reply, nullptr);
|
rv = f_PK11SDR_Encrypt(&keyid, &request, &reply, nullptr);
|
||||||
|
|
||||||
if (rv != SECSuccess) {
|
if (rv == SECSuccess) {
|
||||||
|
rv = encode(reply.data, reply.len, result);
|
||||||
|
if (rv == SECSuccess) {
|
||||||
|
LOG("Encrypted: %s\n", *result);
|
||||||
|
} else {
|
||||||
|
throwError(jenv, "encode");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
throwError(jenv, "PK11SDR_Encrypt");
|
throwError(jenv, "PK11SDR_Encrypt");
|
||||||
goto done;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
rv = encode(reply.data, reply.len, result);
|
|
||||||
if (rv != SECSuccess) {
|
|
||||||
throwError(jenv, "encode");
|
|
||||||
goto done;
|
|
||||||
}
|
|
||||||
LOG("Encrypted: %s\n", *result);
|
|
||||||
} else {
|
} else {
|
||||||
LOG("Decoding: %s\n", value);
|
LOG("Decoding: %s\n", value);
|
||||||
rv = decode(value, &request.data, (int32_t*)&request.len);
|
rv = decode(value, &request.data, &request.len);
|
||||||
if (rv != SECSuccess) {
|
if (rv != SECSuccess) {
|
||||||
throwError(jenv, "decode");
|
throwError(jenv, "decode");
|
||||||
return rv;
|
return rv;
|
||||||
}
|
}
|
||||||
|
|
||||||
rv = f_PK11SDR_Decrypt(&request, &reply, nullptr);
|
rv = f_PK11SDR_Decrypt(&request, &reply, nullptr);
|
||||||
if (rv != SECSuccess) {
|
|
||||||
|
if (rv == SECSuccess) {
|
||||||
|
*result = static_cast<char*>(malloc(reply.len + 1));
|
||||||
|
strncpy(*result, reinterpret_cast<char*>(reply.data), reply.len);
|
||||||
|
(*result)[reply.len] = '\0';
|
||||||
|
|
||||||
|
// This can print sensitive data. Uncomment if you need it.
|
||||||
|
// LOG("Decoded %i letters: %s\n", reply.len, *result);
|
||||||
|
} else {
|
||||||
throwError(jenv, "PK11SDR_Decrypt");
|
throwError(jenv, "PK11SDR_Decrypt");
|
||||||
goto done;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
*result = (char *)malloc(reply.len+1);
|
|
||||||
strncpy(*result, (char *)reply.data, reply.len);
|
|
||||||
(*result)[reply.len] = '\0';
|
|
||||||
|
|
||||||
// This can print sensitive data. Uncomment if you need it.
|
|
||||||
// LOG("Decoded %i letters: %s\n", reply.len, *result);
|
|
||||||
free(request.data);
|
free(request.data);
|
||||||
}
|
}
|
||||||
|
|
||||||
done:
|
|
||||||
f_SECITEM_ZfreeItem(&reply, false);
|
f_SECITEM_ZfreeItem(&reply, false);
|
||||||
return rv;
|
return rv;
|
||||||
}
|
}
|
||||||
|
|
@ -232,64 +235,51 @@ done:
|
||||||
* Base64 encodes the data passed in. The caller must deallocate _retval using free();
|
* Base64 encodes the data passed in. The caller must deallocate _retval using free();
|
||||||
*/
|
*/
|
||||||
SECStatus
|
SECStatus
|
||||||
encode(const unsigned char *data, int32_t dataLen, char **_retval)
|
encode(const uint8_t* data, uint32_t srclen, char** result)
|
||||||
{
|
{
|
||||||
SECStatus rv = SECSuccess;
|
if (srclen > (PR_UINT32_MAX / 4) * 3) {
|
||||||
char *encoded = f_PL_Base64Encode((const char *)data, dataLen, nullptr);
|
return SECFailure;
|
||||||
if (!encoded)
|
|
||||||
rv = SECFailure;
|
|
||||||
if (!*encoded)
|
|
||||||
rv = SECFailure;
|
|
||||||
|
|
||||||
if (rv == SECSuccess) {
|
|
||||||
*_retval = (char *)malloc(strlen(encoded)+1);
|
|
||||||
strcpy(*_retval, encoded);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (encoded) {
|
const uint32_t dstlen = ((srclen + 2) / 3) * 4;
|
||||||
f_PR_Free(encoded);
|
char* const buffer = static_cast<char*>(malloc(dstlen + 1));
|
||||||
|
|
||||||
|
if (!buffer || !f_PL_Base64Encode(reinterpret_cast<const char*>(data), srclen, buffer)) {
|
||||||
|
free(buffer);
|
||||||
|
*result = nullptr;
|
||||||
|
return SECFailure;
|
||||||
}
|
}
|
||||||
|
|
||||||
return rv;
|
buffer[dstlen] = '\0';
|
||||||
|
*result = buffer;
|
||||||
|
return SECSuccess;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Base64 decodes the data passed in. The caller must deallocate result using free();
|
* Base64 decodes the data passed in. The caller must deallocate result using free();
|
||||||
*/
|
*/
|
||||||
SECStatus
|
SECStatus
|
||||||
decode(const char *data, unsigned char **result, int32_t *length)
|
decode(const char* data, uint8_t** result, uint32_t* length)
|
||||||
{
|
{
|
||||||
SECStatus rv = SECSuccess;
|
uint32_t srclen = strlen(data);
|
||||||
uint32_t len = strlen(data);
|
while (srclen && data[srclen - 1] == '=') {
|
||||||
int adjust = 0;
|
srclen--;
|
||||||
|
|
||||||
/* Compute length adjustment */
|
|
||||||
if (len > 0 && data[len-1] == '=') {
|
|
||||||
adjust++;
|
|
||||||
if (data[len-2] == '=') adjust++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
char *decoded;
|
// Avoid overflow when calculating result length.
|
||||||
decoded = f_PL_Base64Decode(data, len, nullptr);
|
const uint32_t dstlen = (srclen / 4) * 3 + ((srclen % 4) * 3) / 4;
|
||||||
if (!decoded) {
|
// At most 2 extra bytes due to padding in input.
|
||||||
return SECFailure;
|
uint8_t* const buffer = static_cast<uint8_t*>(malloc(dstlen + 2));
|
||||||
}
|
|
||||||
if (!*decoded) {
|
if (!buffer || !f_PL_Base64Decode(data, srclen, reinterpret_cast<char*>(buffer))) {
|
||||||
|
free(buffer);
|
||||||
|
*result = nullptr;
|
||||||
|
*length = 0;
|
||||||
return SECFailure;
|
return SECFailure;
|
||||||
}
|
}
|
||||||
|
|
||||||
*length = (len*3)/4 - adjust;
|
buffer[dstlen] = '\0';
|
||||||
LOG("Decoded %i chars into %i chars\n", len, *length);
|
*result = buffer;
|
||||||
|
*length = dstlen;
|
||||||
*result = (unsigned char*)malloc((size_t)len);
|
return SECSuccess;
|
||||||
|
|
||||||
if (!*result) {
|
|
||||||
rv = SECFailure;
|
|
||||||
} else {
|
|
||||||
memcpy((char*)*result, decoded, len);
|
|
||||||
}
|
|
||||||
f_PR_Free(decoded);
|
|
||||||
return rv;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -39,8 +39,4 @@ NSS_WRAPPER(PK11_GetInternalKeySlot, PK11SlotInfo *, void)
|
||||||
NSS_WRAPPER(PK11_NeedUserInit, PRBool, PK11SlotInfo *)
|
NSS_WRAPPER(PK11_NeedUserInit, PRBool, PK11SlotInfo *)
|
||||||
NSS_WRAPPER(PK11_InitPin, SECStatus, PK11SlotInfo*, const char*, const char*)
|
NSS_WRAPPER(PK11_InitPin, SECStatus, PK11SlotInfo*, const char*, const char*)
|
||||||
|
|
||||||
bool setPassword(PK11SlotInfo *slot);
|
|
||||||
SECStatus doCrypto(JNIEnv* jenv, const char *path, const char *value, char** result, bool doEncrypt);
|
|
||||||
SECStatus encode(const unsigned char *data, int32_t dataLen, char **_retval);
|
|
||||||
SECStatus decode(const char *data, unsigned char **result, int32_t * _retval);
|
|
||||||
#endif /* NSS_h */
|
#endif /* NSS_h */
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue