FreeRDP
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Modules Pages
Encryptor Class Reference
Inheritance diagram for Encryptor:
Collaboration diagram for Encryptor:

Instance Methods

(id) - initWithPassword:
 
(NSData *) - encryptData:
 
(NSData *) - decryptData:
 
(NSData *) - encryptString:
 
(NSString *) - decryptString:
 

Properties

NSString * plaintextPassword
 

Detailed Description

Definition at line 27 of file Encryptor.h.

Method Documentation

◆ decryptData:

- (NSData *) decryptData: (NSData *)  encrypted_data

Definition at line 1 of file Encryptor.m.

130 :(NSData *)encrypted_data
131{
132 if ([encrypted_data length] <= TSXEncryptorBlockCipherBlockSize)
133 return nil;
134
135 NSMutableData *plaintext_data =
136 [NSMutableData dataWithLength:[encrypted_data length] + TSXEncryptorBlockCipherBlockSize];
137 size_t data_out_moved = 0;
138
139 int ret =
140 CCCrypt(kCCDecrypt, TSXEncryptorBlockCipherAlgo, TSXEncryptorBlockCipherOptions,
141 [_encryption_key bytes], TSXEncryptorBlockCipherKeySize, [encrypted_data bytes],
142 [encrypted_data bytes] + TSXEncryptorBlockCipherBlockSize,
143 [encrypted_data length] - TSXEncryptorBlockCipherBlockSize,
144 [plaintext_data mutableBytes], [plaintext_data length], &data_out_moved);
145
146 switch (ret)
147 {
148 case kCCSuccess:
149 [plaintext_data setLength:data_out_moved];
150 return plaintext_data;
151
152 case kCCBufferTooSmall: // Our output buffer is big enough to decrypt valid data. This
153 // return code indicates malformed data.
154 case kCCAlignmentError: // Shouldn't get this, since we're using padding.
155 case kCCDecodeError: // Wrong key.
156 return nil;
157
158 default:
159 NSLog(@"%s: uncaught error, ret CCCryptorStatus = %d (encrypted data len = %lu; buffer "
160 @"size = %lu; dom = %lu)",
161 __func__, ret, (unsigned long)[encrypted_data length],
162 (unsigned long)[plaintext_data length], data_out_moved);
163 return nil;
164 }
165
166 return nil;
167}

◆ decryptString:

- (NSString *) decryptString: (NSData *)  encrypted_string

Definition at line 1 of file Encryptor.m.

174 :(NSData *)encrypted_string
175{
176 return [[[NSString alloc] initWithData:[self decryptData:encrypted_string]
177 encoding:NSUTF8StringEncoding] autorelease];
178}

◆ encryptData:

- (NSData *) encryptData: (NSData *)  plaintext_data

Definition at line 1 of file Encryptor.m.

95 :(NSData *)plaintext_data
96{
97 if (![plaintext_data length])
98 return nil;
99
100 NSData *iv = [self randomInitializationVector];
101 NSMutableData *encrypted_data = [NSMutableData
102 dataWithLength:[iv length] + [plaintext_data length] + TSXEncryptorBlockCipherBlockSize];
103 [encrypted_data replaceBytesInRange:NSMakeRange(0, [iv length]) withBytes:[iv bytes]];
104
105 size_t data_out_moved = 0;
106 int ret = CCCrypt(kCCEncrypt, TSXEncryptorBlockCipherAlgo, TSXEncryptorBlockCipherOptions,
107 [_encryption_key bytes], TSXEncryptorBlockCipherKeySize, [iv bytes],
108 [plaintext_data bytes], [plaintext_data length],
109 [encrypted_data mutableBytes] + [iv length],
110 [encrypted_data length] - [iv length], &data_out_moved);
111
112 switch (ret)
113 {
114 case kCCSuccess:
115 [encrypted_data setLength:[iv length] + data_out_moved];
116 return encrypted_data;
117
118 default:
119 NSLog(
120 @"%s: uncaught error, ret CCCryptorStatus = %d (plaintext len = %lu; buffer size = "
121 @"%lu)",
122 __func__, ret, (unsigned long)[plaintext_data length],
123 (unsigned long)([encrypted_data length] - [iv length]));
124 return nil;
125 }
126
127 return nil;
128}

◆ encryptString:

- (NSData *) encryptString: (NSString *)  plaintext_string

Definition at line 1 of file Encryptor.m.

169 :(NSString *)plaintext_string
170{
171 return [self encryptData:[plaintext_string dataUsingEncoding:NSUTF8StringEncoding]];
172}

◆ initWithPassword:

- (id) initWithPassword: (NSString *)  plaintext_password

Definition at line 1 of file Encryptor.m.

30 :(NSString *)plaintext_password
31{
32 if (plaintext_password == nil)
33 return nil;
34
35 if (!(self = [super init]))
36 return nil;
37
38 _plaintext_password = [plaintext_password retain];
39 const char *plaintext_password_data =
40 [plaintext_password length] ? [plaintext_password UTF8String] : " ";
41
42 if (!plaintext_password_data || !strlen(plaintext_password_data))
43 [NSException raise:NSInternalInconsistencyException
44 format:@"%s: plaintext password data is zero length!", __func__];
45
46 uint8_t *derived_key = calloc(1, TSXEncryptorPBKDF2KeySize);
47
48 if (CCKeyDerivationPBKDF != NULL)
49 {
50 int ret = CCKeyDerivationPBKDF(
51 kCCPBKDF2, plaintext_password_data, strlen(plaintext_password_data) - 1,
52 (const uint8_t *)TSXEncryptorPBKDF2Salt, TSXEncryptorPBKDF2SaltLen, kCCPRFHmacAlgSHA1,
53 TSXEncryptorPBKDF2Rounds, derived_key, TSXEncryptorPBKDF2KeySize);
54 // NSLog(@"CCKeyDerivationPBKDF ret = %d; key: %@", ret, [NSData
55 // dataWithBytesNoCopy:derived_key length:TWEncryptorPBKDF2KeySize freeWhenDone:NO]);
56
57 if (ret)
58 {
59 NSLog(@"%s: CCKeyDerivationPBKDF ret == %d, indicating some sort of failure.", __func__,
60 ret);
61 free(derived_key);
62 [self autorelease];
63 return nil;
64 }
65 }
66 else
67 {
68 // iOS 4.x or earlier -- use OpenSSL
69 unsigned long ret = PKCS5_PBKDF2_HMAC_SHA1(
70 plaintext_password_data, (int)strlen(plaintext_password_data) - 1,
71 (const unsigned char *)TSXEncryptorPBKDF2Salt, TSXEncryptorPBKDF2SaltLen,
72 TSXEncryptorPBKDF2Rounds, TSXEncryptorPBKDF2KeySize, derived_key);
73 // NSLog(@"PKCS5_PBKDF2_HMAC_SHA1 ret = %lu; key: %@", ret, [NSData
74 // dataWithBytesNoCopy:derived_key length:TWEncryptorPBKDF2KeySize freeWhenDone:NO]);
75
76 if (ret != 1)
77 {
78 NSLog(@"%s: PKCS5_PBKDF2_HMAC_SHA1 ret == %lu, indicating some sort of failure.",
79 __func__, ret);
80 free(derived_key);
81 [self release];
82 return nil;
83 }
84 }
85
86 _encryption_key = [[NSData alloc] initWithBytesNoCopy:derived_key
87 length:TSXEncryptorPBKDF2KeySize
88 freeWhenDone:YES];
89 return self;
90}

Property Documentation

◆ plaintextPassword

- (NSString*) plaintextPassword
readatomicassign

Definition at line 34 of file Encryptor.h.


The documentation for this class was generated from the following files: