// AddPrivateUIDReply adds the msgReply to the given UID message. func (keyDB *KeyDB) AddPrivateUIDReply( msg *uid.Message, msgReply *uid.MessageReply, ) error { _, err := keyDB.addPrivateUIDReplyQuery.Exec(msgReply.JSON(), msg.JSON()) if err != nil { return err } return nil }
// AddPublicUID adds a public UID message and it's hash chain position to // keyDB. func (keyDB *KeyDB) AddPublicUID(msg *uid.Message, position uint64) error { _, err := keyDB.addPublicUIDQuery.Exec( msg.UIDContent.IDENTITY, msg.UIDContent.MSGCOUNT, position, msg.JSON(), ) if err != nil { return err } return nil }
// AddPrivateUID adds a private uid to keyDB. func (keyDB *KeyDB) AddPrivateUID(msg *uid.Message) error { _, err := keyDB.addPrivateUIDQuery.Exec( msg.UIDContent.IDENTITY, msg.UIDContent.MSGCOUNT, msg.JSON(), msg.PrivateSigKey(), msg.PrivateEncKey(), "", ) if err != nil { return err } return nil }
func TestPublicUID(t *testing.T) { tmpdir, keyDB, err := createDB() if err != nil { t.Fatal(err) } defer os.RemoveAll(tmpdir) defer keyDB.Close() a1, err := uid.Create("*****@*****.**", false, "", "", uid.Strict, hashchain.TestEntry, cipher.RandReader) if err != nil { t.Fatal(err) } a2, err := uid.Create("*****@*****.**", false, "", "", uid.Strict, hashchain.TestEntry, cipher.RandReader) if err != nil { t.Fatal(err) } if err := keyDB.AddPublicUID(a1, 10); err != nil { t.Fatal(err) } if err := keyDB.AddPublicUID(a2, 20); err != nil { t.Fatal(err) } var pos uint64 var rA1 *uid.Message rA1, pos, _, err = keyDB.GetPublicUID("*****@*****.**", 10) if !bytes.Equal(rA1.JSON(), a1.JSON()) { t.Error("UID messages differ") } if pos != 10 { t.Error("a1 position should be 10") } var rA2 *uid.Message rA2, pos, _, err = keyDB.GetPublicUID("*****@*****.**", 30) if !bytes.Equal(rA2.JSON(), a2.JSON()) { t.Error("UID messages differ") } if pos != 20 { t.Error("a2 position should be 20") } }
// DelPrivateUID deletes the given UID message from keyDB. func (keyDB *KeyDB) DelPrivateUID(msg *uid.Message) error { if _, err := keyDB.delPrivateUIDQuery.Exec(msg.JSON()); err != nil { return err } return nil }
func newHeader( sender, recipient *uid.Message, recipientTempHash string, senderSessionPub, nextSenderSessionPub, nextRecipientSessionPubSeen *uid.KeyEntry, nymAddress string, senderSessionCount, senderMessageCount uint64, senderLastKeychainHash string, rand io.Reader, statusCode StatusCode, ) (*header, error) { if len(senderLastKeychainHash) != hashchain.EntryBase64Len { return nil, log.Errorf("msg: last hashchain entry '%s' does not have base64 length %d (but %d)", senderLastKeychainHash, hashchain.EntryBase64Len, len(senderLastKeychainHash)) } h := &header{ Ciphersuite: uid.DefaultCiphersuite, // at the moment we only support one ciphersuite RecipientPubHash: recipient.PubHash(), RecipientTempHash: recipientTempHash, SenderIdentity: sender.Identity(), SenderSessionPub: *senderSessionPub, SenderIdentityPubHash: sender.PubHash(), SenderIdentityPub: *sender.PubKey(), NextSenderSessionPub: nextSenderSessionPub, NextRecipientSessionPubSeen: nextRecipientSessionPubSeen, NymAddress: nymAddress, MaxDelay: 0, // TODO SenderSessionCount: senderSessionCount, SenderMessageCount: senderMessageCount, SenderUID: string(sender.JSON()), SenderLastKeychainHash: senderLastKeychainHash, Status: statusCode, Padding: "", // is set below } // calculate padding length padLen := wiggleRoom // pad sender identity if len(h.SenderIdentity) > identity.MaxLen { return nil, log.Error("msg: sender identity is too long") } padLen += identity.MaxLen - len(h.SenderIdentity) // pad nextSenderSessionPub if nextSenderSessionPub == nil { padLen += length.KeyEntryECDHE25519 - length.Nil } // pad nextRecipientSessionPubSeen if nextRecipientSessionPubSeen == nil { padLen += length.KeyEntryECDHE25519 - length.Nil } // pad nym address if len(h.NymAddress) > length.MaxNymAddress { return nil, log.Error("msg: nym address is too long") } padLen += length.MaxNymAddress - len(h.NymAddress) // pad integers padLen += 20 - digits.Count(h.MaxDelay) padLen += 20 - digits.Count(h.SenderSessionCount) padLen += 20 - digits.Count(h.SenderMessageCount) // pad sender UIDMessage if len(h.SenderUID) > length.MaxUIDMessage { return nil, log.Error("msg: sender UIDMesssage is too long") } padLen += length.MaxUIDMessage - len(h.SenderUID) // generate padding randLen := padLen/2 + padLen%2 pad, err := padding.Generate(randLen, cipher.RandReader) if err != nil { return nil, err } // set padding p := hex.EncodeToString(pad) if padLen%2 == 1 { p = p[:len(p)-1] } h.Padding = p return h, nil }