Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / content / renderer / webcrypto / webcrypto_util.cc
index f8f7068..3124afa 100644 (file)
@@ -13,9 +13,155 @@ namespace content {
 
 namespace webcrypto {
 
-namespace {
+bool Status::IsError() const {
+  return type_ == TYPE_ERROR;
+}
+
+bool Status::IsSuccess() const {
+  return type_ == TYPE_SUCCESS;
+}
+
+bool Status::HasErrorDetails() const {
+  return !error_details_.empty();
+}
+
+std::string Status::ToString() const {
+  return IsSuccess() ? "Success" : error_details_;
+}
+
+Status Status::Success() {
+  return Status(TYPE_SUCCESS);
+}
+
+Status Status::Error() {
+  return Status(TYPE_ERROR);
+}
+
+Status Status::ErrorJwkNotDictionary() {
+  return Status("JWK input could not be parsed to a JSON dictionary");
+}
+
+Status Status::ErrorJwkPropertyMissing(const std::string& property) {
+  return Status("The required JWK property \"" + property + "\" was missing");
+}
+
+Status Status::ErrorJwkPropertyWrongType(const std::string& property,
+                                         const std::string& expected_type) {
+  return Status("The JWK property \"" + property + "\" must be a " +
+                expected_type);
+}
+
+Status Status::ErrorJwkBase64Decode(const std::string& property) {
+  return Status("The JWK property \"" + property +
+                "\" could not be base64 decoded");
+}
+
+Status Status::ErrorJwkExtractableInconsistent() {
+  return Status("The \"extractable\" property of the JWK dictionary is "
+                "inconsistent what that specified by the Web Crypto call");
+}
+
+Status Status::ErrorJwkUnrecognizedAlgorithm() {
+  return Status("The JWK \"alg\" property was not recognized");
+}
+
+Status Status::ErrorJwkAlgorithmInconsistent() {
+  return Status("The JWK \"alg\" property was inconsistent with that specified "
+                "by the Web Crypto call");
+}
+
+Status Status::ErrorJwkAlgorithmMissing() {
+  return Status("The JWK optional \"alg\" property is missing or not a string, "
+                "and one wasn't specified by the Web Crypto call");
+}
+
+Status Status::ErrorJwkUnrecognizedUsage() {
+  return Status("The JWK \"use\" property could not be parsed");
+}
+
+Status Status::ErrorJwkUsageInconsistent() {
+  return Status("The JWK \"use\" property was inconsistent with that specified "
+                "by the Web Crypto call. The JWK usage must be a superset of "
+                "those requested");
+}
+
+Status Status::ErrorJwkRsaPrivateKeyUnsupported() {
+  return Status("JWK RSA key contained \"d\" property: Private key import is "
+                "not yet supported");
+}
+
+Status Status::ErrorJwkUnrecognizedKty() {
+  return Status("The JWK \"kty\" property was unrecognized");
+}
+
+Status Status::ErrorJwkIncorrectKeyLength() {
+  return Status("The JWK \"k\" property did not include the right length "
+                "of key data for the given algorithm.");
+}
+
+Status Status::ErrorImportEmptyKeyData() {
+  return Status("No key data was provided");
+}
+
+Status Status::ErrorUnexpectedKeyType() {
+  return Status("The key is not of the expected type");
+}
+
+Status Status::ErrorIncorrectSizeAesCbcIv() {
+  return Status("The \"iv\" has an unexpected length -- must be 16 bytes");
+}
+
+Status Status::ErrorDataTooLarge() {
+  return Status("The provided data is too large");
+}
+
+Status Status::ErrorUnsupported() {
+  return Status("The requested operation is unsupported");
+}
+
+Status Status::ErrorUnexpected() {
+  return Status("Something unexpected happened...");
+}
+
+Status Status::ErrorInvalidAesGcmTagLength() {
+  return Status("The tag length is invalid: either too large or not a multiple "
+                "of 8 bits");
+}
+
+Status Status::ErrorGenerateKeyPublicExponent() {
+  return Status("The \"publicExponent\" is either empty, zero, or too large");
+}
+
+Status Status::ErrorMissingAlgorithmImportRawKey() {
+  return Status("The key's algorithm must be specified when importing "
+                "raw-formatted key.");
+}
+
+Status Status::ErrorImportRsaEmptyModulus() {
+  return Status("The modulus is empty");
+}
+
+Status Status::ErrorGenerateRsaZeroModulus() {
+  return Status("The modulus bit length cannot be zero");
+}
+
+Status Status::ErrorImportRsaEmptyExponent() {
+  return Status("No bytes for the exponent were provided");
+}
+
+Status Status::ErrorKeyNotExtractable() {
+  return Status("They key is not extractable");
+}
+
+Status Status::ErrorGenerateKeyLength() {
+  return Status("Invalid key length: it is either zero or not a multiple of 8 "
+                "bits");
+}
+
+Status::Status(const std::string& error_details_utf8)
+    : type_(TYPE_ERROR), error_details_(error_details_utf8) {}
 
-}  // namespace
+Status::Status(Type type) : type_(type) {}
 
 const uint8* Uint8VectorStart(const std::vector<uint8>& data) {
   if (data.empty())
@@ -23,7 +169,7 @@ const uint8* Uint8VectorStart(const std::vector<uint8>& data) {
   return &data[0];
 }
 
-void ShrinkBuffer(blink::WebArrayBuffer* buffer, unsigned new_size) {
+void ShrinkBuffer(blink::WebArrayBuffer* buffer, unsigned int new_size) {
   DCHECK_LE(new_size, buffer->byteLength());
 
   if (new_size == buffer->byteLength())
@@ -35,7 +181,8 @@ void ShrinkBuffer(blink::WebArrayBuffer* buffer, unsigned new_size) {
   *buffer = new_buffer;
 }
 
-blink::WebArrayBuffer CreateArrayBuffer(const uint8* data, unsigned data_size) {
+blink::WebArrayBuffer CreateArrayBuffer(const uint8* data,
+                                        unsigned int data_size) {
   blink::WebArrayBuffer buffer = blink::WebArrayBuffer::create(data_size, 1);
   DCHECK(!buffer.isNull());
   if (data_size)  // data_size == 0 might mean the data pointer is invalid
@@ -102,7 +249,7 @@ blink::WebCryptoAlgorithm CreateHmacAlgorithmByHashId(
 
 blink::WebCryptoAlgorithm CreateHmacKeyGenAlgorithm(
     blink::WebCryptoAlgorithmId hash_id,
-    unsigned key_length_bytes) {
+    unsigned int key_length_bytes) {
   DCHECK(IsHashAlgorithm(hash_id));
   // key_length_bytes == 0 means unspecified
   return blink::WebCryptoAlgorithm::adoptParamsAndCreate(
@@ -130,7 +277,7 @@ blink::WebCryptoAlgorithm CreateRsaOaepAlgorithm(
 
 blink::WebCryptoAlgorithm CreateRsaKeyGenAlgorithm(
     blink::WebCryptoAlgorithmId algorithm_id,
-    unsigned modulus_length,
+    unsigned int modulus_length,
     const std::vector<uint8>& public_exponent) {
   DCHECK(algorithm_id == blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5 ||
          algorithm_id == blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 ||