summaryrefslogtreecommitdiff
path: root/rpmio/tkey.c
blob: b6f01c0da46f3a31813c07d8fc39c01a99dd04a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/** \ingroup rpmio signature
 * \file rpmio/tkey.c
 * Routines to handle RFC-2440 detached signatures.
 */

static int _debug = 0;

#include "system.h"
#include "rpmio/digest.h"
#include "debug.h"

static int doit(const char *sig, pgpDig dig, int printing)
{
    const char *s, *t;
    unsigned char * dec;
    size_t declen;
    char * enc;
    int rc;
    int i;

if (_debug)
fprintf(stderr, "*** sig is\n%s\n", sig);

    if ((rc = b64decode(sig, (void **)&dec, &declen)) != 0) {
	fprintf(stderr, "*** b64decode returns %d\n", rc);
	return rc;
    }
    rc = pgpPrtPkts(dec, declen, dig, printing);
    if (rc < 0) {
	fprintf(stderr, "*** pgpPrtPkts returns %d\n", rc);
	return rc;
    }

    if ((enc = b64encode(dec, declen, -1)) == NULL) {
	fprintf(stderr, "*** b64encode failed\n");
	return rc;
    }

if (_debug)
fprintf(stderr, "*** enc is\n%s\n", enc);

rc = 0;
for (i = 0, s = sig, t = enc; *s & *t; i++, s++, t++) {
    if (*s == '\n') s++;
    if (*t == '\n') t++;
    if (*s == *t) continue;
fprintf(stderr, "??? %5d %02x != %02x '%c' != '%c'\n", i, (*s & 0xff), (*t & 0xff), *s, *t);
    rc = 5;
}

    return rc;
}

/* Secret key */
static const char * jbjSecretDSA =
"lQFvBDu6XHwRAwCTIHRgKeIlOFUIEZeJVYSrXn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D+16O4vI8Emlx2JVcGLlq/aWhspvQWIzN6PytA3iKZ6uzesrM7yXmqzgodZUsJh"
"1wwl/0K5OIJn/oD41UayU8RXNER8SzDYvDYsJymFRwE1s58lL7QpSmVmZiBKb2hu"
"c29uIChBUlMgTjNOUFEpIDxqYmpAcmVkaGF0LmNvbT6IVwQTEQIAFwUCO7pcfAUL"
"BwoDBAMVAwIDFgIBAheAAAoJEIHSpVbYjoOZBToAn3TXaAI+bhg51EeyaiFip/6W"
"OVwBAJ44rTtNsgZBQxXISjB64CWxl4VaWQ=="
;

/* Signature */
static const char * abcSignatureDSA =
"iD8DBQA7vII+gdKlVtiOg5kRAvg4AJ0fV3gDBADobAnK2HOkV88bfmFMEgCeNysO"
"nP3dWWJnp0Pnbor7pIob4Dk="
;

int
main (int argc, char *argv[])
{
    pgpDig dig;
    int printing = 1;
    int rc;

    rpmInitCrypto();
    dig = pgpNewDig();

fprintf(stderr, "=============================== GPG Secret Key\n");
    if ((rc = doit(jbjSecretDSA, dig, printing)) != 0)
	fprintf(stderr, "==> FAILED: rc %d\n", rc);

fprintf(stderr, "=============================== GPG Public Key\n");
    if ((rc = doit(jbjPublicDSA, dig, printing)) != 0)
	fprintf(stderr, "==> FAILED: rc %d\n", rc);

fprintf(stderr, "=============================== GPG Signature of \"abc\"\n");
    if ((rc = doit(abcSignatureDSA, dig, printing)) != 0)
	fprintf(stderr, "==> FAILED: rc %d\n", rc);

    {	DIGEST_CTX ctx = rpmDigestInit(PGPHASHALGO_SHA1, RPMDIGEST_NONE);
	struct pgpDigParams_s * dsig = &dig->signature;
	void *digest = NULL;
	size_t digestlen = 0;
	const char * txt = "abc";
	SECItem digitem;
	
	rpmDigestUpdate(ctx, txt, strlen(txt));
	rpmDigestUpdate(ctx, dsig->hash, dsig->hashlen);
	rpmDigestFinal(ctx, &digest, &digestlen, 0);

fprintf(stderr, "\n    hm = [ 160]: %s\n\n", pgpHexStr(digest, digestlen));
	digitem.type = siBuffer;
	digitem.data = digest;
	digitem.len = digestlen;

	rc = VFY_VerifyDigest(&digitem, dig->dsa, dig->dsasig, SEC_OID_ANSIX9_DSA_SIGNATURE_WITH_SHA1_DIGEST, NULL);

	if (digest) {
	    free(digest);
	    digest = NULL;
	}
    }


fprintf(stderr, "=============================== DSA verify: rc %d\n", rc);

    dig = pgpFreeDig(dig);

    return rc;
}