public static GPGKey AsciiArmored2GPGKey(string asciiArmored) { GPGKey key = null; using (var s = GenerateStreamFromString(asciiArmored)) { var pgpPub = new PgpPublicKeyRing(PgpUtilities.GetDecoderStream(s)); var pubKey = pgpPub.GetPublicKey(); key = new GPGKey { Id = null, FullFingerPrint = pubKey.GetFingerprint().ToHexString(), AsciiArmoredPublicKey = asciiArmored, AsciiArmoredPrivateKey = null, Emails = new List <string>(), Names = new List <string>(), KeyUids = new List <GPGKeyUid>(), KeyBits = pubKey.BitStrength, }; foreach (string userId in pubKey.GetUserIds()) { var m = NameObsEmailGPGReg.Match(userId); var m2 = NameEmailGPGReg.Match(userId); var email = ""; var name = ""; var obs = ""; if (m.Success && m.Groups.Count == 4) { email = m.Groups[3].Value.Trim(); obs = m.Groups[2].Value.Trim(); key.Emails.Add(email); var z = NameObsGPGReg.Match(m.Groups[1].Value); if (z.Success && z.Groups.Count == 3) { name = z.Groups[1].Value; obs = z.Groups[2].Value; key.Names.Add(name); } } else if (m2.Success && m2.Groups.Count == 3) { name = m2.Groups[1].Value; email = m2.Groups[2].Value; key.Names.Add(name); key.Emails.Add(email); } else { name = userId; key.Names.Add(name); } key.KeyUids.Add(new GPGKeyUid { Name = name, Email = email, Description = obs, }); } } return(key); }
public string AddKey(string publicKey) { if (Configuration.EnableRethinkSKS) { var conn = DatabaseManager.GlobalDm.GetConnection(); var res = GPGKey.AddGPGKey(conn, Tools.AsciiArmored2GPGKey(publicKey)); if (res.Inserted == 0 && res.Unchanged == 0 && res.Replaced == 0) { return(res.FirstError); } return("OK"); } var addTask = sks.PutSKSKey(publicKey); addTask.Wait(); if (!addTask.Result) { throw new ErrorObjectException(new ErrorObject { ErrorCode = ErrorCodes.InternalServerError, ErrorField = "PublicKey", Message = "Cannot add key in SKS Server" }); } return("OK"); }
public List <GPGKey> SearchByName(string name, int?pageStart, int?pageEnd) { if (Configuration.EnableRethinkSKS) { return(GPGKey.SearchGPGByName(DatabaseManager.GlobalDm.GetConnection(), name, pageStart, pageEnd)); } throw new NotSupportedException("The server does not have RethinkDB enabled so it cannot serve search"); }
public string GetKey(string fingerPrint) { if (!Configuration.EnableRethinkSKS) { var getTask = sks.GetSKSKey(fingerPrint); getTask.Wait(); return(getTask.Result); } var conn = DatabaseManager.GlobalDm.GetConnection(); var key = GPGKey.GetGPGKeyByFingerPrint(conn, fingerPrint); return(key?.AsciiArmoredPublicKey); }