Fix crashes when filenames end up being NULL in some prpls.
[pidgin-git.git] / libpurple / certificate.h
blob232b1a68e842355254c0dede76eaae539494d002
1 /**
2 * @file certificate.h Public-Key Certificate API
3 * @ingroup core
4 * @see @ref certificate-signals
5 * @since 2.2.0
6 */
8 /*
10 * purple
12 * Purple is the legal property of its developers, whose names are too numerous
13 * to list here. Please refer to the COPYRIGHT file distributed with this
14 * source distribution.
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
31 #ifndef _PURPLE_CERTIFICATE_H
32 #define _PURPLE_CERTIFICATE_H
34 #include <time.h>
36 #include <glib.h>
38 #ifdef __cplusplus
39 extern "C" {
40 #endif /* __cplusplus */
43 typedef enum
45 PURPLE_CERTIFICATE_INVALID = 0,
46 PURPLE_CERTIFICATE_VALID = 1
47 } PurpleCertificateVerificationStatus;
49 typedef struct _PurpleCertificate PurpleCertificate;
50 typedef struct _PurpleCertificatePool PurpleCertificatePool;
51 typedef struct _PurpleCertificateScheme PurpleCertificateScheme;
52 typedef struct _PurpleCertificateVerifier PurpleCertificateVerifier;
53 typedef struct _PurpleCertificateVerificationRequest PurpleCertificateVerificationRequest;
55 /**
56 * Callback function for the results of a verification check
57 * @param st Status code
58 * @param userdata User-defined data
60 typedef void (*PurpleCertificateVerifiedCallback)
61 (PurpleCertificateVerificationStatus st,
62 gpointer userdata);
64 /** A certificate instance
66 * An opaque data structure representing a single certificate under some
67 * CertificateScheme
69 struct _PurpleCertificate
71 /** Scheme this certificate is under */
72 PurpleCertificateScheme * scheme;
73 /** Opaque pointer to internal data */
74 gpointer data;
77 /**
78 * Database for retrieval or storage of Certificates
80 * More or less a hash table; all lookups and writes are controlled by a string
81 * key.
83 struct _PurpleCertificatePool
85 /** Scheme this Pool operates for */
86 gchar *scheme_name;
87 /** Internal name to refer to the pool by */
88 gchar *name;
90 /** User-friendly name for this type
91 * ex: N_("SSL Servers")
92 * When this is displayed anywhere, it should be i18ned
93 * ex: _(pool->fullname)
95 gchar *fullname;
97 /** Internal pool data */
98 gpointer data;
101 * Set up the Pool's internal state
103 * Upon calling purple_certificate_register_pool() , this function will
104 * be called. May be NULL.
105 * @return TRUE if the initialization succeeded, otherwise FALSE
107 gboolean (* init)(void);
110 * Uninit the Pool's internal state
112 * Will be called by purple_certificate_unregister_pool() . May be NULL
114 void (* uninit)(void);
116 /** Check for presence of a certificate in the pool using unique ID */
117 gboolean (* cert_in_pool)(const gchar *id);
118 /** Retrieve a PurpleCertificate from the pool */
119 PurpleCertificate * (* get_cert)(const gchar *id);
120 /** Add a certificate to the pool. Must overwrite any other
121 * certificates sharing the same ID in the pool.
122 * @return TRUE if the operation succeeded, otherwise FALSE
124 gboolean (* put_cert)(const gchar *id, PurpleCertificate *crt);
125 /** Delete a certificate from the pool */
126 gboolean (* delete_cert)(const gchar *id);
128 /** Returns a list of IDs stored in the pool */
129 GList * (* get_idlist)(void);
131 void (*_purple_reserved1)(void);
132 void (*_purple_reserved2)(void);
133 void (*_purple_reserved3)(void);
134 void (*_purple_reserved4)(void);
137 /** A certificate type
139 * A CertificateScheme must implement all of the fields in the structure,
140 * and register it using purple_certificate_register_scheme()
142 * There may be only ONE CertificateScheme provided for each certificate
143 * type, as specified by the "name" field.
145 struct _PurpleCertificateScheme
147 /** Name of the certificate type
148 * ex: "x509", "pgp", etc.
149 * This must be globally unique - you may not register more than one
150 * CertificateScheme of the same name at a time.
152 gchar * name;
154 /** User-friendly name for this type
155 * ex: N_("X.509 Certificates")
156 * When this is displayed anywhere, it should be i18ned
157 * ex: _(scheme->fullname)
159 gchar * fullname;
161 /** Imports a certificate from a file
163 * @param filename File to import the certificate from
164 * @return Pointer to the newly allocated Certificate struct
165 * or NULL on failure.
167 PurpleCertificate * (* import_certificate)(const gchar * filename);
170 * Exports a certificate to a file
172 * @param filename File to export the certificate to
173 * @param crt Certificate to export
174 * @return TRUE if the export succeeded, otherwise FALSE
175 * @see purple_certificate_export()
177 gboolean (* export_certificate)(const gchar *filename, PurpleCertificate *crt);
180 * Duplicates a certificate
182 * Certificates are generally assumed to be read-only, so feel free to
183 * do any sort of reference-counting magic you want here. If this ever
184 * changes, please remember to change the magic accordingly.
185 * @return Reference to the new copy
187 PurpleCertificate * (* copy_certificate)(PurpleCertificate *crt);
189 /** Destroys and frees a Certificate structure
191 * Destroys a Certificate's internal data structures and calls
192 * free(crt)
194 * @param crt Certificate instance to be destroyed. It WILL NOT be
195 * destroyed if it is not of the correct
196 * CertificateScheme. Can be NULL
198 void (* destroy_certificate)(PurpleCertificate * crt);
200 /** Find whether "crt" has a valid signature from issuer "issuer"
201 * @see purple_certificate_signed_by() */
202 gboolean (*signed_by)(PurpleCertificate *crt, PurpleCertificate *issuer);
204 * Retrieves the certificate public key fingerprint using SHA1
206 * @param crt Certificate instance
207 * @return Binary representation of SHA1 hash - must be freed using
208 * g_byte_array_free()
210 GByteArray * (* get_fingerprint_sha1)(PurpleCertificate *crt);
213 * Retrieves a unique certificate identifier
215 * @param crt Certificate instance
216 * @return Newly allocated string that can be used to uniquely
217 * identify the certificate.
219 gchar * (* get_unique_id)(PurpleCertificate *crt);
222 * Retrieves a unique identifier for the certificate's issuer
224 * @param crt Certificate instance
225 * @return Newly allocated string that can be used to uniquely
226 * identify the issuer's certificate.
228 gchar * (* get_issuer_unique_id)(PurpleCertificate *crt);
231 * Gets the certificate subject's name
233 * For X.509, this is the "Common Name" field, as we're only using it
234 * for hostname verification at the moment
236 * @see purple_certificate_get_subject_name()
238 * @param crt Certificate instance
239 * @return Newly allocated string with the certificate subject.
241 gchar * (* get_subject_name)(PurpleCertificate *crt);
244 * Check the subject name against that on the certificate
245 * @see purple_certificate_check_subject_name()
246 * @return TRUE if it is a match, else FALSE
248 gboolean (* check_subject_name)(PurpleCertificate *crt, const gchar *name);
250 /** Retrieve the certificate activation/expiration times */
251 gboolean (* get_times)(PurpleCertificate *crt, time_t *activation, time_t *expiration);
253 void (*_purple_reserved1)(void);
254 void (*_purple_reserved2)(void);
255 void (*_purple_reserved3)(void);
256 void (*_purple_reserved4)(void);
259 /** A set of operations used to provide logic for verifying a Certificate's
260 * authenticity.
262 * A Verifier provider must fill out these fields, then register it using
263 * purple_certificate_register_verifier()
265 * The (scheme_name, name) value must be unique for each Verifier - you may not
266 * register more than one Verifier of the same name for each Scheme
268 struct _PurpleCertificateVerifier
270 /** Name of the scheme this Verifier operates on
272 * The scheme will be looked up by name when a Request is generated
273 * using this Verifier
275 gchar *scheme_name;
277 /** Name of the Verifier - case insensitive */
278 gchar *name;
281 * Start the verification process
283 * To be called from purple_certificate_verify once it has
284 * constructed the request. This will use the information in the
285 * given VerificationRequest to check the certificate and callback
286 * the requester with the verification results.
288 * @param vrq Request to process
290 void (* start_verification)(PurpleCertificateVerificationRequest *vrq);
293 * Destroy a completed Request under this Verifier
294 * The function pointed to here is only responsible for cleaning up
295 * whatever PurpleCertificateVerificationRequest::data points to.
296 * It should not call free(vrq)
298 * @param vrq Request to destroy
300 void (* destroy_request)(PurpleCertificateVerificationRequest *vrq);
302 void (*_purple_reserved1)(void);
303 void (*_purple_reserved2)(void);
304 void (*_purple_reserved3)(void);
305 void (*_purple_reserved4)(void);
308 /** Structure for a single certificate request
310 * Useful for keeping track of the state of a verification that involves
311 * several steps
313 struct _PurpleCertificateVerificationRequest
315 /** Reference to the verification logic used */
316 PurpleCertificateVerifier *verifier;
317 /** Reference to the scheme used.
319 * This is looked up from the Verifier when the Request is generated
321 PurpleCertificateScheme *scheme;
324 * Name to check that the certificate is issued to
326 * For X.509 certificates, this is the Common Name
328 gchar *subject_name;
330 /** List of certificates in the chain to be verified (such as that returned by purple_ssl_get_peer_certificates )
332 * This is most relevant for X.509 certificates used in SSL sessions.
333 * The list order should be: certificate, issuer, issuer's issuer, etc.
335 GList *cert_chain;
337 /** Internal data used by the Verifier code */
338 gpointer data;
340 /** Function to call with the verification result */
341 PurpleCertificateVerifiedCallback cb;
342 /** Data to pass to the post-verification callback */
343 gpointer cb_data;
346 /*****************************************************************************/
347 /** @name Certificate Verification Functions */
348 /*****************************************************************************/
349 /*@{*/
352 * Constructs a verification request and passed control to the specified Verifier
354 * It is possible that the callback will be called immediately upon calling
355 * this function. Plan accordingly.
357 * @param verifier Verification logic to use.
358 * @see purple_certificate_find_verifier()
360 * @param subject_name Name that should match the first certificate in the
361 * chain for the certificate to be valid. Will be strdup'd
362 * into the Request struct
364 * @param cert_chain Certificate chain to check. If there is more than one
365 * certificate in the chain (X.509), the peer's
366 * certificate comes first, then the issuer/signer's
367 * certificate, etc. The whole list is duplicated into the
368 * Request struct.
370 * @param cb Callback function to be called with whether the
371 * certificate was approved or not.
372 * @param cb_data User-defined data for the above.
374 void
375 purple_certificate_verify (PurpleCertificateVerifier *verifier,
376 const gchar *subject_name, GList *cert_chain,
377 PurpleCertificateVerifiedCallback cb,
378 gpointer cb_data);
381 * Completes and destroys a VerificationRequest
383 * @param vrq Request to conclude
384 * @param st Success/failure code to pass to the request's
385 * completion callback.
387 void
388 purple_certificate_verify_complete(PurpleCertificateVerificationRequest *vrq,
389 PurpleCertificateVerificationStatus st);
391 /*@}*/
393 /*****************************************************************************/
394 /** @name Certificate Functions */
395 /*****************************************************************************/
396 /*@{*/
399 * Makes a duplicate of a certificate
401 * @param crt Instance to duplicate
402 * @return Pointer to new instance
404 PurpleCertificate *
405 purple_certificate_copy(PurpleCertificate *crt);
408 * Duplicates an entire list of certificates
410 * @param crt_list List to duplicate
411 * @return New list copy
413 GList *
414 purple_certificate_copy_list(GList *crt_list);
417 * Destroys and free()'s a Certificate
419 * @param crt Instance to destroy. May be NULL.
421 void
422 purple_certificate_destroy (PurpleCertificate *crt);
425 * Destroy an entire list of Certificate instances and the containing list
427 * @param crt_list List of certificates to destroy. May be NULL.
429 void
430 purple_certificate_destroy_list (GList * crt_list);
433 * Check whether 'crt' has a valid signature made by 'issuer'
435 * @param crt Certificate instance to check signature of
436 * @param issuer Certificate thought to have signed 'crt'
438 * @return TRUE if 'crt' has a valid signature made by 'issuer',
439 * otherwise FALSE
440 * @todo Find a way to give the reason (bad signature, not the issuer, etc.)
442 gboolean
443 purple_certificate_signed_by(PurpleCertificate *crt, PurpleCertificate *issuer);
446 * Check that a certificate chain is valid
448 * Uses purple_certificate_signed_by() to verify that each PurpleCertificate
449 * in the chain carries a valid signature from the next. A single-certificate
450 * chain is considered to be valid.
452 * @param chain List of PurpleCertificate instances comprising the chain,
453 * in the order certificate, issuer, issuer's issuer, etc.
454 * @return TRUE if the chain is valid. See description.
455 * @todo Specify which certificate in the chain caused a failure
457 gboolean
458 purple_certificate_check_signature_chain(GList *chain);
461 * Imports a PurpleCertificate from a file
463 * @param scheme Scheme to import under
464 * @param filename File path to import from
465 * @return Pointer to a new PurpleCertificate, or NULL on failure
467 PurpleCertificate *
468 purple_certificate_import(PurpleCertificateScheme *scheme, const gchar *filename);
471 * Exports a PurpleCertificate to a file
473 * @param filename File to export the certificate to
474 * @param crt Certificate to export
475 * @return TRUE if the export succeeded, otherwise FALSE
477 gboolean
478 purple_certificate_export(const gchar *filename, PurpleCertificate *crt);
482 * Retrieves the certificate public key fingerprint using SHA1.
484 * @param crt Certificate instance
485 * @return Binary representation of the hash. You are responsible for free()ing
486 * this.
487 * @see purple_base16_encode_chunked()
489 GByteArray *
490 purple_certificate_get_fingerprint_sha1(PurpleCertificate *crt);
493 * Get a unique identifier for the certificate
495 * @param crt Certificate instance
496 * @return String representing the certificate uniquely. Must be g_free()'ed
498 gchar *
499 purple_certificate_get_unique_id(PurpleCertificate *crt);
502 * Get a unique identifier for the certificate's issuer
504 * @param crt Certificate instance
505 * @return String representing the certificate's issuer uniquely. Must be
506 * g_free()'ed
508 gchar *
509 purple_certificate_get_issuer_unique_id(PurpleCertificate *crt);
512 * Gets the certificate subject's name
514 * For X.509, this is the "Common Name" field, as we're only using it
515 * for hostname verification at the moment
517 * @param crt Certificate instance
518 * @return Newly allocated string with the certificate subject.
520 gchar *
521 purple_certificate_get_subject_name(PurpleCertificate *crt);
524 * Check the subject name against that on the certificate
525 * @param crt Certificate instance
526 * @param name Name to check.
527 * @return TRUE if it is a match, else FALSE
529 gboolean
530 purple_certificate_check_subject_name(PurpleCertificate *crt, const gchar *name);
533 * Get the expiration/activation times.
535 * @param crt Certificate instance
536 * @param activation Reference to store the activation time at. May be NULL
537 * if you don't actually want it.
538 * @param expiration Reference to store the expiration time at. May be NULL
539 * if you don't actually want it.
540 * @return TRUE if the requested values were obtained, otherwise FALSE.
542 gboolean
543 purple_certificate_get_times(PurpleCertificate *crt, time_t *activation, time_t *expiration);
545 /*@}*/
547 /*****************************************************************************/
548 /** @name Certificate Pool Functions */
549 /*****************************************************************************/
550 /*@{*/
552 * Helper function for generating file paths in ~/.purple/certificates for
553 * CertificatePools that use them.
555 * All components will be escaped for filesystem friendliness.
557 * @param pool CertificatePool to build a path for
558 * @param id Key to look up a Certificate by. May be NULL.
559 * @return A newly allocated path of the form
560 * ~/.purple/certificates/scheme_name/pool_name/unique_id
562 gchar *
563 purple_certificate_pool_mkpath(PurpleCertificatePool *pool, const gchar *id);
566 * Determines whether a pool can be used.
568 * Checks whether the associated CertificateScheme is loaded.
570 * @param pool Pool to check
572 * @return TRUE if the pool can be used, otherwise FALSE
574 gboolean
575 purple_certificate_pool_usable(PurpleCertificatePool *pool);
578 * Looks up the scheme the pool operates under
580 * @param pool Pool to get the scheme of
582 * @return Pointer to the pool's scheme, or NULL if it isn't loaded.
583 * @see purple_certificate_pool_usable()
585 PurpleCertificateScheme *
586 purple_certificate_pool_get_scheme(PurpleCertificatePool *pool);
589 * Check for presence of an ID in a pool.
590 * @param pool Pool to look in
591 * @param id ID to look for
592 * @return TRUE if the ID is in the pool, else FALSE
594 gboolean
595 purple_certificate_pool_contains(PurpleCertificatePool *pool, const gchar *id);
598 * Retrieve a certificate from a pool.
599 * @param pool Pool to fish in
600 * @param id ID to look up
601 * @return Retrieved certificate, or NULL if it wasn't there
603 PurpleCertificate *
604 purple_certificate_pool_retrieve(PurpleCertificatePool *pool, const gchar *id);
607 * Add a certificate to a pool
609 * Any pre-existing certificate of the same ID will be overwritten.
611 * @param pool Pool to add to
612 * @param id ID to store the certificate with
613 * @param crt Certificate to store
614 * @return TRUE if the operation succeeded, otherwise FALSE
616 gboolean
617 purple_certificate_pool_store(PurpleCertificatePool *pool, const gchar *id, PurpleCertificate *crt);
620 * Remove a certificate from a pool
622 * @param pool Pool to remove from
623 * @param id ID to remove
624 * @return TRUE if the operation succeeded, otherwise FALSE
626 gboolean
627 purple_certificate_pool_delete(PurpleCertificatePool *pool, const gchar *id);
630 * Get the list of IDs currently in the pool.
632 * @param pool Pool to enumerate
633 * @return GList pointing to newly-allocated id strings. Free using
634 * purple_certificate_pool_destroy_idlist()
636 GList *
637 purple_certificate_pool_get_idlist(PurpleCertificatePool *pool);
640 * Destroys the result given by purple_certificate_pool_get_idlist()
642 * @param idlist ID List to destroy
644 void
645 purple_certificate_pool_destroy_idlist(GList *idlist);
647 /*@}*/
649 /*****************************************************************************/
650 /** @name Certificate Subsystem API */
651 /*****************************************************************************/
652 /*@{*/
655 * Initialize the certificate system
657 void
658 purple_certificate_init(void);
661 * Un-initialize the certificate system
663 void
664 purple_certificate_uninit(void);
667 * Get the Certificate subsystem handle for signalling purposes
669 gpointer
670 purple_certificate_get_handle(void);
672 /** Look up a registered CertificateScheme by name
673 * @param name The scheme name. Case insensitive.
674 * @return Pointer to the located Scheme, or NULL if it isn't found.
676 PurpleCertificateScheme *
677 purple_certificate_find_scheme(const gchar *name);
680 * Get all registered CertificateSchemes
682 * @return GList pointing to all registered CertificateSchemes . This value
683 * is owned by libpurple
685 GList *
686 purple_certificate_get_schemes(void);
688 /** Register a CertificateScheme with libpurple
690 * No two schemes can be registered with the same name; this function enforces
691 * that.
693 * @param scheme Pointer to the scheme to register.
694 * @return TRUE if the scheme was successfully added, otherwise FALSE
696 gboolean
697 purple_certificate_register_scheme(PurpleCertificateScheme *scheme);
699 /** Unregister a CertificateScheme from libpurple
701 * @param scheme Scheme to unregister.
702 * If the scheme is not registered, this is a no-op.
704 * @return TRUE if the unregister completed successfully
706 gboolean
707 purple_certificate_unregister_scheme(PurpleCertificateScheme *scheme);
709 /** Look up a registered PurpleCertificateVerifier by scheme and name
710 * @param scheme_name Scheme name. Case insensitive.
711 * @param ver_name The verifier name. Case insensitive.
712 * @return Pointer to the located Verifier, or NULL if it isn't found.
714 PurpleCertificateVerifier *
715 purple_certificate_find_verifier(const gchar *scheme_name, const gchar *ver_name);
718 * Get the list of registered CertificateVerifiers
720 * @return GList of all registered PurpleCertificateVerifier. This value
721 * is owned by libpurple
723 GList *
724 purple_certificate_get_verifiers(void);
727 * Register a CertificateVerifier with libpurple
729 * @param vr Verifier to register.
730 * @return TRUE if register succeeded, otherwise FALSE
732 gboolean
733 purple_certificate_register_verifier(PurpleCertificateVerifier *vr);
736 * Unregister a CertificateVerifier with libpurple
738 * @param vr Verifier to unregister.
739 * @return TRUE if unregister succeeded, otherwise FALSE
741 gboolean
742 purple_certificate_unregister_verifier(PurpleCertificateVerifier *vr);
744 /** Look up a registered PurpleCertificatePool by scheme and name
745 * @param scheme_name Scheme name. Case insensitive.
746 * @param pool_name Pool name. Case insensitive.
747 * @return Pointer to the located Pool, or NULL if it isn't found.
749 PurpleCertificatePool *
750 purple_certificate_find_pool(const gchar *scheme_name, const gchar *pool_name);
753 * Get the list of registered Pools
755 * @return GList of all registered PurpleCertificatePool s. This value
756 * is owned by libpurple
758 GList *
759 purple_certificate_get_pools(void);
762 * Register a CertificatePool with libpurple and call its init function
764 * @param pool Pool to register.
765 * @return TRUE if the register succeeded, otherwise FALSE
767 gboolean
768 purple_certificate_register_pool(PurpleCertificatePool *pool);
771 * Unregister a CertificatePool with libpurple and call its uninit function
773 * @param pool Pool to unregister.
774 * @return TRUE if the unregister succeeded, otherwise FALSE
776 gboolean
777 purple_certificate_unregister_pool(PurpleCertificatePool *pool);
779 /*@}*/
783 * Displays a window showing X.509 certificate information
785 * @param crt Certificate under an "x509" Scheme
786 * @todo Will break on CA certs, as they have no Common Name
788 void
789 purple_certificate_display_x509(PurpleCertificate *crt);
792 * Add a search path for certificates.
794 * @param path Path to search for certificates.
796 void purple_certificate_add_ca_search_path(const char *path);
798 #ifdef __cplusplus
800 #endif /* __cplusplus */
802 #endif /* _PURPLE_CERTIFICATE_H */