Changeset 593779b5


Ignore:
Timestamp:
Aug 23, 2014 11:48:16 PM (6 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
54563b0
Parents:
d7feab1
Message:

Router: Prep for RI sig types:

  • New router.sigType config
  • Generate / regenerate router keys based on config
  • New router.keys2 file format for sig types and padding
  • Fix RouterInfo?.readBytes() signature verification with sig types
  • Catch unset padding in KeysAndCert?.writeBytes()
  • Catch key errors in ReadRouterJob?
  • Show RI sig type on /netdb in console
  • Move some things from Router to startup classes
  • Startup classes package private
  • Buffer readin of key files
  • Remove configurability of router.info and router.keys file locations
Files:
16 edited

Legend:

Unmodified
Added
Removed
  • apps/routerconsole/java/src/net/i2p/router/web/NetDbRenderer.java

    rd7feab1 r593779b5  
    416416            buf.append("<b>" + _("Published") + ":</b> in ").append(DataHelper.formatDuration2(0-age)).append("???<br>\n");
    417417        }
    418         buf.append("<b>" + _("Address(es)") + ":</b> ");
     418        buf.append("<b>").append(_("Signing Key")).append(":</b> ")
     419           .append(info.getIdentity().getSigningPublicKey().getType().toString());
     420        buf.append("<br>\n<b>" + _("Address(es)") + ":</b> ");
    419421        String country = _context.commSystem().getCountry(info.getIdentity().getHash());
    420422        if(country != null) {
  • core/java/src/net/i2p/data/KeysAndCert.java

    rd7feab1 r593779b5  
    115115        if (_padding != null)
    116116            out.write(_padding);
     117        else if (_signingKey.length() < SigningPublicKey.KEYSIZE_BYTES)
     118            throw new DataFormatException("No padding set");
    117119        _signingKey.writeTruncatedBytes(out);
    118120        _certificate.writeBytes(out);
  • router/java/src/net/i2p/data/router/RouterInfo.java

    rd7feab1 r593779b5  
    3737import net.i2p.data.KeysAndCert;
    3838import net.i2p.data.Signature;
     39import net.i2p.data.SimpleDataStructure;
    3940import net.i2p.util.Clock;
    4041import net.i2p.util.Log;
     
    526527        if (_signature != null)
    527528            throw new IllegalStateException();
     529        _identity = new RouterIdentity();
     530        _identity.readBytes(in);
     531        // can't set the digest until we know the sig type
    528532        InputStream din;
    529533        MessageDigest digest;
    530534        if (verifySig) {
    531             digest = SHA1.getInstance();
     535            digest = _identity.getSigningPublicKey().getType().getDigestInstance();
     536            // TODO any better way?
     537            digest.update(_identity.toByteArray());
    532538            din = new DigestInputStream(in, digest);
    533539        } else {
     
    535541            din = in;
    536542        }
    537         _identity = new RouterIdentity();
    538         _identity.readBytes(din);
    539543        // avoid thrashing objects
    540544        //Date when = DataHelper.readDate(in);
     
    566570
    567571        if (verifySig) {
    568             SHA1Hash hash = new SHA1Hash(digest.digest());
     572            SimpleDataStructure hash = _identity.getSigningPublicKey().getType().getHashInstance();
     573            hash.setData(digest.digest());
    569574            _isValid = DSAEngine.getInstance().verifySignature(_signature, hash, _identity.getSigningPublicKey());
    570575            _validated = true;
  • router/java/src/net/i2p/router/KeyManager.java

    rd7feab1 r593779b5  
    1919import java.util.concurrent.ConcurrentHashMap;
    2020
     21import net.i2p.crypto.SigType;
    2122import net.i2p.data.DataFormatException;
    2223import net.i2p.data.DataStructure;
     
    2728import net.i2p.data.SigningPrivateKey;
    2829import net.i2p.data.SigningPublicKey;
     30import net.i2p.router.startup.CreateRouterInfoJob;
    2931import net.i2p.util.Log;
    3032import net.i2p.util.SecureDirectory;
     
    152154            syncPrivateKey(keyDir);
    153155            syncPublicKey(keyDir);
    154             syncSigningKey(keyDir);
    155             syncVerificationKey(keyDir);
     156            SigType type = CreateRouterInfoJob.getSigTypeConfig(getContext());
     157            syncSigningKey(keyDir, type);
     158            syncVerificationKey(keyDir, type);
    156159        }
    157160
     
    182185        }
    183186
    184         private void syncSigningKey(File keyDir) {
     187        /**
     188         *  @param type the SigType to expect on read-in, ignored on write
     189         */
     190        private void syncSigningKey(File keyDir, SigType type) {
    185191            DataStructure ds;
    186192            File keyFile = new File(keyDir, KEYFILE_PRIVATE_SIGNING);
     
    189195                ds = _signingPrivateKey;
    190196            else
    191                 ds = new SigningPrivateKey();
     197                ds = new SigningPrivateKey(type);
    192198            DataStructure readin = syncKey(keyFile, ds, exists);
    193199            if (readin != null && !exists)
     
    195201        }
    196202
    197         private void syncVerificationKey(File keyDir) {
     203        /**
     204         *  @param type the SigType to expect on read-in, ignored on write
     205         */
     206        private void syncVerificationKey(File keyDir, SigType type) {
    198207            DataStructure ds;
    199208            File keyFile = new File(keyDir, KEYFILE_PUBLIC_SIGNING);
     
    202211                ds = _signingPublicKey;
    203212            else
    204                 ds = new SigningPublicKey();
     213                ds = new SigningPublicKey(type);
    205214            DataStructure readin = syncKey(keyFile, ds, exists);
    206215            if (readin != null && !exists)
  • router/java/src/net/i2p/router/Router.java

    rd7feab1 r593779b5  
    9999    public final static String PROP_HIDDEN_HIDDEN = "router.isHidden";
    100100    public final static String PROP_DYNAMIC_KEYS = "router.dynamicKeys";
    101     public final static String PROP_INFO_FILENAME = "router.info.location";
    102     public final static String PROP_INFO_FILENAME_DEFAULT = "router.info";
    103     public final static String PROP_KEYS_FILENAME = "router.keys.location";
    104     public final static String PROP_KEYS_FILENAME_DEFAULT = "router.keys";
    105101    public final static String PROP_SHUTDOWN_IN_PROGRESS = "__shutdownInProgress";
    106102    public final static String DNS_CACHE_TIME = "" + (5*60);
     
    671667            return Boolean.parseBoolean(h);
    672668        return _context.commSystem().isInBadCountry();
    673     }
    674 
    675     /**
    676      *  Only called at startup via LoadRouterInfoJob and RebuildRouterInfoJob.
    677      *  Not called by periodic RepublishLocalRouterInfoJob.
    678      *  We don't want to change the cert on the fly as it changes the router hash.
    679      *  RouterInfo.isHidden() checks the capability, but RouterIdentity.isHidden() checks the cert.
    680      *  There's no reason to ever add a hidden cert?
    681      *  @return the certificate for a new RouterInfo - probably a null cert.
    682      */
    683     public Certificate createCertificate() {
    684         if (_context.getBooleanProperty(PROP_HIDDEN))
    685             return new Certificate(Certificate.CERTIFICATE_TYPE_HIDDEN, null);
    686         return Certificate.NULL_CERT;
    687669    }
    688670   
  • router/java/src/net/i2p/router/networkdb/kademlia/PersistentDataStore.java

    rd7feab1 r593779b5  
    525525                        _log.info("Unable to read the router reference in " + _routerFile.getName(), ioe);
    526526                    corrupt = true;
     527                } catch (Exception e) {
     528                    // key certificate problems, etc., don't let one bad RI kill the whole thing
     529                    if (_log.shouldLog(Log.INFO))
     530                        _log.info("Unable to read the router reference in " + _routerFile.getName(), e);
     531                    corrupt = true;
    527532                } finally {
    528533                    if (fis != null) try { fis.close(); } catch (IOException ioe) {}
  • router/java/src/net/i2p/router/startup/BootCommSystemJob.java

    rd7feab1 r593779b5  
    1717
    1818/** This actually boots almost everything */
    19 public class BootCommSystemJob extends JobImpl {
     19class BootCommSystemJob extends JobImpl {
    2020    private Log _log;
    2121   
  • router/java/src/net/i2p/router/startup/BootNetworkDbJob.java

    rd7feab1 r593779b5  
    1313
    1414/** start up the network database */
    15 public class BootNetworkDbJob extends JobImpl {
     15class BootNetworkDbJob extends JobImpl {
    1616   
    1717    public BootNetworkDbJob(RouterContext ctx) {
  • router/java/src/net/i2p/router/startup/BootPeerManagerJob.java

    rd7feab1 r593779b5  
    1313
    1414/** start up the peer manager */
    15 public class BootPeerManagerJob extends JobImpl {
     15class BootPeerManagerJob extends JobImpl {
    1616   
    1717    public BootPeerManagerJob(RouterContext ctx) {
  • router/java/src/net/i2p/router/startup/BuildTrustedLinksJob.java

    rd7feab1 r593779b5  
    1616 *  For future restricted routes. Does nothing now.
    1717 */
    18 public class BuildTrustedLinksJob extends JobImpl {
     18class BuildTrustedLinksJob extends JobImpl {
    1919    private final Job _next;
    2020   
  • router/java/src/net/i2p/router/startup/CreateRouterInfoJob.java

    rd7feab1 r593779b5  
    1313import java.io.IOException;
    1414import java.io.OutputStream;
     15import java.security.GeneralSecurityException;
    1516import java.util.Properties;
    1617
     18import net.i2p.crypto.SigType;
    1719import net.i2p.data.Certificate;
    1820import net.i2p.data.DataFormatException;
     21import net.i2p.data.DataHelper;
     22import net.i2p.data.KeyCertificate;
    1923import net.i2p.data.PrivateKey;
    2024import net.i2p.data.PublicKey;
     
    2327import net.i2p.data.SigningPrivateKey;
    2428import net.i2p.data.SigningPublicKey;
     29import net.i2p.data.SimpleDataStructure;
    2530import net.i2p.router.Job;
    2631import net.i2p.router.JobImpl;
     
    4146    private final Job _next;
    4247   
    43     public CreateRouterInfoJob(RouterContext ctx, Job next) {
     48    public static final String INFO_FILENAME = "router.info";
     49    static final String KEYS_FILENAME = "router.keys";
     50    static final String KEYS2_FILENAME = "router.keys2";
     51    private static final String PROP_ROUTER_SIGTYPE = "router.sigType";
     52    /** TODO when changing, check isAvailable() and fallback to DSA_SHA1 */
     53    private static final SigType DEFAULT_SIGTYPE = SigType.DSA_SHA1;
     54    static final byte[] KEYS2_MAGIC = DataHelper.getASCII("I2Pkeys2");
     55    static final int KEYS2_UNUSED_BYTES = 28;
     56
     57    CreateRouterInfoJob(RouterContext ctx, Job next) {
    4458        super(ctx);
    4559        _next = next;
     
    6074    /**
    6175     *  Writes 6 files: router.info (standard RI format),
    62      *  router,keys, and 4 individual key files under keyBackup/
    63      *
    64      *  router.keys file format: Note that this is NOT the
     76     *  router.keys2, and 4 individual key files under keyBackup/
     77     *
     78     *  router.keys2 file format: Note that this is NOT the
     79     *  same "eepPriv.dat" format used by the client code.
     80     *<pre>
     81     *   - Magic "I2Pkeys2"
     82     *   - 2 byte crypto type, always 0000 for now
     83     *   - 2 byte sig type, see SigType
     84     *   - 28 bytes unused
     85     *   - Private key (256 bytes)
     86     *   - Signing Private key (20 bytes or see SigType)
     87     *   - Public key (256 bytes)
     88     *   - Random padding for Signing Public Key if less than 128 bytes
     89     *   - Signing Public key (128 bytes or see SigTpe)
     90     *  Total 660 bytes
     91     *</pre>
     92     *
     93     *  Old router.keys file format: Note that this is NOT the
    6594     *  same "eepPriv.dat" format used by the client code.
    6695     *<pre>
     
    75104     */
    76105    RouterInfo createRouterInfo() {
     106        SigType type = getSigTypeConfig(getContext());
    77107        RouterInfo info = new RouterInfo();
    78108        OutputStream fos1 = null;
     
    87117            //info.setPeers(new HashSet());
    88118            info.setPublished(getCurrentPublishDate(getContext()));
     119            Object keypair[] = getContext().keyGenerator().generatePKIKeypair();
     120            PublicKey pubkey = (PublicKey)keypair[0];
     121            PrivateKey privkey = (PrivateKey)keypair[1];
     122            SimpleDataStructure signingKeypair[] = getContext().keyGenerator().generateSigningKeys(type);
     123            SigningPublicKey signingPubKey = (SigningPublicKey)signingKeypair[0];
     124            SigningPrivateKey signingPrivKey = (SigningPrivateKey)signingKeypair[1];
    89125            RouterIdentity ident = new RouterIdentity();
    90             Certificate cert = getContext().router().createCertificate();
     126            Certificate cert = createCertificate(getContext(), signingPubKey);
    91127            ident.setCertificate(cert);
    92             PublicKey pubkey = null;
    93             PrivateKey privkey = null;
    94             SigningPublicKey signingPubKey = null;
    95             SigningPrivateKey signingPrivKey = null;
    96             Object keypair[] = getContext().keyGenerator().generatePKIKeypair();
    97             pubkey = (PublicKey)keypair[0];
    98             privkey = (PrivateKey)keypair[1];
    99             Object signingKeypair[] = getContext().keyGenerator().generateSigningKeypair();
    100             signingPubKey = (SigningPublicKey)signingKeypair[0];
    101             signingPrivKey = (SigningPrivateKey)signingKeypair[1];
    102128            ident.setPublicKey(pubkey);
    103129            ident.setSigningPublicKey(signingPubKey);
     130            byte[] padding;
     131            int padLen = SigningPublicKey.KEYSIZE_BYTES - signingPubKey.length();
     132            if (padLen > 0) {
     133                padding = new byte[padLen];
     134                getContext().random().nextBytes(padding);
     135                ident.setPadding(padding);
     136            } else {
     137                padding = null;
     138            }
    104139            info.setIdentity(ident);
    105140           
     
    109144                throw new DataFormatException("RouterInfo we just built is invalid: " + info);
    110145           
    111             String infoFilename = getContext().getProperty(Router.PROP_INFO_FILENAME, Router.PROP_INFO_FILENAME_DEFAULT);
    112             File ifile = new File(getContext().getRouterDir(), infoFilename);
     146            // remove router.keys
     147            (new File(getContext().getRouterDir(), KEYS_FILENAME)).delete();
     148
     149            // write router.info
     150            File ifile = new File(getContext().getRouterDir(), INFO_FILENAME);
    113151            fos1 = new BufferedOutputStream(new SecureFileOutputStream(ifile));
    114152            info.writeBytes(fos1);
    115153           
    116             String keyFilename = getContext().getProperty(Router.PROP_KEYS_FILENAME, Router.PROP_KEYS_FILENAME_DEFAULT);
    117             File kfile = new File(getContext().getRouterDir(), keyFilename);
     154            // write router.keys2
     155            File kfile = new File(getContext().getRouterDir(), KEYS2_FILENAME);
    118156            fos2 = new BufferedOutputStream(new SecureFileOutputStream(kfile));
     157            fos2.write(KEYS2_MAGIC);
     158            DataHelper.writeLong(fos2, 2, 0);
     159            DataHelper.writeLong(fos2, 2, type.getCode());
     160            fos2.write(new byte[KEYS2_UNUSED_BYTES]);
    119161            privkey.writeBytes(fos2);
    120162            signingPrivKey.writeBytes(fos2);
    121163            pubkey.writeBytes(fos2);
     164            if (padding != null)
     165                fos2.write(padding);
    122166            signingPubKey.writeBytes(fos2);
    123167           
    124168            getContext().keyManager().setKeys(pubkey, privkey, signingPubKey, signingPrivKey);
    125169           
    126             _log.info("Router info created and stored at " + ifile.getAbsolutePath() + " with private keys stored at " + kfile.getAbsolutePath() + " [" + info + "]");
     170            if (_log.shouldLog(Log.INFO))
     171                _log.info("Router info created and stored at " + ifile.getAbsolutePath() + " with private keys stored at " + kfile.getAbsolutePath() + " [" + info + "]");
    127172            getContext().router().eventLog().addEvent(EventLog.REKEYED, ident.calculateHash().toBase64());
     173        } catch (GeneralSecurityException gse) {
     174            _log.log(Log.CRIT, "Error building the new router information", gse);
    128175        } catch (DataFormatException dfe) {
    129176            _log.log(Log.CRIT, "Error building the new router information", dfe);
     
    137184    }
    138185   
     186    /**
     187     *  The configured SigType to expect on read-in
     188     *  @since 0.9.16
     189     */
     190    public static SigType getSigTypeConfig(RouterContext ctx) {
     191        SigType cstype = CreateRouterInfoJob.DEFAULT_SIGTYPE;
     192        String sstype = ctx.getProperty(PROP_ROUTER_SIGTYPE);
     193        if (sstype != null) {
     194            SigType ntype = SigType.parseSigType(sstype);
     195            if (ntype != null && ntype.isAvailable())
     196                cstype = ntype;
     197        }
     198        return cstype;
     199    }
    139200   
    140201    /**
     
    147208        return context.clock().now();
    148209    }
     210
     211    /**
     212     *  Only called at startup via LoadRouterInfoJob and RebuildRouterInfoJob.
     213     *  Not called by periodic RepublishLocalRouterInfoJob.
     214     *  We don't want to change the cert on the fly as it changes the router hash.
     215     *  RouterInfo.isHidden() checks the capability, but RouterIdentity.isHidden() checks the cert.
     216     *  There's no reason to ever add a hidden cert?
     217     *
     218     *  @return the certificate for a new RouterInfo - probably a null cert.
     219     *  @since 0.9.16 moved from Router
     220     */
     221    static Certificate createCertificate(RouterContext ctx, SigningPublicKey spk) {
     222        if (spk.getType() != SigType.DSA_SHA1)
     223            return new KeyCertificate(spk);
     224        if (ctx.getBooleanProperty(Router.PROP_HIDDEN))
     225            return new Certificate(Certificate.CERTIFICATE_TYPE_HIDDEN, null);
     226        return Certificate.NULL_CERT;
     227    }
    149228}
  • router/java/src/net/i2p/router/startup/LoadRouterInfoJob.java

    rd7feab1 r593779b5  
    1616import java.util.concurrent.atomic.AtomicBoolean;
    1717
     18import net.i2p.crypto.SigType;
    1819import net.i2p.data.DataFormatException;
     20import net.i2p.data.DataHelper;
    1921import net.i2p.data.PrivateKey;
    2022import net.i2p.data.PublicKey;
     
    2729import net.i2p.util.Log;
    2830
    29 public class LoadRouterInfoJob extends JobImpl {
     31/**
     32 *  Run once or twice at startup by StartupJob,
     33 *  and then runs BootCommSystemJob
     34 */
     35class LoadRouterInfoJob extends JobImpl {
    3036    private final Log _log;
    3137    private RouterInfo _us;
     
    4652            RebuildRouterInfoJob r = new RebuildRouterInfoJob(getContext());
    4753            r.rebuildRouterInfo(false);
     54            // run a second time
    4855            getContext().jobQueue().addJob(this);
    4956            return;
     
    5562    }
    5663   
     64    /**
     65     *  Loads router.info and router.keys2 or router.keys.
     66     *
     67     *  See CreateRouterInfoJob for file formats
     68     */
    5769    private void loadRouterInfo() {
    58         String routerInfoFile = getContext().getProperty(Router.PROP_INFO_FILENAME, Router.PROP_INFO_FILENAME_DEFAULT);
    5970        RouterInfo info = null;
    60         String keyFilename = getContext().getProperty(Router.PROP_KEYS_FILENAME, Router.PROP_KEYS_FILENAME_DEFAULT);
    61        
    62         File rif = new File(getContext().getRouterDir(), routerInfoFile);
     71        File rif = new File(getContext().getRouterDir(), CreateRouterInfoJob.INFO_FILENAME);
    6372        boolean infoExists = rif.exists();
    64         File rkf = new File(getContext().getRouterDir(), keyFilename);
     73        File rkf = new File(getContext().getRouterDir(), CreateRouterInfoJob.KEYS_FILENAME);
    6574        boolean keysExist = rkf.exists();
     75        File rkf2 = new File(getContext().getRouterDir(), CreateRouterInfoJob.KEYS2_FILENAME);
     76        boolean keys2Exist = rkf2.exists();
    6677       
    6778        InputStream fis1 = null;
     
    7485            // at net.i2p.router.transport.udp.PacketBuilder.buildSessionConfirmedPacket(PacketBuilder.java:574)
    7586            // so pretend the RI isn't there if there is no keyfile
    76             if (infoExists && keysExist) {
     87            if (infoExists && (keys2Exist || keysExist)) {
    7788                fis1 = new BufferedInputStream(new FileInputStream(rif));
    7889                info = new RouterInfo();
     
    8697            }
    8798           
    88             if (keysExist) {
    89                 fis2 = new BufferedInputStream(new FileInputStream(rkf));
     99            if (keys2Exist || keysExist) {
     100                SigType stype;
     101                if (keys2Exist) {
     102                    fis2 = new BufferedInputStream(new FileInputStream(rkf2));
     103                    // read keys2 headers
     104                    byte[] magic = new byte[CreateRouterInfoJob.KEYS2_MAGIC.length];
     105                    DataHelper.read(fis2, magic);
     106                    if (!DataHelper.eq(magic, CreateRouterInfoJob.KEYS2_MAGIC))
     107                        throw new IOException("Bad magic");
     108                    int ctype = (int) DataHelper.readLong(fis2, 2);
     109                    if (ctype != 0)
     110                        throw new IOException("Unsupported RI crypto type " + ctype);
     111                    int sstype = (int) DataHelper.readLong(fis2, 2);
     112                    stype = SigType.getByCode(sstype);
     113                    if (stype == null || !stype.isAvailable())
     114                        throw new IOException("Unsupported RI sig type " + stype);
     115                    DataHelper.skip(fis2, CreateRouterInfoJob.KEYS2_UNUSED_BYTES);
     116                } else {
     117                    fis2 = new BufferedInputStream(new FileInputStream(rkf));
     118                    stype = SigType.DSA_SHA1;
     119                }
     120
     121                // check if the sigtype config changed
     122                SigType cstype = CreateRouterInfoJob.getSigTypeConfig(getContext());
     123                boolean sigTypeChanged = stype != cstype;
     124
    90125                PrivateKey privkey = new PrivateKey();
    91126                privkey.readBytes(fis2);
    92                 if (shouldRebuild(privkey)) {
     127                if (sigTypeChanged || shouldRebuild(privkey)) {
     128                    if (sigTypeChanged)
     129                        _log.logAlways(Log.WARN, "Rebuilding RouterInfo with new signature type " + cstype);
    93130                    _us = null;
    94131                    // windows... close before deleting
     
    101138                    rif.delete();
    102139                    rkf.delete();
     140                    rkf2.delete();
    103141                    return;
    104142                }
    105                 SigningPrivateKey signingPrivKey = new SigningPrivateKey();
     143                SigningPrivateKey signingPrivKey = new SigningPrivateKey(stype);
    106144                signingPrivKey.readBytes(fis2);
    107145                PublicKey pubkey = new PublicKey();
    108146                pubkey.readBytes(fis2);
    109                 SigningPublicKey signingPubKey = new SigningPublicKey();
     147                SigningPublicKey signingPubKey = new SigningPublicKey(stype);
     148                int padLen = SigningPublicKey.KEYSIZE_BYTES - signingPubKey.length();
     149                if (padLen > 0) {
     150                    // we lose the padding as keymanager doesn't store it, what to do?
     151                    DataHelper.skip(fis2, padLen);
     152                }
    110153                signingPubKey.readBytes(fis2);
    111154               
     
    126169            rif.delete();
    127170            rkf.delete();
     171            rkf2.delete();
    128172        } catch (DataFormatException dfe) {
    129173            _log.log(Log.CRIT, "Corrupt router info or keys at " + rif.getAbsolutePath() + " / " + rkf.getAbsolutePath(), dfe);
     
    140184            rif.delete();
    141185            rkf.delete();
     186            rkf2.delete();
    142187        } finally {
    143188            if (fis1 != null) try { fis1.close(); } catch (IOException ioe) {}
  • router/java/src/net/i2p/router/startup/RebuildRouterInfoJob.java

    rd7feab1 r593779b5  
    99 */
    1010
     11import java.io.BufferedInputStream;
    1112import java.io.File;
    1213import java.io.FileInputStream;
    1314import java.io.FileOutputStream;
     15import java.io.InputStream;
    1416import java.io.IOException;
    1517import java.util.Properties;
    1618
     19import net.i2p.crypto.SigType;
    1720import net.i2p.data.Certificate;
    1821import net.i2p.data.DataFormatException;
     22import net.i2p.data.DataHelper;
    1923import net.i2p.data.PrivateKey;
    2024import net.i2p.data.PublicKey;
     
    4549 *
    4650 */
    47 public class RebuildRouterInfoJob extends JobImpl {
     51class RebuildRouterInfoJob extends JobImpl {
    4852    private final Log _log;
    4953   
     
    5862   
    5963    public void runJob() {
     64        throw new UnsupportedOperationException();
     65/****
    6066        _log.debug("Testing to rebuild router info");
    61         String infoFile = getContext().getProperty(Router.PROP_INFO_FILENAME, Router.PROP_INFO_FILENAME_DEFAULT);
    62         File info = new File(getContext().getRouterDir(), infoFile);
    63         String keyFilename = getContext().getProperty(Router.PROP_KEYS_FILENAME, Router.PROP_KEYS_FILENAME_DEFAULT);
    64         File keyFile = new File(getContext().getRouterDir(), keyFilename);
     67        File info = new File(getContext().getRouterDir(), CreateRouterInfoJob.INFO_FILENAME);
     68        File keyFile = new File(getContext().getRouterDir(), CreateRouterInfoJob.KEYS2_FILENAME);
    6569       
    6670        if (!info.exists() || !keyFile.exists()) {
     
    7276        getTiming().setStartAfter(getContext().clock().now() + REBUILD_DELAY);
    7377        getContext().jobQueue().addJob(this);
     78****/
    7479    }
    7580   
     
    7883    }
    7984
     85    /**
     86     *  @param alreadyRunning unused
     87     */
    8088    void rebuildRouterInfo(boolean alreadyRunning) {
    8189        _log.debug("Rebuilding the new router info");
    8290        RouterInfo info = null;
    83         String infoFilename = getContext().getProperty(Router.PROP_INFO_FILENAME, Router.PROP_INFO_FILENAME_DEFAULT);
    84         File infoFile = new File(getContext().getRouterDir(), infoFilename);
    85         String keyFilename = getContext().getProperty(Router.PROP_KEYS_FILENAME, Router.PROP_KEYS_FILENAME_DEFAULT);
    86         File keyFile = new File(getContext().getRouterDir(), keyFilename);
     91        File infoFile = new File(getContext().getRouterDir(), CreateRouterInfoJob.INFO_FILENAME);
     92        File keyFile = new File(getContext().getRouterDir(), CreateRouterInfoJob.KEYS_FILENAME);
     93        File keyFile2 = new File(getContext().getRouterDir(), CreateRouterInfoJob.KEYS2_FILENAME);
    8794       
    88         if (keyFile.exists()) {
     95        if (keyFile2.exists() || keyFile.exists()) {
    8996            // ok, no need to rebuild a brand new identity, just update what we can
    9097            RouterInfo oldinfo = getContext().router().getRouterInfo();
    9198            if (oldinfo == null) {
    9299                info = new RouterInfo();
    93                 FileInputStream fis = null;
     100                InputStream fis = null;
    94101                try {
    95                     fis = new FileInputStream(keyFile);
     102                    SigType stype;
     103                    if (keyFile2.exists()) {
     104                        fis = new BufferedInputStream(new FileInputStream(keyFile2));
     105                        byte[] magic = new byte[CreateRouterInfoJob.KEYS2_MAGIC.length];
     106                        DataHelper.read(fis, magic);
     107                        if (!DataHelper.eq(magic, CreateRouterInfoJob.KEYS2_MAGIC))
     108                            throw new IOException("Bad magic");
     109                        int ctype = (int) DataHelper.readLong(fis, 2);
     110                        if (ctype != 0)
     111                            throw new IOException("Unsupported RI crypto type " + ctype);
     112                        int sstype = (int) DataHelper.readLong(fis, 2);
     113                        stype = SigType.getByCode(sstype);
     114                        if (stype == null || !stype.isAvailable())
     115                            throw new IOException("Unsupported RI sig type " + stype);
     116                        DataHelper.skip(fis, CreateRouterInfoJob.KEYS2_UNUSED_BYTES);
     117                    } else {
     118                        fis = new BufferedInputStream(new FileInputStream(keyFile));
     119                        stype = SigType.DSA_SHA1;
     120                    }
    96121                    PrivateKey privkey = new PrivateKey();
    97122                    privkey.readBytes(fis);
    98                     SigningPrivateKey signingPrivKey = new SigningPrivateKey();
     123                    SigningPrivateKey signingPrivKey = new SigningPrivateKey(stype);
    99124                    signingPrivKey.readBytes(fis);
    100125                    PublicKey pubkey = new PublicKey();
    101126                    pubkey.readBytes(fis);
    102                     SigningPublicKey signingPubKey = new SigningPublicKey();
     127                    SigningPublicKey signingPubKey = new SigningPublicKey(stype);
     128                    byte[] padding;
     129                    int padLen = SigningPublicKey.KEYSIZE_BYTES - signingPubKey.length();
     130                    if (padLen > 0) {
     131                        padding = new byte[padLen];
     132                        DataHelper.read(fis, padding);
     133                    } else {
     134                        padding = null;
     135                    }
    103136                    signingPubKey.readBytes(fis);
    104137                    RouterIdentity ident = new RouterIdentity();
    105                     Certificate cert = getContext().router().createCertificate();
     138                    Certificate cert = CreateRouterInfoJob.createCertificate(getContext(), signingPubKey);
    106139                    ident.setCertificate(cert);
    107140                    ident.setPublicKey(pubkey);
    108141                    ident.setSigningPublicKey(signingPubKey);
     142                    if (padding != null)
     143                        ident.setPadding(padding);
    109144                    info.setIdentity(ident);
    110145                } catch (Exception e) {
     
    161196            // this proc writes the keys and info to the file as well as builds the latest and greatest info
    162197            CreateRouterInfoJob j = new CreateRouterInfoJob(getContext(), null);
    163             info = j.createRouterInfo();
     198            synchronized (getContext().router().routerInfoFileLock) {
     199                info = j.createRouterInfo();
     200            }
    164201        }
    165202       
    166203        //MessageHistory.initialize();
    167204        getContext().router().setRouterInfo(info);
    168         _log.info("Router info rebuilt and stored at " + infoFilename + " [" + info + "]");
     205        _log.info("Router info rebuilt and stored at " + infoFile + " [" + info + "]");
    169206    }
    170207   
  • router/java/src/net/i2p/router/startup/StartAcceptingClientsJob.java

    rd7feab1 r593779b5  
    1313
    1414/** start I2CP interface */
    15 public class StartAcceptingClientsJob extends JobImpl {
     15class StartAcceptingClientsJob extends JobImpl {
    1616   
    1717    public StartAcceptingClientsJob(RouterContext context) {
  • router/java/src/net/i2p/router/startup/WorkingDir.java

    rd7feab1 r593779b5  
    148148        // and only migrate the data files if told to do so
    149149        // (router.keys could be deleted later by a killkeys())
    150         test = new File(oldDirf, "router.keys");
     150        test = new File(oldDirf, CreateRouterInfoJob.KEYS_FILENAME);
    151151        boolean oldInstall = test.exists();
    152152        if (!oldInstall) {
  • router/java/src/net/i2p/router/tasks/PersistRouterInfoJob.java

    rd7feab1 r593779b5  
    1616import net.i2p.data.router.RouterInfo;
    1717import net.i2p.router.JobImpl;
    18 import net.i2p.router.Router;
    1918import net.i2p.router.RouterContext;
     19import net.i2p.router.startup.CreateRouterInfoJob;
    2020import net.i2p.util.Log;
    2121import net.i2p.util.SecureFileOutputStream;
     
    3838            _log.debug("Persisting updated router info");
    3939
    40         String infoFilename = getContext().getProperty(Router.PROP_INFO_FILENAME, Router.PROP_INFO_FILENAME_DEFAULT);
    41         File infoFile = new File(getContext().getRouterDir(), infoFilename);
     40        File infoFile = new File(getContext().getRouterDir(), CreateRouterInfoJob.INFO_FILENAME);
    4241
    4342        RouterInfo info = getContext().router().getRouterInfo();
Note: See TracChangeset for help on using the changeset viewer.