예제 #1
0
        /// <summary>
        /// Create an identity certificate for a public key supplied by the caller.
        /// </summary>
        ///
        /// <param name="certificatePrefix">The name of public key to be signed.</param>
        /// <param name="publicKey">The public key to be signed.</param>
        /// <param name="signerCertificateName">The name of signing certificate.</param>
        /// <param name="notBefore">The notBefore value in the validity field of the generated certificate.</param>
        /// <param name="notAfter">The notAfter vallue in validity field of the generated certificate.</param>
        /// <returns>The generated identity certificate.</returns>
        public IdentityCertificate createIdentityCertificate(
				Name certificatePrefix, PublicKey publicKey,
				Name signerCertificateName, double notBefore, double notAfter)
        {
            IdentityCertificate certificate = new IdentityCertificate();
            Name keyName = getKeyNameFromCertificatePrefix(certificatePrefix);

            Name certificateName = new Name(certificatePrefix);
            certificateName.append("ID-CERT").appendVersion(
                    (long) net.named_data.jndn.util.Common.getNowMilliseconds());

            certificate.setName(certificateName);
            certificate.setNotBefore(notBefore);
            certificate.setNotAfter(notAfter);
            certificate.setPublicKeyInfo(publicKey);
            certificate.addSubjectDescription(new CertificateSubjectDescription(
                    "2.5.4.41", keyName.toUri()));
            try {
                certificate.encode();
            } catch (DerEncodingException ex) {
                throw new SecurityException("DerDecodingException: " + ex);
            } catch (DerDecodingException ex_0) {
                throw new SecurityException("DerEncodingException: " + ex_0);
            }

            Sha256WithRsaSignature sha256Sig = new Sha256WithRsaSignature();

            KeyLocator keyLocator = new KeyLocator();
            keyLocator.setType(net.named_data.jndn.KeyLocatorType.KEYNAME);
            keyLocator.setKeyName(signerCertificateName);

            sha256Sig.setKeyLocator(keyLocator);

            certificate.setSignature(sha256Sig);

            SignedBlob unsignedData = certificate.wireEncode();

            IdentityCertificate signerCertificate;
            try {
                signerCertificate = getCertificate(signerCertificateName);
            } catch (DerDecodingException ex_1) {
                throw new SecurityException("DerDecodingException: " + ex_1);
            }
            Name signerkeyName = signerCertificate.getPublicKeyName();

            Blob sigBits = privateKeyStorage_.sign(unsignedData.signedBuf(),
                    signerkeyName);

            sha256Sig.setSignature(sigBits);

            return certificate;
        }