1 /* Copyright (c) 1998,2011,2014 Apple Inc. All Rights Reserved.
3 * NOTICE: USE OF THE MATERIALS ACCOMPANYING THIS NOTICE IS SUBJECT
4 * TO THE TERMS OF THE SIGNED "FAST ELLIPTIC ENCRYPTION (FEE) REFERENCE
5 * SOURCE CODE EVALUATION AGREEMENT" BETWEEN APPLE, INC. AND THE
6 * ORIGINAL LICENSEE THAT OBTAINED THESE MATERIALS FROM APPLE,
7 * INC. ANY USE OF THESE MATERIALS NOT PERMITTED BY SUCH AGREEMENT WILL
8 * EXPOSE YOU TO LIABILITY.
9 ***************************************************************************
11 * NSCipherFile.m - ObjC wrapper for feeCipherFile
19 #import "NSCipherFile.h"
20 #import "feeCipherFile.h"
22 #import "NSFEEPublicKeyPrivate.h" /* for -feePubKey */
25 * Private instance data.
31 @implementation NSCipherFile
36 _cfPriv *cfPriv = _priv;
38 feeCFileFree(cfPriv->cfile);
45 * Alloc and return an autoreleased NSCipherFile object associated with
48 + newFromCipherText : (NSData *)cipherText
49 encrType : (cipherFileEncrType)encrType
50 sendPubKeyData : (NSData *)sendPubKeyData
51 otherKeyData : (NSData *)otherKeyData
52 sigData : (NSData *)sigData // optional; nil means no signature
53 userData : (unsigned)userData // for caller's convenience
58 result = [[self alloc] autorelease];
59 result->_priv = cfPriv = fmalloc(sizeof(_cfPriv));
60 cfPriv->cfile = feeCFileNewFromCipherText(encrType,
63 [sendPubKeyData bytes],
64 [sendPubKeyData length],
66 [otherKeyData length],
79 * Obtain the contents of a feeCipherFile as NSData.
81 - (NSData *)dataRepresentation
83 _cfPriv *cfPriv = _priv;
85 const unsigned char *rep;
92 frtn = feeCFileDataRepresentation(cfPriv->cfile,
98 result = [NSData dataWithBytesNoCopy:(unsigned char *)rep
104 * Alloc and return an autoreleased NSCipherFile object given a data
107 + newFromDataRepresentation : (NSData *)dataRep
109 NSCipherFile *result;
113 result = [[self alloc] autorelease];
114 result->_priv = cfPriv = fmalloc(sizeof(_cfPriv));
115 frtn = feeCFileNewFromDataRep([dataRep bytes],
127 * Given an NSCipherFile object, obtain its constituent parts.
129 - (cipherFileEncrType)encryptionType
131 _cfPriv *cfPriv = _priv;
136 return feeCFileEncrType(cfPriv->cfile);
139 - (NSData *)cipherText
141 _cfPriv *cfPriv = _priv;
142 const unsigned char *ctext;
148 ctext = feeCFileCipherText(cfPriv->cfile, &ctextLen);
149 return [NSData dataWithBytesNoCopy:(unsigned char *)ctext
153 - (NSData *)sendPubKeyData
155 _cfPriv *cfPriv = _priv;
156 const unsigned char *key;
162 key = feeCFileSendPubKeyData(cfPriv->cfile, &keyLen);
164 return [NSData dataWithBytesNoCopy:(unsigned char *)key
172 - (NSData *)otherKeyData
174 _cfPriv *cfPriv = _priv;
175 const unsigned char *key;
181 key = feeCFileOtherKeyData(cfPriv->cfile, &keyLen);
183 return [NSData dataWithBytesNoCopy:(unsigned char *)key
193 _cfPriv *cfPriv = _priv;
194 const unsigned char *sig;
200 sig = feeCFileSigData(cfPriv->cfile, &sigLen);
202 return [NSData dataWithBytesNoCopy:(unsigned char *)sig
212 _cfPriv *cfPriv = _priv;
217 return feeCFileUserData(cfPriv->cfile);
221 * High-level cipherFile support.
225 * Create a cipherfile of specified cipherFileEncrType for given plaintext.
227 +(feeReturn)createCipherFileForPrivKey : (NSFEEPublicKey *)sendPrivKey
228 recvPubKey : (NSFEEPublicKey *)recvPubKey
229 encrType : (cipherFileEncrType)encrType
230 plainText : (NSData *)plainText
231 genSig : (BOOL)genSig
232 doEnc64 : (BOOL)doEnc64 // YES ==> perform enc64
233 userData : (unsigned)userData // for caller's convenience
234 cipherFileData : (NSData **)cipherFileData // RETURNED
237 unsigned char *cfileData;
238 unsigned cfileDataLen;
239 feePubKey privKey = NULL;
242 privKey = [sendPrivKey feePubKey];
244 frtn = createCipherFile(privKey,
245 [recvPubKey feePubKey],
258 [NSData dataWithBytesNoCopy:(unsigned char *)cfileData
259 length:cfileDataLen];
264 * Parse and decrypt a data representation of an NSCipherFile object.
266 + (feeReturn)parseCipherFileData : (NSFEEPublicKey *)recvPrivKey
267 sendPubKey : (NSFEEPublicKey *)sendPubKey
268 cipherFileData : (NSData *)cipherFileData
269 doDec64 : (BOOL)doDec64
270 encrType : (cipherFileEncrType *)encrType // RETURNED
271 plainText : (NSData **)plainText // RETURNED
272 sigStatus : (feeSigStatus *)sigStatus // RETURNED
273 sigSigner : (NSString **)sigSigner // RETURNED
274 userData : (unsigned *)userData // RETURNED
277 unsigned char *ptext;
281 feePubKey _pubKey = NULL;
283 if(recvPrivKey == nil) {
284 return FR_IllegalArg; // always required
287 _pubKey = [sendPubKey feePubKey];
290 frtn = parseCipherFile([recvPrivKey feePubKey],
292 [cipherFileData bytes],
293 [cipherFileData length],
305 *plainText = [NSData dataWithBytesNoCopy:ptext length:ptextLen];
306 *sigSigner = [NSString stringWithCharacters:signer length:signerLen];
312 * Parse and decrypt an NSCipherFile object obtained via
313 * +newFromDataRepresentation.
315 * recvPrivKey is required in all cases. If sendPubKey is present,
316 * sendPubKey - rather than the embedded sender's public key - will be
317 * used for signature validation.
319 - (feeReturn)decryptCipherFileData : (NSFEEPublicKey *)recvPrivKey
320 sendPubKey : (NSFEEPublicKey *)sendPubKey
321 plainText : (NSData **)plainText // RETURNED
322 sigStatus : (feeSigStatus *)sigStatus // RETURNED
323 sigSigner : (NSString **)sigSigner // RETURNED
325 _cfPriv *cfPriv = _priv;
327 unsigned char *ptext;
331 feePubKey _pubKey = NULL;
334 return FR_IllegalArg;
336 if(recvPrivKey == nil) {
337 return FR_IllegalArg; // always required
340 _pubKey = [sendPubKey feePubKey];
343 frtn = decryptCipherFile(cfPriv->cfile,
344 [recvPrivKey feePubKey],
354 *plainText = [NSData dataWithBytesNoCopy:ptext length:ptextLen];
355 *sigSigner = [NSString stringWithCharacters:signer length:signerLen];