Пример #1
0
        /// <summary>
        /// Derive a new encrypt key from the given decrypt key value.
        /// </summary>
        ///
        /// <param name="keyBits"></param>
        /// <returns>The new encrypt key (DER-encoded public key).</returns>
        public static EncryptKey deriveEncryptKey(Blob keyBits)
        {
            // Decode the PKCS #8 private key. (We don't use RSAPrivateCrtKey because
            // the Android library doesn't have an easy way to decode into it.)
            DerNode parsedNode          = net.named_data.jndn.encoding.der.DerNode.parse(keyBits.buf(), 0);
            IList   pkcs8Children       = parsedNode.getChildren();
            IList   algorithmIdChildren = net.named_data.jndn.encoding.der.DerNode.getSequence(pkcs8Children, 1)
                                          .getChildren();
            String oidString = ((DerNode.DerOid)algorithmIdChildren[0])
                               .toVal().toString();
            Blob rsaPrivateKeyDer = ((DerNode)pkcs8Children[2]).getPayload();

            String RSA_ENCRYPTION_OID = "1.2.840.113549.1.1.1";

            if (!oidString.equals(RSA_ENCRYPTION_OID))
            {
                throw new DerDecodingException(
                          "The PKCS #8 private key is not RSA_ENCRYPTION");
            }

            // Decode the PKCS #1 RSAPrivateKey.
            parsedNode = net.named_data.jndn.encoding.der.DerNode.parse(rsaPrivateKeyDer.buf(), 0);
            IList rsaPrivateKeyChildren = parsedNode.getChildren();
            Blob  modulus        = ((DerNode)rsaPrivateKeyChildren[1]).getPayload();
            Blob  publicExponent = ((DerNode)rsaPrivateKeyChildren[2])
                                   .getPayload();

            System.SecurityPublicKey publicKey = keyFactory_
                                                 .generatePublic(new RSAPublicKeySpec(new Int64(modulus
                                                                                                .getImmutableArray()), new Int64(publicExponent
                                                                                                                                 .getImmutableArray())));

            return(new EncryptKey(new Blob(publicKey.getEncoded(), false)));
        }
Пример #2
0
        /// <summary>
        /// Get the encoded public key for this private key.
        /// </summary>
        ///
        /// <returns>The public key encoding Blob.</returns>
        /// <exception cref="TpmPrivateKey.Error">if no private key is loaded, or errorconverting to a public key.</exception>
        public Blob derivePublicKey()
        {
            if (keyType_ == net.named_data.jndn.security.KeyType.EC)
            {
                throw new TpmPrivateKey.Error(
                          "TODO: derivePublicKey for EC is not implemented");
            }
            else if (keyType_ == net.named_data.jndn.security.KeyType.RSA)
            {
                // Decode the PKCS #1 RSAPrivateKey. (We don't use RSAPrivateCrtKey because
                // the Android library doesn't have an easy way to decode into it.)
                IList rsaPrivateKeyChildren;
                try {
                    DerNode parsedNode = net.named_data.jndn.encoding.der.DerNode.parse(toPkcs1().buf(), 0);
                    rsaPrivateKeyChildren = parsedNode.getChildren();
                } catch (DerDecodingException ex) {
                    throw new TpmPrivateKey.Error("Error parsing RSA PKCS #1 key: "
                                                  + ex);
                }
                Blob modulus = ((DerNode)rsaPrivateKeyChildren[1])
                               .getPayload();
                Blob publicExponent = ((DerNode)rsaPrivateKeyChildren[2])
                                      .getPayload();

                try {
                    System.SecurityPublicKey publicKey = System.KeyFactory.getInstance(
                        "RSA").generatePublic(
                        new RSAPublicKeySpec((modulus
                                              .getImmutableArray()), (
                                                 publicExponent.getImmutableArray())));
                    return(new Blob(publicKey.getEncoded(), false));
                } catch (Exception ex_0) {
                    throw new TpmPrivateKey.Error("Error making RSA public key: "
                                                  + ex_0);
                }
            }
            else
            {
                throw new TpmPrivateKey.Error(
                          "derivePublicKey: The private key is not loaded");
            }
        }