]> git.saurik.com Git - apple/security.git/blob - keychain/SecureObjectSync/SOSPeerRateLimiter.m
Security-59306.11.20.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 self = [super initWithConfig:[self setUpConfigForPeer]];
75 if(self){
76 self.peerID = (__bridge NSString *)(SOSPeerGetID(peer));
77 self.accessGroupRateLimitState = [[NSMutableDictionary alloc] init];
78 self.accessGroupToTimer = [[NSMutableDictionary alloc]init];
79 self.accessGroupToNextMessageToSend = [[NSMutableDictionary alloc]init];
80 }
81 return self;
82 }
83
84 -(enum RateLimitState) stateForAccessGroup:(NSString*) accessGroup
85 {
86 enum RateLimitState stateForAccessGroup;
87 NSNumber *state = [self.accessGroupRateLimitState objectForKey:accessGroup];
88 if(state == nil)
89 {
90 //initialize access group state
91 stateForAccessGroup = RateLimitStateCanSend;
92 NSNumber *initialize = [[NSNumber alloc] initWithLong:stateForAccessGroup];
93 [self.accessGroupRateLimitState setObject:initialize forKey:accessGroup];
94 }else{
95 stateForAccessGroup = [state intValue];
96 }
97 return stateForAccessGroup;
98 }
99 @end
100
101 @implementation KeychainItem
102
103 -(instancetype)initWithAccessGroup:(NSString *)accessGroup
104 {
105 self = [super init];
106 if(self){
107 _accessGroup = accessGroup;
108 }
109 return self;
110 }
111
112 @end