use of org.spongycastle.asn1.DERBitString in project xwiki-commons by xwiki.
the class BcUtils method getX509CertificateHolder.
/**
* Build the structure of an X.509 certificate.
*
* @param tbsCert the to be signed structure
* @param signature the signature
* @return a X.509 certificate holder.
*/
public static X509CertificateHolder getX509CertificateHolder(TBSCertificate tbsCert, byte[] signature) {
ASN1EncodableVector v = new ASN1EncodableVector();
v.add(tbsCert);
v.add(tbsCert.getSignature());
v.add(new DERBitString(signature));
return new X509CertificateHolder(Certificate.getInstance(new DERSequence(v)));
}
use of org.spongycastle.asn1.DERBitString in project OpenUnison by TremoloSecurity.
the class U2fAttestation method Parse.
/**
* Parses a transport extension from an attestation certificate and returns
* a List of HardwareFeatures supported by the security key. The specification of
* the HardwareFeatures in the certificate should match their internal definition in
* device_auth.proto
*
* <p>The expected transport extension value is a BIT STRING containing the enabled
* transports:
*
* <p>FIDOU2FTransports ::= BIT STRING {
* bluetoothRadio(0), -- Bluetooth Classic
* bluetoothLowEnergyRadio(1),
* uSB(2),
* nFC(3)
* }
*
* <p>Note that the BIT STRING must be wrapped in an OCTET STRING.
* An extension that encodes BT, BLE, and NFC then looks as follows:
*
* <p>SEQUENCE (2 elem)
* OBJECT IDENTIFIER 1.3.6.1.4.1.45724.2.1.1
* OCTET STRING (1 elem)
* BIT STRING (4 bits) 1101
*
* @param cert the certificate to parse for extension
* @return the supported transports as a List of HardwareFeatures or null if no extension
* was found
* @throws CertificateParsingException
*/
public static U2fAttestation Parse(X509Certificate cert) throws CertificateParsingException {
ASN1OctetString extValue = X509ExtensionParsingUtil.extractExtensionValue(cert, TRANSPORT_EXTENSION_OID);
if (extValue == null) {
// No Transport extension was found
return new U2fAttestation(null);
}
// Read out the BitString
ASN1Object asn1Object = X509ExtensionParsingUtil.getAsn1Object(extValue.getOctets());
if (asn1Object == null || !(asn1Object instanceof DERBitString)) {
throw new CertificateParsingException("No BitString found in transports extension");
}
DERBitString bitString = (DERBitString) asn1Object;
byte[] values = bitString.getBytes();
BitSet bitSet = BitSet.valueOf(values);
// We might have more defined transports than used by the extension
List<Transports> transports = new ArrayList<Transports>();
for (int i = 0; i < BITS_IN_A_BYTE; i++) {
if (bitSet.get(BITS_IN_A_BYTE - i - 1)) {
transports.add(Transports.values()[i]);
}
}
return new U2fAttestation(transports);
}
use of org.spongycastle.asn1.DERBitString in project Elastos.DID.Java.SDK by elastos.
the class ECKey method extractKeyFromASN1.
private static ECKey extractKeyFromASN1(byte[] asn1privkey) {
//
try {
ASN1InputStream decoder = new ASN1InputStream(asn1privkey);
DLSequence seq = (DLSequence) decoder.readObject();
checkArgument(decoder.readObject() == null, "Input contains extra bytes");
decoder.close();
checkArgument(seq.size() == 4, "Input does not appear to be an ASN.1 OpenSSL EC private key");
checkArgument(((ASN1Integer) seq.getObjectAt(0)).getValue().equals(BigInteger.ONE), "Input is of wrong version");
byte[] privbits = ((ASN1OctetString) seq.getObjectAt(1)).getOctets();
BigInteger privkey = new BigInteger(1, privbits);
ASN1TaggedObject pubkey = (ASN1TaggedObject) seq.getObjectAt(3);
checkArgument(pubkey.getTagNo() == 1, "Input has 'publicKey' with bad tag number");
byte[] pubbits = ((DERBitString) pubkey.getObject()).getBytes();
checkArgument(pubbits.length == 33 || pubbits.length == 65, "Input has 'publicKey' with invalid length");
int encoding = pubbits[0] & 0xFF;
// Only allow compressed(2,3) and uncompressed(4), not infinity(0) or hybrid(6,7)
checkArgument(encoding >= 2 && encoding <= 4, "Input has 'publicKey' with invalid encoding");
// Now sanity check to ensure the pubkey bytes match the privkey.
boolean compressed = isPubKeyCompressed(pubbits);
ECKey key = new ECKey(privkey, (byte[]) null, compressed);
if (!Arrays.equals(key.getPubKey(), pubbits))
throw new IllegalArgumentException("Public key in ASN.1 structure does not match private key.");
return key;
} catch (IOException e) {
// Cannot happen, reading from memory stream.
throw new RuntimeException(e);
}
}
use of org.spongycastle.asn1.DERBitString in project attestation by TokenScript.
the class CryptoTest method testAddressWithReferenceKey.
/**
* Reference key found here https://medium.com/@tunatore/how-to-generate-ethereum-addresses-technical-address-generation-explanation-and-online-course-9a56359f139e
*/
@Test
public void testAddressWithReferenceKey() throws IOException {
String hexKey = "048e66b3e549818ea2cb354fb70749f6c8de8fa484f7530fc447d5fe80a1c424e4f5ae648d648c980ae7095d1efad87161d83886ca4b6c498ac22a93da5099014a";
DERBitString derPk = new DERBitString(Hex.decode(hexKey));
AsymmetricKeyParameter pk = SignatureUtility.restoreDefaultKey(derPk.getEncoded());
String address = SignatureUtility.addressFromKey(pk);
assertEquals("0x00B54E93EE2EBA3086A55F4249873E291D1AB06C", address);
}
use of org.spongycastle.asn1.DERBitString in project attestation by TokenScript.
the class CoSignedIdentifierAttestation method constructSignedAttestation.
static byte[] constructSignedAttestation(SignedIdentifierAttestation unsignedAtt, byte[] signature) {
try {
byte[] rawAtt = unsignedAtt.getDerEncoding();
ASN1EncodableVector res = new ASN1EncodableVector();
res.add(ASN1Primitive.fromByteArray(rawAtt));
res.add(unsignedAtt.getUnsignedAttestation().getSigningAlgorithm());
res.add(new DERBitString(signature));
return new DERSequence(res).getEncoded();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
Aggregations