Search in sources :

Example 1 with CRLEntry

use of net.i2p.router.news.CRLEntry in project i2p.i2p by i2p.

the class NewsFetcher method persistCRLEntries.

/**
 *  Output any updated CRL entries
 *
 *  @since 0.9.26
 */
private void persistCRLEntries(List<CRLEntry> entries) {
    File dir = new SecureFile(_context.getConfigDir(), "certificates");
    if (!dir.exists() && !dir.mkdir()) {
        _log.error("Failed to create CRL directory " + dir);
        return;
    }
    dir = new SecureFile(dir, "revocations");
    if (!dir.exists() && !dir.mkdir()) {
        _log.error("Failed to create CRL directory " + dir);
        return;
    }
    int i = 0;
    for (CRLEntry e : entries) {
        if (e.id == null || e.data == null) {
            if (_log.shouldWarn())
                _log.warn("Bad CRL entry received");
            continue;
        }
        byte[] bid = DataHelper.getUTF8(e.id);
        byte[] hash = new byte[32];
        _context.sha().calculateHash(bid, 0, bid.length, hash, 0);
        String name = "crl-" + Base64.encode(hash) + ".crl";
        File f = new File(dir, name);
        if (f.exists() && f.lastModified() >= e.updated)
            continue;
        OutputStream out = null;
        try {
            byte[] data = DataHelper.getUTF8(e.data);
            // test for validity
            CertUtil.loadCRL(new ByteArrayInputStream(data));
            out = new SecureFileOutputStream(f);
            out.write(data);
        } catch (GeneralSecurityException gse) {
            _log.error("Bad CRL", gse);
        } catch (IOException ioe) {
            _log.error("Failed to write CRL", ioe);
        } finally {
            if (out != null)
                try {
                    out.close();
                } catch (IOException ioe) {
                }
        }
        f.setLastModified(e.updated);
        i++;
    }
    if (i > 0)
        _log.logAlways(Log.WARN, "Stored " + i + " new CRL " + (i > 1 ? "entries" : "entry"));
}
Also used : SecureFile(net.i2p.util.SecureFile) ByteArrayInputStream(java.io.ByteArrayInputStream) SecureFileOutputStream(net.i2p.util.SecureFileOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) GeneralSecurityException(java.security.GeneralSecurityException) SecureFileOutputStream(net.i2p.util.SecureFileOutputStream) CRLEntry(net.i2p.router.news.CRLEntry) IOException(java.io.IOException) SU3File(net.i2p.crypto.SU3File) SecureFile(net.i2p.util.SecureFile) File(java.io.File)

Example 2 with CRLEntry

use of net.i2p.router.news.CRLEntry in project i2p.i2p by i2p.

the class NewsFetcher method processSU3.

/**
 *  Process the fetched su3 news file _tempFile.
 *  Handles 3 types of contained files: xml.gz (preferred), xml, and html (old format fake xml)
 *
 *  @return the temp file contining the HTML-format news.xml
 *  @since 0.9.17
 */
private File processSU3() throws IOException {
    SU3File su3 = new SU3File(_context, _tempFile);
    // real xml, maybe gz, maybe not
    File to1 = new File(_context.getTempDir(), "tmp-" + _context.random().nextInt() + ".xml");
    // real xml
    File to2 = new File(_context.getTempDir(), "tmp2-" + _context.random().nextInt() + ".xml");
    try {
        su3.verifyAndMigrate(to1);
        int type = su3.getFileType();
        if (su3.getContentType() != SU3File.CONTENT_NEWS)
            throw new IOException("bad content type: " + su3.getContentType());
        if (type == SU3File.TYPE_HTML)
            return to1;
        if (type != SU3File.TYPE_XML && type != SU3File.TYPE_XML_GZ)
            throw new IOException("bad file type: " + type);
        File xml;
        if (type == SU3File.TYPE_XML_GZ) {
            gunzip(to1, to2);
            xml = to2;
            to1.delete();
        } else {
            xml = to1;
        }
        NewsXMLParser parser = new NewsXMLParser(_context);
        Node root = parser.parse(xml);
        xml.delete();
        NewsMetadata data = parser.getMetadata();
        List<NewsEntry> entries = parser.getEntries();
        // add entries to the news manager
        ClientAppManager cmgr = _context.clientAppManager();
        if (cmgr != null) {
            NewsManager nmgr = (NewsManager) cmgr.getRegisteredApp(NewsManager.APP_NAME);
            if (nmgr != null) {
                nmgr.addEntries(entries);
                List<Node> nodes = NewsXMLParser.getNodes(root, "entry");
                nmgr.storeEntries(nodes);
            }
        }
        // Persist any new CRL entries
        List<CRLEntry> crlEntries = parser.getCRLEntries();
        if (crlEntries != null)
            persistCRLEntries(crlEntries);
        else
            _log.info("No CRL entries found in news feed");
        // Block any new blocklist entries
        BlocklistEntries ble = parser.getBlocklistEntries();
        if (ble != null && ble.isVerified())
            processBlocklistEntries(ble);
        else
            _log.info("No blocklist entries found in news feed");
        // store entries and metadata in old news.xml format
        String sudVersion = su3.getVersionString();
        String signingKeyName = su3.getSignerString();
        File to3 = new File(_context.getTempDir(), "tmp3-" + _context.random().nextInt() + ".xml");
        outputOldNewsXML(data, entries, sudVersion, signingKeyName, to3);
        return to3;
    } finally {
        to2.delete();
    }
}
Also used : NewsMetadata(net.i2p.router.news.NewsMetadata) Node(org.cybergarage.xml.Node) IOException(java.io.IOException) CRLEntry(net.i2p.router.news.CRLEntry) SU3File(net.i2p.crypto.SU3File) NewsEntry(net.i2p.router.news.NewsEntry) ClientAppManager(net.i2p.app.ClientAppManager) NewsXMLParser(net.i2p.router.news.NewsXMLParser) NewsManager(net.i2p.router.news.NewsManager) BlocklistEntries(net.i2p.router.news.BlocklistEntries) SU3File(net.i2p.crypto.SU3File) SecureFile(net.i2p.util.SecureFile) File(java.io.File)

Aggregations

File (java.io.File)2 IOException (java.io.IOException)2 SU3File (net.i2p.crypto.SU3File)2 CRLEntry (net.i2p.router.news.CRLEntry)2 SecureFile (net.i2p.util.SecureFile)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileOutputStream (java.io.FileOutputStream)1 OutputStream (java.io.OutputStream)1 GeneralSecurityException (java.security.GeneralSecurityException)1 ClientAppManager (net.i2p.app.ClientAppManager)1 BlocklistEntries (net.i2p.router.news.BlocklistEntries)1 NewsEntry (net.i2p.router.news.NewsEntry)1 NewsManager (net.i2p.router.news.NewsManager)1 NewsMetadata (net.i2p.router.news.NewsMetadata)1 NewsXMLParser (net.i2p.router.news.NewsXMLParser)1 SecureFileOutputStream (net.i2p.util.SecureFileOutputStream)1 Node (org.cybergarage.xml.Node)1