README.textile
author Jens Alfke <jens@mooseyard.com>
Fri Aug 07 11:24:53 2009 -0700 (2009-08-07)
changeset 28 54b373aa65ab
parent 9 aa5eb3fd6ebf
permissions -rw-r--r--
Fixed iPhone OS build. (issue 3)
snej@2
     1
h1=. MYCrypto
snej@2
     2
snej@9
     3
p=. Version 0.2 -- 12 April 2009
snej@2
     4
snej@2
     5
p=. By "Jens Alfke":mailto:jens@mooseyard.com <br>
snej@2
     6
Based in part on code by Wade Tregaskis, <br>
snej@2
     7
and sample code by Apple Computer.
snej@2
     8
snej@2
     9
h2. Introduction
snej@2
    10
snej@2
    11
*MYCrypto* is a high-level cryptography API for Mac OS X and iPhone. It's an Objective-C wrapper around the system
snej@2
    12
"*Keychain*":http://developer.apple.com/documentation/Security/Conceptual/keychainServConcepts/02concepts/concepts.html#//apple_ref/doc/uid/TP30000897-CH204-TP9
snej@2
    13
and *CSSM* APIs, which are notoriously hard to use, as well as *CommonCrypto*, which is easier but quite limited.
snej@2
    14
snej@4
    15
MYCrypto gives you easy object-oriented interfaces to:
snej@2
    16
snej@2
    17
* Symmmetric cryptography (session keys and password-based encryption)
snej@2
    18
* Asymmetric cryptography (public and private keys; digital signatures)
snej@5
    19
* Identity certificates (for use with SSL and CMS)
snej@2
    20
* Cryptographic digests/hashes (effectively-unique IDs for data)
snej@2
    21
* The Keychain (a secure, encrypted storage system for keys and passwords)
snej@9
    22
* Cryptographic Message Syntax [CMS] for signing/encrypting data
snej@2
    23
snej@8
    24
It's open source, released under a friendly BSD license.
snej@8
    25
snej@2
    26
h3. Requirements
snej@2
    27
snej@2
    28
* Mac OS X 10.5 or later _[has been tested on 10.5.6]_
snej@6
    29
* or iPhone OS 2.0 or later _[not yet tested; see Limitations section below]_
snej@6
    30
* or iPhone Simulator, for iPhone OS 2.0 or later
snej@3
    31
* The "MYUtilities":http://mooseyard.com/hg/hgwebdir.cgi/MYUtilities library, which is used by MYCrypto.
snej@2
    32
* _Some understanding of security and cryptography on your part!_ Even with convenient APIs, cryptographic operations still require care and knowledge to be used safely. There are already too many "examples":http://en.wikipedia.org/wiki/Wired_Equivalent_Privacy#Flaws of insecure systems that were incorrectly assembled from secure primitives; don't add your app to that list. Please read a good overview like "??Practical Cryptography??":http://www.schneier.com/book-practical.html before attempting anything the least bit fancy.
snej@2
    33
snej@6
    34
h3. How To Get It
snej@2
    35
snej@6
    36
* "Download the current source code":http://mooseyard.com/hg/hgwebdir.cgi/MYCrypto/archive/tip.zip
snej@11
    37
* or to check out the source code using "Mercurial":http://selenic.com/mercurial/:<br>
snej@8
    38
@hg clone http://mooseyard.com/hg/hgwebdir.cgi/MYCrypto/ MYCrypto@
snej@6
    39
* As described above, you'll also need to download or check out MYUtilities and put it in a directory next to MYCrypto.
snej@11
    40
* To file or view bug reports, visit "the project tracker page":http://mooseyard.lighthouseapp.com/projects/29227/home.
snej@6
    41
* Or if you're just looking:
snej@6
    42
** "Browse the source code":http://mooseyard.com/hg/hgwebdir.cgi/MYCrypto/file/tip
snej@6
    43
** "Browse the class documentation":Documentation/html/hierarchy.html
snej@2
    44
snej@11
    45
h3. How To Build It
snej@11
    46
snej@11
    47
With Xcode, of course. But before the _first_ time you build MYCrypto.xcode, you'll need to tell Xcode where the MYUtilities sources are. You do this by setting up a named 'Source Tree':
snej@11
    48
snej@11
    49
# Open Xcode's Preferences panel
snej@11
    50
# Click the "Source Trees" icon at the top
snej@11
    51
# Click the "+" button to add a new item to the list
snej@11
    52
# Fill in the Setting Name as "@MYUtilities@", the Display Name also as "@MYUtilities@", and the Path as the absolute filesystem path to where you downloaded MYUtilities to. _Do not use a "~" in this path!_ The compiler won't understand it and will give you errors.
snej@11
    53
snej@11
    54
Now you're golden. From now on you can just open MYCrypto.xcode and press the Build button.
snej@11
    55
snej@11
    56
(So far, the MYCrypto project doesn't build anything that's useful to you, like a framework ... just a tiny program that runs the unit-tests. You can add the source files to your own projects to use them.)
snej@11
    57
snej@2
    58
h2. Overview
snej@2
    59
snej@2
    60
The class hierarchy of MYCrypto looks like this:
snej@2
    61
snej@5
    62
* "_MYKeychain_":Documentation/html/interfaceMYKeychain.html
snej@5
    63
* "_MYKeychainItem_":Documentation/html/interfaceMYKeychainItem.html
snej@5
    64
** "_MYKey_":Documentation/html/interfaceMYKey.html
snej@5
    65
*** "MYSymmetricKey":Documentation/html/interfaceMYSymmetricKey.html
snej@5
    66
*** "MYPublicKey":Documentation/html/interfaceMYPublicKey.html
snej@5
    67
*** "MYPrivateKey":Documentation/html/interfaceMYPrivateKey.html
snej@5
    68
*** "MYCertificate":Documentation/html/interfaceMYCertificate.html
snej@5
    69
**** "MYIdentity":Documentation/html/interfaceMYIdentity.html
snej@5
    70
* "_MYDigest_":Documentation/html/interfaceMYDigest.html
snej@5
    71
** "MYSHA1Digest":Documentation/html/interfaceMYSHA1Digest.html
snej@5
    72
** "MYSHA256Digest":Documentation/html/interfaceMYSHA256Digest.html
snej@2
    73
* "MYCryptor":Documentation/html/interfaceMYCryptor.html
snej@9
    74
* "MYEncoder":Documentation/html/interfaceMYEncoder.html
snej@9
    75
* "MYDecoder":Documentation/html/interfaceMYDecoder.html
snej@9
    76
* "MYSigner":Documentation/html/interfaceMYSigner.html
snej@2
    77
snej@5
    78
(_Italicized_ classes are abstract.)
snej@2
    79
snej@11
    80
h2. Examples
snej@11
    81
snej@11
    82
h3. Creating an RSA key-pair
snej@11
    83
snej@11
    84
<pre>
snej@11
    85
MYPrivateKey *keyPair = [[MYKeychain defaultKeychain] generateRSAKeyPairOfSize: 2048];
snej@11
    86
</pre>
snej@11
    87
snej@11
    88
h3. Creating a self-signed identity certificate:
snej@11
    89
snej@11
    90
<pre>
snej@11
    91
NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:
snej@11
    92
		@"alice", @"Common Name",
snej@11
    93
		@"Alice", @"Given Name",
snej@11
    94
		@"Lidell", @"Surname",
snej@11
    95
		nil];
snej@11
    96
MYIdentity *ident = [keyPair createSelfSignedIdentityWithAttributes: attrs];
snej@11
    97
snej@11
    98
NSData *certData = ident.certificateData;
snej@11
    99
</pre>
snej@11
   100
snej@11
   101
h3. Signing and encrypting a message:
snej@11
   102
snej@11
   103
<pre>
snej@11
   104
NSData *cleartext = [@"Attack at dawn" dataUsingEncoding: NSUTF8StringEncoding];
snej@11
   105
MYEncoder *encoder = [[MYEncoder alloc] init];
snej@11
   106
[encoder addSigner: ident];
snej@11
   107
[encoder addRecipient: bob];
snej@11
   108
[encoder addRecipient: carla];
snej@11
   109
[encoder addData: cleartext];
snej@11
   110
[encoder finish];
snej@11
   111
NSData *ciphertext = encoder.encodedData;
snej@11
   112
snej@11
   113
sendMessage(ciphertext);
snej@11
   114
</pre>
snej@11
   115
snej@11
   116
h3. Verifying and decoding a message:
snej@11
   117
snej@11
   118
<pre>
snej@11
   119
NSData *ciphertext = receiveMessage();
snej@11
   120
NSError *error;
snej@11
   121
MYDecoder *decoder = [[MYDecoder alloc] initWithData: ciphertext error: &error];
snej@11
   122
if (!decoder)
snej@11
   123
    return NO;
snej@11
   124
snej@11
   125
if (!decoder.isSigned)
snej@11
   126
    return NO;
snej@11
   127
decoder.policy = [MYCertificate X509Policy];
snej@11
   128
NSMutableArray *signerCerts = [NSMutableArray array];
snej@11
   129
for (MYSigner *signer in decoder.signers) {
snej@11
   130
    if (signer.status != kCMSSignerValid) {
snej@11
   131
        return NO;
snej@11
   132
    [signerCerts addObject: signer.certificate];
snej@11
   133
}
snej@11
   134
snej@11
   135
NSData *plaintext = decoder.content;
snej@11
   136
processMessage(plaintext, signerCerts);
snej@11
   137
</pre>
snej@11
   138
snej@6
   139
h2. Current Limitations
snej@6
   140
snej@6
   141
h3. First off, the biggest caveat of all:
snej@6
   142
snej@9
   143
* *MYCrypto 0.2 is new code and has not yet been used in any real projects. Expect bugs.* (I'm talking about my wrapper/glue code. The underlying cryptographic functionality provided by the OS is robust.)
snej@6
   144
snej@9
   145
h3. Further issues with the 0.2 release:
snej@6
   146
snej@6
   147
* *MYCrypto does not yet work on the iPhone.* It currently builds, but runs into problems at runtime. I'm currently trying to figure these out. (The iPhone OS Security APIs are very different from the Mac OS X ones, and I'm much less familiar with them.) However, it does work in the iPhone Simulator, which uses the OS X APIs.
snej@6
   148
* Exporting symmetric keys in wrapped (encrypted) form will fail. Currently they can be exported only as raw key data.
snej@6
   149
* Importing symmetric keys, in any form, will fail ... kind of a deal-breaker for using them across two computers, unfortunately.
snej@6
   150
snej@6
   151
h3. Current API limitations, to be remedied in the future:
snej@6
   152
snej@6
   153
* No API for accessing passwords; fortunately there are several other utility libraries that provide this. And if your code is doing cryptographic operations, it probably needs to store the keys themselves, not passwords.
snej@6
   154
* No evaluation of trust in certificates (i.e. SecTrust and related APIs.)
snej@6
   155
* Error reporting is too limited. Most methods indicate an error by returning nil, NULL or NO, but don't provide the standard "out" NSError parameter to provide more information. Expect the API to be refactored in the near future to remedy this.
snej@6
   156
snej@2
   157
h2. References
snej@2
   158
snej@2
   159
* "??Security Overview??":http://developer.apple.com/documentation/Security/Conceptual/Security_Overview/Introduction/Introduction.html (Apple)
snej@2
   160
* "??Secure Coding Guide??":http://developer.apple.com/documentation/Security/Conceptual/SecureCodingGuide/Introduction.html (Apple)
snej@2
   161
snej@2
   162
* "??Common Security: CDSA and CSSM, Version 2??":http://www.opengroup.org/publications/catalog/c914.htm (The Open Group)
snej@2
   163
snej@2
   164
* "??Practical Cryptography??":http://www.schneier.com/book-practical.html (Ferguson and Schneier)
snej@5
   165
* "??Handbook of Applied Cryptography??":http://www.cacr.math.uwaterloo.ca/hac/ (Menezes, van Oorschot, Vanstone) -- free download!
snej@2
   166
* "??The Devil's InfoSec Dictionary??":http://www.csoonline.com/article/220527/The_Devil_s_Infosec_Dictionary (CSO Online)