MYParsedCertificate.h
author Jens Alfke <jens@mooseyard.com>
Fri Jun 05 08:57:18 2009 -0700 (2009-06-05)
changeset 20 df9da0f6b358
parent 19 f6c91b9da05b
permissions -rw-r--r--
Factored out the name accessors of MYParsedCertificate into a new class MYCertificateName, so that both subject and issuer can be accessed. A bit of other cleanup too.
     1 //
     2 //  MYParsedCertificate.h
     3 //  MYCrypto
     4 //
     5 //  Created by Jens Alfke on 6/2/09.
     6 //  Copyright 2009 Jens Alfke. All rights reserved.
     7 //
     8 
     9 #import <Foundation/Foundation.h>
    10 @class MYCertificateName, MYCertificate, MYPublicKey, MYPrivateKey, MYOID;
    11 
    12 /** A parsed X.509 certificate. Can be used to get more info about an existing cert,
    13     to modify and regenerate a self-signed cert, or to create a new self-signed cert. */
    14 @interface MYParsedCertificate : NSObject 
    15 {
    16     @private
    17     NSData *_data;
    18     NSArray *_root;
    19     MYCertificate *_issuerCertificate;
    20 }
    21 
    22 /** Initializes an instance by parsing an existing X.509 certificate's data. */
    23 - (id) initWithCertificateData: (NSData*)data error: (NSError**)outError;
    24 
    25 /** The raw data of the certificate. */
    26 @property (readonly) NSData* certificateData;
    27 
    28 /** The date/time at which the certificate first becomes valid. */
    29 @property (retain) NSDate *validFrom;
    30 
    31 /** The date/time at which the certificate expires. */
    32 @property (retain) NSDate *validTo;
    33 
    34 /** Information about the identity of the owner of this certificate. */
    35 @property (readonly) MYCertificateName *subject;
    36 
    37 /** Information about the identity that signed/authorized this certificate. */
    38 @property (readonly) MYCertificateName *issuer;
    39 
    40 /** Returns YES if the issuer is the same as the subject. (Aka a "self-signed" certificate.) */
    41 @property (readonly) BOOL isRoot;
    42 
    43 /** The public key of the subject of the certificate. */
    44 @property (readonly) MYPublicKey *subjectPublicKey;
    45 
    46 /** Associates the certificate to its issuer.
    47     If the cert is not self-signed, you must manually set this property before validating. */
    48 @property (retain) MYCertificate* issuerCertificate;
    49 
    50 /** Checks that the issuer's signature is valid and hasn't been tampered with.
    51     If the certificate is root/self-signed, the subjectPublicKey is used to check the signature;
    52     otherwise, the issuer property needs to have been set and its publicKey will be used. */
    53 - (BOOL) validateSignature;
    54 
    55 
    56 // Generating certificates:
    57 
    58 /** Initializes a blank instance which can be used to create a new certificate.
    59     The certificate will not contain anything yet other than the public key.
    60     The desired attributes should be set, and then the -selfSignWithPrivateKey:error method called. */
    61 - (id) initWithPublicKey: (MYPublicKey*)pubKey;
    62 
    63 /** Has the certificate been signed yet? */
    64 @property (readonly) BOOL isSigned;
    65 
    66 /** Signs the certificate using the given private key, which must be the counterpart of the
    67     public key stored in the certificate.
    68     The subject attributes will be copied to the issuer attributes.
    69     If no valid date range has been set yet, it will be set to a range of one year starting from
    70     the current time.
    71     A unique serial number based on the current time will be set.
    72     After this method returns successfully, access the certificateData property to get the
    73     encoded certificate. */
    74 - (BOOL) selfSignWithPrivateKey: (MYPrivateKey*)privateKey error: (NSError**)outError;
    75 
    76 @end
    77 
    78 
    79 
    80 /** An X.509 Name structure, describing the subject or issuer of a certificate.
    81     Changing a property value of an instance associated with an already-signed certificate will
    82     raise an exception. */
    83 @interface MYCertificateName : NSObject
    84 {
    85     @private
    86     NSArray *_components;
    87 }
    88 
    89 /** The "common name" (nickname, whatever). */
    90 @property (copy) NSString *commonName;
    91 
    92 /** The given/first name. */
    93 @property (copy) NSString *givenName;
    94 
    95 /** The surname / last name / family name. */
    96 @property (copy) NSString *surname;
    97 
    98 /** A description. */
    99 @property (copy) NSString *nameDescription;
   100 
   101 /** The raw email address. */
   102 @property (copy) NSString *emailAddress;
   103 
   104 /** Lower-level accessor that returns the value associated with the given OID. */
   105 - (NSString*) stringForOID: (MYOID*)oid;
   106 
   107 /** Lower-level accessor that sets the value associated with the given OID. */
   108 - (void) setString: (NSString*)value forOID: (MYOID*)oid;
   109 
   110 @end