1. /*
  2. * @(#)PolicyQualifierInfo.java 1.9 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.security.cert;
  8. import java.io.IOException;
  9. import sun.misc.HexDumpEncoder;
  10. import sun.security.util.DerValue;
  11. /**
  12. * An immutable policy qualifier represented by the ASN.1 PolicyQualifierInfo
  13. * structure.
  14. *
  15. * <p>The ASN.1 definition is as follows:
  16. * <p><pre>
  17. * PolicyQualifierInfo ::= SEQUENCE {
  18. * policyQualifierId PolicyQualifierId,
  19. * qualifier ANY DEFINED BY policyQualifierId }
  20. * </pre>
  21. * <p>
  22. * A certificate policies extension, if present in an X.509 version 3
  23. * certificate, contains a sequence of one or more policy information terms,
  24. * each of which consists of an object identifier (OID) and optional
  25. * qualifiers. In an end-entity certificate, these policy information terms
  26. * indicate the policy under which the certificate has been issued and the
  27. * purposes for which the certificate may be used. In a CA certificate, these
  28. * policy information terms limit the set of policies for certification paths
  29. * which include this certificate.
  30. * <p>
  31. * A <code>Set</code> of <code>PolicyQualifierInfo</code> objects are returned
  32. * by the {@link PolicyNode#getPolicyQualifiers PolicyNode.getPolicyQualifiers}
  33. * method. This allows applications with specific policy requirements to
  34. * process and validate each policy qualifier. Applications that need to
  35. * process policy qualifiers should explicitly set the
  36. * <code>policyQualifiersRejected</code> flag to false (by calling the
  37. * {@link PKIXParameters#setPolicyQualifiersRejected
  38. * PKIXParameters.setPolicyQualifiersRejected} method) before validating
  39. * a certification path.
  40. *
  41. * <p>Note that the PKIX certification path validation algorithm specifies
  42. * that any policy qualifier in a certificate policies extension that is
  43. * marked critical must be processed and validated. Otherwise the
  44. * certification path must be rejected. If the
  45. * <code>policyQualifiersRejected</code> flag is set to false, it is up to
  46. * the application to validate all policy qualifiers in this manner in order
  47. * to be PKIX compliant.
  48. *
  49. * <p><b>Concurrent Access</b>
  50. *
  51. * <p>All <code>PolicyQualifierInfo</code> objects must be immutable and
  52. * thread-safe. That is, multiple threads may concurrently invoke the
  53. * methods defined in this class on a single <code>PolicyQualifierInfo</code>
  54. * object (or more than one) with no ill effects. Requiring
  55. * <code>PolicyQualifierInfo</code> objects to be immutable and thread-safe
  56. * allows them to be passed around to various pieces of code without
  57. * worrying about coordinating access.
  58. *
  59. * @author seth proctor
  60. * @author Sean Mullan
  61. * @version 1.9 01/23/03
  62. * @since 1.4
  63. */
  64. public final class PolicyQualifierInfo {
  65. private byte [] mEncoded;
  66. private String mId;
  67. private byte [] mData;
  68. private String pqiString;
  69. /**
  70. * Creates an instance of <code>PolicyQualifierInfo</code> from the
  71. * encoded bytes. The encoded byte array is copied on construction.
  72. *
  73. * @param encoded a byte array containing the qualifier in DER encoding
  74. * @exception IOException thrown if the byte array does not represent a
  75. * valid and parsable policy qualifier
  76. */
  77. public PolicyQualifierInfo(byte[] encoded) throws IOException {
  78. mEncoded = (byte[]) encoded.clone();
  79. DerValue val = new DerValue(mEncoded);
  80. if (val.tag != DerValue.tag_Sequence)
  81. throw new IOException("Invalid encoding for PolicyQualifierInfo");
  82. mId = (val.data.getDerValue()).getOID().toString();
  83. byte [] tmp = val.data.toByteArray();
  84. if (tmp == null) {
  85. mData = null;
  86. } else {
  87. mData = new byte[tmp.length];
  88. System.arraycopy(tmp, 0, mData, 0, tmp.length);
  89. }
  90. }
  91. /**
  92. * Returns the <code>policyQualifierId</code> field of this
  93. * <code>PolicyQualifierInfo</code>. The <code>policyQualifierId</code>
  94. * is an Object Identifier (OID) represented by a set of nonnegative
  95. * integers separated by periods.
  96. *
  97. * @return the OID (never <code>null</code>)
  98. */
  99. public String getPolicyQualifierId() {
  100. return mId;
  101. }
  102. /**
  103. * Returns the ASN.1 DER encoded form of this
  104. * <code>PolicyQualifierInfo</code>.
  105. *
  106. * @return the ASN.1 DER encoded bytes (never <code>null</code>).
  107. * Note that a copy is returned, so the data is cloned each time
  108. * this method is called.
  109. */
  110. public byte[] getEncoded() {
  111. return (byte[]) mEncoded.clone();
  112. }
  113. /**
  114. * Returns the ASN.1 DER encoded form of the <code>qualifier</code>
  115. * field of this <code>PolicyQualifierInfo</code>.
  116. *
  117. * @return the ASN.1 DER encoded bytes of the <code>qualifier</code>
  118. * field. Note that a copy is returned, so the data is cloned each
  119. * time this method is called.
  120. */
  121. public byte[] getPolicyQualifier() {
  122. return (mData == null ? null : (byte[]) mData.clone());
  123. }
  124. /**
  125. * Return a printable representation of this
  126. * <code>PolicyQualifierInfo</code>.
  127. *
  128. * @return a <code>String</code> describing the contents of this
  129. * <code>PolicyQualifierInfo</code>
  130. */
  131. public String toString() {
  132. if (pqiString != null)
  133. return pqiString;
  134. HexDumpEncoder enc = new HexDumpEncoder();
  135. StringBuffer sb = new StringBuffer();
  136. sb.append("PolicyQualifierInfo: [\n");
  137. sb.append(" qualifierID: " + mId + "\n");
  138. sb.append(" qualifier: " +
  139. (mData == null ? "null" : enc.encodeBuffer(mData)) + "\n");
  140. sb.append("]");
  141. pqiString = sb.toString();
  142. return pqiString;
  143. }
  144. }