/
keyring.h
616 lines (508 loc) · 24.4 KB
/
keyring.h
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
/*---------------------------------------------------------------------------*\
____ _ _ __ _ __ ___ _ _
|_ / || | '_ \ '_ \/ -_) '_|
/__|\_, | .__/ .__/\___|_|
|__/|_| |_|
\*---------------------------------------------------------------------------*/
#ifndef ZMART_KEYRINGCALLBACKS_H
#define ZMART_KEYRINGCALLBACKS_H
#include <stdlib.h>
#include <iostream>
#include <zypp/base/Logger.h>
#include <zypp/ZYppCallbacks.h>
#include <zypp/Pathname.h>
#include <zypp/KeyRing.h>
#include <zypp/Digest.h>
#include "Zypper.h"
#include "utils/prompt.h"
#include "Table.h"
///////////////////////////////////////////////////////////////////
namespace zypp
{ /////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
namespace
{
inline void hintFingerprint()
{
Zypper::instance().out().notePar( 4, _("A GPG pubkey is clearly identified by its fingerprint. Do not rely on the key's name. If you are not sure whether the presented key is authentic, ask the repository provider or check their web site. Many providers maintain a web page showing the fingerprints of the GPG keys they are using.") );
}
inline void hintUnsignedData()
{
Zypper::instance().out().notePar( 4, _("Signing data enables the recipient to verify that no modifications occurred after the data were signed. Accepting data with no, wrong or unknown signature can lead to a corrupted system and in extreme cases even to a system compromise.") );
}
inline void hintIfMasterIndex( const std::string & file_r )
{
if ( file_r == "repomd.xml" || file_r == "content" )
{
// translator: %1% is a file name
Zypper::instance().out().notePar( 4, str::Format(_("File '%1%' is the repositories master index file. It ensures the integrity of the whole repo.") ) % file_r );
}
}
inline void warnCanNotVerifyFile()
{
Zypper::instance().out().warningPar( 4, str::Format(_("We can't verify that no one meddled with this file, so it might not be trustworthy anymore! You should not continue unless you know it's safe.") ) );
}
inline void warnFileModifiedAfterSigning()
{
Zypper::instance().out().warningPar( 4, str::Format(_("This file was modified after it has been signed. This may have been a malicious change, so it might not be trustworthy anymore! You should not continue unless you know it's safe.") ) );
Zypper::instance().out().notePar( 4, str::Format(_("This might be a transient issue if the server is in the midst of receiving new data. The data file and its signature are two files which must fit together. In case the request hit the server in the midst of updating them, the signature verification might fail. After a few minutes, when the server has updated its data, it should work again.") ) );
}
std::ostream & dumpKeyInfo( std::ostream & str, const PublicKeyData & key, const KeyContext & context = KeyContext() )
{
Zypper & zypper = Zypper::instance();
if ( zypper.out().type() == Out::TYPE_XML )
{
{
xmlout::Node parent( str, "gpgkey-info", xmlout::Node::optionalContent );
if ( !context.empty() )
{
dumpAsXmlOn( *parent, context.repoInfo().asUserString(), "repository" );
}
dumpAsXmlOn( *parent, key.name(), "key-name" );
dumpAsXmlOn( *parent, key.fingerprint(), "key-fingerprint" );
dumpAsXmlOn( *parent, key.algoName(), "key-algorithm" );
dumpAsXmlOn( *parent, key.created(), "key-created" );
dumpAsXmlOn( *parent, key.expires(), "key-expires" );
dumpAsXmlOn( *parent, key.rpmName(), "rpm-name" );
}
return str;
}
Table t;
t.lineStyle( TableLineStyle::none );
if ( !context.empty() )
{
t << ( TableRow() << "" << _("Repository:") << context.repoInfo().asUserString() );
}
t << ( TableRow() << "" << _("Key Fingerprint:") << str::gapify( key.fingerprint(), 4 ) )
<< ( TableRow() << "" << _("Key Name:") << key.name() )
<< ( TableRow() << "" << _("Key Algorithm:") << key.algoName() )
<< ( TableRow() << "" << _("Key Created:") << key.created() )
<< ( TableRow() << "" << _("Key Expires:") << key.expiresAsString() );
for ( const PublicSubkeyData & sub : key.subkeys() )
t << ( TableRow() << "" << _("Subkey:") << sub.asString() );
t << ( TableRow() << "" << _("Rpm Name:") << key.rpmName() );
return str << t;
}
inline std::ostream & dumpKeyInfo( std::ostream & str, const PublicKey & key, const KeyContext & context = KeyContext() )
{ return dumpKeyInfo( str, key.keyData(), context ); }
} // namespace
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
// KeyRingReceive
///////////////////////////////////////////////////////////////////
struct KeyRingReceive : public callback::ReceiveReport<KeyRingReport>
{
KeyRingReceive()
: _gopts(Zypper::instance().config())
{}
////////////////////////////////////////////////////////////////////
virtual void infoVerify( const std::string & file_r, const PublicKeyData & keyData_r, const KeyContext & context = KeyContext() )
{
if ( keyData_r.expired() )
{
Zypper::instance().out().warning( str::Format(_("The gpg key signing file '%1%' has expired.")) % file_r );
dumpKeyInfo( (std::ostream&)ColorStream(std::cout,ColorContext::MSG_WARNING), keyData_r, context );
}
else if ( keyData_r.daysToLive() < 15 )
{
Zypper::instance().out().info( str::Format(
PL_( "The gpg key signing file '%1%' will expire in %2% day.",
"The gpg key signing file '%1%' will expire in %2% days.",
keyData_r.daysToLive() )) % file_r % keyData_r.daysToLive() );
dumpKeyInfo( std::cout, keyData_r, context );
}
else if ( Zypper::instance().out().verbosity() > Out::NORMAL )
{
dumpKeyInfo( std::cout, keyData_r, context );
}
}
////////////////////////////////////////////////////////////////////
virtual bool askUserToAcceptUnsignedFile(
const std::string & file, const KeyContext & context)
{
if (_gopts.no_gpg_checks)
{
MIL << "Accepting unsigned file (" << file << ", repo: "
<< (context.empty() ? "(unknown)" : context.repoInfo().alias())
<< ")" << std::endl;
if (context.empty())
Zypper::instance().out().warning(
str::Format(_("Accepting an unsigned file '%s'.")) % file,
Out::HIGH);
else
Zypper::instance().out().warning(
str::Format(_("Accepting an unsigned file '%s' from repository '%s'."))
% file % context.repoInfo().asUserString(),
Out::HIGH);
return true;
}
std::string msg;
if ( context.empty() )
// translator: %1% is a file name
msg = str::Format(_("File '%1%' is unsigned.") ) % file;
else
// translator: %1% is a file name, %2% a repositories name
msg = str::Format(_("File '%1%' from repository '%2%' is unsigned.") ) % file % context.repoInfo().asUserString();
Zypper::instance().out().warning( msg );
hintUnsignedData();
if ( !context.empty() )
hintIfMasterIndex( file );
warnCanNotVerifyFile();
Zypper::instance().out().gap();
const std::string & question { text::join( msg, text::qContinue(), "\n" ) };
return read_bool_answer(PROMPT_YN_GPG_UNSIGNED_FILE_ACCEPT, question, false);
}
////////////////////////////////////////////////////////////////////
virtual bool askUserToAcceptUnknownKey(
const std::string & file,
const std::string & id,
const KeyContext & context)
{
if (_gopts.no_gpg_checks)
{
MIL
<< "Accepting file signed with an unknown key ("
<< file << "," << id << ", repo: "
<< (context.empty() ? "(unknown)" : context.repoInfo().alias())
<< ")" << std::endl;
if (context.empty())
Zypper::instance().out().warning(str::Format(
_("Accepting file '%s' signed with an unknown key '%s'."))
% file % id);
else
Zypper::instance().out().warning(str::Format(
_("Accepting file '%s' from repository '%s' signed with an unknown key '%s'."))
% file % context.repoInfo().asUserString() % id);
return true;
}
std::string msg;
if ( context.empty() )
// translator: %1% is a file name, %2% is a gpg key ID
msg = str::Format(_("File '%1%' is signed with an unknown key '%2%'.") ) % file % id;
else
// translator: %1% is a file name, %2% is a gpg key ID, %3% a repositories name
msg = str::Format(_("File '%1%' from repository '%3%' is signed with an unknown key '%2%'.") ) % file % id % context.repoInfo().asUserString();
Zypper::instance().out().warning( msg );
hintUnsignedData();
if ( !context.empty() )
hintIfMasterIndex( file );
warnCanNotVerifyFile();
Zypper::instance().out().gap();
const std::string & question { text::join( msg, text::qContinue(), "\n" ) };
return read_bool_answer(PROMPT_YN_GPG_UNKNOWN_KEY_ACCEPT, question, false);
}
virtual KeyRingReport::KeyTrust askUserToAcceptKey(
const PublicKey &key, const KeyContext & context)
{
return askUserToAcceptKey( key, context, true );
}
KeyRingReport::KeyTrust askUserToAcceptKey(
const PublicKey &key_r, const KeyContext & context_r, bool canTrustTemporarily_r )
{
Zypper & zypper = Zypper::instance();
std::ostringstream s;
s << std::endl;
if (_gopts.gpg_auto_import_keys)
s << _("Automatically importing the following key:") << std::endl;
else if ( _gopts.no_gpg_checks && canTrustTemporarily_r )
s << _("Automatically trusting the following key:") << std::endl;
else
s << _("New repository or package signing key received:") << std::endl;
// gpg key info
dumpKeyInfo( s << std::endl, key_r, context_r ) << std::endl;
// if --gpg-auto-import-keys or --no-gpg-checks print info and don't ask
if (_gopts.gpg_auto_import_keys)
{
MIL << "Automatically importing key " << key_r << std::endl;
zypper.out().info(s.str());
hintFingerprint();
return KeyRingReport::KEY_TRUST_AND_IMPORT;
}
else if (_gopts.no_gpg_checks && canTrustTemporarily_r )
{
MIL << "Automatically trusting key " << key_r << std::endl;
zypper.out().info(s.str());
hintFingerprint();
return KeyRingReport::KEY_TRUST_TEMPORARILY;
}
// ask the user
// FIXME: The embedded key table will reset the prompt color and
// the actual question after the table is rendered normal. That's
// why we print the 1st part here in PROMPT color and just pass the
// question to propmpt().
zypper.out().info( ColorString( s.str(), ColorContext::PROMPT ).str() );
hintUnsignedData();
hintFingerprint();
zypper.out().gap();
s.str( std::string() ); // clear the string
if ( canTrustTemporarily_r ) {
// translators: this message is shown after showing description of the key
s << _("Do you want to reject the key, trust temporarily, or trust always?");
} else {
// translators: this message is shown after showing description of the key
s << _("Do you want to reject the key, or trust always?");
}
// only root has access to rpm db where keys are stored
bool canimport = geteuid() == 0 || _gopts.changedRoot;
if ( !canimport && !canTrustTemporarily_r )
return KeyRingReport::KEY_DONT_TRUST;
PromptOptions popts;
if ( canTrustTemporarily_r ) {
if (canimport)
// translators: r/t/a stands for Reject/TrustTemporarily/trustAlways(import)
// translate to whatever is appropriate for your language
// The anserws must be separated by slash characters '/' and must
// correspond to reject/trusttemporarily/trustalways in that order.
// The answers should be lower case letters.
popts.setOptions(_("r/t/a/"), 0);
else
// translators: the same as r/t/a, but without 'a'
popts.setOptions(_("r/t"), 0);
} else {
// translators: r/a stands for Reject/trustAlways(import)
// translate to whatever is appropriate for your language
// The anserws must be separated by slash characters '/' and must
// correspond to reject/trusttemporarily/trustalways in that order.
// The answers should be lower case letters.
popts.setOptions(_("r/a/"), 0);
}
int off = 0;
// translators: help text for the 'r' option in the 'r/t/a' prompt
popts.setOptionHelp( off, _("Don't trust the key.") );
if ( canTrustTemporarily_r ) {
// translators: help text for the 't' option in the 'r/t/a' prompt
popts.setOptionHelp( (++off), _("Trust the key temporarily.") );
}
if (canimport)
// translators: help text for the 'a' option in the 'r/t/a' prompt
popts.setOptionHelp( (++off), _("Trust the key and import it into trusted keyring.") );
zypper.out().prompt(PROMPT_YN_GPG_KEY_TRUST, s.str(), popts);
unsigned prep = get_prompt_reply(zypper, PROMPT_YN_GPG_KEY_TRUST, popts);
if ( !canTrustTemporarily_r && prep == 1 )
return KeyRingReport::KEY_TRUST_AND_IMPORT;
else {
switch (prep)
{
case 1:
return KeyRingReport::KEY_TRUST_TEMPORARILY;
case 2:
return KeyRingReport::KEY_TRUST_AND_IMPORT;
case 0:
default:
break;
}
}
return KeyRingReport::KEY_DONT_TRUST;
}
////////////////////////////////////////////////////////////////////
virtual bool askUserToAcceptVerificationFailed(
const std::string & file,
const PublicKey & key,
const KeyContext & context )
{
if (_gopts.no_gpg_checks)
{
MIL << str::Format("Ignoring failed signature verification for %s")
% file << std::endl;
std::ostringstream msg;
if (context.empty())
msg << str::Format(
_("Ignoring failed signature verification for file '%s'!")) % file;
else
msg << str::Format(
_("Ignoring failed signature verification for file '%s'"
" from repository '%s'!")) % file
% context.repoInfo().asUserString();
msg
<< std::endl
<< _("Double-check this is not caused by some malicious"
" changes in the file!");
Zypper::instance().out().warning(msg.str(), Out::QUIET);
return true;
}
std::string msg;
if ( context.empty() )
// translator: %1% is a file name
msg = str::Format(_("Signature verification failed for file '%1%'.") ) % file;
else
// translator: %1% is a file name, %2% a repositories name
msg = str::Format(_("Signature verification failed for file '%1%' from repository '%2%'.") ) % file % context.repoInfo().asUserString();
Zypper::instance().out().error( msg );
hintUnsignedData();
if ( !context.empty() )
hintIfMasterIndex( file );
warnFileModifiedAfterSigning();
Zypper::instance().out().gap();
return read_bool_answer( PROMPT_YN_GPG_CHECK_FAILED_IGNORE, text::join( msg, text::qContinue() ), false);
}
////////////////////////////////////////////////////////////////////
virtual void report ( const UserData & data )
{
if ( data.type() == zypp::ContentType( KeyRingReport::ACCEPT_PACKAGE_KEY_REQUEST ) )
return askUserToAcceptPackageKey( data );
else if ( data.type() == zypp::ContentType( KeyRingReport::KEYS_NOT_IMPORTED_REPORT ) )
return reportKeysNotImportedReport( data );
else if ( data.type() == zypp::ContentType( KeyRingReport::REPORT_AUTO_IMPORT_KEY ) )
return reportAutoImportKey( data );
WAR << "Unhandled report() call" << endl;
}
void askUserToAcceptPackageKey( const UserData & data )
{
if ( !data.hasvalue("PublicKey") || !data.hasvalue(("KeyContext")) ) {
WAR << "Missing arguments in report call for content type: " << data.type() << endl;
return;
}
const PublicKey &key = data.get<PublicKey>("PublicKey");
const KeyContext &ctx = data.get<KeyContext>("KeyContext");
KeyRingReport::KeyTrust res = askUserToAcceptKey(key,ctx, false);
data.set("TrustKey", res == KeyRingReport::KEY_TRUST_AND_IMPORT);
return;
}
void reportKeysNotImportedReport( const UserData & data )
{
if ( !data.hasvalue("Keys") )
{
WAR << "Missing arguments in report call for content type: " << data.type() << endl;
return;
}
Zypper & zypper = Zypper::instance();
zypper.out().notePar(_("The rpm database seems to contain old V3 version gpg keys which are meanwhile obsolete and considered insecure:") );
zypper.out().gap();
for ( const Edition & ed : data.get( "Keys", std::set<Edition>() ) )
zypper.out().info( str::Str() << /*indent8*/" gpg-pubkey-" << ed );
Zypper::instance().out().par( 4,
str::Format(_("To see details about a key call '%1%'.") )
% "rpm -qi GPG-PUBKEY-VERSION" );
Zypper::instance().out().par( 4,
str::Format(_("Unless you believe the key in question is still in use, you can remove it from the rpm database calling '%1%'.") )
% "rpm -e GPG-PUBKEY-VERSION" );
zypper.out().gap();
}
void reportAutoImportKey( const UserData & data_r )
{
if ( not ( data_r.hasvalue("KeyDataList") && data_r.hasvalue("KeySigning") && data_r.hasvalue("KeyContext") ) ) {
WAR << "Missing arguments in report call for content type: " << data_r.type() << endl;
return;
}
const std::list<PublicKeyData> & keyDataList { data_r.get<std::list<PublicKeyData>>("KeyDataList") };
const PublicKeyData & keySigning { data_r.get<PublicKeyData>("KeySigning") };
const KeyContext & context { data_r.get<KeyContext>("KeyContext") };
Zypper & zypper { Zypper::instance() };
// translator: %1% is the number of keys, %2% the name of a repository
zypper.out().notePar( str::Format( PL_( "Received %1% new package signing key from repository \"%2%\":",
"Received %1% new package signing keys from repository \"%2%\":",
keyDataList.size() )) % keyDataList.size() % context.repoInfo().asUserString() );
zypper.out().par( 2,_("Those additional keys are usually used to sign packages shipped by the repository. In order to validate those packages upon download and installation the new keys will be imported into the rpm database.") );
auto newTag { HIGHLIGHTString(_("New:") ) };
for ( const auto & kd : keyDataList ) {
zypper.out().gap();
dumpKeyInfo( std::cout << " " << newTag << endl, kd );
}
zypper.out().par( 2,HIGHLIGHTString(_("The repository metadata introducing the new keys have been signed and validated by the trusted key:")) );
zypper.out().gap();
dumpKeyInfo( std::cout, keySigning, context );
zypper.out().gap();
}
private:
const Config & _gopts;
};
///////////////////////////////////////////////////////////////////
// DigestReceive
///////////////////////////////////////////////////////////////////
struct DigestReceive : public callback::ReceiveReport<DigestReport>
{
DigestReceive() : _gopts(Zypper::instance().config()) {}
////////////////////////////////////////////////////////////////////
virtual bool askUserToAcceptNoDigest( const Pathname &file )
{
std::string question = (str::Format(_("No digest for file %s.")) % file).str() + " " + text::qContinue();
return read_bool_answer(PROMPT_GPG_NO_DIGEST_ACCEPT, question, _gopts.no_gpg_checks);
}
////////////////////////////////////////////////////////////////////
virtual bool askUserToAccepUnknownDigest( const Pathname &file, const std::string &name )
{
std::string question = (str::Format(_("Unknown digest %s for file %s.")) %name % file).str() + " " + text::qContinue();
return read_bool_answer(PROMPT_GPG_UNKNOWN_DIGEST_ACCEPT, question, _gopts.no_gpg_checks);
}
////////////////////////////////////////////////////////////////////
virtual bool askUserToAcceptWrongDigest( const Pathname &file, const std::string &requested, const std::string &found )
{
Zypper & zypper = Zypper::instance();
std::string unblock( found.substr( 0, 4 ) );
zypper.out().gap();
// translators: !!! BOOST STYLE PLACEHOLDERS ( %N% - reorder and multiple occurrence is OK )
// translators: %1% - a file name
// translators: %2% - full path name
// translators: %3%, %4% - checksum strings (>60 chars), please keep them aligned
zypper.out().warning( str::Format(_(
"Digest verification failed for file '%1%'\n"
"[%2%]\n"
"\n"
" expected %3%\n"
" but got %4%\n" ) )
% file.basename()
% file
% requested
% found
);
zypper.out().info( MSG_WARNINGString(_(
"Accepting packages with wrong checksums can lead to a corrupted system "
"and in extreme cases even to a system compromise." ) ).str()
);
zypper.out().gap();
// translators: !!! BOOST STYLE PLACEHOLDERS ( %N% - reorder and multiple occurrence is OK )
// translators: %1% - abbreviated checksum (4 chars)
zypper.out().info( str::Format(_(
"However if you made certain that the file with checksum '%1%..' is secure, correct\n"
"and should be used within this operation, enter the first 4 characters of the checksum\n"
"to unblock using this file on your own risk. Empty input will discard the file.\n" ) )
% unblock
);
// translators: A prompt option
PromptOptions popts( unblock+"/"+_("discard"), 1 );
// translators: A prompt option help text
popts.setOptionHelp( 0, _("Unblock using this file on your own risk.") );
// translators: A prompt option help text
popts.setOptionHelp( 1, _("Discard the file.") );
popts.setShownCount( 1 );
// translators: A prompt text
zypper.out().prompt( PROMPT_GPG_WRONG_DIGEST_ACCEPT, _("Unblock or discard?"), popts );
int reply = get_prompt_reply( zypper, PROMPT_GPG_WRONG_DIGEST_ACCEPT, popts );
return( reply == 0 );
}
private:
const Config & _gopts;
};
///////////////////////////////////////////////////////////////////
}; // namespace zypp
///////////////////////////////////////////////////////////////////
class KeyRingCallbacks {
private:
KeyRingReceive _keyRingReport;
public:
KeyRingCallbacks()
{
_keyRingReport.connect();
}
~KeyRingCallbacks()
{
_keyRingReport.disconnect();
}
};
class DigestCallbacks {
private:
DigestReceive _digestReport;
public:
DigestCallbacks()
{
_digestReport.connect();
}
~DigestCallbacks()
{
_digestReport.disconnect();
}
};
#endif // ZMART_KEYRINGCALLBACKS_H
// Local Variables:
// c-basic-offset: 2
// End: