1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/PublicKey.cc
13 //#include "zypp/base/Logger.h"
15 #include "zypp/base/String.h"
16 #include "zypp/base/Regex.h"
17 #include "zypp/PublicKey.h"
18 #include "zypp/ExternalProgram.h"
19 #include "zypp/TmpPath.h"
20 #include "zypp/PathInfo.h"
21 #include "zypp/base/Exception.h"
22 #include "zypp/base/Logger.h"
26 ///////////////////////////////////////////////////////////////////
28 { /////////////////////////////////////////////////////////////////
30 // CLASS NAME : PublicKey::Impl
32 /** PublicKey implementation. */
33 struct PublicKey::Impl
38 Impl(const Pathname &file)
41 MIL << "Done reading key" << std::endl;
45 /** Offer default Impl. */
46 static shared_ptr<Impl> nullimpl()
48 static shared_ptr<Impl> _nullimpl( new Impl );
53 std::string asString() const
55 return "[" + id() + "] [" + name() + "] [" + fingerprint() + "]";
58 std::string armoredData() const
61 std::string id() const
64 std::string name() const
67 std::string fingerprint() const
68 { return _fingerprint; }
72 return _data_file.path();
78 void readFromFile( const Pathname &keyfile)
80 static str::regex rxColons("^([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):\n$");
82 PathInfo info(keyfile);
83 MIL << "Reading pubkey from " << keyfile << " of size " << info.size() << " and sha1 " << filesystem::checksum(keyfile, "sha1")<< endl;
84 if ( !info.isExist() )
85 ZYPP_THROW(Exception("Can't read public key from " + keyfile.asString() + ", file not found"));
87 if ( copy( keyfile, _data_file.path() ) != 0 )
88 ZYPP_THROW(Exception("Can't copy public key data from " + keyfile.asString() + " to " + _data_file.path().asString() ));
91 filesystem::TmpDir dir;
96 "--no-default-keyring",
98 dir.path().asString().c_str(),
107 _data_file.path().asString().c_str(),
111 ExternalProgram prog(argv,ExternalProgram::Discard_Stderr, false, -1, true);
116 // pub:-:1024:17:A84EDAE89C800ACA:2000-10-19:2008-06-21::-:SuSE Package Signing Key <build@suse.de>:
118 for(line = prog.receiveLine(), count=0; !line.empty(); line = prog.receiveLine(), count++ )
120 //MIL << "[" << line << "]" << std::endl;
122 if(str::regex_match(line, what, rxColons))
124 if ( what[1] == "pub" )
130 else if ( what[1] == "fpr" )
132 _fingerprint = what[10];
134 //dumpRegexpResults(what);
139 if (_id.size() == 0 )
140 ZYPP_THROW(BadKeyException("File " + keyfile.asString() + " doesn't contain public key data" , keyfile));
146 std::string _fingerprint;
148 filesystem::TmpFile _data_file;
149 //Pathname _data_file;
151 friend Impl * rwcowClone<Impl>( const Impl * rhs );
152 /** clone for RWCOW_pointer */
154 { return new Impl( *this ); }
156 ///////////////////////////////////////////////////////////////////
158 ///////////////////////////////////////////////////////////////////
160 // METHOD NAME : PublicKey::PublicKey
161 // METHOD TYPE : Ctor
163 PublicKey::PublicKey()
164 : _pimpl( Impl::nullimpl() )
167 PublicKey::PublicKey( const Pathname &file )
168 : _pimpl( new Impl(file) )
170 ///////////////////////////////////////////////////////////////////
172 // METHOD NAME : PublicKey::~PublicKey
173 // METHOD TYPE : Dtor
175 PublicKey::~PublicKey()
178 ///////////////////////////////////////////////////////////////////
180 // Forward to implementation:
182 ///////////////////////////////////////////////////////////////////
184 std::string PublicKey::asString() const
186 return _pimpl->asString();
189 std::string PublicKey::armoredData() const
190 { return _pimpl->armoredData(); }
192 std::string PublicKey::id() const
193 { return _pimpl->id(); }
195 std::string PublicKey::name() const
196 { return _pimpl->name(); }
198 std::string PublicKey::fingerprint() const
199 { return _pimpl->fingerprint(); }
201 Pathname PublicKey::path() const
202 { return _pimpl->path(); }
204 bool PublicKey::operator==( PublicKey b ) const
206 return (b.id() == id()) && (b.fingerprint() == fingerprint() );
209 bool PublicKey::operator==( std::string sid ) const
214 /////////////////////////////////////////////////////////////////
216 ///////////////////////////////////////////////////////////////////