コード例 #1
0
ファイル: Producer.cs プロジェクト: redfood/ndn-dot-net
        /// <summary>
        /// This is called from an expressInterest OnData to check that the encryption
        /// key contained in data fits the timeSlot. This sends a refined interest if
        /// required.
        /// </summary>
        ///
        /// <param name="interest">The interest given to expressInterest.</param>
        /// <param name="data">The fetched Data packet.</param>
        /// <param name="timeSlot_0">The time slot as milliseconds since Jan 1, 1970 UTC.</param>
        /// <param name="onEncryptedKeys_1">encrypted content key Data packets. If onEncryptedKeys is null, this does not use it.</param>
        internal void handleCoveringKey(Interest interest, Data data,
                                        double timeSlot_0, Producer.OnEncryptedKeys onEncryptedKeys_1, net.named_data.jndn.encrypt.EncryptError.OnError onError_2)
        {
            double timeCount = Math.Round(timeSlot_0, MidpointRounding.AwayFromZero);

            Producer.KeyRequest keyRequest = (Producer.KeyRequest)ILOG.J2CsMapping.Collections.Collections.Get(keyRequests_, timeCount);

            Name interestName = interest.getName();
            Name keyName      = data.getName();

            double begin = net.named_data.jndn.encrypt.Schedule.fromIsoString(keyName
                                                                              .get(START_TIME_STAMP_INDEX).getValue().toString());
            double end = net.named_data.jndn.encrypt.Schedule.fromIsoString(keyName.get(END_TIME_STAMP_INDEX)
                                                                            .getValue().toString());

            if (timeSlot_0 >= end)
            {
                // If the received E-KEY covers some earlier period, try to retrieve an
                // E-KEY covering a later one.
                Exclude timeRange = new Exclude(interest.getExclude());
                excludeBefore(timeRange, keyName.get(START_TIME_STAMP_INDEX));
                ILOG.J2CsMapping.Collections.Collections.Put(keyRequest.repeatAttempts, interestName, 0);

                sendKeyInterest(new Interest(interestName).setExclude(timeRange)
                                .setChildSelector(1), timeSlot_0, onEncryptedKeys_1, onError_2);
            }
            else
            {
                // If the received E-KEY covers the content key, encrypt the content.
                Blob encryptionKey = data.getContent();
                // If everything is correct, save the E-KEY as the current key.
                if (encryptContentKey(encryptionKey, keyName, timeSlot_0,
                                      onEncryptedKeys_1, onError_2))
                {
                    Producer.KeyInfo keyInfo = (Producer.KeyInfo)ILOG.J2CsMapping.Collections.Collections.Get(eKeyInfo_, interestName);
                    keyInfo.beginTimeSlot = begin;
                    keyInfo.endTimeSlot   = end;
                    keyInfo.keyBits       = encryptionKey;
                }
            }
        }
コード例 #2
0
ファイル: Producer.cs プロジェクト: redfood/ndn-dot-net
        /// <summary>
        /// Create the content key corresponding to the timeSlot. This first checks if
        /// the content key exists. For an existing content key, this returns the
        /// content key name directly. If the key does not exist, this creates one and
        /// encrypts it using the corresponding E-KEYs. The encrypted content keys are
        /// passed to the onEncryptedKeys callback.
        /// </summary>
        ///
        /// <param name="timeSlot_0">The time slot as milliseconds since Jan 1, 1970 UTC.</param>
        /// <param name="onEncryptedKeys_1">content key Data packets. If onEncryptedKeys is null, this does not use it. NOTE: The library will log any exceptions thrown by this callback, but for better error handling the callback should catch and properly handle any exceptions.</param>
        /// <param name="onError_2">better error handling the callback should catch and properly handle any exceptions.</param>
        /// <returns>The content key name.</returns>
        public Name createContentKey(double timeSlot_0,
                                     Producer.OnEncryptedKeys onEncryptedKeys_1, net.named_data.jndn.encrypt.EncryptError.OnError onError_2)
        {
            double hourSlot = getRoundedTimeSlot(timeSlot_0);

            // Create the content key name.
            Name contentKeyName = new Name(namespace_);

            contentKeyName.append(net.named_data.jndn.encrypt.algo.Encryptor.NAME_COMPONENT_C_KEY);
            contentKeyName.append(net.named_data.jndn.encrypt.Schedule.toIsoString(hourSlot));

            Blob contentKeyBits;

            // Check if we have created the content key before.
            if (database_.hasContentKey(timeSlot_0))
            {
                // We have created the content key. Return its name directly.
                return(contentKeyName);
            }

            // We haven't created the content key. Create one and add it into the database.
            AesKeyParams aesParams = new AesKeyParams(128);

            contentKeyBits = net.named_data.jndn.encrypt.algo.AesAlgorithm.generateKey(aesParams).getKeyBits();
            database_.addContentKey(timeSlot_0, contentKeyBits);

            // Now we need to retrieve the E-KEYs for content key encryption.
            double timeCount = Math.Round(timeSlot_0, MidpointRounding.AwayFromZero);

            ILOG.J2CsMapping.Collections.Collections.Put(keyRequests_, timeCount, new Producer.KeyRequest(eKeyInfo_.Count));
            Producer.KeyRequest keyRequest = (Producer.KeyRequest)ILOG.J2CsMapping.Collections.Collections.Get(keyRequests_, timeCount);

            // Check if the current E-KEYs can cover the content key.
            Exclude timeRange = new Exclude();

            excludeAfter(timeRange,
                         new Name.Component(net.named_data.jndn.encrypt.Schedule.toIsoString(timeSlot_0)));
            new ILOG.J2CsMapping.Collections.IteratorAdapter(eKeyInfo_.GetEnumerator());
            for (IIterator i = new ILOG.J2CsMapping.Collections.IteratorAdapter(eKeyInfo_.GetEnumerator()); i.HasNext();)
            {
                // For each current E-KEY.
                DictionaryEntry  entry   = (DictionaryEntry)i.Next();
                Producer.KeyInfo keyInfo = (Producer.KeyInfo)((DictionaryEntry)entry).Value;
                if (timeSlot_0 < keyInfo.beginTimeSlot ||
                    timeSlot_0 >= keyInfo.endTimeSlot)
                {
                    // The current E-KEY cannot cover the content key, so retrieve one.
                    ILOG.J2CsMapping.Collections.Collections.Put(keyRequest.repeatAttempts, ((DictionaryEntry)entry).Key, 0);
                    sendKeyInterest(
                        new Interest((Name)((DictionaryEntry)entry).Key).setExclude(
                            timeRange).setChildSelector(1), timeSlot_0,
                        onEncryptedKeys_1, onError_2);
                }
                else
                {
                    // The current E-KEY can cover the content key.
                    // Encrypt the content key directly.
                    Name eKeyName = new Name((Name)((DictionaryEntry)entry).Key);
                    eKeyName.append(net.named_data.jndn.encrypt.Schedule.toIsoString(keyInfo.beginTimeSlot));
                    eKeyName.append(net.named_data.jndn.encrypt.Schedule.toIsoString(keyInfo.endTimeSlot));
                    encryptContentKey(keyInfo.keyBits, eKeyName, timeSlot_0,
                                      onEncryptedKeys_1, onError_2);
                }
            }

            return(contentKeyName);
        }