Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/security/provider/certpath/OCSPNonceExtension.java
38923 views
/*1* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.security.provider.certpath;2627import java.io.IOException;28import java.util.Objects;29import java.security.SecureRandom;3031import sun.security.x509.Extension;32import sun.security.x509.PKIXExtensions;33import sun.security.util.Debug;34import sun.security.util.DerValue;3536/**37* Represent the OCSP Nonce Extension.38* This extension, if present, provides a nonce value in OCSP requests39* and responses. This will cryptographically bind requests and responses40* and help to prevent replay attacks (see RFC 6960, section 4.4.1).41*42* @see Extension43*/44public final class OCSPNonceExtension extends Extension {4546/**47* Attribute name.48*/49private static final String EXTENSION_NAME = "OCSPNonce";50private byte[] nonceData = null;5152/**53* Create an {@code OCSPNonceExtension} by providing the nonce length.54* The criticality is set to false, and the OID for the extension will55* be the value defined by "id-pkix-ocsp-nonce" from RFC 6960.56*57* @param length the number of random bytes composing the nonce58*59* @throws IOException if any errors happen during encoding of the60* extension.61* @throws IllegalArgumentException if length is not a positive integer.62*/63public OCSPNonceExtension(int length) throws IOException {64this(false, length);65}6667/**68* Create an {@code OCSPNonceExtension} by providing the nonce length and69* criticality setting. The OID for the extension will70* be the value defined by "id-pkix-ocsp-nonce" from RFC 6960.71*72* @param isCritical a boolean flag indicating whether the criticality bit73* is set for this extension74* @param length the number of random bytes composing the nonce75*76* @throws IOException if any errors happen during encoding of the77* extension.78* @throws IllegalArgumentException if length is not a positive integer.79*/80public OCSPNonceExtension(boolean isCritical, int length)81throws IOException {82this.extensionId = PKIXExtensions.OCSPNonce_Id;83this.critical = isCritical;8485if (length > 0) {86SecureRandom rng = new SecureRandom();87this.nonceData = new byte[length];88rng.nextBytes(nonceData);89this.extensionValue = new DerValue(DerValue.tag_OctetString,90nonceData).toByteArray();91} else {92throw new IllegalArgumentException(93"Length must be a positive integer");94}95}9697/**98* Create an {@code OCSPNonceExtension} by providing a nonce value.99* The criticality is set to false, and the OID for the extension will100* be the value defined by "id-pkix-ocsp-nonce" from RFC 6960.101*102* @param incomingNonce The nonce data to be set for the extension. This103* must be a non-null array of at least one byte long.104*105* @throws IOException if any errors happen during encoding of the106* extension.107* @throws IllegalArgumentException if the incomingNonce length is not a108* positive integer.109* @throws NullPointerException if the incomingNonce is null.110*/111public OCSPNonceExtension(byte[] incomingNonce) throws IOException {112this(false, incomingNonce);113}114115/**116* Create an {@code OCSPNonceExtension} by providing a nonce value and117* criticality setting. The OID for the extension will118* be the value defined by "id-pkix-ocsp-nonce" from RFC 6960.119*120* @param isCritical a boolean flag indicating whether the criticality bit121* is set for this extension122* @param incomingNonce The nonce data to be set for the extension. This123* must be a non-null array of at least one byte long.124*125* @throws IOException if any errors happen during encoding of the126* extension.127* @throws IllegalArgumentException if the incomingNonce length is not a128* positive integer.129* @throws NullPointerException if the incomingNonce is null.130*/131public OCSPNonceExtension(boolean isCritical, byte[] incomingNonce)132throws IOException {133this.extensionId = PKIXExtensions.OCSPNonce_Id;134this.critical = isCritical;135136Objects.requireNonNull(incomingNonce, "Nonce data must be non-null");137if (incomingNonce.length > 0) {138this.nonceData = incomingNonce.clone();139this.extensionValue = new DerValue(DerValue.tag_OctetString,140nonceData).toByteArray();141} else {142throw new IllegalArgumentException(143"Nonce data must be at least 1 byte in length");144}145}146147/**148* Return the nonce bytes themselves, without any DER encoding.149*150* @return A copy of the underlying nonce bytes151*/152public byte[] getNonceValue() {153return nonceData.clone();154}155156/**157* Returns a printable representation of the {@code OCSPNonceExtension}.158*159* @return a string representation of the extension.160*/161@Override162public String toString() {163StringBuilder sb = new StringBuilder();164sb.append(super.toString()).append(EXTENSION_NAME).append(": ");165sb.append((nonceData == null) ? "" : Debug.toString(nonceData));166sb.append("\n");167return sb.toString();168}169170/**171* Return the name of the extension as a {@code String}172*173* @return the name of the extension174*/175public String getName() {176return EXTENSION_NAME;177}178}179180181