internal ASN1 GetASN1(byte encoding) { byte encode = encoding; if (encode == 0xFF) { encode = SelectBestEncoding(); } ASN1 asn1 = new ASN1(0x30); asn1.Add(ASN1Convert.FromOid(oid)); switch (encode) { case 0x13: // PRINTABLESTRING asn1.Add(new ASN1(0x13, Encoding.ASCII.GetBytes(attrValue))); break; case 0x16: // IA5STRING asn1.Add(new ASN1(0x16, Encoding.ASCII.GetBytes(attrValue))); break; case 0x1E: // BMPSTRING asn1.Add(new ASN1(0x1E, Encoding.BigEndianUnicode.GetBytes(attrValue))); break; } return(asn1); }
// Note: PKCS#8 doesn't define how to generate the key required for encryption // so you're on your own. Just don't try to copy the big guys too much ;) // Netscape: http://www.cs.auckland.ac.nz/~pgut001/pubs/netscape.txt // Microsoft: http://www.cs.auckland.ac.nz/~pgut001/pubs/breakms.txt public byte[] GetBytes() { if (_algorithm == null) { throw new CryptographicException("No algorithm OID specified"); } ASN1 encryptionAlgorithm = new ASN1(0x30); encryptionAlgorithm.Add(ASN1Convert.FromOid(_algorithm)); // parameters ANY DEFINED BY algorithm OPTIONAL if ((_iterations > 0) || (_salt != null)) { ASN1 salt = new ASN1(0x04, _salt); ASN1 iterations = ASN1Convert.FromInt32(_iterations); ASN1 parameters = new ASN1(0x30); parameters.Add(salt); parameters.Add(iterations); encryptionAlgorithm.Add(parameters); } // encapsulates EncryptedData into an OCTET STRING ASN1 encryptedData = new ASN1(0x04, _data); ASN1 encryptedPrivateKeyInfo = new ASN1(0x30); encryptedPrivateKeyInfo.Add(encryptionAlgorithm); encryptedPrivateKeyInfo.Add(encryptedData); return(encryptedPrivateKeyInfo.GetBytes()); }
public byte[] GetBytes() { ASN1 privateKeyAlgorithm = new ASN1(0x30); privateKeyAlgorithm.Add(ASN1Convert.FromOid(_algorithm)); privateKeyAlgorithm.Add(new ASN1(0x05)); // ASN.1 NULL ASN1 pki = new ASN1(0x30); pki.Add(new ASN1(0x02, new byte [1] { (byte)_version })); pki.Add(privateKeyAlgorithm); pki.Add(new ASN1(0x04, _key)); if (_list.Count > 0) { ASN1 attributes = new ASN1(0xA0); foreach (ASN1 attribute in _list) { attributes.Add(attribute); } pki.Add(attributes); } return(pki.GetBytes()); }
static public ASN1 AlgorithmIdentifier(string oid, ASN1 parameters) { ASN1 ai = new ASN1(0x30); ai.Add(ASN1Convert.FromOid(oid)); ai.Add(parameters); return(ai); }
static public ASN1 AlgorithmIdentifier(string oid) { ASN1 ai = new ASN1(0x30); ai.Add(ASN1Convert.FromOid(oid)); ai.Add(new ASN1(0x05)); // NULL return(ai); }
static private bool IsOid(string oid) { try { ASN1 asn = ASN1Convert.FromOid(oid); return(asn.Tag == 0x06); } catch { return(false); } }
static public ASN1 Attribute(string oid, ASN1 value) { ASN1 attr = new ASN1(0x30); attr.Add(ASN1Convert.FromOid(oid)); ASN1 aset = attr.Add(new ASN1(0x31)); aset.Add(value); return(attr); }
internal ASN1 GetASN1() { // ContentInfo ::= SEQUENCE { ASN1 contentInfo = new ASN1(0x30); // contentType ContentType, -> ContentType ::= OBJECT IDENTIFIER contentInfo.Add(ASN1Convert.FromOid(contentType)); // content [0] EXPLICIT ANY DEFINED BY contentType OPTIONAL if ((content != null) && (content.Count > 0)) { contentInfo.Add(content); } return(contentInfo); }