// Based on RFCs 5480 and 5915, a named curve is used whenever possible. public IEcKey GetPublicKey(byte[] q, string curve) { ECPublicKeyParameters ecPublicKeyParameter; if (curve == "curve25519") { ecPublicKeyParameter = GetNonStandardCurve(q, curve); } else { DerObjectIdentifier curveOid = ECNamedCurveTable.GetOid(curve) ?? CustomNamedCurves.GetOid(curve); X9ECParameters curveParameters = CustomNamedCurves.GetByOid(curveOid) ?? ECNamedCurveTable.GetByOid(curveOid); ECPoint qPoint = curveParameters.Curve.DecodePoint(q); ecPublicKeyParameter = new ECPublicKeyParameters("EC", qPoint, curveOid); } byte[] publicKeyContent = GetPublicKey(ecPublicKeyParameter); int keyLength = GetKeyLength(ecPublicKeyParameter); string curveName = curveNameMapper.MapCurveToName(ecPublicKeyParameter.Parameters.Curve); return(new EcKey(publicKeyContent, AsymmetricKeyType.Public, keyLength, curveName)); }
/** * Parse a secret key from one of the GPG S expression keys. * * @return a secret key object. */ public static PgpSecretKey ParseSecretKeyFromSExpr(Stream inputStream, char[] passPhrase) { SXprUtilities.SkipOpenParenthesis(inputStream); string type = SXprUtilities.ReadString(inputStream, inputStream.ReadByte()); if (type.Equals("protected-private-key")) { SXprUtilities.SkipOpenParenthesis(inputStream); string curveName; string keyType = SXprUtilities.ReadString(inputStream, inputStream.ReadByte()); if (keyType.Equals("ecc")) { SXprUtilities.SkipOpenParenthesis(inputStream); string curveID = SXprUtilities.ReadString(inputStream, inputStream.ReadByte()); curveName = SXprUtilities.ReadString(inputStream, inputStream.ReadByte()); if (curveName.StartsWith("NIST ")) { curveName = curveName.Substring("NIST ".Length); } SXprUtilities.SkipCloseParenthesis(inputStream); } else { throw new PgpException("no curve details found"); } byte[] qVal; SXprUtilities.SkipOpenParenthesis(inputStream); type = SXprUtilities.ReadString(inputStream, inputStream.ReadByte()); if (type.Equals("q")) { qVal = SXprUtilities.ReadBytes(inputStream, inputStream.ReadByte()); } else { throw new PgpException("no q value found"); } PublicKeyPacket pubPacket = new PublicKeyPacket(PublicKeyAlgorithmTag.ECDsa, DateTime.UtcNow, new ECDsaPublicBcpgKey(ECNamedCurveTable.GetOid(curveName), new BigInteger(1, qVal))); SXprUtilities.SkipCloseParenthesis(inputStream); byte[] dValue = GetDValue(inputStream, passPhrase, curveName); // TODO: check SHA-1 hash. return(new PgpSecretKey(new SecretKeyPacket(pubPacket, SymmetricKeyAlgorithmTag.Null, null, null, new ECSecretBcpgKey(new BigInteger(1, dValue)).GetEncoded()), new PgpPublicKey(pubPacket))); } throw new PgpException("unknown key type found"); }
internal static PgpSecretKey DoParseSecretKeyFromSExpr(Stream inputStream, byte[] rawPassPhrase, bool clearPassPhrase) { SXprUtilities.SkipOpenParenthesis(inputStream); string text = SXprUtilities.ReadString(inputStream, inputStream.ReadByte()); if (text.Equals("protected-private-key")) { SXprUtilities.SkipOpenParenthesis(inputStream); string text2 = SXprUtilities.ReadString(inputStream, inputStream.ReadByte()); if (text2.Equals("ecc")) { SXprUtilities.SkipOpenParenthesis(inputStream); SXprUtilities.ReadString(inputStream, inputStream.ReadByte()); string text3 = SXprUtilities.ReadString(inputStream, inputStream.ReadByte()); if (Platform.StartsWith(text3, "NIST ")) { text3 = text3.Substring("NIST ".get_Length()); } SXprUtilities.SkipCloseParenthesis(inputStream); SXprUtilities.SkipOpenParenthesis(inputStream); text = SXprUtilities.ReadString(inputStream, inputStream.ReadByte()); if (text.Equals("q")) { byte[] bytes = SXprUtilities.ReadBytes(inputStream, inputStream.ReadByte()); PublicKeyPacket publicKeyPacket = new PublicKeyPacket(PublicKeyAlgorithmTag.ECDsa, global::System.DateTime.get_UtcNow(), new ECDsaPublicBcpgKey(ECNamedCurveTable.GetOid(text3), new BigInteger(1, bytes))); SXprUtilities.SkipCloseParenthesis(inputStream); byte[] dValue = GetDValue(inputStream, rawPassPhrase, clearPassPhrase, text3); return(new PgpSecretKey(new SecretKeyPacket(publicKeyPacket, SymmetricKeyAlgorithmTag.Null, null, null, new ECSecretBcpgKey(new BigInteger(1, dValue)).GetEncoded()), new PgpPublicKey(publicKeyPacket))); } throw new PgpException("no q value found"); } throw new PgpException("no curve details found"); } throw new PgpException("unknown key type found"); }