1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/PublicKey.cc
17 #include "zypp/base/Gettext.h"
18 #include "zypp/base/String.h"
19 #include "zypp/base/Regex.h"
20 #include "zypp/PublicKey.h"
21 #include "zypp/ExternalProgram.h"
22 #include "zypp/TmpPath.h"
23 #include "zypp/PathInfo.h"
24 #include "zypp/base/Exception.h"
25 #include "zypp/base/LogTools.h"
26 #include "zypp/Date.h"
27 #include "zypp/TmpPath.h"
33 ///////////////////////////////////////////////////////////////////
35 { /////////////////////////////////////////////////////////////////
37 ///////////////////////////////////////////////////////////////////
38 /// \class PublicKeyData::Impl
39 /// \brief PublicKeyData implementation.
40 ///////////////////////////////////////////////////////////////////
41 struct PublicKeyData::Impl
45 std::string _fingerprint;
50 /** Offer default Impl. */
51 static shared_ptr<Impl> nullimpl()
53 static shared_ptr<Impl> _nullimpl( new Impl );
58 friend Impl * rwcowClone<Impl>( const Impl * rhs );
59 /** clone for RWCOW_pointer */
61 { return new Impl( *this ); }
63 ///////////////////////////////////////////////////////////////////
65 ///////////////////////////////////////////////////////////////////
66 /// class PublicKeyData
67 ///////////////////////////////////////////////////////////////////
69 PublicKeyData::PublicKeyData()
70 : _pimpl( Impl::nullimpl() )
73 PublicKeyData::~PublicKeyData()
76 PublicKeyData::operator bool() const
77 { return !_pimpl->_fingerprint.empty(); }
79 std::string PublicKeyData::id() const
80 { return _pimpl->_id; }
82 std::string PublicKeyData::name() const
83 { return _pimpl->_name; }
85 std::string PublicKeyData::fingerprint() const
86 { return _pimpl->_fingerprint; }
88 Date PublicKeyData::created() const
89 { return _pimpl->_created; }
91 Date PublicKeyData::expires() const
92 { return _pimpl->_expires; }
94 bool PublicKeyData::expired() const
95 { return( _pimpl->_expires && _pimpl->_expires < Date::now() ); }
97 int PublicKeyData::daysToLive() const
99 if ( _pimpl->_expires )
101 Date exp( _pimpl->_expires - Date::now() );
102 int ret = exp / Date::day;
103 if ( exp < 0 ) ret -= 1;
109 std::string PublicKeyData::expiresAsString() const
111 if ( !_pimpl->_expires )
112 { // translators: an annotation to a gpg keys expiry date
113 return _("(does not expire)");
115 std::string ret( _pimpl->_expires.asString() );
116 int ttl( daysToLive() );
121 { // translators: an annotation to a gpg keys expiry date
122 ret += _("(EXPIRED)");
125 { // translators: an annotation to a gpg keys expiry date
126 ret += _("(expires within 24h)");
129 { // translators: an annotation to a gpg keys expiry date
130 ret += str::form( _PL("(expires in %d day)", "(expires in %d days)", ttl ), ttl );
136 std::string PublicKeyData::gpgPubkeyVersion() const
137 { return _pimpl->_id.empty() ? _pimpl->_id : str::toLower( _pimpl->_id.substr(8,8) ); }
139 std::string PublicKeyData::gpgPubkeyRelease() const
140 { return _pimpl->_created ? str::hexstring( _pimpl->_created ).substr(2) : std::string(); }
142 std::string PublicKeyData::asString() const
144 return str::form( "[%s-%s] [%s] [%s] [TTL %d]",
146 gpgPubkeyRelease().c_str(),
147 _pimpl->_name.c_str(),
148 _pimpl->_fingerprint.c_str(),
152 std::ostream & dumpOn( std::ostream & str, const PublicKeyData & obj )
154 str << "[" << obj.name() << "]" << endl;
155 str << " fpr " << obj.fingerprint() << endl;
156 str << " id " << obj.id() << endl;
157 str << " cre " << Date::ValueType(obj.created()) << ' ' << obj.created() << endl;
158 str << " exp " << Date::ValueType(obj.expires()) << ' ' << obj.expiresAsString() << endl;
159 str << " ttl " << obj.daysToLive() << endl;
160 str << " rpm " << obj.gpgPubkeyVersion() << "-" << obj.gpgPubkeyRelease() << endl;
165 bool operator==( const PublicKeyData & lhs, const PublicKeyData & rhs )
166 { return ( lhs.fingerprint() == rhs.fingerprint() && lhs.created() == rhs.created() ); }
169 ///////////////////////////////////////////////////////////////////
170 /// \class PublicKeyScanner::Impl
171 /// \brief PublicKeyScanner implementation.
172 ///////////////////////////////////////////////////////////////////
173 struct PublicKeyScanner::Impl
175 std::vector<std::string> _words;
176 enum { pNONE, pPUB, pSIG, pFPR, pUID } _parseEntry;
177 bool _parseOff; // no 'sub:' key parsing
180 : _parseEntry( pNONE )
184 void scan( std::string & line_r, std::list<PublicKeyData> & keys_r )
186 // pub:-:1024:17:A84EDAE89C800ACA:971961473:1214043198::-:SuSE Package Signing Key <build@suse.de>:
187 // fpr:::::::::79C179B2E1C820C1890F9994A84EDAE89C800ACA:
188 // sig:::17:A84EDAE89C800ACA:1087899198:::::[selfsig]::13x:
189 // sig:::17:9E40E310000AABA4:980442706::::[User ID not found]:10x:
190 // sig:::1:77B2E6003D25D3D9:980443247::::[User ID not found]:10x:
191 // sig:::17:A84EDAE89C800ACA:1318348291:::::[selfsig]::13x:
192 // sub:-:2048:16:197448E88495160C:971961490:1214043258::: [expires: 2008-06-21]
193 // sig:::17:A84EDAE89C800ACA:1087899258:::::[keybind]::18x:
194 if ( line_r.empty() )
197 // quick check for interesting entries, no parsing in subkeys
202 if ( line_r[1] == 'u' && line_r[2] == 'b' && line_r[3] == ':' )
210 if ( line_r[1] == 'p' && line_r[2] == 'r' && line_r[3] == ':' )
215 if ( line_r[1] == 'i' && line_r[2] == 'd' && line_r[3] == ':' )
220 if ( line_r[1] == 'i' && line_r[2] == 'g' && line_r[3] == ':' )
222 else if ( line_r[1] == 'u' && line_r[2] == 'b' && line_r[3] == ':' )
229 if ( _parseOff || _parseEntry == pNONE )
232 if ( line_r[line_r.size()-1] == '\n' )
233 line_r.erase( line_r.size()-1 );
234 // DBG << line_r << endl;
237 str::splitFields( line_r, std::back_inserter(_words), ":" );
239 PublicKeyData * key( &keys_r.back() );
241 switch ( _parseEntry )
244 keys_r.push_back( PublicKeyData() ); // reset upon new key
245 key = &keys_r.back();
246 key->_pimpl->_id = _words[4];
247 key->_pimpl->_name = str::replaceAll( _words[9], "\\x3a", ":" );
248 key->_pimpl->_created = Date(str::strtonum<Date::ValueType>(_words[5]));
249 key->_pimpl->_expires = Date(str::strtonum<Date::ValueType>(_words[6]));
253 // Update creation/modification date from signatures type "13x".
254 if ( ( _words.size() > 10 && _words[10] == "13x" )
255 || ( _words.size() > 12 && _words[12] == "13x" ) )
257 Date cdate(str::strtonum<Date::ValueType>(_words[5]));
258 if ( key->_pimpl->_created < cdate )
259 key->_pimpl->_created = cdate;
264 if ( key->_pimpl->_fingerprint.empty() )
265 key->_pimpl->_fingerprint = _words[9];
269 if ( ! _words[9].empty() )
270 key->_pimpl->_name = str::replaceAll( _words[9], "\\x3a", ":" );
274 break; // intentionally no default:
278 ///////////////////////////////////////////////////////////////////
280 ///////////////////////////////////////////////////////////////////
281 // class PublicKeyScanner
282 ///////////////////////////////////////////////////////////////////
284 PublicKeyScanner::PublicKeyScanner()
288 PublicKeyScanner::~PublicKeyScanner()
291 void PublicKeyScanner::scan( std::string line_r )
292 { _pimpl->scan( line_r, _keys ); }
295 ///////////////////////////////////////////////////////////////////
296 /// \class PublicKey::Impl
297 /// \brief PublicKey implementation.
298 ///////////////////////////////////////////////////////////////////
299 struct PublicKey::Impl
304 Impl( const Pathname & keyFile_r )
306 PathInfo info( keyFile_r );
307 MIL << "Taking pubkey from " << keyFile_r << " of size " << info.size() << " and sha1 " << filesystem::checksum(keyFile_r, "sha1") << endl;
309 if ( !info.isExist() )
310 ZYPP_THROW(Exception("Can't read public key from " + keyFile_r.asString() + ", file not found"));
312 if ( filesystem::hardlinkCopy( keyFile_r, _dataFile.path() ) != 0 )
313 ZYPP_THROW(Exception("Can't copy public key data from " + keyFile_r.asString() + " to " + _dataFile.path().asString() ));
318 Impl( const filesystem::TmpFile & sharedFile_r )
319 : _dataFile( sharedFile_r )
322 Impl( const filesystem::TmpFile & sharedFile_r, const PublicKeyData & keyData_r )
323 : _dataFile( sharedFile_r )
324 , _keyData( keyData_r )
328 WAR << "Invalid PublicKeyData supplied: scanning from file" << endl;
334 const PublicKeyData & keyData() const
337 Pathname path() const
338 { return _dataFile.path(); }
340 const std::list<PublicKeyData> & hiddenKeys() const
341 { return _hiddenKeys; }
346 PathInfo info( _dataFile.path() );
347 MIL << "Reading pubkey from " << info.path() << " of size " << info.size() << " and sha1 " << filesystem::checksum(info.path(), "sha1") << endl;
349 static filesystem::TmpDir dir;
354 "--no-default-keyring",
356 "--with-fingerprint",
359 dir.path().asString().c_str(),
365 _dataFile.path().asString().c_str(),
368 ExternalProgram prog( argv, ExternalProgram::Discard_Stderr, false, -1, true );
370 PublicKeyScanner scanner;
371 for ( std::string line = prog.receiveLine(); !line.empty(); line = prog.receiveLine() )
373 scanner.scan( line );
377 switch ( scanner._keys.size() )
380 ZYPP_THROW( BadKeyException( "File " + _dataFile.path().asString() + " doesn't contain public key data" , _dataFile.path() ) );
385 _keyData = scanner._keys.back();
390 WAR << "File " << _dataFile.path().asString() << " contains multiple keys: " << scanner._keys << endl;
391 _keyData = scanner._keys.back();
392 scanner._keys.pop_back();
393 _hiddenKeys.swap( scanner._keys );
397 MIL << "Read pubkey from " << info.path() << ": " << _keyData << endl;
401 filesystem::TmpFile _dataFile;
402 PublicKeyData _keyData;
403 std::list<PublicKeyData> _hiddenKeys;
406 /** Offer default Impl. */
407 static shared_ptr<Impl> nullimpl()
409 static shared_ptr<Impl> _nullimpl( new Impl );
414 friend Impl * rwcowClone<Impl>( const Impl * rhs );
415 /** clone for RWCOW_pointer */
417 { return new Impl( *this ); }
419 ///////////////////////////////////////////////////////////////////
421 ///////////////////////////////////////////////////////////////////
423 ///////////////////////////////////////////////////////////////////
424 PublicKey::PublicKey()
425 : _pimpl( Impl::nullimpl() )
428 PublicKey::PublicKey( const Pathname & file )
429 : _pimpl( new Impl( file ) )
432 PublicKey::PublicKey( const filesystem::TmpFile & sharedfile )
433 : _pimpl( new Impl( sharedfile ) )
436 PublicKey::PublicKey( const filesystem::TmpFile & sharedfile, const PublicKeyData & keydata )
437 : _pimpl( new Impl( sharedfile, keydata ) )
440 PublicKey::~PublicKey()
443 const PublicKeyData & PublicKey::keyData() const
444 { return _pimpl->keyData(); }
446 Pathname PublicKey::path() const
447 { return _pimpl->path(); }
449 const std::list<PublicKeyData> & PublicKey::hiddenKeys() const
450 { return _pimpl->hiddenKeys(); }
452 std::string PublicKey::id() const
453 { return keyData().id(); }
455 std::string PublicKey::name() const
456 { return keyData().name(); }
458 std::string PublicKey::fingerprint() const
459 { return keyData().fingerprint(); }
461 Date PublicKey::created() const
462 { return keyData().created(); }
464 Date PublicKey::expires() const
465 { return keyData().expires(); }
467 bool PublicKey::expired() const
468 { return keyData().expired(); }
470 int PublicKey::daysToLive() const
471 { return keyData().daysToLive(); }
473 std::string PublicKey::expiresAsString() const
474 { return keyData().expiresAsString(); }
476 std::string PublicKey::gpgPubkeyVersion() const
477 { return keyData().gpgPubkeyVersion(); }
479 std::string PublicKey::gpgPubkeyRelease() const
480 { return keyData().gpgPubkeyRelease(); }
482 std::string PublicKey::asString() const
483 { return keyData().asString(); }
485 bool PublicKey::operator==( const PublicKey & rhs ) const
486 { return rhs.keyData() == keyData(); }
488 bool PublicKey::operator==( const std::string & sid ) const
489 { return sid == id(); }
491 std::ostream & dumpOn( std::ostream & str, const PublicKey & obj )
492 { return dumpOn( str, obj.keyData() ); }
494 /////////////////////////////////////////////////////////////////
496 ///////////////////////////////////////////////////////////////////