Branch data Line data Source code
1 : : /* 2 : : * gnome-keyring 3 : : * 4 : : * Copyright (C) 2011 Collabora Ltd. 5 : : * 6 : : * This program is free software; you can redistribute it and/or modify 7 : : * it under the terms of the GNU Lesser General Public License as 8 : : * published by the Free Software Foundation; either version 2.1 of 9 : : * the License, or (at your option) any later version. 10 : : * 11 : : * This program is distributed in the hope that it will be useful, but 12 : : * WITHOUT ANY WARRANTY; without even the implied warranty of 13 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 : : * Lesser General Public License for more details. 15 : : * 16 : : * You should have received a copy of the GNU Lesser General Public 17 : : * License along with this program; if not, see <http://www.gnu.org/licenses/>. 18 : : * 19 : : * Author: Stef Walter <stefw@collabora.co.uk> 20 : : */ 21 : : 22 : : #include "config.h" 23 : : 24 : : #include "gcr-fingerprint.h" 25 : : #include "gcr-subject-public-key.h" 26 : : 27 : : #include "gcr/gcr-oids.h" 28 : : 29 : : #include "egg/egg-asn1x.h" 30 : : #include "egg/egg-asn1-defs.h" 31 : : 32 : : #include <glib.h> 33 : : 34 : : /** 35 : : * gcr_fingerprint_from_subject_public_key_info: 36 : : * @key_info: (array length=n_key_info): DER encoded subjectPublicKeyInfo structure 37 : : * @n_key_info: length of DER encoded structure 38 : : * @checksum_type: the type of fingerprint to create 39 : : * @n_fingerprint: (out): the length of fingerprint returned 40 : : * 41 : : * Create a key fingerprint for a DER encoded subjectPublicKeyInfo. The 42 : : * fingerprint is created so that it will be identical for a key and its 43 : : * corresponding certificate. 44 : : * 45 : : * Note that in the case of certificates this is not a fingerprint of the 46 : : * actual certificate data, but rather of the public key contained in a 47 : : * certificate. 48 : : * 49 : : * Returns: (transfer full) (nullable) (array length=n_fingerprint): the 50 : : * fingerprint or %NULL if the input was invalid. 51 : : */ 52 : : guchar * 53 : 7 : gcr_fingerprint_from_subject_public_key_info (const guchar *key_info, 54 : : gsize n_key_info, 55 : : GChecksumType checksum_type, 56 : : gsize *n_fingerprint) 57 : : { 58 : : GChecksum *check; 59 : : guint8 *fingerprint; 60 : : 61 [ - + ]: 7 : g_return_val_if_fail (key_info, NULL); 62 [ - + ]: 7 : g_return_val_if_fail (n_key_info, NULL); 63 [ - + ]: 7 : g_return_val_if_fail (n_fingerprint, NULL); 64 : : 65 : 7 : check = g_checksum_new (checksum_type); 66 [ - + ]: 7 : g_return_val_if_fail (check, NULL); 67 : : 68 : 7 : g_checksum_update (check, key_info, n_key_info); 69 : : 70 : 7 : *n_fingerprint = g_checksum_type_get_length (checksum_type); 71 : 7 : fingerprint = g_malloc (*n_fingerprint); 72 : 7 : g_checksum_get_digest (check, fingerprint, n_fingerprint); 73 : : 74 : 7 : g_checksum_free (check); 75 : 7 : return fingerprint; 76 : : } 77 : : 78 : : /** 79 : : * gcr_fingerprint_from_attributes: 80 : : * @attrs: attributes for key or certificate 81 : : * @checksum_type: the type of fingerprint to create 82 : : * @n_fingerprint: (out): the length of fingerprint returned 83 : : * 84 : : * Create a key fingerprint for a certificate, public key or private key. 85 : : * Note that this is not a fingerprint of certificate data, which you would 86 : : * use gcr_certificate_get_fingerprint() for. 87 : : * 88 : : * Returns: (transfer full) (nullable) (array length=n_fingerprint): the 89 : : * fingerprint or %NULL if the input was invalid. 90 : : */ 91 : : guchar * 92 : 4 : gcr_fingerprint_from_attributes (GckAttributes *attrs, 93 : : GChecksumType checksum_type, 94 : : gsize *n_fingerprint) 95 : : { 96 : 4 : gpointer fingerprint = NULL; 97 : : GBytes *info; 98 : : GNode *asn; 99 : : 100 [ - + ]: 4 : g_return_val_if_fail (attrs != NULL, NULL); 101 [ - + ]: 4 : g_return_val_if_fail (n_fingerprint, NULL); 102 : : 103 : 4 : asn = _gcr_subject_public_key_for_attributes (attrs); 104 : : 105 [ + - ]: 4 : if (asn != NULL) { 106 : 4 : info = egg_asn1x_encode (asn, NULL); 107 : 4 : fingerprint = gcr_fingerprint_from_subject_public_key_info (g_bytes_get_data (info, NULL), 108 : : g_bytes_get_size (info), 109 : : checksum_type, 110 : : n_fingerprint); 111 : 4 : g_bytes_unref (info); 112 : : } 113 : : 114 : 4 : egg_asn1x_destroy (asn); 115 : 4 : return fingerprint; 116 : : }