コード例 #1
0
 private void setPublicPointY(ASN1OctetString publicPointY) throws IllegalArgumentException {
   if ((options & Y) == 0) {
     options |= Y;
     this.publicPointY = publicPointY.getOctets();
   } else {
     throw new IllegalArgumentException("Public Point Y already set");
   }
 }
コード例 #2
0
 private void setBasePointG(ASN1OctetString basePointG) throws IllegalArgumentException {
   if ((options & G) == 0) {
     options |= G;
     this.basePointG = basePointG.getOctets();
   } else {
     throw new IllegalArgumentException("Base Point G already set");
   }
 }
コード例 #3
0
  ECDSAPublicKey(ASN1Sequence seq) throws IllegalArgumentException {
    Enumeration en = seq.getObjects();

    this.usage = ASN1ObjectIdentifier.getInstance(en.nextElement());

    options = 0;
    while (en.hasMoreElements()) {
      Object obj = en.nextElement();

      if (obj instanceof ASN1TaggedObject) {
        ASN1TaggedObject to = (ASN1TaggedObject) obj;
        switch (to.getTagNo()) {
          case 0x1:
            setPrimeModulusP(UnsignedInteger.getInstance(to).getValue());
            break;
          case 0x2:
            setFirstCoefA(UnsignedInteger.getInstance(to).getValue());
            break;
          case 0x3:
            setSecondCoefB(UnsignedInteger.getInstance(to).getValue());
            break;
          case 0x4:
            setBasePointG(ASN1OctetString.getInstance(to, false));
            break;
          case 0x5:
            setOrderOfBasePointR(UnsignedInteger.getInstance(to).getValue());
            break;
          case 0x6:
            setPublicPointY(ASN1OctetString.getInstance(to, false));
            break;
          case 0x7:
            setCofactorF(UnsignedInteger.getInstance(to).getValue());
            break;
          default:
            options = 0;
            throw new IllegalArgumentException("Unknown Object Identifier!");
        }
      } else {
        throw new IllegalArgumentException("Unknown Object Identifier!");
      }
    }
    if (options != 0x20 && options != 0x7F) {
      throw new IllegalArgumentException("All options must be either present or absent!");
    }
  }
コード例 #4
0
  /**
   * Create a SubjectPublicKeyInfo public key.
   *
   * @param publicKey the SubjectPublicKeyInfo encoding
   * @return the appropriate key parameter
   * @throws java.io.IOException on an error encoding the key
   */
  public static SubjectPublicKeyInfo createSubjectPublicKeyInfo(AsymmetricKeyParameter publicKey)
      throws IOException {
    if (publicKey instanceof RSAKeyParameters) {
      RSAKeyParameters pub = (RSAKeyParameters) publicKey;

      return new SubjectPublicKeyInfo(
          new AlgorithmIdentifier(PKCSObjectIdentifiers.rsaEncryption, DERNull.INSTANCE),
          new RSAPublicKey(pub.getModulus(), pub.getExponent()));
    } else if (publicKey instanceof DSAPublicKeyParameters) {
      DSAPublicKeyParameters pub = (DSAPublicKeyParameters) publicKey;

      return new SubjectPublicKeyInfo(
          new AlgorithmIdentifier(X9ObjectIdentifiers.id_dsa), new ASN1Integer(pub.getY()));
    } else if (publicKey instanceof ECPublicKeyParameters) {
      ECPublicKeyParameters pub = (ECPublicKeyParameters) publicKey;
      ECDomainParameters domainParams = pub.getParameters();
      ASN1Encodable params;

      if (domainParams == null) {
        params = new X962Parameters(DERNull.INSTANCE); // Implicitly CA
      } else if (domainParams instanceof ECNamedDomainParameters) {
        params = new X962Parameters(((ECNamedDomainParameters) domainParams).getName());
      } else {
        X9ECParameters ecP =
            new X9ECParameters(
                domainParams.getCurve(),
                domainParams.getG(),
                domainParams.getN(),
                domainParams.getH(),
                domainParams.getSeed());

        params = new X962Parameters(ecP);
      }

      ASN1OctetString p = (ASN1OctetString) new X9ECPoint(pub.getQ()).toASN1Primitive();

      return new SubjectPublicKeyInfo(
          new AlgorithmIdentifier(X9ObjectIdentifiers.id_ecPublicKey, params), p.getOctets());
    } else {
      throw new IOException("key parameters not recognised.");
    }
  }