1 |
/* getkey-openpgp.c - Kludge to use OpenPGP to emulate GPGME. |
2 |
* Copyright (C) 2002, 2003 Timo Schulz |
3 |
* |
4 |
* This file is part of MyGPGME. |
5 |
* |
6 |
* MyGPGME is free software; you can redistribute it and/or modify |
7 |
* it under the terms of the GNU General Public License as published by |
8 |
* the Free Software Foundation; either version 2 of the License, or |
9 |
* (at your option) any later version. |
10 |
* |
11 |
* MyGPGME is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU General Public License |
17 |
* along with this program; if not, write to the Free Software |
18 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
19 |
*/ |
20 |
|
21 |
#include <stdio.h> |
22 |
#include <sys/types.h> |
23 |
#include <string.h> |
24 |
#include <stdlib.h> |
25 |
#include <windows.h> |
26 |
|
27 |
#include "gpgme-config.h" |
28 |
#ifdef WITH_OPENPGP |
29 |
#include "context.h" |
30 |
#include "ops.h" |
31 |
#include "util.h" |
32 |
#include "key.h" |
33 |
#include "openpgp.h" |
34 |
|
35 |
|
36 |
static void |
37 |
set_subkey( struct subkey_s * s, PKT_public_key * pk ) |
38 |
{ |
39 |
s->key_algo = pk->pubkey_algo; |
40 |
s->timestamp = pk->timestamp; |
41 |
s->flags.can_encrypt = gpgme_key_cability_from_algo( s->key_algo ) & 0x02; |
42 |
s->flags.can_certify = gpgme_key_cability_from_algo( s->key_algo ) & 0x01; |
43 |
s->flags.can_sign = gpgme_key_cability_from_algo( s->key_algo ) & 0x01; |
44 |
if( pk->expiredate ) |
45 |
s->flags.expired = pk->expiredate> pk->timestamp? 1 : 0; |
46 |
s->flags.revoked = pk->is_revoked? 1 : 0; |
47 |
} |
48 |
|
49 |
|
50 |
static void |
51 |
set_subkey_fprints( struct subkey_s * s, byte *fpr, size_t fprlen ) |
52 |
{ |
53 |
char skeyid[16+1], sfpr[40+1]; |
54 |
int i; |
55 |
|
56 |
for( i = 0; i < 8; i++ ) |
57 |
sprintf( skeyid + 2*i, "%02X", fpr[i+12] ); |
58 |
skeyid[16] = '\0'; |
59 |
memcpy( s->keyid, skeyid, 16 ); |
60 |
s->keyid[16] = '\0'; |
61 |
for( i = 0; i < 20; i++ ) |
62 |
sprintf( sfpr + 2*i, "%02X", fpr[i] ); |
63 |
sfpr[40] = '\0'; |
64 |
s->fingerprint = malloc( 41 ); |
65 |
if( s->fingerprint ) { |
66 |
memcpy( s->fingerprint, sfpr, 40 ); |
67 |
s->fingerprint[40] = '\0'; |
68 |
} |
69 |
} |
70 |
|
71 |
|
72 |
int |
73 |
gpgme_getkey_bykeyid( void *fd_handle, const char * keyid, gpgme_key_t *r_key ) |
74 |
{ |
75 |
PACKET * pkt = NULL; |
76 |
gpg_kbnode_t key = NULL, ctx = NULL, n; |
77 |
gpgme_key_t _key = NULL; |
78 |
gpg_iobuf_t inp = fd_handle; |
79 |
struct subkey_s * s; |
80 |
u32 kid[2], pk_keyid; |
81 |
byte fprint[21] = {0}; |
82 |
char buf[4]; |
83 |
size_t i; |
84 |
|
85 |
if( !fd_handle ) |
86 |
return mk_error( Invalid_Value ); |
87 |
|
88 |
if( !strncmp( keyid, "0x", 2 ) ) |
89 |
keyid += 2; |
90 |
for( i = 0; i < (strlen( keyid )+1) / 8; i++ ) { |
91 |
strncpy( buf, keyid + 8*i, 8 ); |
92 |
kid[i] = strtoul( buf, NULL, 16 ); |
93 |
} |
94 |
if( strlen( keyid ) == 8 ) { |
95 |
kid[1] = kid[0]; |
96 |
kid[0] = 0; |
97 |
} |
98 |
|
99 |
gpg_iobuf_seek( inp, 0 ); |
100 |
while( !gpg_read_keyblock( inp, &pkt, &key ) ) { |
101 |
while( (n = gpg_walk_kbnode( key, &ctx, 1 )) != NULL ) { |
102 |
switch( n->pkt->pkttype ) { |
103 |
case PKT_PUBLIC_KEY: |
104 |
case PKT_PUBLIC_SUBKEY: |
105 |
pk_keyid = gpg_keyid_from_pk( n->pkt->pkt.public_key, fprint ); |
106 |
if( n->pkt->pkttype == PKT_PUBLIC_SUBKEY && _key ) |
107 |
; |
108 |
else if( pk_keyid != kid[1] ) |
109 |
break; |
110 |
if( !_key ) |
111 |
_gpgme_key_new( &_key ); |
112 |
s = _gpgme_key_add_subkey( _key ); |
113 |
if( s ) { |
114 |
set_subkey( s, n->pkt->pkt.public_key ); |
115 |
set_subkey_fprints( s, fprint, 20 ); |
116 |
} |
117 |
break; |
118 |
|
119 |
case PKT_USER_ID: |
120 |
if( !_key ) |
121 |
continue; |
122 |
_gpgme_key_append_name( _key, n->pkt->pkt.user_id->name, NULL ); |
123 |
break; |
124 |
} |
125 |
} |
126 |
gpg_release_kbnode( key ); |
127 |
key = NULL; |
128 |
ctx = NULL; |
129 |
if( _key ) |
130 |
break; |
131 |
} |
132 |
if( r_key ) |
133 |
*r_key = _key; |
134 |
return 0; |
135 |
} |
136 |
|
137 |
|
138 |
#endif /*WITH_OPENPGP*/ |