cst.h

Go to the documentation of this file.
00001 /*
00002  * This file is part of certman
00003  *
00004  * Copyright (C) 2006 Nokia Corporation.
00005  *
00006  * Contact: Ed Bartosh <Eduard.Bartosh@nokia.com>
00007  * Author: Ed Bartosh <Eduard.Bartosh@nokia.com>
00008  *
00009  * This library is free software; you can redistribute it and/or
00010  * modify it under the terms of the GNU Lesser General Public License
00011  * version 2.1 as published by the Free Software Foundation.
00012  *
00013  * This library is distributed in the hope that it will be useful, but
00014  * WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with this library; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
00021  * 02110-1301 USA
00022  *
00023  */
00024 
00040 #ifndef CST_H_
00041 #define CST_H_
00042 
00043 
00044 #include <openssl/x509.h>
00045 #include <openssl/evp.h>
00046 #include <stdio.h>
00047 #include <time.h>
00048 #include <glib.h>
00049 #include <db/db.h>
00050 
00051 #ifdef __cplusplus
00052 extern "C" {
00053 #endif
00054 
00055 
00058 #define CST_ERROR_OK                    0 
00059 #define CST_ERROR_NOT_FOUND             1
00060 #define CST_ERROR_STRUCTURE_CORRUPT     2
00061 #define CST_ERROR_CREATE_FILE           3
00062 #define CST_ERROR_CERT_EXIST            4
00063 #define CST_ERROR_CRL_EXIST             6
00064 #define CST_ERROR_STORAGE_IS_READONLY   7
00065 #define CST_ERROR_KEY_NOTFOUND          8
00066 #define CST_ERROR_CERT_NOTFOUND         9
00067 #define CST_ERROR_NOT_IMPLEMENTED      10
00068 #define CST_ERROR_NOT_INIT             11
00069 #define CST_ERROR_UNDEF                12
00070 #define CST_ERROR_PARAM_INCORRECT      13
00071 #define CST_ERROR_NOT_OPEN             14
00072 #define CST_ERROR_ASSIGN_INCORRECT     15
00073 #define CST_ERROR_CRL_NOT_VALID        16
00074 #define CST_ERROR_CHAIN_INCOMPLETE     17
00075 #define CST_ERROR_CAPABILITY_NOTFOUND  18    
00076 #define CST_ERROR_INCORRECT_PURPOSE    19
00077 
00078 #define CST_ERROR_IO                   20
00079 #define CST_ERROR_NOSPC                21
00080 #define CST_ERROR_DBSTRUCTURE_CORRUPT  22
00081 
00082 #define CST_ERROR_LOCK                 23
00083 
00084 #define CST_ERROR_PASSWORD_WRONG       24    
00085 #define CST_ERROR_BAD_INTERNAL_FORMAT  25
00086 #define CST_ERROR_EXPORT               CST_ERROR_UNDEF    
00087 #define CST_ERROR_UNDEF_FILE_ERROR     CST_ERROR_UNDEF
00088 #define CST_ERROR_CANCEL               30
00089     
00092 #if 1    
00093     #define CST_DEBUG_LOCK    
00094 #endif
00095     
00096 /* Structures */
00097 
00103 typedef guint cst_t_seqnum;         
00104 
00110 typedef guint64 cst_t_mcount;
00111 
00113     typedef struct CST_st {
00114         int readonly;               
00115         char *filename;             
00116         unsigned char *password;    
00117         int stub;                   
00123         GTree *certs;         
00124 
00129         GTree *keys;
00130 
00131         /* @brief CRL list */
00132         /* GTree *crls; */
00133 
00135         GSList *simple_crls;
00136 
00137         /* Index structures */
00138         GTree *idx_cert_name;   
00139         GTree *idx_cert_dns;    
00140         GTree *idx_cert_email;  
00141         GHashTable *idx_cert_serial; 
00142         GHashTable *idx_cert_fingerprint; 
00144         /* New indexes by integer uid */
00145         GHashTable *idx_cert_uid; 
00146         GHashTable *idx_key_uid;  
00148         DB * db; 
00150         cst_t_mcount modification_count; 
00153         GStaticRWLock rwlock; 
00154     } CST;
00155 
00161     typedef guint8 cst_t_cert_folder;  
00162     
00166 #define CST_FOLDER_CA       1  
00167 
00168 #define CST_FOLDER_PERSONAL 2  
00169 
00170 #define CST_FOLDER_OTHER    3    
00171 
00172 #define CST_FOLDER_SITE     4  
00173 
00174 #define CST_FOLDER_UNKNOWN  0  
00175 
00182     typedef guint32 cst_t_cert_purpose;
00185 #define CST_PURPOSE_NONE        0x0000 
00186 #define CST_PURPOSE_CA          0x0001 
00187 #define CST_PURPOSE_SMIME_SGN   0x0002
00188 #define CST_PURPOSE_SMIME_ENC   0x0004
00189 #define CST_PURPOSE_SSL_SERVER  0x0008
00190 #define CST_PURPOSE_SSL_CLIENT  0x0010
00191 #define CST_PURPOSE_SSL_WLAN    0x0020    
00192 #define CST_PURPOSE_CRL_SIGN    0x0040
00193 #define CST_PURPOSE_ALL         0xFFFF
00194 
00200 #define CST_STATE_VALID         0x0001
00201 #define CST_STATE_NOTVALID      0x0002
00202 #define CST_STATE_EXPIRED       0x0004
00203 #define CST_STATE_REVOKED       0x0008
00204 
00208 #define CST_STACK_OF_X509 STACK_OF(X509)
00209 #define CST_STACK_OF_CRL STACK_OF(X509_CRL)    
00210 #define CST_STACK_OF_ASN1_OBJECT STACK_OF(ASN1_OBJECT)    
00211 
00213 /* Storage config and etc. functions */
00214 
00230     extern CST *CST_open(const int readonly, unsigned char *password);
00231 
00246     extern CST *CST_open_file(const char *filename, const int readonly,
00247                               unsigned char *password);
00248 
00263     extern int CST_create_file(const char *filename, unsigned char *password);
00264 
00278     extern int CST_save(CST * st);
00279 
00280 
00289     extern void CST_free(CST * st);
00290 
00291 
00298     extern int CST_last_error();
00299 
00300 
00301 /* Import/export/backup */
00302 
00314     extern int CST_backup(CST * st, const char *filename, unsigned char *password);
00315 
00316 
00317 /* Certificate */
00318 
00319     typedef int (*cst_pkcs12_confirm_cb) (X509 * xcert,
00320                             cst_t_cert_folder * folder,
00321                             cst_t_cert_purpose * purpose,
00322                             unsigned char ** out_password,
00323                             int is_pair, 
00324                             int *cancel,
00325                             void *data);
00326     
00327     typedef int (*cst_pkcs12_error_cb) (X509 * xcert, int error, void *data);
00328     
00365     extern void CST_import_PKCS12(CST * st, 
00366             FILE * file, 
00367             cst_pkcs12_confirm_cb confirm_cb,
00368             cst_pkcs12_error_cb error_cb,
00369             unsigned char *password,
00370             void * user_data,
00371             GError **error);   
00372     
00392     extern int CST_import_cert(CST * st, FILE * file, unsigned char *password);
00393     
00404     extern int CST_import_cert_DER(CST * st, FILE * file);
00405 
00420     extern int CST_import_cert_f(CST * st, FILE * file, unsigned char *password, const cst_t_cert_folder folder);
00421     
00435     extern int CST_import_cert_f_DER(CST * st, FILE * file, const cst_t_cert_folder folder);
00436    
00451     extern cst_t_seqnum CST_import_cert_adv(CST * st, FILE * file, 
00452                                      const cst_t_cert_folder folder, 
00453                                      GError **error);
00454     
00469     extern cst_t_seqnum CST_import_cert_adv_DER(CST * st, FILE * file, 
00470                                          const cst_t_cert_folder folder, 
00471                                          GError **error);
00472     
00484     extern int CST_export_all(CST * st, FILE * file,
00485                               const cst_t_cert_folder folder);
00497     extern int CST_export_all_DER(CST * st, FILE * file,
00498                               const cst_t_cert_folder folder);
00499     
00511     extern int CST_export_cert(CST * st, X509 * cert, FILE * file);
00512     
00524     extern int CST_export_cert_DER(CST * st, X509 * cert, FILE * file);   
00525     
00537     extern int CST_export_cert_by_id(CST * st, const cst_t_seqnum certID, FILE * file);
00538 
00550     extern int CST_export_cert_by_id_DER(CST * st, const cst_t_seqnum certID, FILE * file);
00551     
00562     extern int CST_delete_cert(CST * st, const cst_t_seqnum certID);
00563 
00575     extern int CST_append_X509(CST * st, X509 * cert);
00576    
00599     extern GSList * CST_append_sk_X509(CST * st, CST_STACK_OF_X509 * list);
00600 
00611     extern int CST_import_CRL(CST * st, FILE * file);
00612     
00623     extern int CST_import_CRL_DER(CST * st, FILE * file);
00624 
00632     extern GSList * CST_get_all_crl(CST * st);
00633 
00644     extern int CST_delete_crl(CST * st, const cst_t_seqnum crlID);
00645 
00654     extern X509_CRL * CST_get_CRL(CST * st, const cst_t_seqnum crlID);
00655     
00656 
00657 
00658 /* Private key */
00659 
00674     extern int CST_import_priv_key(CST * st, X509_NAME * account,
00675                                    FILE * file, unsigned char *inpass,
00676                                    unsigned char *outpass);
00690     extern int CST_import_priv_key_DER(CST * st, X509_NAME * account,
00691                                    FILE * file, unsigned char *outpass);
00692 
00708     extern cst_t_seqnum CST_import_priv_key_adv(CST * st, X509_NAME * account,
00709                                    FILE * file, unsigned char *inpass,
00710                                    unsigned char *outpass,
00711                                    GError **error);
00726     extern cst_t_seqnum CST_import_priv_key_adv_DER(CST * st, X509_NAME * account,
00727                                    FILE * file, unsigned char *outpass, 
00728                                    GError **error);   
00741     extern int CST_export_priv_key(CST * st, EVP_PKEY * key, FILE * file,
00742                                    unsigned char *password);
00743     
00756     extern int CST_export_priv_key_DER(CST * st, EVP_PKEY * key, FILE * file,
00757                                    unsigned char *password);
00758     
00772     extern int CST_append_priv_key(CST * st, X509_NAME * account,
00773                                    EVP_PKEY * key, unsigned char *password);
00774 
00775 
00776 /* Public key */
00777 
00790     extern int CST_import_pub_key(CST * st, X509_NAME * account,
00791                                   FILE * file);
00804     extern int CST_import_pub_key_DER(CST * st, X509_NAME * account,
00805                                   FILE * file);
00806 
00820     extern cst_t_seqnum CST_import_pub_key_adv(CST * st, X509_NAME * account,
00821                                   FILE * file, GError **error);
00835     extern cst_t_seqnum CST_import_pub_key_adv_DER(CST * st, X509_NAME * account,
00836                                   FILE * file, GError **error);   
00848     extern int CST_export_all_pub_key(CST * st, X509_NAME * account,
00849                                       FILE * file);
00850     
00862     extern int CST_export_all_pub_key_DER(CST * st, X509_NAME * account,
00863                                       FILE * file);
00864 
00876     extern int CST_export_pub_key(CST * st, EVP_PKEY * key, FILE * file);
00877     
00889     extern int CST_export_pub_key_DER(CST * st, EVP_PKEY * key, FILE * file);
00890     
00903     extern int CST_append_pub_key(CST * st, X509_NAME * account,
00904                                   EVP_PKEY * key);
00905 
00914     extern X509_NAME * CST_get_key_account(CST * st, cst_t_seqnum keyID);
00915     
00916 /* Delete */
00917 
00928     extern int CST_delete_all_pub_key(CST * st, X509_NAME * account);
00929 
00940     extern int CST_delete_all_priv_key(CST * st, X509_NAME * account);
00941 
00952     extern int CST_delete_pub_key(CST * st, 
00953                                  const cst_t_seqnum keyID);
00954 
00965     extern int CST_delete_priv_key(CST * st, 
00966                                    const cst_t_seqnum keyID);
00967 
00968 /* Search */
00969 
00982     extern GSList * CST_search_by_subj_name(CST * st,
00983                                             X509_NAME *
00984                                             subject_name);
00985 
00998     extern GSList * CST_search_by_email(CST * st,
00999                                         const char *email);
01000 
01014     extern GSList * CST_search_by_domain_name(CST * st,
01015                                               const char
01016                                               *domain_name);
01017 
01030     extern GSList * CST_search_by_serial(CST * st, const char *serial);   
01031 
01044     extern GSList * CST_search_by_fingerprint(CST * st, const char *fingerprint);
01045     
01055     extern cst_t_seqnum CST_search_issuer(CST * st, X509 * cert);    
01056     
01067     extern CST_STACK_OF_X509 * CST_get_chain(CST * st, X509 * cert);
01068 
01079     extern GSList * CST_get_chain_id_by_id(CST * st, const cst_t_seqnum certID);
01080 
01091     extern GSList * CST_get_chain_id(CST * st, X509 * x);
01092     
01106     extern GSList * CST_search_by_folder_and_purpose(CST * st,
01107                                           const cst_t_cert_folder folder,
01108                                           const cst_t_cert_purpose purpose);
01121     extern GSList * CST_search_by_folder(CST * st,
01122                                          const cst_t_cert_folder
01123                                          folder);
01124 
01137     extern GSList * CST_priv_key_search_by_name(CST * st,
01138                                X509_NAME * account);
01139     
01152     extern GSList * CST_pub_key_search_by_name(CST * st,
01153                                                X509_NAME * account);
01154    
01167     extern GSList * CST_all_expired(CST * st);
01168 
01179     extern GSList * CST_all_revoked(CST * st);
01180 
01192     extern GSList * CST_search_by_purpose(CST * st,
01193                                           const cst_t_cert_purpose
01194                                           purpose);
01195 
01196 
01197 /* Cert */
01198 
01199 
01204     extern X509_NAME *CST_get_issued_by_dn(X509 * cert);
01205 
01210     extern X509_NAME *CST_get_subject_dn(X509 * cert);
01211 
01216     extern time_t CST_get_valid_from(X509 * cert);
01217 
01222     extern time_t CST_get_valid_to(X509 * cert);
01223 
01228     extern int CST_is_expired(X509 * cert);
01229     
01234     extern ASN1_INTEGER *CST_get_serial_number(X509 * cert);
01235     
01240     extern char *CST_get_serial_number_t(X509 * cert);
01241 
01246     extern char *CST_get_fingerprint(X509 * cert);
01247     
01252     extern char *CST_get_fingerprint_MD5(X509 * cert);
01253     
01258     extern char *CST_get_fingerprint_SHA1(X509 * cert);
01259 
01264     extern char *CST_get_email(X509 * cert);
01265     
01270     extern char *CST_get_domain_name(X509 * cert);   
01271 
01276     extern char *CST_get_public_key_alg(X509 * cert);
01277     
01287     extern int CST_check_purpose_x(X509 * x, const cst_t_cert_purpose purposes);
01288     
01299     extern int CST_check_purpose(CST * st, const cst_t_seqnum certID, 
01300                                  const cst_t_cert_purpose purpose);
01301     
01309     extern int CST_is_root(X509 * cert);
01310   
01319     extern int CST_is_root_id(CST * st, const cst_t_seqnum certID);
01320 
01328     extern int CST_is_CA(X509 * cert);    
01329     
01334     extern char *CST_EVP_PKEY_to_text(EVP_PKEY * key);    
01335     
01340     extern int CST_is_revoked(CST * st, X509 * cert);
01341 
01347     extern int CST_is_network(CST * st, X509 * cert);
01348 
01354     extern char *CST_get_network_URL(CST * st, X509 * cert);
01355 
01356     
01366     extern int CST_set_default(CST * st, const cst_t_seqnum certID); 
01367     
01376     extern X509 *CST_default_cert(CST * st, const char *email);
01377 
01386     extern cst_t_seqnum CST_default_cert_id(CST * st, const char *email);
01387     
01396     extern int CST_is_default(CST * st, const cst_t_seqnum certID);
01397 
01407     extern cst_t_seqnum CST_search_by_UID(CST * st, X509_NAME * issuer,
01408                                    ASN1_INTEGER * serial);
01409 
01419     extern cst_t_seqnum CST_search_by_X509(CST * st, X509 * xcert);
01420     
01433     extern int CST_assign(CST * st, 
01434                           const cst_t_seqnum certID, 
01435                           const cst_t_seqnum keyID, 
01436                           unsigned char *password);
01437 
01448     extern EVP_PKEY *CST_get_priv_key_by_UID(CST * st,
01449                                              X509_NAME * issuer,
01450                                              ASN1_INTEGER * serial,
01451                                              unsigned char *password);
01452 
01462     extern EVP_PKEY *CST_get_priv_key(CST * st, X509 * cert, 
01463                                       unsigned char *password);
01464 
01473     extern cst_t_seqnum CST_get_assigned_key(CST * st, const cst_t_seqnum certID);   
01474     
01484     extern EVP_PKEY * CST_get_priv_key_default(CST * st,
01485                                     char *email,
01486                                     unsigned char *password);
01487     
01492     extern int CST_set_folder(CST * st, const cst_t_seqnum certID, 
01493                               const cst_t_cert_folder f);
01494 
01499     extern cst_t_cert_folder CST_get_folder(CST * st, 
01500                                             const cst_t_seqnum certID);
01501 
01506     extern int CST_set_purpose(CST * st, 
01507                                const cst_t_seqnum certID,
01508                                const cst_t_cert_purpose p,
01509                                const int value);
01510 
01515     extern int CST_is_purpose(CST * st, 
01516                               const cst_t_seqnum certID,
01517                               const cst_t_cert_purpose p);
01518 
01523     extern int CST_is_valid(CST * st, X509 * cert);
01524 
01529     extern int CST_is_valid_f(CST * st, FILE * file, GError **error);
01530     
01535     extern int CST_is_valid_f_DER(CST * st, FILE * file, GError **error);
01536     
01541     extern int CST_is_valid_for(CST * st, 
01542             X509 * cert, const cst_t_cert_purpose purpose);
01543     
01544 /* S/MIME Capabilities */
01545     
01550     extern int CST_set_capability(CST * st, 
01551             const cst_t_seqnum certID,
01552             ASN1_OBJECT * oid, 
01553             unsigned char *data, int data_length);
01554 
01555 
01560     extern unsigned char * CST_get_capability_data(CST * st, 
01561             const cst_t_seqnum certID, 
01562             ASN1_OBJECT * oid, int *data_length);
01563 
01568     extern CST_STACK_OF_ASN1_OBJECT * CST_get_capabilities(CST * st, 
01569             const cst_t_seqnum certID);
01570 
01575     extern int CST_is_capability(CST * st,
01576             const cst_t_seqnum certID, ASN1_OBJECT * oid);
01577 
01582     extern int CST_delete_capability(CST * st, 
01583             const cst_t_seqnum certID, 
01584             ASN1_OBJECT * oid);
01585 
01592     extern int CST_get_state(CST * st, X509 * cert);
01593 
01598     extern X509 * CST_get_cert(CST * st, const cst_t_seqnum certID);
01599 
01604     extern EVP_PKEY * CST_get_key(CST * st, const cst_t_seqnum keyID, 
01605             unsigned char *password);
01606 
01611     extern EVP_PKEY * CST_get_pub_key(CST * st, const cst_t_seqnum keyID);
01612     
01673 #ifdef __cplusplus
01674 }
01675 #endif
01676 #endif                          /* CST_H_ */

Generated on Wed Jul 5 13:29:45 2006 for Certman by  doxygen 1.4.6