]> the.earth.li Git - onak.git/blob - photoid.c
Bump debhelper compat level to 13
[onak.git] / photoid.c
1 /*
2  * photoid.c - Routines for OpenPGP id photos.
3  *
4  * Copyright 2004 Jonathan McDowell <noodles@earth.li>
5  *
6  * This program is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the Free
8  * Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program.  If not, see <https://www.gnu.org/licenses/>.
17  */
18
19 #include <stdlib.h>
20
21 #include "keystructs.h"
22 #include "onak.h"
23 #include "photoid.h"
24
25 /**
26  *      getphoto - returns an OpenPGP packet containing a photo id.
27  *      @key: The key to return the photo id from.
28  *      @index: The index of the photo to return.
29  *      @photo: The photo data.
30  *      @length: The length of the photo data.
31  *
32  *      This function returns the photo data contained in a supplied key.
33  *      index specifies which photo id should be returned. If there's no such
34  *      photo id NULL is returned. The returned data pointer refers to the key
35  *      data supplied rather than a copy of it.
36  */
37 onak_status_t getphoto(struct openpgp_publickey *key, int index,
38                 unsigned char **photo, size_t *length)
39 {
40         struct openpgp_signedpacket_list *curuid = NULL;
41         int                               i = 0;
42         int                               j = 0;
43
44         if (key == NULL || photo == NULL || length == NULL)
45                 return ONAK_E_INVALID_PARAM;
46
47         *photo = NULL;
48         
49         curuid = key->uids;
50         i = 0;
51         while (*photo == NULL && curuid != NULL && i <= index) {
52                 if (curuid->packet->tag == 17) {
53                         if (i == index) {
54                                 j = 0;
55                                 *length = curuid->packet->data[j++];
56                                 if (*length < 192) {
57                                         /* length is correct */
58                                 } else if (*length < 255) {
59                                         *length -= 192;
60                                         *length <<= 8;
61                                         *length += curuid->packet->data[j++];
62                                         *length +=  192;
63                                 } else {
64                                         *length = curuid->packet->data[j++];
65                                         *length <<= 8;
66                                         *length += curuid->packet->data[j++];
67                                         *length <<= 8;
68                                         *length += curuid->packet->data[j++];
69                                         *length <<= 8;
70                                         *length += curuid->packet->data[j++];
71                                 }
72                                 j++;
73                                 *length -= 17;
74                                 *photo = &(curuid->packet->data[j+16]);
75                         } else {
76                                 i++;
77                         }
78                 }
79                 curuid = curuid->next;
80         }
81
82         return *photo == NULL ? ONAK_E_NOT_FOUND : ONAK_E_OK;
83 }