Path: blob/master/core/crypto/crypto_resource_format.cpp
45997 views
/**************************************************************************/1/* crypto_resource_format.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "crypto_resource_format.h"3132#include "core/crypto/crypto.h"3334Ref<Resource> ResourceFormatLoaderCrypto::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode) {35String el = p_path.get_extension().to_lower();36if (el == "crt") {37X509Certificate *cert = X509Certificate::create();38if (cert) {39cert->load(p_path);40}41return cert;42} else if (el == "key") {43CryptoKey *key = CryptoKey::create();44if (key) {45key->load(p_path, false);46}47return key;48} else if (el == "pub") {49CryptoKey *key = CryptoKey::create();50if (key) {51key->load(p_path, true);52}53return key;54}55return nullptr;56}5758void ResourceFormatLoaderCrypto::get_recognized_extensions(List<String> *p_extensions) const {59p_extensions->push_back("crt");60p_extensions->push_back("key");61p_extensions->push_back("pub");62}6364bool ResourceFormatLoaderCrypto::handles_type(const String &p_type) const {65return p_type == "X509Certificate" || p_type == "CryptoKey";66}6768String ResourceFormatLoaderCrypto::get_resource_type(const String &p_path) const {69String el = p_path.get_extension().to_lower();70if (el == "crt") {71return "X509Certificate";72} else if (el == "key" || el == "pub") {73return "CryptoKey";74}75return "";76}7778Error ResourceFormatSaverCrypto::save(const Ref<Resource> &p_resource, const String &p_path, uint32_t p_flags) {79Error err;80Ref<X509Certificate> cert = p_resource;81Ref<CryptoKey> key = p_resource;82if (cert.is_valid()) {83err = cert->save(p_path);84} else if (key.is_valid()) {85err = key->save(p_path, p_path.has_extension("pub"));86} else {87ERR_FAIL_V(ERR_INVALID_PARAMETER);88}89ERR_FAIL_COND_V_MSG(err != OK, err, vformat("Cannot save Crypto resource to file '%s'.", p_path));90return OK;91}9293void ResourceFormatSaverCrypto::get_recognized_extensions(const Ref<Resource> &p_resource, List<String> *p_extensions) const {94const X509Certificate *cert = Object::cast_to<X509Certificate>(*p_resource);95const CryptoKey *key = Object::cast_to<CryptoKey>(*p_resource);96if (cert) {97p_extensions->push_back("crt");98}99if (key) {100if (!key->is_public_only()) {101p_extensions->push_back("key");102}103p_extensions->push_back("pub");104}105}106107bool ResourceFormatSaverCrypto::recognize(const Ref<Resource> &p_resource) const {108return Object::cast_to<X509Certificate>(*p_resource) || Object::cast_to<CryptoKey>(*p_resource);109}110111112