(msg *ttnpb.Message, config lorawanConfig)
| 78 | } |
| 79 | |
| 80 | func decodeUplink(msg *ttnpb.Message, config lorawanConfig) (*lorawanDecodedFrame, error) { |
| 81 | pld := msg.GetMacPayload() |
| 82 | devAddr := types.MustDevAddr(pld.FHdr.DevAddr).OrZero() |
| 83 | macBuf := getMacBuffer(pld) |
| 84 | cmdsInFOpts := len(pld.FHdr.FOpts) > 0 |
| 85 | if len(macBuf) > 0 && (!cmdsInFOpts || macspec.EncryptFOpts(config.MACVersion)) { |
| 86 | if config.NwkSEncKey.IsZero() { |
| 87 | logger.Warn("No NwkSEncKey provided, skipping decryption of MAC buffer") |
| 88 | } else { |
| 89 | logger.Debug("Decrypting MAC buffer") |
| 90 | encOpts := macspec.EncryptionOptions(config.MACVersion, macspec.UplinkFrame, pld.FPort, cmdsInFOpts) |
| 91 | for msb := range uint32(0xff) { |
| 92 | fCnt := msb<<8 | pld.FHdr.FCnt |
| 93 | macBuf, err := crypto.DecryptUplink(config.NwkSEncKey, devAddr, fCnt, macBuf, encOpts...) |
| 94 | if err == nil { |
| 95 | setMacBuffer(pld, macBuf) |
| 96 | break |
| 97 | } |
| 98 | logger.WithField("f_cnt", fCnt).Debug("Failed attempt to decrypt MAC buffer") |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | var macCommands []*ttnpb.MACCommand |
| 103 | for r := bytes.NewReader(macBuf); r.Len() > 0; { |
| 104 | cmd := &ttnpb.MACCommand{} |
| 105 | if err := lorawan.DefaultMACCommands.ReadUplink(config.Band, r, cmd); err != nil { |
| 106 | logger.WithError(err).Warn("Failed to read MAC command") |
| 107 | } else { |
| 108 | macCommands = append(macCommands, cmd) |
| 109 | } |
| 110 | } |
| 111 | if pld.FPort > 0 { |
| 112 | if config.AppSKey.IsZero() { |
| 113 | logger.Warn("No AppSKey provided, skipping application payload decryption") |
| 114 | } else { |
| 115 | logger.Debug("Decrypting application payload") |
| 116 | buf, err := crypto.DecryptUplink(config.AppSKey, devAddr, pld.FHdr.FCnt, pld.FrmPayload) |
| 117 | if err != nil { |
| 118 | logger.WithField("f_cnt", pld.FHdr.FCnt).Debug("Failed attempt to decrypt FrmPayload") |
| 119 | } else { |
| 120 | pld.FrmPayload = buf |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | msg.Payload = &ttnpb.Message_MacPayload{ |
| 125 | MacPayload: pld, |
| 126 | } |
| 127 | if !config.FNwkSIntKey.IsZero() { |
| 128 | logger.Debug("Verification of the uplink message MIC is not implemented yet") |
| 129 | } |
| 130 | return &lorawanDecodedFrame{ |
| 131 | Message: msg, |
| 132 | MACCommands: macCommands, |
| 133 | }, nil |
| 134 | } |
| 135 | |
| 136 | func decodeJoinAccept(msg *ttnpb.Message, config lorawanConfig) (*lorawanDecodedFrame, error) { |
| 137 | pld := msg.GetJoinAcceptPayload() |
no test coverage detected