]> git.saurik.com Git - apple/security.git/blob - keychain/SecureObjectSync/SOSPeerRateLimiter.m
Security-59754.41.1.tar.gz
[apple/security.git] / keychain / SecureObjectSync / SOSPeerRateLimiter.m
1 //
2 // SOSPeerRateLimiter.m
3 // SecureObjectSyncServer
4 //
5
6 #import <Foundation/Foundation.h>
7 #import <keychain/ckks/RateLimiter.h>
8 #import "keychain/SecureObjectSync/SOSPeerRateLimiter.h"
9
10 #include "keychain/SecureObjectSync/SOSPeer.h"
11 #include <utilities/SecCFError.h>
12 #include <utilities/SecCFRelease.h>
13 #include <utilities/SecCFWrappers.h>
14
15 //
16 // RateLimiting Code per Peer
17
18 @implementation PeerRateLimiter
19
20 @synthesize peerID = peerID;
21
22 -(NSDictionary*) setUpConfigForPeer
23 {
24 NSData *configData = [@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\
25 <!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\
26 <plist version=\"1.0\">\
27 <dict>\
28 <key>general</key>\
29 <dict>\
30 <key>maxStateSize</key>\
31 <integer>250</integer>\
32 <key>maxItemAge</key>\
33 <integer>3600</integer>\
34 <key>overloadDuration</key>\
35 <integer>1800</integer>\
36 <key>name</key>\
37 <string>SOS</string>\
38 <key>MAType</key>\
39 <string></string>\
40 </dict>\
41 <key>groups</key>\
42 <array>\
43 <dict>\
44 <key>property</key>\
45 <string>global</string>\
46 <key>capacity</key>\
47 <integer>1000</integer>\
48 <key>rate</key>\
49 <integer>10</integer>\
50 <key>badness</key>\
51 <integer>1</integer>\
52 </dict>\
53 <dict>\
54 <key>property</key>\
55 <string>accessGroup</string>\
56 <key>capacity</key>\
57 <integer>50</integer>\
58 <key>rate</key>\
59 <integer>900</integer>\
60 <key>badness</key>\
61 <integer>3</integer>\
62 </dict>\
63 </array>\
64 </dict>\
65 </plist>\
66 " dataUsingEncoding:NSUTF8StringEncoding];
67
68 NSError *err = nil;
69 return ([NSPropertyListSerialization propertyListWithData:configData options:NSPropertyListImmutable format:nil error:&err]);
70 }
71
72 -(instancetype)initWithPeer:(SOSPeerRef)peer
73 {
74 if ((self = [super initWithConfig:[self setUpConfigForPeer]])) {
75 self.peerID = (__bridge NSString *)(SOSPeerGetID(peer));
76 self.accessGroupRateLimitState = [[NSMutableDictionary alloc] init];
77 self.accessGroupToTimer = [[NSMutableDictionary alloc]init];
78 self.accessGroupToNextMessageToSend = [[NSMutableDictionary alloc]init];
79 }
80 return self;
81 }
82
83 -(enum RateLimitState) stateForAccessGroup:(NSString*) accessGroup
84 {
85 enum RateLimitState stateForAccessGroup;
86 NSNumber *state = [self.accessGroupRateLimitState objectForKey:accessGroup];
87 if(state == nil)
88 {
89 //initialize access group state
90 stateForAccessGroup = RateLimitStateCanSend;
91 NSNumber *initialize = [[NSNumber alloc] initWithLong:stateForAccessGroup];
92 [self.accessGroupRateLimitState setObject:initialize forKey:accessGroup];
93 }else{
94 stateForAccessGroup = [state intValue];
95 }
96 return stateForAccessGroup;
97 }
98 @end
99
100 @implementation KeychainItem
101
102 -(instancetype)initWithAccessGroup:(NSString *)accessGroup
103 {
104 if ((self = [super init])) {
105 _accessGroup = accessGroup;
106 }
107 return self;
108 }
109
110 @end