-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
AsnConvert an array to a Set #50
Comments
This is specifically a problem for what I'm trying to do which is sign the I would suspect this could be a problem in any area where there's no canonical definition of a type and instead it's just declared as a Another example would be encoding OIDs ( |
const signedData = new SignedData();
signedData.signerInfos.push(new SignerInfo({
signedAttrs: [
new Attribute({
attrType: "1.2.3.4.5.6",
attrValues: [
new Uint8Array([2, 1, 3]).buffer,
]
})
]
}));
const raw = AsnConvert.serialize(signedData);
console.log(Convert.ToHex(raw)); Output
ASN.1SEQUENCE (4 elem)
INTEGER 0
SET (0 elem)
SEQUENCE (1 elem)
OBJECT IDENTIFIER
SET (1 elem)
SEQUENCE (6 elem)
INTEGER 0
SEQUENCE (2 elem)
SEQUENCE (0 elem)
INTEGER 0
SEQUENCE (1 elem)
OBJECT IDENTIFIER
[0] (1 elem)
SEQUENCE (2 elem)
OBJECT IDENTIFIER 1.2.3.4.5.6
SET (1 elem)
INTEGER 3
SEQUENCE (1 elem)
OBJECT IDENTIFIER
OCTET STRING (0 byte) |
Method AsnConvert.fromJSON(1); // INTEGER 1
AsnConvert.fromJSON("Hello"); // PrintableString Hello
AsnConvert.fromJSON([ 1, 2, 3]); // SET INTEGER 1 INTEGER 2 INTEGER 3 @dhensby What do you think about it? |
@microshine - from your first reply, that encodes the entire In terms of a I feel like the "right" response would be that the props weren't assigned their encoding rules only by the annotation, but also by having content types that can be encoded standalone. At the moment Perhaps even having a standalone "basic" type of @AsnType({ type: AsnTypeTypes.Set, itemType: AsnPropTypes.Any })
export class Set<T> extends AsnArray<T> {
} |
Here's a workaround to get the data that should be signed (the
|
I'm just doing this at the moment to get around it: const {
AsnType,
AsnArray,
AsnTypeTypes,
AsnPropTypes,
} = require('@peculiar/asn1-schema');
class Set extends AsnArray {}
// this is a little hack to allow us to define an Asn1 type `Set` which acts as an array
AsnType({ type: AsnTypeTypes.Set, itemType: AsnPropTypes.Any })(Set);
module.exports = { Set }; Then I can encode the Set (truncated example): // signed attributes must be an ordered set
const signedAttrs = new Set(authenticatedAttributes.map(({ type, value }) => {
return AsnConvert.serialize(new Attribute({
attrType: type,
attrValues: [AsnConvert.serialize(value)],
}));
}).sort((a, b) => Buffer.compare(Buffer.from(a), Buffer.from(b))));
// encode the Set for signing
const encodedAttrs = AsnConvert.serialize(signedAttrs);
// perform your signing somehow
const signature = await signer(Buffer.from(encodedAttrs));
// construct the signer info for use in SignedData
return new SignerInfo({
version: CMSVersion.v1,
sid: new SignerIdentifier({
issuerAndSerialNumber: new IssuerAndSerialNumber({
issuer: certificate.tbsCertificate.issuer,
serialNumber: certificate.tbsCertificate.serialNumber,
}),
}),
digestAlgorithm: new DigestAlgorithmIdentifier({
algorithm: digestAlgOid,
parameters: null,
}),
// it would be nice to re-use the `signedAttrs` from above, but I'm not sure that's possible
signedAttrs: authenticatedAttributes.map(({ type, value }) => {
return new Attribute({
attrType: type,
attrValues: [AsnConvert.serialize(value)],
});
}).sort((a, b) => Buffer.compare(Buffer.from(AsnConvert.serialize(a)), Buffer.from(AsnConvert.serialize(b)))),
signatureAlgorithm: new SignatureAlgorithmIdentifier({
algorithm: id_rsaEncryption,
parameters: null,
}),
signature: new OctetString(signature),
}); |
I need to convert an array of
Attribute
s to an encoded Set, however passing a raw array toAsnConvert
results inError: Cannot get schema for 'Array' target
(unsurprisingly).example code:
The text was updated successfully, but these errors were encountered: