pEpObjCAdapter/NSError+PEP.m
author Dirk Zimmermann <dz@pep.security>
Mon, 11 Feb 2019 11:40:05 +0100
branchIOSAD-103
changeset 782 e694f991ef35
parent 780 2af66006a06b
child 784 d585116a1af8
permissions -rw-r--r--
IOS-1475 PEP_KEY_IMPORT_STATUS_UNKNOWN is not an error.
     1 //
     2 //  NSError+PEP.m
     3 //  pEpObjCAdapter
     4 //
     5 //  Created by Dirk Zimmermann on 20.02.18.
     6 //  Copyright © 2018 p≡p. All rights reserved.
     7 //
     8 
     9 #import "NSError+PEP.h"
    10 
    11 static NSString *s_pEpAdapterDomain = @"security.pEp.ObjCAdapter";
    12 
    13 @implementation NSError (Extension)
    14 
    15 + (NSError * _Nonnull)errorWithPEPStatus:(PEP_STATUS)status
    16                                 userInfo:(NSDictionary<NSErrorUserInfoKey, id> * _Nonnull)dict
    17 {
    18     switch (status) {
    19         case PEP_STATUS_OK:
    20         case PEP_DECRYPTED:
    21         case PEP_UNENCRYPTED:
    22         case PEP_DECRYPT_NO_KEY:
    23         case PEP_KEY_IMPORT_STATUS_UNKNOWN:
    24             return nil;
    25             break;
    26 
    27         default:
    28             if (![dict objectForKey:NSLocalizedDescriptionKey]) {
    29                 NSMutableDictionary *dict2 = [NSMutableDictionary dictionaryWithDictionary:dict];
    30                 [dict2 setValue:localizedErrorStringFromPEPStatus(status)
    31                          forKey:NSLocalizedDescriptionKey];
    32                 dict = dict2;
    33             }
    34             return [NSError errorWithDomain:s_pEpAdapterDomain code:status userInfo:dict];
    35             break;
    36     }
    37 }
    38 
    39 + (NSError * _Nonnull)errorWithPEPStatus:(PEP_STATUS)status
    40 {
    41     NSDictionary *userInfo = [NSDictionary new];
    42     return [self errorWithPEPStatus:status userInfo:userInfo];
    43 }
    44 
    45 + (BOOL)setError:(NSError * _Nullable * _Nullable)error fromPEPStatus:(PEP_STATUS)status
    46 {
    47     NSError *theError = [self errorWithPEPStatus:status];
    48     if (theError) {
    49         if (error) {
    50             *error = theError;
    51         }
    52         return YES;
    53     } else {
    54         if (error) {
    55             *error = nil;
    56         }
    57         return NO;
    58     }
    59 }
    60 
    61 /**
    62  Could in theory return a fully localized version of the underlying error.
    63  */
    64 NSString * _Nonnull localizedErrorStringFromPEPStatus(PEP_STATUS status) {
    65     return stringFromPEPStatus(status);
    66 }
    67 
    68 NSString * _Nonnull stringFromPEPStatus(PEP_STATUS status) {
    69     switch (status) {
    70         case PEP_STATUS_OK: return @"PEP_STATUS_OK";
    71         case PEP_INIT_CANNOT_LOAD_GPGME: return @"PEP_INIT_CANNOT_LOAD_GPGME";
    72         case PEP_INIT_GPGME_INIT_FAILED: return @"PEP_INIT_GPGME_INIT_FAILED";
    73         case PEP_INIT_NO_GPG_HOME: return @"PEP_INIT_NO_GPG_HOME";
    74         case PEP_INIT_NETPGP_INIT_FAILED: return @"PEP_INIT_NETPGP_INIT_FAILED";
    75         case PEP_INIT_CANNOT_DETERMINE_GPG_VERSION: return @"PEP_INIT_CANNOT_DETERMINE_GPG_VERSION";
    76         case PEP_INIT_UNSUPPORTED_GPG_VERSION: return @"PEP_INIT_UNSUPPORTED_GPG_VERSION";
    77         case PEP_INIT_CANNOT_CONFIG_GPG_AGENT: return @"PEP_INIT_CANNOT_CONFIG_GPG_AGENT";
    78         case PEP_INIT_SQLITE3_WITHOUT_MUTEX: return @"PEP_INIT_SQLITE3_WITHOUT_MUTEX";
    79         case PEP_INIT_CANNOT_OPEN_DB: return @"PEP_INIT_CANNOT_OPEN_DB";
    80         case PEP_INIT_CANNOT_OPEN_SYSTEM_DB: return @"PEP_INIT_CANNOT_OPEN_SYSTEM_DB";
    81         case PEP_KEY_NOT_FOUND: return @"PEP_KEY_NOT_FOUND";
    82         case PEP_KEY_HAS_AMBIG_NAME: return @"PEP_KEY_HAS_AMBIG_NAME";
    83         case PEP_GET_KEY_FAILED: return @"PEP_GET_KEY_FAILED";
    84         case PEP_CANNOT_EXPORT_KEY: return @"PEP_CANNOT_EXPORT_KEY";
    85         case PEP_CANNOT_EDIT_KEY: return @"PEP_CANNOT_EDIT_KEY";
    86         case PEP_KEY_UNSUITABLE: return @"PEP_KEY_UNSUITABLE";
    87         case PEP_CANNOT_FIND_IDENTITY: return @"PEP_CANNOT_FIND_IDENTITY";
    88         case PEP_CANNOT_SET_PERSON: return @"PEP_CANNOT_SET_PERSON";
    89         case PEP_CANNOT_SET_PGP_KEYPAIR: return @"PEP_CANNOT_SET_PGP_KEYPAIR";
    90         case PEP_CANNOT_SET_IDENTITY: return @"PEP_CANNOT_SET_IDENTITY";
    91         case PEP_CANNOT_SET_TRUST: return @"PEP_CANNOT_SET_TRUST";
    92         case PEP_KEY_BLACKLISTED: return @"PEP_KEY_BLACKLISTED";
    93         case PEP_CANNOT_FIND_PERSON: return @"PEP_CANNOT_FIND_PERSON";
    94         case PEP_CANNOT_FIND_ALIAS: return @"PEP_CANNOT_FIND_ALIAS";
    95         case PEP_CANNOT_SET_ALIAS: return @"PEP_CANNOT_SET_ALIAS";
    96         case PEP_UNENCRYPTED: return @"PEP_UNENCRYPTED";
    97         case PEP_VERIFIED: return @"PEP_VERIFIED";
    98         case PEP_DECRYPTED: return @"PEP_DECRYPTED";
    99         case PEP_DECRYPTED_AND_VERIFIED: return @"PEP_DECRYPTED_AND_VERIFIED";
   100         case PEP_DECRYPT_WRONG_FORMAT: return @"PEP_DECRYPT_WRONG_FORMAT";
   101         case PEP_DECRYPT_NO_KEY: return @"PEP_DECRYPT_NO_KEY";
   102         case PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH: return @"PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH";
   103         case PEP_VERIFY_NO_KEY: return @"PEP_VERIFY_NO_KEY";
   104         case PEP_VERIFIED_AND_TRUSTED: return @"PEP_VERIFIED_AND_TRUSTED";
   105         case PEP_CANNOT_DECRYPT_UNKNOWN: return @"PEP_CANNOT_DECRYPT_UNKNOWN";
   106         case PEP_TRUSTWORD_NOT_FOUND: return @"PEP_TRUSTWORD_NOT_FOUND";
   107         case PEP_TRUSTWORDS_FPR_WRONG_LENGTH: return @"PEP_TRUSTWORDS_FPR_WRONG_LENGTH";
   108         case PEP_TRUSTWORDS_DUPLICATE_FPR: return @"PEP_TRUSTWORDS_DUPLICATE_FPR";
   109         case PEP_CANNOT_CREATE_KEY: return @"PEP_CANNOT_CREATE_KEY";
   110         case PEP_CANNOT_SEND_KEY: return @"PEP_CANNOT_SEND_KEY";
   111         case PEP_PHRASE_NOT_FOUND: return @"PEP_PHRASE_NOT_FOUND";
   112         case PEP_SEND_FUNCTION_NOT_REGISTERED: return @"PEP_SEND_FUNCTION_NOT_REGISTERED";
   113         case PEP_CONTRAINTS_VIOLATED: return @"PEP_CONTRAINTS_VIOLATED";
   114         case PEP_CANNOT_ENCODE: return @"PEP_CANNOT_ENCODE";
   115         case PEP_SYNC_NO_NOTIFY_CALLBACK: return @"PEP_SYNC_NO_NOTIFY_CALLBACK";
   116         case PEP_SYNC_ILLEGAL_MESSAGE: return @"PEP_SYNC_ILLEGAL_MESSAGE";
   117         case PEP_SYNC_NO_INJECT_CALLBACK: return @"PEP_SYNC_NO_INJECT_CALLBACK";
   118         case PEP_CANNOT_INCREASE_SEQUENCE: return @"PEP_CANNOT_INCREASE_SEQUENCE";
   119         case PEP_STATEMACHINE_INVALID_STATE: return @"PEP_STATEMACHINE_INVALID_STATE";
   120         case PEP_STATEMACHINE_INVALID_EVENT: return @"PEP_STATEMACHINE_INVALID_EVENT";
   121         case PEP_STATEMACHINE_INVALID_CONDITION: return @"PEP_STATEMACHINE_INVALID_CONDITION";
   122         case PEP_STATEMACHINE_INVALID_ACTION: return @"PEP_STATEMACHINE_INVALID_ACTION";
   123         case PEP_STATEMACHINE_INHIBITED_EVENT: return @"PEP_STATEMACHINE_INHIBITED_EVENT";
   124         case PEP_COMMIT_FAILED: return @"PEP_COMMIT_FAILED";
   125         case PEP_MESSAGE_CONSUME: return @"PEP_MESSAGE_CONSUME";
   126         case PEP_MESSAGE_IGNORE: return @"PEP_MESSAGE_IGNORE";
   127         case PEP_RECORD_NOT_FOUND: return @"PEP_RECORD_NOT_FOUND";
   128         case PEP_CANNOT_CREATE_TEMP_FILE: return @"PEP_CANNOT_CREATE_TEMP_FILE";
   129         case PEP_ILLEGAL_VALUE: return @"PEP_ILLEGAL_VALUE";
   130         case PEP_BUFFER_TOO_SMALL: return @"PEP_BUFFER_TOO_SMALL";
   131         case PEP_OUT_OF_MEMORY: return @"PEP_OUT_OF_MEMORY";
   132         case PEP_UNKNOWN_ERROR: return @"PEP_UNKNOWN_ERROR";
   133         case PEP_VERSION_MISMATCH: return @"PEP_VERSION_MISMATCH";
   134         case PEP_CANNOT_REENCRYPT: return @"PEP_CANNOT_REENCRYPT";
   135         case PEP_UNKNOWN_DB_ERROR: return @"PEP_UNKNOWN_DB_ERROR";
   136         case PEP_MALFORMED_KEY_RESET_MSG: return @"PEP_MALFORMED_KEY_RESET_MSG";
   137         case PEP_KEY_NOT_RESET: return @"PEP_KEY_NOT_RESET";
   138         case PEP_KEY_IMPORTED: return @"PEP_KEY_IMPORTED";
   139         case PEP_NO_KEY_IMPORTED: return @"PEP_NO_KEY_IMPORTED";
   140         case PEP_KEY_IMPORT_STATUS_UNKNOWN: return @"PEP_KEY_IMPORT_STATUS_UNKNOWN";
   141         case PEP_SYNC_NO_CHANNEL: return @"PEP_SYNC_NO_CHANNEL";
   142         case PEP_SYNC_CANNOT_ENCRYPT: return @"PEP_SYNC_CANNOT_ENCRYPT";
   143         case PEP_SYNC_NO_MESSAGE_SEND_CALLBACK: return @"PEP_SYNC_NO_MESSAGE_SEND_CALLBACK";
   144         case PEP_STATEMACHINE_ERROR: return @"PEP_STATEMACHINE_ERROR";
   145         case PEP_NO_TRUST: return @"PEP_NO_TRUST";
   146         case PEP_STATEMACHINE_CANNOT_SEND: return @"PEP_STATEMACHINE_CANNOT_SEND";
   147     }
   148 }
   149 
   150 - (NSString * _Nullable)pEpErrorString
   151 {
   152     if ([self.domain isEqualToString:s_pEpAdapterDomain]) {
   153         return stringFromPEPStatus((PEP_STATUS) self.code);
   154     } else {
   155         return nil;
   156     }
   157 }
   158 
   159 @end