*/
using System;
+using Tizen.Internals.Errors;
internal static partial class Interop
{
private const int TizenErrorKeyManager = -0x01E10000;
+ private const string LogTag = "Tizen.Security.SecureRepository";
internal enum KeyManagerError : int
{
- // TODO : Add reference to real Tizen project
- //None = Tizen.Internals.Errors.ErrorCode.None,
- //InvalidParameter = Tizen.Internals.Errors.ErrorCode.InvalidParameter
- None = 0, // CKMC_ERROR_NONE
- InvalidParameter = -22, // CKMC_ERROR_INVALID_PARAMETER
+ None = ErrorCode.None,
+ InvalidParameter = ErrorCode.InvalidParameter,
VerificationFailed = TizenErrorKeyManager | 0x0D // CKMC_ERROR_VERIFICATION_FAILED
};
- internal class KeyManagerExceptionFactory
+ internal static void CheckNThrowException(int err, string msg)
{
- internal const string LogTag = "Tizen.Security.SecureRepository";
-
- internal static void CheckNThrowException(int err, string msg)
+ switch (err)
{
- if (err == (int)KeyManagerError.None)
+ case (int)KeyManagerError.None:
return;
-
- switch (err)
- {
- case (int)KeyManagerError.InvalidParameter:
- throw new ArgumentException(msg + ", error=" + Interop.GetErrorMessage(err));
- default:
- throw new InvalidOperationException(msg + ", error=" + Interop.GetErrorMessage(err));
- }
+ case (int)KeyManagerError.InvalidParameter:
+ throw new ArgumentException(string.Format("[{0}] {1}, error={2}",
+ LogTag, msg, ErrorFacts.GetErrorMessage(err)));
+ default:
+ throw new InvalidOperationException(string.Format("[{0}] {1}, error={2}",
+ LogTag, msg, ErrorFacts.GetErrorMessage(err)));
}
}
}
public readonly IntPtr caChain;
}
-
- static public string GetErrorMessage(int errorCode)
- {
- IntPtr errorPtr = CkmcTypes.GetErrorMessage(errorCode);
- return Marshal.PtrToStringAuto(errorPtr);
- }
-
-
internal static partial class CkmcTypes
{
[DllImport(Libraries.KeyManagerClient, EntryPoint = "ckmc_key_free", CallingConvention = CallingConvention.Cdecl)]
[DllImport(Libraries.KeyManagerClient, EntryPoint = "ckmc_generate_new_params", CallingConvention = CallingConvention.Cdecl)]
public static extern int CkmcGenerateNewParam(int algoType, out IntPtr paramList);
// int ckmc_generate_new_params(ckmc_algo_type_e type, ckmc_param_list_h *pparams);
-
- [DllImport(Libraries.TizenBaseCommon, EntryPoint = "get_error_message", CallingConvention = CallingConvention.Cdecl)]
- public static extern IntPtr GetErrorMessage(int err);
- // char *get_error_message(int err);
-
}
}
-
{
public const string Privilege = "libprivilege-info.so.1";
public const string KeyManagerClient = "libkey-manager-client.so.1";
- public const string TizenBaseCommon = "libcapi-base-common.so.0";
}
}
IntPtr ptr = new IntPtr();
int ret = Interop.CkmcManager.CkmcGetCert(alias, password, out ptr);
- Interop.KeyManagerExceptionFactory.CheckNThrowException(ret, "Failed to get certificate. alias=" + alias);
+ Interop.CheckNThrowException(ret, "Failed to get certificate. alias=" + alias);
return new Certificate(ptr);
}
{
IntPtr ptr = new IntPtr();
int ret = Interop.CkmcManager.CkmcGetCertAliasList(out ptr);
- Interop.KeyManagerExceptionFactory.CheckNThrowException(ret, "Failed to get certificate aliases.");
+ Interop.CheckNThrowException(ret, "Failed to get certificate aliases.");
return new SafeAliasListHandle(ptr).Aliases;
}
static public void SaveCertificate(string alias, Certificate cert, Policy policy)
{
int ret = Interop.CkmcManager.CkmcSaveCert(alias, cert.ToCkmcCert(), policy.ToCkmcPolicy());
- Interop.KeyManagerExceptionFactory.CheckNThrowException(ret, "Failed to save certificate. alias=" + alias);
+ Interop.CheckNThrowException(ret, "Failed to save certificate. alias=" + alias);
}
/// <summary>
int ret = Interop.CkmcManager.CkmcGetCertChain(new PinnedObject(certificate.ToCkmcCert()),
untrustedCerts.ToCkmcCertificateListPtr(), out ptrCertChain);
- Interop.KeyManagerExceptionFactory.CheckNThrowException(ret, "Failed to get certificate chain");
+ Interop.CheckNThrowException(ret, "Failed to get certificate chain");
SafeCertificateListHandle certChain = new SafeCertificateListHandle(ptrCertChain);
return certChain.Certificates;
int ret = Interop.CkmcManager.CkmcGetCertChainWithTrustedCerts(new PinnedObject(certificate.ToCkmcCert()),
untrustedCerts.ToCkmcCertificateListPtr(), trustedCerts.ToCkmcCertificateListPtr(), useTrustedSystemCertificates,
out ptrCertChain);
- Interop.KeyManagerExceptionFactory.CheckNThrowException(ret, "Failed to get certificate chain with trusted certificates");
+ Interop.CheckNThrowException(ret, "Failed to get certificate chain with trusted certificates");
SafeCertificateListHandle certChain = new SafeCertificateListHandle(ptrCertChain);
int ocspStatus = (int)OcspStatus.Good;
SafeCertificateListHandle certChain = new SafeCertificateListHandle(certificateChain);
int ret = Interop.CkmcManager.CkmcOcspCheck(certChain.ToCkmcCertificateListPtr(), ref ocspStatus);
- Interop.KeyManagerExceptionFactory.CheckNThrowException(ret, "Failed to get certificate chain with trusted certificates");
+ Interop.CheckNThrowException(ret, "Failed to get certificate chain with trusted certificates");
return (OcspStatus)ocspStatus;
}
}
Interop.CkmcRawBuffer cipherTextBuff = new Interop.CkmcRawBuffer(new PinnedObject(cipherText), cipherText.Length);
int ret = Interop.CkmcManager.CkmcDecryptData(Parameters.PtrCkmcParamList, keyAlias, password, cipherTextBuff, out ptrPlainText);
- Interop.KeyManagerExceptionFactory.CheckNThrowException(ret, "Failed to decrypt data");
+ Interop.CheckNThrowException(ret, "Failed to decrypt data");
return new SafeRawBufferHandle(ptrPlainText).Data;
}
Interop.CkmcRawBuffer plainTextBuff = new Interop.CkmcRawBuffer(new PinnedObject(plainText), plainText.Length);
int ret = Interop.CkmcManager.CkmcEncryptData(Parameters.PtrCkmcParamList, keyAlias, password, plainTextBuff, out ptrCipherText);
- Interop.KeyManagerExceptionFactory.CheckNThrowException(ret, "Failed to encrypt data");
+ Interop.CheckNThrowException(ret, "Failed to encrypt data");
return new SafeRawBufferHandle(ptrCipherText).Data;
}
protected void Add(CipherParameterName name, long value)
{
int ret = Interop.CkmcTypes.CkmcParamListSetInteger(PtrCkmcParamList, (int)name, value);
- Interop.KeyManagerExceptionFactory.CheckNThrowException(ret, "Failed to add parameter.");
+ Interop.CheckNThrowException(ret, "Failed to add parameter.");
}
/// <summary>
{
Interop.CkmcRawBuffer rawBuff = new Interop.CkmcRawBuffer(new PinnedObject(value), value.Length);
int ret = Interop.CkmcTypes.CkmcParamListSetBuffer(PtrCkmcParamList, (int)name, new PinnedObject(rawBuff));
- Interop.KeyManagerExceptionFactory.CheckNThrowException(ret, "Failed to add parameter.");
+ Interop.CheckNThrowException(ret, "Failed to add parameter.");
}
/// <summary>
{
long value = 0;
int ret = Interop.CkmcTypes.CkmcParamListGetInteger(PtrCkmcParamList, (int)name, out value);
- Interop.KeyManagerExceptionFactory.CheckNThrowException(ret, "Failed to get parameter.");
+ Interop.CheckNThrowException(ret, "Failed to get parameter.");
return value;
}
IntPtr ptr = new IntPtr();
int ret = Interop.CkmcTypes.CkmcParamListGetBuffer(PtrCkmcParamList, (int)name, out ptr);
- Interop.KeyManagerExceptionFactory.CheckNThrowException(ret, "Failed to get parameter.");
+ Interop.CheckNThrowException(ret, "Failed to get parameter.");
return new SafeRawBufferHandle(ptr).Data;
}
int ret = Interop.CkmcManager.CkmcCreateSignature(privateKeyAlias, password, messageBuff,
hash, rsaPadding, out ptrSignature);
- Interop.KeyManagerExceptionFactory.CheckNThrowException(ret, "Failed to generate signature");
+ Interop.CheckNThrowException(ret, "Failed to generate signature");
return new SafeRawBufferHandle(ptrSignature).Data;
}
signatureBuff, hash, rsaPadding);
if (ret == (int)Interop.KeyManagerError.VerificationFailed)
return false;
- Interop.KeyManagerExceptionFactory.CheckNThrowException(ret, "Failed to verify signature");
+ Interop.CheckNThrowException(ret, "Failed to verify signature");
return true;
}
IntPtr ptr = new IntPtr();
int ret = Interop.CkmcManager.CkmcGetData(alias, password, out ptr);
- Interop.KeyManagerExceptionFactory.CheckNThrowException(ret, "Failed to get certificate. alias=" + alias);
+ Interop.CheckNThrowException(ret, "Failed to get certificate. alias=" + alias);
return new SafeRawBufferHandle(ptr).Data;
}
{
IntPtr ptr = new IntPtr();
int ret = Interop.CkmcManager.CkmcGetDataAliasList(out ptr);
- Interop.KeyManagerExceptionFactory.CheckNThrowException(ret, "Failed to get data aliases");
+ Interop.CheckNThrowException(ret, "Failed to get data aliases");
return new SafeAliasListHandle(ptr).Aliases;
}
Interop.CkmcRawBuffer rawBuff = new Interop.CkmcRawBuffer(new PinnedObject(data), data.Length);
int ret = Interop.CkmcManager.CkmcSaveData(alias, rawBuff, policy.ToCkmcPolicy());
- Interop.KeyManagerExceptionFactory.CheckNThrowException(ret, "Failed to save data. alias=" + alias);
+ Interop.CheckNThrowException(ret, "Failed to save data. alias=" + alias);
}
}
}
IntPtr ptr = new IntPtr();
int ret = Interop.CkmcManager.CkmcGetKey(alias, password, out ptr);
- Interop.KeyManagerExceptionFactory.CheckNThrowException(ret, "Failed to get key. alias=" + alias);
+ Interop.CheckNThrowException(ret, "Failed to get key. alias=" + alias);
return new Key(ptr);
}
{
IntPtr ptr = new IntPtr();
int ret = Interop.CkmcManager.CkmcGetKeyAliasList(out ptr);
- Interop.KeyManagerExceptionFactory.CheckNThrowException(ret, "Failed to get key aliases.");
+ Interop.CheckNThrowException(ret, "Failed to get key aliases.");
return new SafeAliasListHandle(ptr).Aliases;
}
static public void SaveKey(string alias, Key key, Policy policy)
{
int ret = Interop.CkmcManager.CkmcSaveKey(alias, key.ToCkmcKey(), policy.ToCkmcPolicy());
- Interop.KeyManagerExceptionFactory.CheckNThrowException(ret, "Failed to save Key. alias=" + alias);
+ Interop.CheckNThrowException(ret, "Failed to save Key. alias=" + alias);
}
/// <summary>
{
int ret = Interop.CkmcManager.CkmcCreateKeyPairRsa(size, privateKeyAlias, publicKeyAlias,
privateKeyPolicy.ToCkmcPolicy(), publicKeyPolicy.ToCkmcPolicy());
- Interop.KeyManagerExceptionFactory.CheckNThrowException(ret, "Failed to Create RSA Key Pair");
+ Interop.CheckNThrowException(ret, "Failed to Create RSA Key Pair");
}
/// <summary>
{
int ret = Interop.CkmcManager.CkmcCreateKeyPairDsa(size, privateKeyAlias, publicKeyAlias,
privateKeyPolicy.ToCkmcPolicy(), publicKeyPolicy.ToCkmcPolicy());
- Interop.KeyManagerExceptionFactory.CheckNThrowException(ret, "Failed to Create DSA Key Pair");
+ Interop.CheckNThrowException(ret, "Failed to Create DSA Key Pair");
}
/// <summary>
{
int ret = Interop.CkmcManager.CkmcCreateKeyPairEcdsa((int)type, privateKeyAlias, publicKeyAlias,
privateKeyPolicy.ToCkmcPolicy(), publicKeyPolicy.ToCkmcPolicy());
- Interop.KeyManagerExceptionFactory.CheckNThrowException(ret, "Failed to Create ECDSA Key Pair");
+ Interop.CheckNThrowException(ret, "Failed to Create ECDSA Key Pair");
}
/// <summary>
static public void CreateKeyAes(int size, string keyAlias, Policy policy)
{
int ret = Interop.CkmcManager.CkmcCreateKeyAes(size, keyAlias, policy.ToCkmcPolicy());
- Interop.KeyManagerExceptionFactory.CheckNThrowException(ret, "Failed to AES Key");
+ Interop.CheckNThrowException(ret, "Failed to AES Key");
}
}
}
static public void RemoveAlias(string alias)
{
int ret = Interop.CkmcManager.CkmcRemoveAlias(alias);
- Interop.KeyManagerExceptionFactory.CheckNThrowException(ret, "Failed to remove alias. alias=" + alias);
+ Interop.CheckNThrowException(ret, "Failed to remove alias. alias=" + alias);
}
/// <summary>
static public void SetPermission(string alias, string otherPackageId, int permissions)
{
int ret = Interop.CkmcManager.CkmcSetPermission(alias, otherPackageId, permissions);
- Interop.KeyManagerExceptionFactory.CheckNThrowException(ret, "Failed to set permission. alias=" + alias);
+ Interop.CheckNThrowException(ret, "Failed to set permission. alias=" + alias);
}
}
}
IntPtr ptr = new IntPtr();
int ret = Interop.CkmcTypes.CkmcPkcs12Load(filePath, filePassword, out ptr);
- Interop.KeyManagerExceptionFactory.CheckNThrowException(ret, "Failed to load PKCS12. file=" + filePath);
+ Interop.CheckNThrowException(ret, "Failed to load PKCS12. file=" + filePath);
return new Pkcs12(ptr);
}
IntPtr ptr = new IntPtr();
int ret = Interop.CkmcManager.CkmcGetPkcs12(alias, keyPassword, cerificatePassword, out ptr);
- Interop.KeyManagerExceptionFactory.CheckNThrowException(ret, "Failed to get PKCS12. alias=" + alias);
+ Interop.CheckNThrowException(ret, "Failed to get PKCS12. alias=" + alias);
return new Pkcs12(ptr);
}
new PinnedObject(pkcs12.ToCkmcPkcs12()),
keyPolicy.ToCkmcPolicy(),
certificatePolicy.ToCkmcPolicy());
- Interop.KeyManagerExceptionFactory.CheckNThrowException(ret, "Failed to save PKCS12. alias=" + alias);
+ Interop.CheckNThrowException(ret, "Failed to save PKCS12. alias=" + alias);
}
}
}
{
IntPtr certPtr;
ret = Interop.CkmcTypes.CkmcCertNew(cert.Binary, (uint)cert.Binary.Length, (int)cert.Format, out certPtr);
- Interop.KeyManagerExceptionFactory.CheckNThrowException(ret, "Failed to create new Certificate.");
+ Interop.CheckNThrowException(ret, "Failed to create new Certificate.");
IntPtr outCertList;
if (previous == IntPtr.Zero)
{
ret = Interop.CkmcTypes.CkmcCertListNew(certPtr, out outCertList);
- Interop.KeyManagerExceptionFactory.CheckNThrowException(ret, "Failed to create new CertificateList.");
+ Interop.CheckNThrowException(ret, "Failed to create new CertificateList.");
first = outCertList;
previous = outCertList;
}
else
{
ret = Interop.CkmcTypes.CkmcCertListAdd(previous, certPtr, out outCertList);
- Interop.KeyManagerExceptionFactory.CheckNThrowException(ret, "Failed to add Certificate to CertificateList.");
+ Interop.CheckNThrowException(ret, "Failed to add Certificate to CertificateList.");
previous = outCertList;
}
}