未验证 提交 c09d7d52 编写于 作者: O openharmony_ci 提交者: Gitee

!6942 【OpenHarmony 4.0.1.2】【安全子系统】【cryptoFramework】修改证书管理相关的接口

Merge pull request !6942 from Qwink2016/qwink1216
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
"type": "OHJSUnitTest", "type": "OHJSUnitTest",
"test-timeout": "900000", "test-timeout": "900000",
"shell-timeout": "900000", "shell-timeout": "900000",
"testcase-timeout": "60000",
"bundle-name": "com.openharmony.cryptoFramework2", "bundle-name": "com.openharmony.cryptoFramework2",
"package-name": "com.openharmony.cryptoFramework2" "package-name": "com.openharmony.cryptoFramework2"
}, },
......
...@@ -246,7 +246,7 @@ export default function CertificateJsunit() { ...@@ -246,7 +246,7 @@ export default function CertificateJsunit() {
await certCallback await certCallback
.checkValidityX509CertCallback("der", "20330830000000+0800") .checkValidityX509CertCallback("der", "20330830000000+0800")
.then((data) => { .then((data) => {
expect(data == 17630004).assertTrue(); expect(data == 19030004).assertTrue();
}) })
.catch((err) => { .catch((err) => {
expect(null).assertFail(); expect(null).assertFail();
...@@ -264,7 +264,7 @@ export default function CertificateJsunit() { ...@@ -264,7 +264,7 @@ export default function CertificateJsunit() {
await certCallback await certCallback
.checkValidityX509CertCallback("der", "NULL") .checkValidityX509CertCallback("der", "NULL")
.then((data) => { .then((data) => {
expect(data == 17630001).assertTrue(); expect(data == 19030001).assertTrue();
}) })
.catch((err) => { .catch((err) => {
expect(null).assertFail(); expect(null).assertFail();
...@@ -272,7 +272,7 @@ export default function CertificateJsunit() { ...@@ -272,7 +272,7 @@ export default function CertificateJsunit() {
await certCallback await certCallback
.checkValidityX509CertCallback("der", "string") .checkValidityX509CertCallback("der", "string")
.then((data) => { .then((data) => {
expect(data == 17630001).assertTrue(); expect(data == 19030001).assertTrue();
}) })
.catch((err) => { .catch((err) => {
expect(null).assertFail(); expect(null).assertFail();
......
...@@ -15,15 +15,13 @@ ...@@ -15,15 +15,13 @@
*/ */
import { describe, beforeAll, afterEach, it, expect } from "@ohos/hypium"; import { describe, beforeAll, afterEach, it, expect } from "@ohos/hypium";
import { import { testSecurityRandomPromise } from "./utils/securityrandom/publicSecurityRandomPromise";
testSecurityRandomPromise,
testSecurityRandomEnumPromise,
} from "./utils/securityrandom/publicSecurityRandomPromise";
import { import {
testSecurityRandomCallback, testSecurityRandomCallback,
testSecurityRandomLengthCallback, testSecurityRandomLengthCallback,
testSecurityRandomCallbackSeed, testSecurityRandomCallbackSeed,
} from "./utils/securityrandom/publicSecurityRandomCallback"; } from "./utils/securityrandom/publicSecurityRandomCallback";
import { testSecurityRandomEnumCommon } from "./utils/securityrandom/publicSecurityRandomCommon";
export default function SecurityRandomJsunit() { export default function SecurityRandomJsunit() {
describe("SecurityRandomJsunit", function () { describe("SecurityRandomJsunit", function () {
...@@ -136,7 +134,7 @@ export default function SecurityRandomJsunit() { ...@@ -136,7 +134,7 @@ export default function SecurityRandomJsunit() {
* @tc.desc cover 100% Enumerated values * @tc.desc cover 100% Enumerated values
*/ */
it("Security_crypto_framework_Random_0500", 0, async function (done) { it("Security_crypto_framework_Random_0500", 0, async function (done) {
testSecurityRandomEnumPromise(); testSecurityRandomEnumCommon();
done(); done();
}); });
}); });
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
import { expect } from "@ohos/hypium"; import { expect } from "@ohos/hypium";
import cryptoFramework from "@ohos.security.cryptoFramework"; import cryptoFramework from "@ohos.security.cryptoFramework";
import cert from "@ohos.security.cert";
import { import {
stringTouInt8Array, stringTouInt8Array,
uInt8ArrayToString, uInt8ArrayToString,
...@@ -38,14 +39,14 @@ function createX509CertInstanceCallback(certType) { ...@@ -38,14 +39,14 @@ function createX509CertInstanceCallback(certType) {
var encodingData; var encodingData;
if (certType == "der") { if (certType == "der") {
certformat = cryptoFramework.EncodingFormat.FORMAT_DER; certformat = cert.EncodingFormat.FORMAT_DER;
encodingData = new Uint8Array( encodingData = new Uint8Array(
selfSignedCaCertDer.match(/[\da-f]{2}/gi).map(function (h) { selfSignedCaCertDer.match(/[\da-f]{2}/gi).map(function (h) {
return parseInt(h, 16); return parseInt(h, 16);
}) })
); );
} else { } else {
certformat = cryptoFramework.EncodingFormat.FORMAT_PEM; certformat = cert.EncodingFormat.FORMAT_PEM;
encodingData = stringTouInt8Array(selfSignedCaCertPem); encodingData = stringTouInt8Array(selfSignedCaCertPem);
} }
...@@ -55,7 +56,7 @@ function createX509CertInstanceCallback(certType) { ...@@ -55,7 +56,7 @@ function createX509CertInstanceCallback(certType) {
}; };
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
cryptoFramework.createX509Cert(encodingBlob, (err, data) => { cert.createX509Cert(encodingBlob, (err, data) => {
if (data == null) { if (data == null) {
console.error( console.error(
"[callback] create X509 Cert failed! err code: " + err.code "[callback] create X509 Cert failed! err code: " + err.code
...@@ -77,10 +78,10 @@ function createX509CertInstanceforCrlTestCallback(flag) { ...@@ -77,10 +78,10 @@ function createX509CertInstanceforCrlTestCallback(flag) {
var invalidParams = false; var invalidParams = false;
if (flag == "normal") { if (flag == "normal") {
certformat = cryptoFramework.EncodingFormat.FORMAT_PEM; certformat = cert.EncodingFormat.FORMAT_PEM;
encodingData = stringTouInt8Array(testCert); encodingData = stringTouInt8Array(testCert);
} else if (flag == "error") { } else if (flag == "error") {
certformat = cryptoFramework.EncodingFormat.FORMAT_PEM; certformat = cert.EncodingFormat.FORMAT_PEM;
encodingData = stringTouInt8Array(testErrorCert); encodingData = stringTouInt8Array(testErrorCert);
} else { } else {
invalidParams = true; invalidParams = true;
...@@ -92,7 +93,7 @@ function createX509CertInstanceforCrlTestCallback(flag) { ...@@ -92,7 +93,7 @@ function createX509CertInstanceforCrlTestCallback(flag) {
}; };
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
cryptoFramework.createX509Cert(encodingBlob, (err, data) => { cert.createX509Cert(encodingBlob, (err, data) => {
if (data == null) { if (data == null) {
console.error( console.error(
"[callback] create X509 Cert failed! err code: " + err.code "[callback] create X509 Cert failed! err code: " + err.code
...@@ -112,22 +113,6 @@ function createX509CertInstanceforCrlTestCallback(flag) { ...@@ -112,22 +113,6 @@ function createX509CertInstanceforCrlTestCallback(flag) {
}); });
} }
async function getX509CertPublicKey(certInstance) {
return new Promise((resolve, reject) => {
certInstance.getPublicKey((err, publicKey) => {
if (err) {
console.error("[Callback]getPublicKey failed. error is " + err);
reject(err);
} else {
console.log(
"[Callback]getPublicKey success. publicKey is " + publicKey
);
resolve(publicKey);
}
});
});
}
async function verifyX509Cert(certInstance, pubKey) { async function verifyX509Cert(certInstance, pubKey) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
certInstance.verify(pubKey, (err) => { certInstance.verify(pubKey, (err) => {
...@@ -144,13 +129,12 @@ async function verifyX509Cert(certInstance, pubKey) { ...@@ -144,13 +129,12 @@ async function verifyX509Cert(certInstance, pubKey) {
async function verifyX509CertCallback(certType, flag) { async function verifyX509CertCallback(certType, flag) {
var gInstance; var gInstance;
var pubKey;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
createX509CertInstanceCallback(certType) createX509CertInstanceCallback(certType)
.then((instance) => { .then((instance) => {
gInstance = instance; gInstance = instance;
return getX509CertPublicKey(gInstance); pubKey = gInstance.getPublicKey();
})
.then((pubKey) => {
expect(pubKey != null).assertTrue(); expect(pubKey != null).assertTrue();
if (flag != undefined) { if (flag != undefined) {
if (flag == "wrong") { if (flag == "wrong") {
...@@ -177,31 +161,12 @@ async function verifyX509CertCallback(certType, flag) { ...@@ -177,31 +161,12 @@ async function verifyX509CertCallback(certType, flag) {
}); });
} }
async function checkValidityWithDate(certInstance, date) {
return new Promise((resolve, reject) => {
certInstance.checkValidityWithDate(date, (err) => {
if (err) {
console.error(
"[Callback]checkValidityWithDate failed. error is " + err
);
reject(err);
} else {
console.log("[Callback]checkValidityWithDate success!");
resolve();
}
});
});
}
async function checkValidityX509CertCallback(certType, date) { async function checkValidityX509CertCallback(certType, date) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
createX509CertInstanceCallback(certType) createX509CertInstanceCallback(certType)
.then((instance) => { .then((instance) => {
return checkValidityWithDate(instance, date); instance.checkValidityWithDate(date);
}) resolve(null);
.then((result) => {
console.warn("[callback] checkValidityX509Cert success!");
resolve(result);
}) })
.catch((err) => { .catch((err) => {
console.error( console.error(
...@@ -353,23 +318,14 @@ async function checkGetEncodedX509CertCallback(certType) { ...@@ -353,23 +318,14 @@ async function checkGetEncodedX509CertCallback(certType) {
} }
async function checkGetPublicKeyX509CertCallback(certType) { async function checkGetPublicKeyX509CertCallback(certType) {
var gInstance; var publicKey;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
createX509CertInstanceCallback(certType) createX509CertInstanceCallback(certType)
.then((instance) => { .then((instance) => {
gInstance = instance; publicKey = instance.getPublicKey();
gInstance.getPublicKey((err, publicKey) => { console.warn("[callback] publicKey success, publicKey is:" + publicKey);
if (err) { expect(publicKey != null).assertTrue();
console.error("[callback] getPublicKey failed, err is:" + err); resolve();
reject(err);
} else {
console.warn(
"[callback] getPublicKey success, publicKey is:" + publicKey
);
expect(publicKey != null).assertTrue();
resolve();
}
});
}) })
.catch((err) => { .catch((err) => {
console.error("[callback] getPublickey failed! error is: " + err); console.error("[callback] getPublickey failed! error is: " + err);
...@@ -383,14 +339,14 @@ function createX509CrlInstanceCallback(certType) { ...@@ -383,14 +339,14 @@ function createX509CrlInstanceCallback(certType) {
var encodingData; var encodingData;
if (certType == "der") { if (certType == "der") {
certformat = cryptoFramework.EncodingFormat.FORMAT_DER; certformat = cert.EncodingFormat.FORMAT_DER;
encodingData = new Uint8Array( encodingData = new Uint8Array(
testCrlDer.match(/[\da-f]{2}/gi).map(function (h) { testCrlDer.match(/[\da-f]{2}/gi).map(function (h) {
return parseInt(h, 16); return parseInt(h, 16);
}) })
); );
} else { } else {
certformat = cryptoFramework.EncodingFormat.FORMAT_PEM; certformat = cert.EncodingFormat.FORMAT_PEM;
encodingData = stringTouInt8Array(testCrlPem); encodingData = stringTouInt8Array(testCrlPem);
} }
...@@ -400,7 +356,7 @@ function createX509CrlInstanceCallback(certType) { ...@@ -400,7 +356,7 @@ function createX509CrlInstanceCallback(certType) {
}; };
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
cryptoFramework.createX509Crl(encodingBlob, (err, data) => { cert.createX509Crl(encodingBlob, (err, data) => {
if (err) { if (err) {
console.error( console.error(
"[callback] createX509Crl failed! error code is: " + err.code "[callback] createX509Crl failed! error code is: " + err.code
...@@ -417,20 +373,13 @@ function createX509CrlInstanceCallback(certType) { ...@@ -417,20 +373,13 @@ function createX509CrlInstanceCallback(certType) {
} }
function checkIsRevokedX509CrlCallback(crlInstance, flag) { function checkIsRevokedX509CrlCallback(crlInstance, flag) {
var status;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
createX509CertInstanceforCrlTestCallback(flag) createX509CertInstanceforCrlTestCallback(flag)
.then((certInstance) => { .then((certInstance) => {
crlInstance.isRevoked(certInstance, (err, status) => { status = crlInstance.isRevoked(certInstance);
if (err) { console.warn("[callback] isRevoked status is:" + status);
console.error( resolve(status);
"[callback] isRevoked failed! err code is:" + err.code
);
reject(err);
} else {
console.warn("[callback] isRevoked status is:" + status);
resolve(status);
}
});
}) })
.catch((err) => { .catch((err) => {
console.error("[callback] isRevoked failed! err is: " + err); console.error("[callback] isRevoked failed! err is: " + err);
...@@ -512,7 +461,7 @@ function verifyX509CrlCallback(crlType, flag) { ...@@ -512,7 +461,7 @@ function verifyX509CrlCallback(crlType, flag) {
var globalCrlInstance; var globalCrlInstance;
if (crlType == "pem" || crlType == "der") { if (crlType == "pem" || crlType == "der") {
certformat = cryptoFramework.EncodingFormat.FORMAT_DER; certformat = cert.EncodingFormat.FORMAT_DER;
priKeyEncodingData = new Uint8Array( priKeyEncodingData = new Uint8Array(
crlVerifyPriKeyHex.match(/[\da-f]{2}/gi).map(function (h) { crlVerifyPriKeyHex.match(/[\da-f]{2}/gi).map(function (h) {
return parseInt(h, 16); return parseInt(h, 16);
...@@ -544,7 +493,7 @@ function verifyX509CrlCallback(crlType, flag) { ...@@ -544,7 +493,7 @@ function verifyX509CrlCallback(crlType, flag) {
var asyKeyGenerator = var asyKeyGenerator =
cryptoFramework.createAsyKeyGenerator("RSA1024|PRIMES_3"); cryptoFramework.createAsyKeyGenerator("RSA1024|PRIMES_3");
expect(asyKeyGenerator != null).assertTrue(); expect(asyKeyGenerator != null).assertTrue();
cryptoFramework cert
.createX509Crl(crlEncodingBlob) .createX509Crl(crlEncodingBlob)
.then((crlInstance) => { .then((crlInstance) => {
expect(crlInstance != null).assertTrue(); expect(crlInstance != null).assertTrue();
...@@ -640,16 +589,11 @@ async function getX509CrlInfoCallback(certType, processType) { ...@@ -640,16 +589,11 @@ async function getX509CrlInfoCallback(certType, processType) {
expect(signatureAlgParams != null).assertTrue(); expect(signatureAlgParams != null).assertTrue();
break; break;
case "getTbsInfo": case "getTbsInfo":
gInstance.getTbsInfo((err, tbsInfo) => { let tbsInfo = gInstance.getTbsInfo();
if (err) { console.log(
console.error("[callback] getTbsInfo failed! error is: " + err); processType + " is : " + uInt8ArrayToString(tbsInfo.data)
reject(err); );
} expect(tbsInfo != null).assertTrue();
expect(tbsInfo != null).assertTrue();
console.log(
processType + " is : " + uInt8ArrayToString(tbsInfo.data)
);
});
break; break;
default: default:
console.log("Invalid parameter !"); console.log("Invalid parameter !");
...@@ -664,32 +608,12 @@ async function getX509CrlInfoCallback(certType, processType) { ...@@ -664,32 +608,12 @@ async function getX509CrlInfoCallback(certType, processType) {
}); });
} }
async function getRevokedCertWithCert(crlInstance, certInstance) {
return new Promise((resolve, reject) => {
crlInstance.getRevokedCertWithCert(certInstance, (err, x509CrlEntry) => {
if (err) {
console.error(
"[Callback]getRevokedCertWithCert failed. error is " + err
);
reject(err);
} else {
console.log(
"[Callback]getRevokedCertWithCert success. x509CrlEntry is " +
x509CrlEntry
);
resolve(x509CrlEntry);
}
});
});
}
function checkGetRevokedCertWithCertX509CrlCallback(crlInstance, flag) { function checkGetRevokedCertWithCertX509CrlCallback(crlInstance, flag) {
var x509CrlEntry;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
createX509CertInstanceforCrlTestCallback(flag) createX509CertInstanceforCrlTestCallback(flag)
.then((certInstance) => { .then((certInstance) => {
return getRevokedCertWithCert(crlInstance, certInstance); x509CrlEntry = crlInstance.getRevokedCertWithCert(certInstance);
})
.then((x509CrlEntry) => {
expect(x509CrlEntry != null).assertTrue(); expect(x509CrlEntry != null).assertTrue();
let num = x509CrlEntry.getSerialNumber(); let num = x509CrlEntry.getSerialNumber();
console.warn("[num] getRevokedCertWithCert num is:" + num); console.warn("[num] getRevokedCertWithCert num is:" + num);
...@@ -705,13 +629,12 @@ function checkGetRevokedCertWithCertX509CrlCallback(crlInstance, flag) { ...@@ -705,13 +629,12 @@ function checkGetRevokedCertWithCertX509CrlCallback(crlInstance, flag) {
} }
async function checkGetRevokedCertsX509CrlCallback(certType) { async function checkGetRevokedCertsX509CrlCallback(certType) {
var gInstance;
var gIndex; var gIndex;
var revocation;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
createX509CrlInstanceCallback(certType) createX509CrlInstanceCallback(certType)
.then((instance) => { .then((instance) => {
gInstance = instance; instance.getRevokedCerts((err, certs) => {
gInstance.getRevokedCerts((err, certs) => {
expect(certs != null && certs.length != 0).assertTrue(); expect(certs != null && certs.length != 0).assertTrue();
console.warn("[callback] Crl get gevoked certs success"); console.warn("[callback] Crl get gevoked certs success");
for (var i = 0; i < certs.length; i++) { for (var i = 0; i < certs.length; i++) {
...@@ -722,20 +645,19 @@ async function checkGetRevokedCertsX509CrlCallback(certType) { ...@@ -722,20 +645,19 @@ async function checkGetRevokedCertsX509CrlCallback(certType) {
" serialNumber is: " + " serialNumber is: " +
certs[gIndex].getSerialNumber() certs[gIndex].getSerialNumber()
); );
certs[gIndex].getRevocationDate((err1, revocation) => { revocation = certs[gIndex].getRevocationDate();
expect(revocation != null).assertTrue(); expect(revocation != null).assertTrue();
console.log(
"[callback] certs i: " +
gIndex +
" revocation date is: " +
revocation
);
certs[gIndex].getEncoded((err2, eData) => {
expect(eData != null).assertTrue();
console.log( console.log(
"[callback] certs i: " + "[callback] certs i: " + gIndex + " getEncoded is: " + eData
gIndex +
" revocation date is: " +
revocation
); );
certs[gIndex].getEncoded((err2, eData) => {
expect(eData != null).assertTrue();
console.log(
"[callback] certs i: " + gIndex + " getEncoded is: " + eData
);
});
}); });
} }
}); });
...@@ -749,26 +671,24 @@ async function checkGetRevokedCertsX509CrlCallback(certType) { ...@@ -749,26 +671,24 @@ async function checkGetRevokedCertsX509CrlCallback(certType) {
} }
async function checkGetRevokedCertX509CrlCallback(certType) { async function checkGetRevokedCertX509CrlCallback(certType) {
var gInstance; var certIssuer;
var cert1;
var revocation;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
createX509CrlInstanceCallback(certType) createX509CrlInstanceCallback(certType)
.then((instance) => { .then((instance) => {
gInstance = instance; cert1 = instance.getRevokedCert(1);
gInstance.getRevokedCert(1, (err, cert) => { expect(cert1 != null).assertTrue();
expect(cert != null).assertTrue(); console.warn("[callback] Crl get gevoked cert success");
console.warn("[callback] Crl get gevoked cert success"); revocation = cert1.getRevocationDate();
cert.getRevocationDate((err1, revocation) => { expect(revocation != null).assertTrue();
expect(revocation != null).assertTrue(); console.log("[callback] cert revocation date is: " + revocation);
console.log("[callback] cert revocation date is: " + revocation); certIssuer = cert1.getCertIssuer();
}); expect(certIssuer != null).assertTrue();
cert.getCertIssuer((err2, certIssuer) => { console.log(
expect(certIssuer != null).assertTrue(); "[Callback] cert certIssuer is: " +
console.log( +uInt8ArrayToString(certIssuer.data)
"[callback] cert certIssuer is: " + );
uInt8ArrayToString(certIssuer.data)
);
});
});
resolve(); resolve();
}) })
.catch((err) => { .catch((err) => {
...@@ -781,7 +701,7 @@ async function checkGetRevokedCertX509CrlCallback(certType) { ...@@ -781,7 +701,7 @@ async function checkGetRevokedCertX509CrlCallback(certType) {
function checkValidateOfCertChainValidatorCallback(algName, flag) { function checkValidateOfCertChainValidatorCallback(algName, flag) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
try { try {
var validator = cryptoFramework.createCertChainValidator(algName); var validator = cert.createCertChainValidator(algName);
var algorithm = validator.algorithm; var algorithm = validator.algorithm;
console.log( console.log(
"createCertChainValidator success! algorithm is: " + algorithm "createCertChainValidator success! algorithm is: " + algorithm
...@@ -835,7 +755,7 @@ function checkValidateOfCertChainValidatorCallback(algName, flag) { ...@@ -835,7 +755,7 @@ function checkValidateOfCertChainValidatorCallback(algName, flag) {
var certChainData = { var certChainData = {
data: dataArray, data: dataArray,
count: 2, count: 2,
encodingFormat: cryptoFramework.EncodingFormat.FORMAT_PEM, encodingFormat: cert.EncodingFormat.FORMAT_PEM,
}; };
console.log("certChainData.data is: " + certChainData.data); console.log("certChainData.data is: " + certChainData.data);
validator.validate(certChainData, (err) => { validator.validate(certChainData, (err) => {
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
import { expect } from "@ohos/hypium"; import { expect } from "@ohos/hypium";
import cryptoFramework from "@ohos.security.cryptoFramework"; import cryptoFramework from "@ohos.security.cryptoFramework";
import cert from "@ohos.security.cert";
import { import {
stringTouInt8Array, stringTouInt8Array,
uInt8ArrayToString, uInt8ArrayToString,
...@@ -38,14 +39,14 @@ function createX509CertInstancePromise(certType) { ...@@ -38,14 +39,14 @@ function createX509CertInstancePromise(certType) {
var encodingData; var encodingData;
if (certType == "der") { if (certType == "der") {
certformat = cryptoFramework.EncodingFormat.FORMAT_DER; certformat = cert.EncodingFormat.FORMAT_DER;
encodingData = new Uint8Array( encodingData = new Uint8Array(
selfSignedCaCertDer.match(/[\da-f]{2}/gi).map(function (h) { selfSignedCaCertDer.match(/[\da-f]{2}/gi).map(function (h) {
return parseInt(h, 16); return parseInt(h, 16);
}) })
); );
} else { } else {
certformat = cryptoFramework.EncodingFormat.FORMAT_PEM; certformat = cert.EncodingFormat.FORMAT_PEM;
encodingData = stringTouInt8Array(selfSignedCaCertPem); encodingData = stringTouInt8Array(selfSignedCaCertPem);
} }
...@@ -55,7 +56,7 @@ function createX509CertInstancePromise(certType) { ...@@ -55,7 +56,7 @@ function createX509CertInstancePromise(certType) {
}; };
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
cryptoFramework cert
.createX509Cert(encodingBlob) .createX509Cert(encodingBlob)
.then((data) => { .then((data) => {
console.warn( console.warn(
...@@ -78,10 +79,10 @@ function createX509CertInstanceforCrlTestPromise(flag) { ...@@ -78,10 +79,10 @@ function createX509CertInstanceforCrlTestPromise(flag) {
var invalidParams = false; var invalidParams = false;
if (flag == "normal") { if (flag == "normal") {
certformat = cryptoFramework.EncodingFormat.FORMAT_PEM; certformat = cert.EncodingFormat.FORMAT_PEM;
encodingData = stringTouInt8Array(testCert); encodingData = stringTouInt8Array(testCert);
} else if (flag == "error") { } else if (flag == "error") {
certformat = cryptoFramework.EncodingFormat.FORMAT_PEM; certformat = cert.EncodingFormat.FORMAT_PEM;
encodingData = stringTouInt8Array(testErrorCert); encodingData = stringTouInt8Array(testErrorCert);
} else { } else {
invalidParams = true; invalidParams = true;
...@@ -93,7 +94,7 @@ function createX509CertInstanceforCrlTestPromise(flag) { ...@@ -93,7 +94,7 @@ function createX509CertInstanceforCrlTestPromise(flag) {
}; };
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
cryptoFramework cert
.createX509Cert(encodingBlob) .createX509Cert(encodingBlob)
.then((data) => { .then((data) => {
console.warn( console.warn(
...@@ -116,13 +117,12 @@ function createX509CertInstanceforCrlTestPromise(flag) { ...@@ -116,13 +117,12 @@ function createX509CertInstanceforCrlTestPromise(flag) {
async function verifyX509CertPromise(certType, flag) { async function verifyX509CertPromise(certType, flag) {
var gInstance; var gInstance;
var publicKey;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
createX509CertInstancePromise(certType) createX509CertInstancePromise(certType)
.then((instance) => { .then((instance) => {
gInstance = instance; gInstance = instance;
return gInstance.getPublicKey(); publicKey = gInstance.getPublicKey();
})
.then((publicKey) => {
expect(publicKey != null).assertTrue(); expect(publicKey != null).assertTrue();
console.log("[promise] getPublickey success!"); console.log("[promise] getPublickey success!");
if (flag != undefined) { if (flag != undefined) {
...@@ -149,15 +149,11 @@ async function verifyX509CertPromise(certType, flag) { ...@@ -149,15 +149,11 @@ async function verifyX509CertPromise(certType, flag) {
} }
async function checkValidityX509CertPromise(certType, date) { async function checkValidityX509CertPromise(certType, date) {
var gInstance; var result;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
createX509CertInstancePromise(certType) createX509CertInstancePromise(certType)
.then((instance) => { .then((instance) => {
gInstance = instance; result = instance.checkValidityWithDate(date);
return gInstance.checkValidityWithDate(date);
})
.then((result) => {
console.warn("[promise] checkValidityX509Cert success!");
resolve(result); resolve(result);
}) })
.catch((err) => { .catch((err) => {
...@@ -304,14 +300,11 @@ async function checkGetEncodedX509CertPromise(certType) { ...@@ -304,14 +300,11 @@ async function checkGetEncodedX509CertPromise(certType) {
} }
async function checkGetPublicKeyX509CertPromise(certType) { async function checkGetPublicKeyX509CertPromise(certType) {
var gInstance; var publicKey;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
createX509CertInstancePromise(certType) createX509CertInstancePromise(certType)
.then((instance) => { .then((instance) => {
gInstance = instance; publicKey = instance.getPublicKey();
return gInstance.getPublicKey();
})
.then((publicKey) => {
console.warn("[promise] publicKey success, publicKey is:" + publicKey); console.warn("[promise] publicKey success, publicKey is:" + publicKey);
expect(publicKey != null).assertTrue(); expect(publicKey != null).assertTrue();
resolve(); resolve();
...@@ -328,14 +321,14 @@ function createX509CrlInstancePromise(certType) { ...@@ -328,14 +321,14 @@ function createX509CrlInstancePromise(certType) {
var encodingData; var encodingData;
if (certType == "der") { if (certType == "der") {
certformat = cryptoFramework.EncodingFormat.FORMAT_DER; certformat = cert.EncodingFormat.FORMAT_DER;
encodingData = new Uint8Array( encodingData = new Uint8Array(
testCrlDer.match(/[\da-f]{2}/gi).map(function (h) { testCrlDer.match(/[\da-f]{2}/gi).map(function (h) {
return parseInt(h, 16); return parseInt(h, 16);
}) })
); );
} else { } else {
certformat = cryptoFramework.EncodingFormat.FORMAT_PEM; certformat = cert.EncodingFormat.FORMAT_PEM;
encodingData = stringTouInt8Array(testCrlPem); encodingData = stringTouInt8Array(testCrlPem);
} }
...@@ -345,7 +338,7 @@ function createX509CrlInstancePromise(certType) { ...@@ -345,7 +338,7 @@ function createX509CrlInstancePromise(certType) {
}; };
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
cryptoFramework cert
.createX509Crl(encodingBlob) .createX509Crl(encodingBlob)
.then((data) => { .then((data) => {
console.warn( console.warn(
...@@ -363,13 +356,11 @@ function createX509CrlInstancePromise(certType) { ...@@ -363,13 +356,11 @@ function createX509CrlInstancePromise(certType) {
} }
function checkIsRevokedX509CrlPromise(crlInstance, flag) { function checkIsRevokedX509CrlPromise(crlInstance, flag) {
var status;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
createX509CertInstanceforCrlTestPromise(flag) createX509CertInstanceforCrlTestPromise(flag)
.then((certInstance) => { .then((certInstance) => {
let status = crlInstance.isRevoked(certInstance); status = crlInstance.isRevoked(certInstance);
return status;
})
.then((status) => {
console.warn("[promise] isRevoked status is:" + status); console.warn("[promise] isRevoked status is:" + status);
resolve(status); resolve(status);
}) })
...@@ -413,7 +404,7 @@ function verifyX509CrlPromise(crlType, flag) { ...@@ -413,7 +404,7 @@ function verifyX509CrlPromise(crlType, flag) {
var crlEncodingData; var crlEncodingData;
var globalCrlInstance; var globalCrlInstance;
if (crlType == "pem" || crlType == "der") { if (crlType == "pem" || crlType == "der") {
certFormat = cryptoFramework.EncodingFormat.FORMAT_DER; certFormat = cert.EncodingFormat.FORMAT_DER;
priKeyEncodingData = new Uint8Array( priKeyEncodingData = new Uint8Array(
crlVerifyPriKeyHex.match(/[\da-f]{2}/gi).map(function (h) { crlVerifyPriKeyHex.match(/[\da-f]{2}/gi).map(function (h) {
return parseInt(h, 16); return parseInt(h, 16);
...@@ -446,7 +437,7 @@ function verifyX509CrlPromise(crlType, flag) { ...@@ -446,7 +437,7 @@ function verifyX509CrlPromise(crlType, flag) {
var asyKeyGenerator = var asyKeyGenerator =
cryptoFramework.createAsyKeyGenerator("RSA1024|PRIMES_3"); cryptoFramework.createAsyKeyGenerator("RSA1024|PRIMES_3");
expect(asyKeyGenerator != null); expect(asyKeyGenerator != null);
cryptoFramework cert
.createX509Crl(crlEncodingBlob) .createX509Crl(crlEncodingBlob)
.then((crlInstance) => { .then((crlInstance) => {
expect(crlInstance != null).assertTrue(); expect(crlInstance != null).assertTrue();
...@@ -542,18 +533,11 @@ async function getX509CrlInfoPromise(certType, processType) { ...@@ -542,18 +533,11 @@ async function getX509CrlInfoPromise(certType, processType) {
expect(signatureAlgParams != null).assertTrue(); expect(signatureAlgParams != null).assertTrue();
break; break;
case "getTbsInfo": case "getTbsInfo":
gInstance let tbsInfo = gInstance.getTbsInfo();
.getTbsInfo() console.log(
.then((tbsInfo) => { processType + " is : " + uInt8ArrayToString(tbsInfo.data)
expect(tbsInfo != null).assertTrue(); );
console.log( expect(tbsInfo != null).assertTrue();
processType + " is : " + uInt8ArrayToString(tbsInfo.data)
);
})
.catch((err) => {
console.error("[promise] getTbsInfo failed! error is: " + err);
reject(err);
});
break; break;
default: default:
console.log("Invalid parameter !"); console.log("Invalid parameter !");
...@@ -573,9 +557,6 @@ function checkGetRevokedCertWithCertX509CrlPromise(crlInstance, flag) { ...@@ -573,9 +557,6 @@ function checkGetRevokedCertWithCertX509CrlPromise(crlInstance, flag) {
createX509CertInstanceforCrlTestPromise(flag) createX509CertInstanceforCrlTestPromise(flag)
.then((certInstance) => { .then((certInstance) => {
let crlEntry = crlInstance.getRevokedCertWithCert(certInstance); let crlEntry = crlInstance.getRevokedCertWithCert(certInstance);
return crlEntry;
})
.then((crlEntry) => {
console.warn("[promise] getRevokedCertWithCert is:" + crlEntry); console.warn("[promise] getRevokedCertWithCert is:" + crlEntry);
let num = crlEntry.getSerialNumber(); let num = crlEntry.getSerialNumber();
console.warn("[promise] getRevokedCertWithCert num is:" + num); console.warn("[promise] getRevokedCertWithCert num is:" + num);
...@@ -589,13 +570,12 @@ function checkGetRevokedCertWithCertX509CrlPromise(crlInstance, flag) { ...@@ -589,13 +570,12 @@ function checkGetRevokedCertWithCertX509CrlPromise(crlInstance, flag) {
} }
async function checkGetRevokedCertsX509CrlPromise(certType) { async function checkGetRevokedCertsX509CrlPromise(certType) {
var gInstance;
var gIndex; var gIndex;
var revocation;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
createX509CrlInstancePromise(certType) createX509CrlInstancePromise(certType)
.then((instance) => { .then((instance) => {
gInstance = instance; return instance.getRevokedCerts();
return gInstance.getRevokedCerts();
}) })
.then((certs) => { .then((certs) => {
expect(certs != null && certs.length != 0).assertTrue(); expect(certs != null && certs.length != 0).assertTrue();
...@@ -608,20 +588,14 @@ async function checkGetRevokedCertsX509CrlPromise(certType) { ...@@ -608,20 +588,14 @@ async function checkGetRevokedCertsX509CrlPromise(certType) {
" serialNumber is: " + " serialNumber is: " +
certs[gIndex].getSerialNumber() certs[gIndex].getSerialNumber()
); );
certs[gIndex] revocation = certs[gIndex].getRevocationDate();
.getRevocationDate() expect(revocation != null).assertTrue();
.then((revocation) => { console.log(
expect(revocation != null).assertTrue(); "[Promise] certs i: " +
console.log( gIndex +
"[Promise] certs i: " + " revocation date is: " +
gIndex + revocation
" revocation date is: " + );
revocation
);
})
.catch((err) => {
reject(err);
});
certs[gIndex] certs[gIndex]
.getEncoded() .getEncoded()
.then((eData) => { .then((eData) => {
...@@ -647,39 +621,23 @@ async function checkGetRevokedCertsX509CrlPromise(certType) { ...@@ -647,39 +621,23 @@ async function checkGetRevokedCertsX509CrlPromise(certType) {
} }
async function checkGetRevokedCertX509CrlPromise(certType) { async function checkGetRevokedCertX509CrlPromise(certType) {
var gInstance; var certIssuer;
var revocation;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
createX509CrlInstancePromise(certType) createX509CrlInstancePromise(certType)
.then((instance) => { .then((instance) => {
gInstance = instance; let cert1 = instance.getRevokedCert(1);
return gInstance.getRevokedCert(1); expect(cert1 != null).assertTrue();
})
.then((cert) => {
expect(cert != null).assertTrue();
console.warn("[Promise] Crl get gevoked certs success"); console.warn("[Promise] Crl get gevoked certs success");
cert revocation = cert1.getRevocationDate();
.getRevocationDate() expect(revocation != null).assertTrue();
.then((revocation) => { console.log("[Promise] cert revocation date is: " + revocation);
expect(revocation != null).assertTrue(); certIssuer = cert1.getCertIssuer();
console.log("[Promise] cert revocation date is: " + revocation); expect(certIssuer != null).assertTrue();
}) console.log(
.catch((err) => { "[Promise] cert certIssuer is: " +
console.error("[Promise] cert revocation date is: " + err); +uInt8ArrayToString(certIssuer.data)
reject(err); );
});
cert
.getCertIssuer()
.then((certIssuer) => {
expect(certIssuer != null).assertTrue();
console.log(
"[Promise] cert certIssuer is: " +
+uInt8ArrayToString(certIssuer.data)
);
})
.catch((err) => {
console.error("[Promise] cert certIssuer is: " + err);
reject(err);
});
resolve(); resolve();
}) })
.catch((err) => { .catch((err) => {
...@@ -692,7 +650,7 @@ async function checkGetRevokedCertX509CrlPromise(certType) { ...@@ -692,7 +650,7 @@ async function checkGetRevokedCertX509CrlPromise(certType) {
function checkValidateOfCertChainValidatorPromise(algName, flag) { function checkValidateOfCertChainValidatorPromise(algName, flag) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
try { try {
var validator = cryptoFramework.createCertChainValidator(algName); var validator = cert.createCertChainValidator(algName);
var algorithm = validator.algorithm; var algorithm = validator.algorithm;
console.log( console.log(
"createCertChainValidator success! algorithm is: " + algorithm "createCertChainValidator success! algorithm is: " + algorithm
...@@ -746,7 +704,7 @@ function checkValidateOfCertChainValidatorPromise(algName, flag) { ...@@ -746,7 +704,7 @@ function checkValidateOfCertChainValidatorPromise(algName, flag) {
var certChainData = { var certChainData = {
data: dataArray, data: dataArray,
count: 2, count: 2,
encodingFormat: cryptoFramework.EncodingFormat.FORMAT_PEM, encodingFormat: cert.EncodingFormat.FORMAT_PEM,
}; };
console.log("certChainData.data is: " + certChainData.data); console.log("certChainData.data is: " + certChainData.data);
validator validator
......
...@@ -35,13 +35,12 @@ async function testSecurityRandomCallback(length) { ...@@ -35,13 +35,12 @@ async function testSecurityRandomCallback(length) {
console.log( console.log(
"[callback] rand result first:" + uInt8ArrayToShowStr(randData.data) "[callback] rand result first:" + uInt8ArrayToShowStr(randData.data)
); );
} try {
rand.setSeed(randData, (err1) => { rand.setSeed(randData);
if (err1) { } catch (err1) {
console.error("[callback]setSeed catch err:" + err1);
reject(err1); reject(err1);
} }
}); }
}); });
rand.generateRandom(length, (err, randData) => { rand.generateRandom(length, (err, randData) => {
...@@ -64,10 +63,15 @@ async function testSecurityRandomLengthCallback(length) { ...@@ -64,10 +63,15 @@ async function testSecurityRandomLengthCallback(length) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
rand = cryptoFramework.createRandom(); rand = cryptoFramework.createRandom();
expect(rand != null).assertTrue(); expect(rand != null).assertTrue();
if(length != null) { if (length != null) {
rand.generateRandom(length, (err, randData) => { rand.generateRandom(length, (err, randData) => {
if (err) { if (err) {
console.error("[callback]generateRandom catch error:" + err + "[callback]: error code: " + err.code); console.error(
"[callback]generateRandom catch error:" +
err +
"[callback]: error code: " +
err.code
);
expect(err.code == 401).assertTrue(); expect(err.code == 401).assertTrue();
resolve(); resolve();
} else { } else {
...@@ -76,31 +80,45 @@ async function testSecurityRandomLengthCallback(length) { ...@@ -76,31 +80,45 @@ async function testSecurityRandomLengthCallback(length) {
} }
}); });
} else { } else {
try{ try {
rand.generateRandom(length); rand.generateRandom(length);
reject(); reject();
} catch (error) { } catch (error) {
console.error("[callback]generateRandom catch err1:" + error + "[callback]: error code: " + error.code); console.error(
"[callback]generateRandom catch err1:" +
error +
"[callback]: error code: " +
error.code
);
resolve(); resolve();
} }
} }
}); });
} }
async function testSecurityRandomCallbackSeed(length) { async function testSecurityRandomCallbackSeed(data) {
var rand; var rand;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
rand = cryptoFramework.createRandom(); rand = cryptoFramework.createRandom();
expect(rand != null).assertTrue(); expect(rand != null).assertTrue();
try{ try {
rand.setSeed(length); rand.setSeed(data);
reject(); reject();
} catch (error) { } catch (error) {
console.error("[callback]setSeed catch err:" + error + "[callback]: error code: " + error.code); console.error(
"[callback]setSeed catch err:" +
error +
"[callback]: error code: " +
error.code
);
expect(error.code == 401).assertTrue(); expect(error.code == 401).assertTrue();
resolve(); resolve();
} }
}); });
} }
export { testSecurityRandomCallback, testSecurityRandomLengthCallback, testSecurityRandomCallbackSeed }; export {
testSecurityRandomCallback,
testSecurityRandomLengthCallback,
testSecurityRandomCallbackSeed,
};
/*
* Copyright (C) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { expect } from "@ohos/hypium";
import cryptoFramework from "@ohos.security.cryptoFramework";
import cert from "@ohos.security.cert";
function testSecurityRandomEnumCommon() {
expect(401).assertEqual(cryptoFramework.Result.INVALID_PARAMS);
expect(401).assertEqual(cert.CertResult.INVALID_PARAMS);
expect(801).assertEqual(cryptoFramework.Result.NOT_SUPPORT);
expect(801).assertEqual(cert.CertResult.NOT_SUPPORT);
expect(17620001).assertEqual(cryptoFramework.Result.ERR_OUT_OF_MEMORY);
expect(19020001).assertEqual(cert.CertResult.ERR_OUT_OF_MEMORY);
expect(17620002).assertEqual(cryptoFramework.Result.ERR_RUNTIME_ERROR);
expect(19020002).assertEqual(cert.CertResult.ERR_RUNTIME_ERROR);
expect(17630001).assertEqual(cryptoFramework.Result.ERR_CRYPTO_OPERATION);
expect(19030001).assertEqual(cert.CertResult.ERR_CRYPTO_OPERATION);
expect(19030002).assertEqual(cert.CertResult.ERR_CERT_SIGNATURE_FAILURE);
expect(19030003).assertEqual(cert.CertResult.ERR_CERT_NOT_YET_VALID);
expect(19030004).assertEqual(cert.CertResult.ERR_CERT_HAS_EXPIRED);
expect(19030005).assertEqual(
cert.CertResult.ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY
);
expect(19030006).assertEqual(cert.CertResult.ERR_KEYUSAGE_NO_CERTSIGN);
expect(19030007).assertEqual(
cert.CertResult.ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE
);
expect(0).assertEqual(cert.EncodingFormat.FORMAT_DER);
expect(1).assertEqual(cert.EncodingFormat.FORMAT_PEM);
expect(0).assertEqual(cryptoFramework.CryptoMode.ENCRYPT_MODE);
expect(1).assertEqual(cryptoFramework.CryptoMode.DECRYPT_MODE);
}
export { testSecurityRandomEnumCommon };
...@@ -60,28 +60,4 @@ async function testSecurityRandomPromise(length) { ...@@ -60,28 +60,4 @@ async function testSecurityRandomPromise(length) {
}); });
} }
function testSecurityRandomEnumPromise() { export { testSecurityRandomPromise };
expect(401).assertEqual(cryptoFramework.Result.INVALID_PARAMS);
expect(801).assertEqual(cryptoFramework.Result.NOT_SUPPORT);
expect(17620001).assertEqual(cryptoFramework.Result.ERR_OUT_OF_MEMORY);
expect(17620002).assertEqual(cryptoFramework.Result.ERR_RUNTIME_ERROR);
expect(17630001).assertEqual(cryptoFramework.Result.ERR_CRYPTO_OPERATION);
expect(17630002).assertEqual(
cryptoFramework.Result.ERR_CERT_SIGNATURE_FAILURE
);
expect(17630003).assertEqual(cryptoFramework.Result.ERR_CERT_NOT_YET_VALID);
expect(17630004).assertEqual(cryptoFramework.Result.ERR_CERT_HAS_EXPIRED);
expect(17630005).assertEqual(
cryptoFramework.Result.ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY
);
expect(17630006).assertEqual(cryptoFramework.Result.ERR_KEYUSAGE_NO_CERTSIGN);
expect(17630007).assertEqual(
cryptoFramework.Result.ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE
);
expect(0).assertEqual(cryptoFramework.EncodingFormat.FORMAT_DER);
expect(1).assertEqual(cryptoFramework.EncodingFormat.FORMAT_PEM);
expect(0).assertEqual(cryptoFramework.CryptoMode.ENCRYPT_MODE);
expect(1).assertEqual(cryptoFramework.CryptoMode.DECRYPT_MODE);
}
export { testSecurityRandomPromise, testSecurityRandomEnumPromise };
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册