use of org.mozilla.jss.asn1.BOOLEAN in project jaxdb by jaxdb.
the class Decompiler method createDDL.
public static Schema createDDL(final Connection connection) throws SQLException {
final DBVendor vendor = DBVendor.valueOf(connection.getMetaData());
final Decompiler decompiler = Decompiler.getDecompiler(vendor);
final DatabaseMetaData metaData = connection.getMetaData();
try (final ResultSet tableRows = metaData.getTables(null, null, null, new String[] { "TABLE" })) {
final Schema schema = new Schema();
final Map<String, List<$CheckReference>> tableNameToChecks = decompiler.getCheckConstraints(connection);
final Map<String, List<$Table.Constraints.Unique>> tableNameToUniques = decompiler.getUniqueConstraints(connection);
final Map<String, $Table.Indexes> tableNameToIndexes = decompiler.getIndexes(connection);
final Map<String, Map<String, $ForeignKeyUnary>> tableNameToForeignKeys = decompiler.getForeignKeys(connection);
final Map<String, $Column> columnNameToColumn = new HashMap<>();
final Map<Integer, $Column> columnNumberToColumn = new TreeMap<>();
final Map<String, TreeMap<Short, String>> indexNameToIndex = new HashMap<>();
final Map<String, String> indexNameToType = new HashMap<>();
final Map<String, Boolean> indexNameToUnique = new HashMap<>();
while (tableRows.next()) {
final String tableName = tableRows.getString(3);
final $Table table = new Schema.Table();
table.setName$(new $Named.Name$(tableName.toLowerCase()));
schema.addTable(table);
try (final ResultSet columnRows = metaData.getColumns(null, null, tableName, null)) {
while (columnRows.next()) {
final String columnName = columnRows.getString("COLUMN_NAME").toLowerCase();
final String typeName = columnRows.getString("TYPE_NAME");
final int columnSize = columnRows.getInt("COLUMN_SIZE");
final String _default = columnRows.getString("COLUMN_DEF");
final int index = columnRows.getInt("ORDINAL_POSITION");
final String nullable = columnRows.getString("IS_NULLABLE");
final String autoIncrement = columnRows.getString("IS_AUTOINCREMENT");
final int decimalDigits = columnRows.getInt("DECIMAL_DIGITS");
final $Column column = decompiler.makeColumn(columnName.toLowerCase(), typeName, columnSize, decimalDigits, _default, nullable.length() == 0 ? null : "YES".equals(nullable), autoIncrement.length() == 0 ? null : "YES".equals(autoIncrement));
columnNameToColumn.put(columnName, column);
columnNumberToColumn.put(index, column);
}
columnNumberToColumn.values().forEach(table::addColumn);
try (final ResultSet primaryKeyRows = metaData.getPrimaryKeys(null, null, tableName)) {
while (primaryKeyRows.next()) {
final String columnName = primaryKeyRows.getString("COLUMN_NAME").toLowerCase();
if (table.getConstraints() == null)
table.setConstraints(new $Table.Constraints());
if (table.getConstraints().getPrimaryKey() == null)
table.getConstraints().setPrimaryKey(new $Table.Constraints.PrimaryKey());
final $Table.Constraints.PrimaryKey.Column column = new $Table.Constraints.PrimaryKey.Column();
column.setName$(new $Table.Constraints.PrimaryKey.Column.Name$(columnName));
table.getConstraints().getPrimaryKey().addColumn(column);
}
}
final List<$Table.Constraints.Unique> uniques = tableNameToUniques == null ? null : tableNameToUniques.get(tableName);
if (uniques != null && uniques.size() > 0) {
if (table.getConstraints() == null)
table.setConstraints(new $Table.Constraints());
for (final $Table.Constraints.Unique unique : uniques) table.getConstraints().addUnique(unique);
}
try (final ResultSet indexRows = metaData.getIndexInfo(null, null, tableName, false, true)) {
while (indexRows.next()) {
final String columnName = indexRows.getString("COLUMN_NAME").toLowerCase();
if (columnName == null)
continue;
final String indexName = indexRows.getString("INDEX_NAME").toLowerCase();
TreeMap<Short, String> indexes = indexNameToIndex.get(indexName);
if (indexes == null)
indexNameToIndex.put(indexName, indexes = new TreeMap<>());
final short ordinalPosition = indexRows.getShort("ORDINAL_POSITION");
indexes.put(ordinalPosition, columnName);
final String type = getType(indexRows.getShort("TYPE"));
final String currentType = indexNameToType.get(indexName);
if (currentType == null)
indexNameToType.put(indexName, type);
else if (!type.equals(currentType))
throw new IllegalStateException("Expected " + type + " = " + currentType);
final boolean unique = !indexRows.getBoolean("NON_UNIQUE");
final Boolean currentUnique = indexNameToUnique.get(indexName);
if (currentUnique == null)
indexNameToUnique.put(indexName, unique);
else if (unique != currentUnique)
throw new IllegalStateException("Expected " + unique + " = " + currentType);
}
}
final $Table.Indexes indexes = tableNameToIndexes == null ? null : tableNameToIndexes.get(tableName);
if (indexes != null)
table.setIndexes(indexes);
final List<$CheckReference> checks = tableNameToChecks == null ? null : tableNameToChecks.get(tableName);
if (checks != null)
for (final $CheckReference check : checks) addCheck(columnNameToColumn.get(check.getColumn$().text()), check);
final Map<String, $ForeignKeyUnary> foreignKeys = tableNameToForeignKeys == null ? null : tableNameToForeignKeys.get(tableName);
if (foreignKeys != null)
for (final Map.Entry<String, $ForeignKeyUnary> entry : foreignKeys.entrySet()) columnNameToColumn.get(entry.getKey().toLowerCase()).setForeignKey(entry.getValue());
}
columnNameToColumn.clear();
columnNumberToColumn.clear();
indexNameToIndex.clear();
indexNameToType.clear();
}
return schema;
}
}
use of org.mozilla.jss.asn1.BOOLEAN in project jaxdb by jaxdb.
the class SQLiteDecompiler method makeColumn.
@Override
$Column makeColumn(final String columnName, final String typeName, final long size, final int decimalDigits, final String _default, final Boolean nullable, final Boolean autoIncrement) {
final $Column column;
if (typeName.startsWith("BIGINT")) {
final $Bigint type = newColumn($Bigint.class);
if (size != 2000000000)
type.setPrecision$(new $Bigint.Precision$((byte) size));
if (_default != null)
type.setDefault$(new $Bigint.Default$(Long.valueOf(_default)));
if (autoIncrement != null && autoIncrement)
type.setGenerateOnInsert$(new $Bigint.GenerateOnInsert$($Integer.GenerateOnInsert$.AUTO_5FINCREMENT));
column = type;
} else if (typeName.startsWith("BINARY")) {
final $Binary type = newColumn($Binary.class);
final Long length = getLength(typeName);
if (length != null)
type.setLength$(new $Binary.Length$(length));
column = type;
} else if (typeName.startsWith("BLOB")) {
final $Blob type = newColumn($Blob.class);
final Long length = getLength(typeName);
if (length != null)
type.setLength$(new $Blob.Length$(length));
column = type;
} else if ("BOOLEAN".equals(typeName)) {
final $Boolean type = newColumn($Boolean.class);
if (_default != null)
type.setDefault$(new $Boolean.Default$(Boolean.parseBoolean(_default)));
column = type;
} else if (typeName.startsWith("VARCHAR") || typeName.startsWith("CHARACTER")) {
final $Char type = newColumn($Char.class);
if (typeName.startsWith("VARCHAR"))
type.setVarying$(new $Char.Varying$(true));
final Long length = getLength(typeName);
if (length != null)
type.setLength$(new $Char.Length$(length));
if (_default != null)
type.setDefault$(new $Char.Default$(_default.substring(1, _default.length() - 1)));
column = type;
} else if (typeName.startsWith("TEXT")) {
final $Clob type = newColumn($Clob.class);
final Long length = getLength(typeName);
if (length != null)
type.setLength$(new $Clob.Length$(length));
column = type;
} else if ("DATE".equals(typeName)) {
final $Date type = newColumn($Date.class);
if (_default != null)
type.setDefault$(new $Date.Default$(_default.substring(1, _default.length() - 1)));
column = type;
} else if ("DATETIME".equals(typeName)) {
final $Datetime type = newColumn($Datetime.class);
if (size != 2000000000)
type.setPrecision$(new $Datetime.Precision$((byte) size));
if (_default != null)
type.setDefault$(new $Datetime.Default$(_default.substring(1, _default.length() - 1)));
column = type;
} else if (typeName.startsWith("DECIMAL")) {
final $Decimal type = newColumn($Decimal.class);
if (!"DECIMAL(15,0)".equals(typeName)) {
final int open = typeName.indexOf('(');
if (open > 0) {
final int comma = typeName.indexOf(',', open + 1);
if (comma > open) {
final int close = typeName.indexOf(')', comma + 1);
if (close > comma) {
type.setPrecision$(new $Decimal.Precision$(Integer.valueOf(typeName.substring(open + 1, comma).trim())));
type.setScale$(new $Decimal.Scale$(Integer.valueOf(typeName.substring(comma + 1, close).trim())));
}
}
}
}
if (_default != null)
type.setDefault$(new $Decimal.Default$(new BigDecimal(_default)));
column = type;
} else if ("DOUBLE".equals(typeName)) {
final $Double type = newColumn($Double.class);
if (_default != null)
type.setDefault$(new $Double.Default$(Double.valueOf(_default)));
column = type;
} else // }
if ("FLOAT".equals(typeName)) {
final $Float type = newColumn($Float.class);
if (_default != null)
type.setDefault$(new $Float.Default$(Float.valueOf(_default)));
column = type;
} else if (typeName.startsWith("INT") || typeName.startsWith("MEDIUMINT")) {
final $Int type = newColumn($Int.class);
if (size != 2000000000)
type.setPrecision$(new $Int.Precision$((byte) size));
if (_default != null)
type.setDefault$(new $Int.Default$(Integer.valueOf(_default)));
if ("INTEGER".equals(typeName))
type.setGenerateOnInsert$(new $Int.GenerateOnInsert$($Integer.GenerateOnInsert$.AUTO_5FINCREMENT));
column = type;
} else if ("SMALLINT".equals(typeName)) {
final $Smallint type = newColumn($Smallint.class);
if (size != 2000000000)
type.setPrecision$(new $Smallint.Precision$((byte) size));
if (_default != null)
type.setDefault$(new $Smallint.Default$(Short.valueOf(_default)));
if (autoIncrement != null && autoIncrement)
type.setGenerateOnInsert$(new $Smallint.GenerateOnInsert$($Integer.GenerateOnInsert$.AUTO_5FINCREMENT));
column = type;
} else if ("TIME".equals(typeName)) {
final $Time type = newColumn($Time.class);
if (size != 2000000000)
type.setPrecision$(new $Time.Precision$((byte) size));
if (_default != null)
type.setDefault$(new $Time.Default$(_default.substring(1, _default.length() - 1)));
column = type;
} else if ("TINYINT".equals(typeName)) {
final $Tinyint type = newColumn($Tinyint.class);
if (size != 2000000000)
type.setPrecision$(new $Tinyint.Precision$((byte) size));
if (_default != null)
type.setDefault$(new $Tinyint.Default$(Byte.valueOf(_default)));
if (autoIncrement != null && autoIncrement)
type.setGenerateOnInsert$(new $Tinyint.GenerateOnInsert$($Integer.GenerateOnInsert$.AUTO_5FINCREMENT));
column = type;
} else {
throw new UnsupportedOperationException("Unsupported column type: " + typeName);
}
column.setName$(new $Column.Name$(columnName));
if (nullable != null && !nullable)
column.setNull$(new $Column.Null$(false));
return column;
}
use of org.mozilla.jss.asn1.BOOLEAN in project jss by dogtagpki.
the class IssuingDistributionPoint method encode.
@Override
public void encode(Tag implicitTag, OutputStream ostream) throws IOException {
SEQUENCE seq = new SEQUENCE();
DerOutputStream derOut;
try {
// is a CHOICE, the [0] tag is forced to be EXPLICIT.
if (fullName != null) {
EXPLICIT distPoint = new EXPLICIT(Tag.get(0), fullNameEncoding);
seq.addElement(distPoint);
} else if (relativeName != null) {
derOut = new DerOutputStream();
relativeName.encode(derOut);
ANY raw = new ANY(derOut.toByteArray());
ByteArrayOutputStream bos = new ByteArrayOutputStream();
raw.encodeWithAlternateTag(Tag.get(1), bos);
ANY distPointName = new ANY(bos.toByteArray());
EXPLICIT distPoint = new EXPLICIT(Tag.get(0), distPointName);
seq.addElement(distPoint);
}
if (onlyContainsUserCerts != false) {
seq.addElement(Tag.get(1), new BOOLEAN(true));
}
if (onlyContainsCACerts != false) {
seq.addElement(Tag.get(2), new BOOLEAN(true));
}
// Encodes the ReasonFlags.
if (onlySomeReasons != null) {
derOut = new DerOutputStream();
derOut.putUnalignedBitString(onlySomeReasons);
ANY raw = new ANY(derOut.toByteArray());
ByteArrayOutputStream bos = new ByteArrayOutputStream();
raw.encodeWithAlternateTag(Tag.get(3), bos);
ANY reasonEncoding = new ANY(bos.toByteArray());
seq.addElement(reasonEncoding);
}
if (indirectCRL != false) {
seq.addElement(Tag.get(4), new BOOLEAN(true));
}
seq.encode(implicitTag, ostream);
} catch (InvalidBERException e) {
// the Sun encoding classes
throw new IOException(e.toString());
}
}
use of org.mozilla.jss.asn1.BOOLEAN in project jss by dogtagpki.
the class SignerInfo method createDigestInfo.
private SEQUENCE createDigestInfo(byte[] data, boolean doDigest) throws NoSuchAlgorithmException {
if (data == null || data.length == 0) {
throw new IllegalArgumentException("Data to digest must be supplied");
}
SEQUENCE digestInfo = new SEQUENCE();
digestInfo.addElement(this.digestAlgorithm);
byte[] digest;
if (doDigest) {
MessageDigest md = MessageDigest.getInstance(DigestAlgorithm.fromOID(this.digestAlgorithm.getOID()).toString());
digest = md.digest(data);
} else {
digest = data;
}
digestInfo.addElement(new OCTET_STRING(digest));
return digestInfo;
}
use of org.mozilla.jss.asn1.BOOLEAN in project jss by dogtagpki.
the class SignerInfo method verifyWithSignedAttributes.
/**
* Verifies a SignerInfo with signed attributes. If signed
* attributes are present, then two particular attributes must
* be present: <ul>
* <li>PKCS #9 Content-Type, the type of content that is being signed.
* This must match the contentType parameter.
* <li>PKCS #9 Message-Digest, the digest of the content that is being
* signed. This must match the messageDigest parameter.
* </ul>
* After these two attributes are verified to be both present and correct,
* the encryptedDigest field of the SignerInfo is verified to be the
* signature of the contents octets of the DER encoding of the
* signedAttributes field.
*/
private void verifyWithSignedAttributes(byte[] messageDigest, OBJECT_IDENTIFIER contentType, PublicKey pubkey) throws NotInitializedException, NoSuchAlgorithmException, InvalidKeyException, TokenException, SignatureException {
int numAttrib = signedAttributes.size();
if (numAttrib < 2) {
throw new SignatureException("At least two signed attributes must be present:" + " content-type and message-digest");
}
// go through the signed attributes, verifying the
// interesting ones
boolean foundContentType = false;
boolean foundMessageDigest = false;
for (int i = 0; i < numAttrib; i++) {
if (!(signedAttributes.elementAt(i) instanceof Attribute)) {
throw new SignatureException("Element of signedAttributes is not an Attribute");
}
Attribute attrib = (Attribute) signedAttributes.elementAt(i);
if (attrib.getType().equals(CONTENT_TYPE)) {
// content-type. Compare with what was passed in.
SET vals = attrib.getValues();
if (vals.size() != 1) {
throw new SignatureException("Content-Type attribute " + " does not have exactly one value");
}
ASN1Value val = vals.elementAt(0);
OBJECT_IDENTIFIER ctype;
try {
if (val instanceof OBJECT_IDENTIFIER) {
ctype = (OBJECT_IDENTIFIER) val;
} else if (val instanceof ANY) {
ctype = (OBJECT_IDENTIFIER) ((ANY) val).decodeWith(OBJECT_IDENTIFIER.getTemplate());
} else {
// what the heck is it? not what it's supposed to be
throw new InvalidBERException("Content-Type signed attribute has unexpected" + " content type");
}
} catch (InvalidBERException e) {
throw new SignatureException("Content-Type signed attribute does not have " + "OBJECT IDENTIFIER value");
}
// contentType parameter
if (!ctype.equals(contentType)) {
throw new SignatureException("Content-type in signed attributes does not " + "match content-type being verified");
}
// content type is A-OK
foundContentType = true;
} else if (attrib.getType().equals(MESSAGE_DIGEST)) {
SET vals = attrib.getValues();
if (vals.size() != 1) {
throw new SignatureException("Message-digest attribute does not have" + " exactly one value");
}
ASN1Value val = vals.elementAt(0);
byte[] mdigest;
try {
if (val instanceof OCTET_STRING) {
mdigest = ((OCTET_STRING) val).toByteArray();
} else if (val instanceof ANY) {
OCTET_STRING os;
os = (OCTET_STRING) ((ANY) val).decodeWith(OCTET_STRING.getTemplate());
mdigest = os.toByteArray();
} else {
// what the heck is it? not what it's supposed to be
throw new InvalidBERException("Content-Type signed attribute has unexpected" + " content type");
}
} catch (InvalidBERException e) {
throw new SignatureException("Message-digest attribute does not" + " have OCTET STRING value");
}
// message digest being verified
if (!byteArraysAreSame(mdigest, messageDigest)) {
throw new SignatureException("Message-digest attribute does not" + " match message digest being verified");
}
// message digest is A-OK
foundMessageDigest = true;
}
// we don't care about other attributes
}
if (!foundContentType) {
throw new SignatureException("Signed attributes does not contain" + " PKCS #9 content-type attribute");
}
if (!foundMessageDigest) {
throw new SignatureException("Signed attributes does not contain" + " PKCS #9 message-digest attribute");
}
SignatureAlgorithm sigAlg = SignatureAlgorithm.fromOID(digestEncryptionAlgorithm.getOID());
// All the signed attributes are present and correct.
// Now verify the signature.
CryptoToken token = CryptoManager.getInstance().getInternalCryptoToken();
Signature sig;
// verify the contents octets of the DER encoded signed attribs
byte[] encoding = ASN1Util.encode(signedAttributes);
byte[] toBeVerified;
if (sigAlg.getRawAlg() == SignatureAlgorithm.RSASignature) {
// create DigestInfo structure
SEQUENCE digestInfo = createDigestInfo(encoding, true);
toBeVerified = ASN1Util.encode(digestInfo);
sig = token.getSignatureContext(SignatureAlgorithm.RSASignature);
} else {
toBeVerified = encoding;
sig = token.getSignatureContext(sigAlg);
}
sig.initVerify(pubkey);
sig.update(toBeVerified);
if (!sig.verify(encryptedDigest.toByteArray())) {
// signature is invalid
throw new SignatureException("encryptedDigest was not the correct" + " signature of the contents octets of the DER-encoded" + " signed attributes");
}
// SUCCESSFULLY VERIFIED
}
Aggregations