MYCertificate now checks validity of self-signed certs loaded from the keychain (because the Security framework doesn't validate self-signed certs.)
5 // Created by Jens Alfke on 6/2/09.
6 // Copyright 2009 Jens Alfke. All rights reserved.
9 #import <Foundation/Foundation.h>
10 @class MYCertificateName, MYCertificate, MYIdentity, MYPublicKey, MYPrivateKey, MYOID;
12 /** A parsed X.509 certificate; provides access to the names and metadata. */
13 @interface MYCertificateInfo : NSObject
20 /** Initialize by parsing X.509 certificate data.
21 (More commonly you'll get an instance via MYCertificate's 'info' property.) */
22 - (id) initWithCertificateData: (NSData*)data error: (NSError**)outError;
24 /** The date/time at which the certificate first becomes valid. */
25 @property (retain, readonly) NSDate *validFrom;
27 /** The date/time at which the certificate expires. */
28 @property (retain, readonly) NSDate *validTo;
30 /** Information about the identity of the owner of this certificate. */
31 @property (readonly) MYCertificateName *subject;
33 /** Information about the identity that signed/authorized this certificate. */
34 @property (readonly) MYCertificateName *issuer;
36 /** Returns YES if the issuer is the same as the subject. (Aka a "self-signed" certificate.) */
37 @property (readonly) BOOL isRoot;
39 /** Verifies the certificate's signature, using the given public key.
40 If the certificate is root/self-signed, use the cert's own subject public key. */
41 - (BOOL) verifySignatureWithKey: (MYPublicKey*)issuerPublicKey;
47 /** A mutable, unsigned certificate that can be filled out and then signed by the issuer.
48 Used to generate an identity certificate for a key-pair. */
49 @interface MYCertificateRequest : MYCertificateInfo
52 MYPublicKey *_publicKey;
55 /** Initializes a blank instance which can be used to create a new certificate.
56 The certificate will not contain anything yet other than the public key.
57 The desired attributes should be set, and then the -selfSignWithPrivateKey:error method called. */
58 - (id) initWithPublicKey: (MYPublicKey*)pubKey;
60 /** The date/time at which the certificate first becomes valid. Settable. */
61 @property (retain) NSDate *validFrom;
63 /** The date/time at which the certificate expires. Settable */
64 @property (retain) NSDate *validTo;
66 /** Encodes the certificate request in X.509 format -- this is NOT a certificate!
67 It has to be sent to a Certificate Authority to be signed.
68 If you want to generate a self-signed certificate, use one of the self-signing methods instead. */
69 - (NSData*) requestData: (NSError**)outError;
71 /** Signs the certificate using the given private key, which must be the counterpart of the
72 public key stored in the certificate, and returns the encoded certificate data.
73 The subject attributes will be copied to the issuer attributes.
74 If no valid date range has been set yet, it will be set to a range of one year starting from
76 A unique serial number based on the current time will be set. */
77 - (NSData*) selfSignWithPrivateKey: (MYPrivateKey*)privateKey error: (NSError**)outError;
79 /** Signs the certificate using the given private key, which must be the counterpart of the
80 public key stored in the certificate; adds the certificate to the keychain;
81 and returns a MYIdentity representing the paired certificate and private key. */
82 - (MYIdentity*) createSelfSignedIdentityWithPrivateKey: (MYPrivateKey*)privateKey
83 error: (NSError**)outError;
88 /** An X.509 Name structure, describing the subject or issuer of a certificate.
89 The properties are settable only if this instance belongs to a MYCertificateRequest;
90 otherwise trying to set them will raise an exception. */
91 @interface MYCertificateName : NSObject
97 /** The "common name" (nickname, whatever). */
98 @property (copy) NSString *commonName;
100 /** The given/first name. */
101 @property (copy) NSString *givenName;
103 /** The surname / last name / family name. */
104 @property (copy) NSString *surname;
106 /** A description. */
107 @property (copy) NSString *nameDescription;
109 /** The raw email address. */
110 @property (copy) NSString *emailAddress;
112 /** Lower-level accessor that returns the value associated with the given OID. */
113 - (NSString*) stringForOID: (MYOID*)oid;
115 /** Lower-level accessor that sets the value associated with the given OID. */
116 - (void) setString: (NSString*)value forOID: (MYOID*)oid;