Skip to content

Commit

Permalink
Merge pull request #518 from metsma/reuse
Browse files Browse the repository at this point in the history
Reuse code
  • Loading branch information
Counter178 authored Jan 22, 2025
2 parents d7ee779 + 6e4da02 commit b8f8eb3
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 106 deletions.
2 changes: 0 additions & 2 deletions CryptoLib/CryptoLib/Ldap/Addressee.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@
@property (nonatomic, strong) NSString *givenName;
@property (nonatomic, strong) NSString *surname;
@property (nonatomic, strong) NSString *identifier;
@property (nonatomic, strong) NSString *type;
@property (nonatomic, strong) NSData *cert;
@property (nonatomic, strong) NSDate *validTo;
@property (nonatomic, strong) NSArray<NSString*> *policyIdentifiers;

@end
2 changes: 1 addition & 1 deletion MoppApp/MoppApp/AddresseeViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ extension AddresseeViewController : ContainerFoundAddresseeCellDelegate {
if !selectedAddressees.contains(where: {(
($0.givenName != nil && $0.givenName == addressee.givenName &&
$0.surname != nil && $0.surname == addressee.surname) ||
$0.identifier == addressee.identifier) && $0.type == addressee.type && $0.validTo == addressee.validTo
$0.identifier == addressee.identifier) && $0.cert == addressee.cert && $0.validTo == addressee.validTo
}) {
selectedAddressees.insert(addressee, at: 0)
}
Expand Down
26 changes: 14 additions & 12 deletions MoppLib/MoppLib/MoppLibCertificate.mm
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,22 @@ + (MoppLibCertificateOrganization)certificateOrganization:(const digidoc::X509Ce
for (const std::string &policy: cert.certificatePolicies()) {
[policies addObject:[NSString stringWithUTF8String:policy.c_str()]];
}
EIDType eidType = [MoppLibManager eidTypeFromCertificatePolicies:policies];

switch (eidType) {
case EIDTypeUnknown:
case EIDTypeESeal:
return Unknown;
case EIDTypeMobileID:
return MobileID;
case EIDTypeSmartID:
return SmartID;
case EIDTypeDigiID:
return DigiID;
case EIDTypeIDCard:
for (NSString *policyID in policies) {
if ([policyID hasPrefix:@"1.3.6.1.4.1.10015.1.1"]
|| [policyID hasPrefix:@"1.3.6.1.4.1.51361.1.1.1"])
return IDCard;
else if ([policyID hasPrefix:@"1.3.6.1.4.1.10015.1.2"]
|| [policyID hasPrefix:@"1.3.6.1.4.1.51361.1.1"]
|| [policyID hasPrefix:@"1.3.6.1.4.1.51455.1.1"])
return DigiID;
else if ([policyID hasPrefix:@"1.3.6.1.4.1.10015.1.3"]
|| [policyID hasPrefix:@"1.3.6.1.4.1.10015.11.1"])
return MobileID;
else if ([policyID hasPrefix:@"1.3.6.1.4.1.10015.7.3"]
|| [policyID hasPrefix:@"1.3.6.1.4.1.10015.7.1"]
|| [policyID hasPrefix:@"1.3.6.1.4.1.10015.2.1"])
return ESeal;
}
return Unknown;
}
Expand Down
1 change: 1 addition & 0 deletions MoppLib/MoppLib/PublicInterface/MoppLibCerificatetData.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ typedef NS_ENUM(int, MoppLibCertificateOrganization) {
SmartID,
DigiID,
EResident,
ESeal,
Unknown
};

Expand Down
46 changes: 0 additions & 46 deletions MoppLib/MoppLib/PublicInterface/MoppLibCryptoActions.m
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,7 @@ - (void)parseCdocInfo:(NSString *)fullPath success:(CdocContainerBlock)success f
}
for (Addressee* addressee in response.addressees) {
MoppLibCerificatetData *certData = [MoppLibCerificatetData new];
addressee.policyIdentifiers = [MoppLibDigidocManager certificatePolicyIdentifiers:addressee.cert];
[MoppLibCertificate certData:certData updateWithDerEncoding:addressee.cert];
addressee.type = [self formatTypeToString :certData.organization];
addressee.validTo = certData.expiryDate;
}
}
Expand All @@ -77,23 +75,6 @@ - (void)parseCdocInfo:(NSString *)fullPath success:(CdocContainerBlock)success f
});
}

- (NSString*)formatTypeToString:(MoppLibCertificateOrganization)formatType {
NSString *result = nil;
switch(formatType) {
case DigiID:
result = @"DIGI-ID";
break;
case IDCard:
result = @"ID-CARD";
break;
default:
result = @"E-SEAL";
break;
}

return result;
}

- (void)decryptData:(NSString *)fullPath withPin1:(NSString*)pin1 success:(DecryptedDataBlock)success failure:(FailureBlock)failure {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSError *error;
Expand Down Expand Up @@ -147,26 +128,6 @@ - (void)encryptData:(NSString *)fullPath withDataFiles:(NSArray*)dataFiles withA
});
}

- (MoppLibCertificateOrganization)parseEIDType:(NSArray<NSString *>*)certPolicies {

EIDType eidType = [MoppLibManager eidTypeFromCertificatePolicies:certPolicies];

switch (eidType) {
case EIDTypeUnknown:
case EIDTypeESeal:
return Unknown;
case EIDTypeMobileID:
return MobileID;
case EIDTypeSmartID:
return SmartID;
case EIDTypeDigiID:
return DigiID;
case EIDTypeIDCard:
return IDCard;
}
return Unknown;
}

- (void)searchLdapData:(NSString *)identifier success:(LdapBlock)success failure:(FailureBlock)failure configuration:(MoppLdapConfiguration *) moppLdapConfiguration {

Reachability *reachability = [Reachability reachabilityForInternetConnection];
Expand Down Expand Up @@ -203,8 +164,6 @@ - (void)searchLdapData:(NSString *)identifier success:(LdapBlock)success failure
MoppLibCertificateInfo *certInfo = [MoppLibCertificateInfo alloc];
NSArray<NSString *> *certPolicies = [certInfo certificatePolicies:(certData)];
NSArray<NSNumber *> *certKeyUsages = [certInfo keyUsages:(certData)];

addressee.policyIdentifiers = certPolicies;

if (key.cn != NULL) {
NSArray *cn = [key.cn componentsSeparatedByString:@","];
Expand All @@ -214,14 +173,9 @@ - (void)searchLdapData:(NSString *)identifier success:(LdapBlock)success failure
addressee.identifier = cn[2];
} else {
addressee.identifier = cn[0];
addressee.type = @"E-SEAL";
}
}

if (addressee.type == nil) {
addressee.type = [self formatTypeToString:[self parseEIDType:certPolicies]];
}

if (([certInfo hasKeyEnciphermentUsage:(certKeyUsages)] || [certInfo hasKeyAgreementUsage:(certKeyUsages)]) &&
![certInfo isServerAuthKeyPurpose:(certData)] &&
(![certInfo isESealType:(certPolicies)] || ![certInfo isTlsClientAuthKeyPurpose:(certData)]) &&
Expand Down
11 changes: 0 additions & 11 deletions MoppLib/MoppLib/PublicInterface/MoppLibManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,6 @@
#import "MoppLibRoleAddressData.h"
#import "MoppLibProxyConfiguration.h"

typedef NS_ENUM(NSUInteger, EIDType) {
EIDTypeUnknown,
EIDTypeMobileID,
EIDTypeSmartID,
EIDTypeDigiID,
EIDTypeIDCard,
EIDTypeESeal
};

@interface MoppLibManager : NSObject

+ (MoppLibManager *)sharedInstance;
Expand All @@ -58,8 +49,6 @@ typedef NS_ENUM(NSUInteger, EIDType) {
- (NSString *)appVersion;
- (NSString *)iOSVersion;
- (NSString *)userAgent;
+ (EIDType)eidTypeFromCertificate:(NSData*)certData;
+ (EIDType)eidTypeFromCertificatePolicies:(NSArray<NSString*>*)certificatePolicies;
+ (NSArray *)certificatePolicyIdentifiers:(NSData *)certData;
+ (NSString *)sanitize:(NSString *)text;

Expand Down
34 changes: 0 additions & 34 deletions MoppLib/MoppLib/PublicInterface/MoppLibManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -73,40 +73,6 @@ - (NSString *)userAgent {
return [[MoppLibDigidocManager sharedInstance] userAgent];
}

+ (EIDType)eidTypeFromCertificate:(NSData*)certData {
NSArray<NSString*> *policyIdentifiers = [MoppLibDigidocManager certificatePolicyIdentifiers:certData];
if ([policyIdentifiers count] == 0) {
return EIDTypeUnknown;
}

return [self eidTypeFromCertificatePolicies:policyIdentifiers];
}

+ (EIDType)eidTypeFromCertificatePolicies:(NSArray<NSString*>*)policyIdentifiers {
if ([policyIdentifiers count] == 0) {
return EIDTypeUnknown;
}

for (NSString *policyID in policyIdentifiers) {
if ([policyID hasPrefix:@"1.3.6.1.4.1.10015.1.1"]
|| [policyID hasPrefix:@"1.3.6.1.4.1.51361.1.1.1"])
return EIDTypeIDCard;
else if ([policyID hasPrefix:@"1.3.6.1.4.1.10015.1.2"]
|| [policyID hasPrefix:@"1.3.6.1.4.1.51361.1.1"]
|| [policyID hasPrefix:@"1.3.6.1.4.1.51455.1.1"])
return EIDTypeDigiID;
else if ([policyID hasPrefix:@"1.3.6.1.4.1.10015.1.3"]
|| [policyID hasPrefix:@"1.3.6.1.4.1.10015.11.1"])
return EIDTypeMobileID;
else if ([policyID hasPrefix:@"1.3.6.1.4.1.10015.7.3"]
|| [policyID hasPrefix:@"1.3.6.1.4.1.10015.7.1"]
|| [policyID hasPrefix:@"1.3.6.1.4.1.10015.2.1"])
return EIDTypeESeal;
}

return EIDTypeUnknown;
}

+ (NSArray *)certificatePolicyIdentifiers:(NSData *)certData {
return [MoppLibDigidocManager certificatePolicyIdentifiers:certData];
}
Expand Down

0 comments on commit b8f8eb3

Please sign in to comment.