From 0904bd95736ecf866d9b4ca413097a604be6dd1f Mon Sep 17 00:00:00 2001 From: Kyungwook Tak Date: Wed, 9 Nov 2016 20:03:58 +0900 Subject: [PATCH] Add exception handlings Change-Id: Id76e751fcc61de9188d4a3c52c194c81dae5d45c Signed-off-by: Kyungwook Tak --- .../CertificateManager.cs | 107 +++++++++--- .../Tizen.Security.SecureRepository/DataManager.cs | 26 ++- .../Tizen.Security.SecureRepository/KeyManager.cs | 193 +++++++++++++++------ 3 files changed, 246 insertions(+), 80 deletions(-) diff --git a/src/Tizen.Security.SecureRepository/Tizen.Security.SecureRepository/CertificateManager.cs b/src/Tizen.Security.SecureRepository/Tizen.Security.SecureRepository/CertificateManager.cs index c0dc121..925b71f 100644 --- a/src/Tizen.Security.SecureRepository/Tizen.Security.SecureRepository/CertificateManager.cs +++ b/src/Tizen.Security.SecureRepository/Tizen.Security.SecureRepository/CertificateManager.cs @@ -29,16 +29,24 @@ namespace Tizen.Security.SecureRepository /// Gets a certificate from secure repository. /// /// The name of a certificate to retrieve. - /// The password used in decrypting a certificate value. - /// If password of policy is provided in SaveCertificate(), the same password should be provided. + /// + /// The password used in decrypting a certificate value. If password of + /// policy is provided in SaveCertificate(), the same password should be + /// provided. /// /// A certificate specified by alias. - /// Alias argument is null or invalid format. + /// + /// Alias argument is null or invalid format. + /// /// - /// Certificate does not exist with the alias or certificate-protecting password isn't matched. + /// Certificate does not exist with the alias or certificate-protecting + /// password isn't matched. /// static public Certificate Get(string alias, string password) { + if (alias == null) + throw new ArgumentNullException("alias cannot be null"); + IntPtr ptr = IntPtr.Zero; try @@ -83,11 +91,20 @@ namespace Tizen.Security.SecureRepository /// /// The name of a certificate to be stored. /// The certificate's binary value to be stored. - /// The policy about how to store a certificate securely. - /// Alias argument is null or invalid format. cert argument is invalid format. - /// Certificate with alias does already exist. + /// + /// The policy about how to store a certificate securely. + /// + /// + /// Alias argument is null or invalid format. cert argument is invalid format. + /// + /// + /// Certificate with alias does already exist. + /// static public void Save(string alias, Certificate cert, Policy policy) { + if (alias == null || cert == null || policy == null) + throw new ArgumentNullException("More than one of argument is null"); + Interop.CheckNThrowException( Interop.CkmcManager.SaveCert( alias, cert.ToCkmcCert(), policy.ToCkmcPolicy()), @@ -98,19 +115,32 @@ namespace Tizen.Security.SecureRepository /// Verifies a certificate chain and returns that chain. /// /// The certificate to be verified. - /// The untrusted CA certificates to be used in verifying a certificate chain. + /// + /// The untrusted CA certificates to be used in verifying a certificate chain. + /// /// A newly created certificate chain. - /// Some of certificate in arguments is invalid. + /// + /// Some of certificate in arguments is invalid. + /// /// /// Some of certificate in arguments is expired or not valid yet. /// Certificate cannot build chain. /// Root certificate is not in trusted system certificate store. /// - /// The trusted root certificate of the chain should exist in the system's certificate storage. - /// The trusted root certificate of the chain in system's certificate storage is added to the certificate chain. - static public IEnumerable GetCertificateChain(Certificate certificate, - IEnumerable untrustedCertificates) + /// + /// The trusted root certificate of the chain should exist in the system's + /// certificate storage. + /// + /// + /// The trusted root certificate of the chain in system's certificate storage + /// is added to the certificate chain. + /// + static public IEnumerable GetCertificateChain( + Certificate certificate, IEnumerable untrustedCertificates) { + if (certificate == null) + throw new ArgumentNullException("Certificate is null"); + IntPtr ptrCertChain = IntPtr.Zero; IntPtr certPtr = IntPtr.Zero; IntPtr untrustedPtr = IntPtr.Zero; @@ -140,25 +170,41 @@ namespace Tizen.Security.SecureRepository } /// - /// Verifies a certificate chain and returns that chain using user entered trusted and untrusted CA certificates. + /// Verifies a certificate chain and returns that chain using user entered + /// trusted and untrusted CA certificates. /// /// The certificate to be verified. - /// The untrusted CA certificates to be used in verifying a certificate chain. - /// The trusted CA certificates to be used in verifying a certificate chain. - /// The flag indicating the use of the trusted root certificates in the system's certificate storage. + /// + /// The untrusted CA certificates to be used in verifying a certificate chain. + /// + /// + /// The trusted CA certificates to be used in verifying a certificate chain. + /// + /// + /// The flag indicating the use of the trusted root certificates in the + /// system's certificate storage. + /// /// A newly created certificate chain. - /// Some of certificate in arguments is invalid. + /// + /// Some of certificate in arguments is invalid. + /// /// /// Some of certificate in arguments is expired or not valid yet. /// Certificate cannot build chain. /// Root certificate is not in trusted system certificate store. /// - /// The trusted root certificate of the chain in system's certificate storage is added to the certificate chain. - static public IEnumerable GetCertificateChain(Certificate certificate, - IEnumerable untrustedCertificates, - IEnumerable trustedCertificates, - bool useTrustedSystemCertificates) + /// + /// The trusted root certificate of the chain in system's certificate storage + /// is added to the certificate chain. + /// + static public IEnumerable GetCertificateChain( + Certificate certificate, IEnumerable untrustedCertificates, + IEnumerable trustedCertificates, + bool useTrustedSystemCertificates) { + if (certificate == null) + throw new ArgumentNullException("Certificate is null"); + IntPtr certPtr = IntPtr.Zero; IntPtr untrustedPtr = IntPtr.Zero; IntPtr trustedPtr = IntPtr.Zero; @@ -194,12 +240,21 @@ namespace Tizen.Security.SecureRepository /// /// Perform OCSP which checks certificate is whether revoked or not. /// - /// Valid certificate chain to perform OCSP check. + /// + /// Valid certificate chain to perform OCSP check. + /// /// A status result of OCSP check. - /// certificateChain is not valid chain or certificate. - /// some of certificate in chain is expired or not valid yet. + /// + /// certificateChain is not valid chain or certificate. + /// + /// + /// some of certificate in chain is expired or not valid yet. + /// static public OcspStatus CheckOcsp(IEnumerable certificateChain) { + if (certificateChain == null) + throw new ArgumentNullException("Certificate chain is null"); + IntPtr ptr = IntPtr.Zero; try diff --git a/src/Tizen.Security.SecureRepository/Tizen.Security.SecureRepository/DataManager.cs b/src/Tizen.Security.SecureRepository/Tizen.Security.SecureRepository/DataManager.cs index ef81277..a365a74 100644 --- a/src/Tizen.Security.SecureRepository/Tizen.Security.SecureRepository/DataManager.cs +++ b/src/Tizen.Security.SecureRepository/Tizen.Security.SecureRepository/DataManager.cs @@ -29,16 +29,23 @@ namespace Tizen.Security.SecureRepository /// Gets data from secure repository. /// /// The name of a certificate to retrieve. - /// The password used in decrypting a data value. - /// If password of policy is provided in SaveData(), the same password should be provided. + /// + /// The password used in decrypting a data value. + /// If password of policy is provided in SaveData(), the same password should + /// be provided. /// /// Data specified by alias. - /// Alias argument is null or invalid format. + /// + /// Alias argument is null or invalid format. + /// /// /// Data does not exist with the alias or data-protecting password isn't matched. /// static public byte[] Get(string alias, string password) { + if (alias == null) + throw new ArgumentNullException("alias cannot be null"); + IntPtr ptr = IntPtr.Zero; try @@ -84,10 +91,19 @@ namespace Tizen.Security.SecureRepository /// The name of data to be stored. /// The binary value to be stored. /// The policy about how to store data securely. - /// Alias argument is null or invalid format. Data policy cannot be unextractable. - /// Data with alias does already exist. + /// + /// Alias argument is null or invalid format. Data policy cannot be unextractable. + /// + /// + /// Data with alias does already exist. + /// static public void Save(string alias, byte[] data, Policy policy) { + if (alias == null || policy == null) + throw new ArgumentNullException("alias and policy should be null"); + else if (policy.Extractable == false) + throw new ArgumentException("Data should be extractable"); + Interop.CheckNThrowException( Interop.CkmcManager.SaveData( alias, diff --git a/src/Tizen.Security.SecureRepository/Tizen.Security.SecureRepository/KeyManager.cs b/src/Tizen.Security.SecureRepository/Tizen.Security.SecureRepository/KeyManager.cs index 7dfe572..99876de 100644 --- a/src/Tizen.Security.SecureRepository/Tizen.Security.SecureRepository/KeyManager.cs +++ b/src/Tizen.Security.SecureRepository/Tizen.Security.SecureRepository/KeyManager.cs @@ -30,15 +30,21 @@ namespace Tizen.Security.SecureRepository /// The name of a key to retrieve. /// /// The password used in decrypting a key value. - /// If password of policy is provided in SaveKey(), the same password should be provided. + /// If password of policy is provided in SaveKey(), the same password should + /// be provided. /// /// A key specified by alias. - /// Alias argument is null or invalid format. + /// + /// Alias argument is null or invalid format. + /// /// /// Key does not exist with the alias or key-protecting password isn't matched. /// static public Key Get(string alias, string password) { + if (alias == null) + throw new ArgumentNullException("alias cannot be null"); + IntPtr ptr = IntPtr.Zero; try @@ -84,95 +90,184 @@ namespace Tizen.Security.SecureRepository /// The name of a key to be stored. /// The key's binary value to be stored. /// The policy about how to store a key securely. - /// Alias argument is null or invalid format. key argument is invalid format. - /// Key with alias does already exist. - /// Type in key may be set to KeyType.None as an input. Type is determined inside secure reposioty during storing keys. - /// If password in policy is provided, the key is additionally encrypted with the password in policy. + /// + /// Alias argument is null or invalid format. key argument is invalid format. + /// + /// + /// Key with alias does already exist. + /// + /// + /// Type in key may be set to KeyType.None as an input. + /// Type is determined inside secure reposioty during storing keys. + /// + /// + /// If password in policy is provided, the key is additionally encrypted with + /// the password in policy. + /// static public void Save(string alias, Key key, Policy policy) { - int ret = Interop.CkmcManager.SaveKey(alias, key.ToCkmcKey(), policy.ToCkmcPolicy()); - Interop.CheckNThrowException(ret, "Failed to save Key. alias=" + alias); + if (alias == null || key == null || policy == null) + throw new ArgumentNullException("More than one of argument is null"); + + Interop.CheckNThrowException( + Interop.CkmcManager.SaveKey( + alias, key.ToCkmcKey(), policy.ToCkmcPolicy()), + "Failed to save Key. alias=" + alias); } /// - /// Creates RSA private/public key pair and stores them inside secure repository based on each policy. + /// Creates RSA private/public key pair and stores them inside secure repository + /// based on each policy. /// - /// The size of key strength to be created. 1024, 2048, and 4096 are supported. + /// + /// The size of key strength to be created. 1024, 2048, and 4096 are supported. + /// /// The name of private key to be stored. /// The name of public key to be stored. - /// The policy about how to store a private key securely. - /// The policy about how to store a public key securely. - /// size is invalid. privateKeyAlias or publicKeyAlias is null or invalid format. - /// Key with privateKeyAlias or publicKeyAlias does already exist. - /// If password in policy is provided, the key is additionally encrypted with the password in policy. - static public void CreateRsaKeyPair(int size, string privateKeyAlias, string publicKeyAlias, - Policy privateKeyPolicy, Policy publicKeyPolicy) + /// + /// The policy about how to store a private key securely. + /// + /// + /// The policy about how to store a public key securely. + /// + /// + /// size is invalid. privateKeyAlias or publicKeyAlias is null or invalid format. + /// + /// + /// Key with privateKeyAlias or publicKeyAlias does already exist. + /// + /// + /// If password in policy is provided, the key is additionally encrypted with the + /// password in policy. + /// + static public void CreateRsaKeyPair( + int size, string privateKeyAlias, string publicKeyAlias, + Policy privateKeyPolicy, Policy publicKeyPolicy) { if (size != 1024 && size != 2048 && size != 4096) throw new ArgumentException(string.Format("Invalid key size({0})", size)); + else if (privateKeyAlias == null || publicKeyAlias == null || + privateKeyPolicy == null || publicKeyPolicy == null) + throw new ArgumentNullException("alias and policy should not be null"); - int ret = Interop.CkmcManager.CreateKeyPairRsa((UIntPtr)size, privateKeyAlias, publicKeyAlias, - privateKeyPolicy.ToCkmcPolicy(), publicKeyPolicy.ToCkmcPolicy()); - Interop.CheckNThrowException(ret, "Failed to Create RSA Key Pair"); + Interop.CheckNThrowException( + Interop.CkmcManager.CreateKeyPairRsa( + (UIntPtr)size, privateKeyAlias, publicKeyAlias, + privateKeyPolicy.ToCkmcPolicy(), publicKeyPolicy.ToCkmcPolicy()), + "Failed to Create RSA Key Pair"); } /// - /// Creates DSA private/public key pair and stores them inside secure repository based on each policy. + /// Creates DSA private/public key pair and stores them inside secure repository + /// based on each policy. /// - /// The size of key strength to be created. 1024, 2048, 3072, and 4096 are supported. + /// + /// The size of key strength to be created. 1024, 2048, 3072, and 4096 are + /// supported. + /// /// The name of private key to be stored. /// The name of public key to be stored. - /// The policy about how to store a private key securely. - /// The policy about how to store a public key securely. - /// size is invalid. privateKeyAlias or publicKeyAlias is null or invalid format. - /// Key with privateKeyAlias or publicKeyAlias does already exist. - /// If password in policy is provided, the key is additionally encrypted with the password in policy. - static public void CreateDsaKeyPair(int size, string privateKeyAlias, string publicKeyAlias, - Policy privateKeyPolicy, Policy publicKeyPolicy) + /// + /// The policy about how to store a private key securely. + /// + /// + /// The policy about how to store a public key securely. + /// + /// + /// size is invalid. privateKeyAlias or publicKeyAlias is null or invalid format. + /// + /// + /// Key with privateKeyAlias or publicKeyAlias does already exist. + /// + /// + /// If password in policy is provided, the key is additionally encrypted with + /// the password in policy. + /// + static public void CreateDsaKeyPair( + int size, string privateKeyAlias, string publicKeyAlias, + Policy privateKeyPolicy, Policy publicKeyPolicy) { if (size != 1024 && size != 2048 && size != 3072 && size != 4096) throw new ArgumentException(string.Format("Invalid key size({0})", size)); + else if (privateKeyAlias == null || publicKeyAlias == null || + privateKeyPolicy == null || publicKeyPolicy == null) + throw new ArgumentNullException("alias and policy should not be null"); - int ret = Interop.CkmcManager.CreateKeyPairDsa((UIntPtr)size, privateKeyAlias, publicKeyAlias, - privateKeyPolicy.ToCkmcPolicy(), publicKeyPolicy.ToCkmcPolicy()); - Interop.CheckNThrowException(ret, "Failed to Create DSA Key Pair"); + Interop.CheckNThrowException( + Interop.CkmcManager.CreateKeyPairDsa( + (UIntPtr)size, privateKeyAlias, publicKeyAlias, + privateKeyPolicy.ToCkmcPolicy(), publicKeyPolicy.ToCkmcPolicy()), + "Failed to Create DSA Key Pair"); } /// - /// Creates ECDSA private/public key pair and stores them inside secure repository based on each policy. + /// Creates ECDSA private/public key pair and stores them inside secure repository + /// based on each policy. /// /// The type of elliptic curve of ECDSA. /// The name of private key to be stored. /// The name of public key to be stored. - /// The policy about how to store a private key securely. - /// The policy about how to store a public key securely. - /// Elliptic curve type is invalid. privateKeyAlias or publicKeyAlias is null or invalid format. - /// Key with privateKeyAlias or publicKeyAlias does already exist. - /// If password in policy is provided, the key is additionally encrypted with the password in policy. - static public void CreateEcdsaKeyPair(EllipticCurveType type, string privateKeyAlias, string publicKeyAlias, - Policy privateKeyPolicy, Policy publicKeyPolicy) + /// + /// The policy about how to store a private key securely. + /// + /// + /// The policy about how to store a public key securely. + /// + /// + /// Elliptic curve type is invalid. privateKeyAlias or publicKeyAlias is null or + /// invalid format. + /// + /// + /// Key with privateKeyAlias or publicKeyAlias does already exist. + /// + /// + /// If password in policy is provided, the key is additionally encrypted with + /// the password in policy. + /// + static public void CreateEcdsaKeyPair( + EllipticCurveType type, string privateKeyAlias, string publicKeyAlias, + Policy privateKeyPolicy, Policy publicKeyPolicy) { - int ret = Interop.CkmcManager.CreateKeyPairEcdsa((int)type, privateKeyAlias, publicKeyAlias, - privateKeyPolicy.ToCkmcPolicy(), publicKeyPolicy.ToCkmcPolicy()); - Interop.CheckNThrowException(ret, "Failed to Create ECDSA Key Pair"); + if (privateKeyAlias == null || publicKeyAlias == null || + privateKeyPolicy == null || publicKeyPolicy == null) + throw new ArgumentNullException("alias and policy should not be null"); + + Interop.CheckNThrowException( + Interop.CkmcManager.CreateKeyPairEcdsa( + (int)type, privateKeyAlias, publicKeyAlias, + privateKeyPolicy.ToCkmcPolicy(), publicKeyPolicy.ToCkmcPolicy()), + "Failed to Create ECDSA Key Pair"); } /// /// Creates AES key and stores it inside secure repository based on each policy. /// - /// The size of key strength to be created. 128, 192 and256 are supported. + /// + /// The size of key strength to be created. 128, 192 and 256 are supported. + /// /// The name of key to be stored. /// The policy about how to store the key securely. - /// Key size is invalid. keyAlias is null or invalid format. - /// Key with privateKeyAlias or publicKeyAlias does already exist. - /// If password in policy is provided, the key is additionally encrypted with the password in policy. + /// + /// Key size is invalid. keyAlias is null or invalid format. + /// + /// + /// Key with privateKeyAlias or publicKeyAlias does already exist. + /// + /// + /// If password in policy is provided, the key is additionally encrypted with + /// the password in policy. + /// static public void CreateAesKey(int size, string keyAlias, Policy policy) { if (size != 128 && size != 192 && size != 256) throw new ArgumentException(string.Format("Invalid key size({0})", size)); + else if (keyAlias == null || policy == null) + throw new ArgumentNullException("alias and policy should not be null"); - int ret = Interop.CkmcManager.CreateKeyAes((UIntPtr)size, keyAlias, policy.ToCkmcPolicy()); - Interop.CheckNThrowException(ret, "Failed to AES Key"); + Interop.CheckNThrowException( + Interop.CkmcManager.CreateKeyAes( + (UIntPtr)size, keyAlias, policy.ToCkmcPolicy()), + "Failed to AES Key"); } // to be static class safely -- 2.7.4