📄 publickeystorebuilder.java
字号:
/* * @(#)PublicKeyStoreBuilder.java 1.3 01/08/02 * * Copyright (c) 2001 Sun Microsystems, Inc., 901 San Antonio Road, * Palo Alto, CA 94303, U.S.A. All Rights Reserved. * * Sun Microsystems, Inc. has intellectual property rights relating * to the technology embodied in this software. In particular, and * without limitation, these intellectual property rights may include * one or more U.S. patents, foreign patents, or pending * applications. Sun, Sun Microsystems, the Sun logo, Java, KJava, * and all Sun-based and Java-based marks are trademarks or * registered trademarks of Sun Microsystems, Inc. in the United * States and other countries. * * This software is distributed under licenses restricting its use, * copying, distribution, and decompilation. No part of this * software may be reproduced in any form by any means without prior * written authorization of Sun and its licensors, if any. * * FEDERAL ACQUISITIONS: Commercial Software -- Government Users * Subject to Standard License Terms and Conditions */package com.sun.midp.publickeystore;import java.io.*;import java.util.*;import com.sun.ksecurity.RSAPublicKey;import com.sun.kssl.X509Certificate;import com.sun.midp.io.Base64;/** * A read-write {@link PublicKeyStore} that adds and updates keys * using certificates. For security purposes this should not be used as a * trusted public keystore. */public class PublicKeyStoreBuilder extends PublicKeyStoreBuilderBase { /** * Constructs an empty read-write keystore. */ public PublicKeyStoreBuilder() { super(); }; /** * Constructs a read-write keystore from a serialized keystore created * by this class. * @param in stream to read a keystore serialized by * {@link PublicKeyStoreBuilder#serialize(OutputStream)} from * @exception IOException if the key storage was corrupted */ public PublicKeyStoreBuilder(InputStream in) throws IOException { super(in); } /** * Adds the public key from a certificate to this keystore. * @param base64Cert base64 encoded X.509 certificate * @return name of the key's owner, or null if a key for the subject is * already in the store */ public String addKeyUsingCertificate(String base64Cert) { return addKeyUsingCertificate(base64Cert, null); } /** * Adds the public key from a certificate to this keystore and assigns * the key to a security domain. * @param base64Cert base64 encoded X.509 certificate * @param domain name of security domain to assign to the public key * @return name of the key's owner, or null if a key for the subject is * already in the store */ public String addKeyUsingCertificate(String base64Cert, String domain) { X509Certificate cert; cert = base64ToCertificate(base64Cert); if (cert == null) { return null; } if (!addKeyUsingCertificate(cert, domain)) { return null; } return cert.getSubject(); } /** * Adds the public key from a certificate to this keystore. * @param cert KSSL Certificate * @return true if successful, or false if a key for the subject is * already in the store */ public boolean addKeyUsingCertificate(X509Certificate cert) { return addKeyUsingCertificate(cert, null); } /** * Adds the public key from a certificate to this keystore and assigns * the key to a security domain. * @param cert parsed X.509 certificate * @param domain name of security domain to assign to the public key * @return true if successful, or false if a key for the subject is * already in the store */ public synchronized boolean addKeyUsingCertificate(X509Certificate cert, String domain) { PublicKeyInfo keyInfo; if (domain == null) { // null is the same as untrusted domain = "untrusted"; } keyInfo = CertificateToKeyInfo(cert, domain); return addKey(keyInfo); } /** * Updates a certificate issuer's public key information with the subject * information in the certificate. * @param base64Cert base64 encoded X.509 certificate * @return true if successful, or false if not */ public boolean updateKeyUsingCertificate(String base64Cert) { X509Certificate cert; cert = base64ToCertificate(base64Cert); if (cert == null) { return false; } return updateKeyUsingCertificate(cert); } /** * Updates a certificate issuer's public key information with the subject * information in the certificate. * @param cert KSSL certificate * @return true if successful, or false if not */ public synchronized boolean updateKeyUsingCertificate( X509Certificate cert) { String owner; PublicKeyInfo keyInfo; owner = cert.getIssuer(); keyInfo = CertificateToKeyInfo(cert, null); return updateKey(owner, keyInfo); } /** * Gets the public key from a certificate and assigns * the key to a security domain. * * @param cert parsed X.509 certificate * @param domain name of security domain to assign to the public key * @return public key information, including the given domain */ private PublicKeyInfo CertificateToKeyInfo(X509Certificate cert, String domain) { RSAPublicKey key; int modulusLen; byte [] modulus; int exponentLen; byte [] exponent; key = (RSAPublicKey)cert.getPublicKey(); modulusLen = key.getSize() / 8; modulus = new byte[modulusLen]; exponentLen = key.getExponent(modulus, (short) 0); key.getModulus(modulus, (short) 0); exponent = new byte[exponentLen]; exponentLen = key.getExponent(exponent, (short) 0); return new PublicKeyInfo(cert.getSubject(), cert.getNotBefore().getTime(), cert.getNotAfter().getTime(), modulus, exponent, domain); } /** * Parse a base64 encoded certificate. * * @param base64Cert base64 encoded X.509 certificate * @return parsed X.509 certificate */ private X509Certificate base64ToCertificate(String base64Cert) { byte[] decodedCert; try { decodedCert = Base64.decode(base64Cert, 0, base64Cert.length()); return X509Certificate.generateCertificate(decodedCert, 0, decodedCert.length); } catch (Exception e) { return null; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -